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.

statement must match the address in your file’s org directive. A missing ret instruction in<br />

the routine will also cause the system to crash.<br />

Example: Creating a Sine Wave<br />

When you have the simple routine working, you’re ready to move on to bigger things. For<br />

the sine-wave project, we’ll begin by generating a sine wave entirely with BASIC-52<br />

statements. This way, we can first test the added circuits as well as the algorithm, or sequence<br />

of steps, that we plan to use to generate the sine wave. It also illustrates the speed limits of<br />

BASIC-52.<br />

<strong>The</strong> Circuits<br />

Calling Assembly-language Routines<br />

Figure 13-2 shows the circuit that interfaces to the 8052. I adapted the circuit from an<br />

example in National Semiconductor’s data sheet for the DAC0832.<br />

U1 is a DAC0832 digital-to-analog converter, or DAC, which converts data inputs D0-D7<br />

into an analog voltage. D0-D7 are controlled by an output port at E400h. You may change<br />

this address to match any output port on your system.<br />

Listing 13-4. Sine-wave generator for Figure 13-2’s circuit.<br />

10 REM Begins by calculating and storing sine values<br />

20 REM for 256 locations along a sine wave.<br />

30 REM Line 100 converts a position in the sine wave<br />

40 REM (0-255) to the radians required by the sine<br />

50 REM operator: (0.0246 = 2*PI/256). Adding 1 to the<br />

60 REM sines makes all values positive, from 0 to +2.<br />

70 REM Multiplying by 127.5 results in values that<br />

80 REM range from 0 to 255.<br />

90 FOR I=0 TO 255<br />

100 XBY(3000H+I)=INT((SIN(I*.0246)+1)*127.5+.5)<br />

110 NEXT I<br />

120 PRINT “Sine values are stored in RAM (3000h-30FFh)”<br />

130 PRINT “Press Control+C to quit”<br />

140 REM Write the values in sequence to E400h<br />

150 DO<br />

160 FOR I=3000H TO 30FFH<br />

170 XBY(0E400H)=XBY(I)<br />

180 NEXT I<br />

190 WHILE 1=1<br />

200 END<br />

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

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

Saved successfully!

Ooh no, something went wrong!