The Microcontroller Idea Book - Jan Axelson's Lakeview Research
The Microcontroller Idea Book - Jan Axelson's Lakeview Research
The Microcontroller Idea Book - Jan Axelson's Lakeview Research
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Subroutines have two advantages. First, they help you to break up your program code<br />
into discrete units, with each having a specific purpose. This makes the program code<br />
easier to debug and easier to understand in general, especially if you return to it a week,<br />
month, or year after writing it when the details are no longer fresh in your mind.<br />
Second, subroutines make it easier to reuse your code if you have a similar task in<br />
another project. For example, all or most of the code involved with controlling a display<br />
module can usually be written as a subroutine, or perhaps a series of subroutines. This<br />
way, if you want to use the same display module in more than one project, you can<br />
reuse the code without having to pick through your previous programs to find the<br />
program lines that you need.<br />
• Keep program lines short. Short lines are easier to edit with BASIC-52’s line editor,<br />
which requires retyping the entire line to make a change. <strong>The</strong>y’re also easier to read. If<br />
you upload programs from disk, shorter lines can eliminate problems caused by<br />
BASIC-52’s not having enough time to process each line before the next one arrives.<br />
Although BASIC-52 allows you to place multiple statements on one line, with up to 79<br />
characters per line, shorter is better.<br />
<strong>The</strong>re are two situations where you might want to combine a series of short lines into<br />
fewer, longer program lines: when the program has to execute as fast as possible, or<br />
when you need to store the program in the smallest possible space. Even then, though,<br />
you can develop the program with short lines, and combine them only after the program<br />
is debugged and ready for permanent storage.<br />
• Check syntax and spelling carefully. BASIC-52’s syntax consists of the rules of<br />
grammar and punctuation that your program lines must follow. For example, a FOR<br />
loop must include a variable, limits, and a NEXT instruction. Leave any of these out,<br />
and your loop won’t work. <strong>The</strong>re’s no room for spelling errors either. BASIC-52<br />
doesn’t know that you meant LIST when you typed LSIT.<br />
• Document your programs. Many of BASIC-52’s keywords aren’t too hard to<br />
decipher. For example, it makes sense that the STOP instruction halts program<br />
execution. But your own comments throughout the program can help you remember<br />
why you wrote each program line, and what it’s supposed to accomplish.<br />
BASIC-52 allows you to add comments, preceded by REM (remark). Try to write<br />
comments that do more than just define the keywords in the line. Also explain the<br />
purpose behind what you are doing. For example, this comment<br />
10 REM read value from external memory<br />
20 A=XBY(0FE00H)<br />
Programming<br />
does nothing more than define the BASIC-52 instruction that follows. In contrast,<br />
<strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong> 67