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 13<br />

FOR loop by loading FFh into register dpl (the lower byte of dptr), and decrementing dpl<br />

repeatedly until it equals zero.<br />

In assembly language, you also do not have built-in conveniences like BASIC-52’s ability<br />

to terminate a program on CONTROL+C. You have to add these features yourself. In Listing<br />

13-5, after each complete cycle of the sine wave, the program checks the serial port’s receive<br />

flag. If the flag is set, it means that the user has pressed a key, and the program returns to<br />

the BASIC-52 prompt. Otherwise, the program begins another cycle of the sine wave.<br />

To run Listing 13-5, create a source file with your text editor, assemble it, and upload it to<br />

RAM as before. Edit Listing 13-4 by removing lines 150-180 and adding this line:<br />

150 CALL 3100h<br />

Now when you run Listing 13-4, you should again see a sine wave at VOUT, but at a much<br />

higher frequency.<br />

With a 12-Megahertz crystal, the sine wave should be around 350 Hertz, or 2.8 milliseconds<br />

per cycle. You can verify this by consulting the 8052’s data book, which tells the number of<br />

machine cycles required to execute each instruction. At 12 Megahertz, each machine cycle<br />

is 1 microsecond, and one complete cycle requires 11 microseconds multiplied by 255 points<br />

on the wave, plus 6 microseconds to test the serial flag, or 2811 microseconds total.<br />

With different crystal frequencies, the output frequency will vary in direct proportion. For<br />

example, with a 6-Megahertz crystal, the sine wave will be half as fast.<br />

To slow down the sine wave, you can add “do-nothing” instructions to the code. For example<br />

adding a nop (no operation) instruction in the main loop will add 1 microsecond to the time<br />

between points on the wave, for a frequency of 326 Hertz. For long delays, you can insert<br />

a timing loop that executes after each point in the wave.<br />

Listing 13-5 still relies on BASIC-52 to calculate the sine values and store them in RAM.<br />

Although you can also write these parts in assembly language, doing so in BASIC is much<br />

easier, and doesn’t affect the frequency of the sine wave that results. Even if you later decide<br />

to write this part in assembly language, with BASIC-52 you can test each section of the code<br />

as you go along.<br />

When you have your assembly-language routine in the form you want it, you can use Listing<br />

B-2 or an EPROM programmer to store the code in EPROM. If your EPROM has different<br />

addressing than the RAM you used to test the code, you must change the ORG directive in<br />

the source file to match the new location, and reassemble the file before you program it into<br />

the EPROM.<br />

230 <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!