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

<strong>The</strong> DAC is configured in its flow-through and voltage-switching modes. In flow-through<br />

mode, the analog output continuously reflects the data inputs. <strong>The</strong> chip has several control<br />

signals for latching inputs and outputs, but these aren’t needed by our circuit.<br />

In voltage-switching mode, the analog output is a voltage proportional to the value of the<br />

byte formed by D0-D7. An LM385 2.5-volt reference is applied across current output<br />

terminals IOUT1 and IOUT2, and the output appears at VREF. (This configuration is the inverse<br />

of the device’s current-switching mode, where VREF is an input and IOUT1 and IOUT2 are<br />

outputs, as their names suggest.)<br />

Op amp U2A buffers the output, and U2B is a low-pass filter that helps to smooth VOUT.<br />

A BASIC Program<br />

Listing 13-4 causes a sine wave to appear at VOUT. <strong>The</strong> sine wave represents the value of<br />

the trigonometric sine function for an angle that varies continuously from 0 to 360 degrees,<br />

or 0 to 6.28 (2*PI) radians. Lines 90-110 are a loop that selects 256 equally-spaced points<br />

along one cycle of the sine wave, calculates the sine for each, and stores the values in RAM.<br />

<strong>The</strong> program uses BASIC-52’s SIN operator in calculating the values. Sine values normally<br />

vary from +1 to -1, but line 100 adjusts the values so that they vary from 0 to 255, which is<br />

the range of inputs accepted by the 8-bit DAC. Using these values, 0 is the negative peak,<br />

255 is the positive peak, and the zero crossing occurs midway between points 127 and 128.<br />

To generate the sine wave, Lines 150-180 are a loop that reads each value in sequence from<br />

RAM and writes it to an output port at E400h. After writing a complete cycle, the program<br />

loops back and begins another. <strong>The</strong> sine wave repeats endlessly, until the user presses<br />

CONTROL+C.<br />

Listing 13-4 creates a perfectly good sine wave, but at a very low frequency. Using<br />

12-Megahertz crystal to clock the 8052, the frequency is only about 0.7 Hertz, or 1.5 seconds<br />

per cycle.<br />

Adding Assembly Language<br />

To speed things up, Listing 13-5 is an assembly-language routine that performs the functions<br />

of lines 150-180 in Listing 13-4. As in the original program, Listing 13-5 copies values in<br />

sequence from RAM to E400h, repeating the sequence after 256 writes. <strong>The</strong> routine<br />

illustrates a couple of major differences between BASIC and assembly-language programming.<br />

One is that assembly language has no built-in FOR, DO, or WHILE loops. Instead, you<br />

have to create loop structures from the instructions available. Listing 13-5 creates a 256-step<br />

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