18.11.2014 Views

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

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5<br />

Writing a short BASIC-52 program involves these steps:<br />

Define what you want to do<br />

Write program lines to accomplish it<br />

Test the results<br />

As necessary, revise and retest<br />

Longer programs involve the same basic steps, except that you can divide the program into<br />

a series of smaller tasks, or modules, and program and test each individually. <strong>The</strong>n, when<br />

the modules are working, you can combine them in one big program and test the result.<br />

Modular programming can save a lot of headaches by limiting the amount of untested<br />

program code you have to work with at one time. A long, untested program almost certainly<br />

contains many errors will be hard to find and fix. It’s much easier in the long run to test the<br />

pieces first, and BASIC-52 makes this easy to do.<br />

Command and Run Modes<br />

BASIC-52 has two modes of operation: command and run. Command mode refers to<br />

anything you type without a line number. BASIC-52 executes these lines immediately after<br />

you press ENTER. Run mode refers to running stored programs with the RUN command.<br />

A program consists of a series of program lines, with each line beginning with a line number.<br />

BASIC-52 includes some keywords that you can use only in command mode, but not in<br />

programs. PROG is an example. Most of BASIC-52’s other keywords are usable in either<br />

command or run mode. A few, such as DO...WHILE, are usable in RUN mode only.<br />

Tips for Writing BASIC-52 Programs<br />

<strong>The</strong> following advice is intended to make your programs easier to write and debug, and to<br />

help you avoid some common mistakes:<br />

• Number program lines by 10s. Each line in a BASIC-52 program must begin with a<br />

line number. BASIC-52 uses the numbers to order the statements. Traditionally, BASIC<br />

programs begin at line 10, and count up in multiples of 10: 20, 30, and so on. This way,<br />

if you later discover that you need to add a few lines in the middle, you can, using the<br />

unused numbers that remain.<br />

• Divide long programs into modules. Break up big projects. Use subroutines for<br />

independent functions. A subroutine is a block of statements that the main program<br />

jumps to with a GOSUB statement. At the end of the subroutine, a RETURN statement<br />

causes the program to jump back to the program line following the GOSUB statement.<br />

66 <strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!