ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1675

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter 4: Basic Digital to Analog Conversion· Page 75

Modify the Code

If the D/A converter works as we expect, each time the value of n is incremented by 1, the D/A output should increment by 0.2 volts. Try starting with n=0 by modifying the value of n in the DAC subroutine.

n = 0

Then run the program again with n=1.

n = 1

Then change n to 2 and run the program a third time.

n = 2

Continue for each value up to n=15.

Figure 4-4 shows a sample measurement of a resistive ladder D/A conversion with the value of n set to 15. Remember that the 10% resistors lead to some error in the output. In Chapter #3, we were interested in programming our DVM for accurate calculations to the nearest hundredth of a volt. In this experiment, anything within 10% of the expected value is fine. So your end output when n=15 might be as low as 2.7 volts or as high as 3.3 volts. If the errors are larger than that, double check to make sure that a 1k resistor didn’t get swapped with a 2k somewhere in the resistive ladder.

Page 76 · Basic Analog and Digital

Figure 4-4

Debug Terminal Output for Program Listing 4.1, Revision 1.

Addressing

Until now, we’ve been addressing each of the I/O lines one at a time. This works well whenever you need to have control over the status of a particular control line. For example, a single LED, is easily addressed by the individual I/O pin to which it is connected using OUTp=value, where p is a pin number between 0 and 15, and value is either 0 or 1.

Addressed: When an I/O pin is addressed, it means that a value has been written into a particular place in the BASIC Stamp's RAM. For example, a particular memory location can be set high to set a given I/O pin to be an output. Another address might be used to set the pin high or low.

Performing these operations one bit at a time is not always efficient. The memory locations are adjacent to each other so that the operations can be performed 1 nibble (4-bits) at a time or one byte (8-bits) at a time, or even a word (16-bits) at a time.

Since the I/O pins (P4 through P7) are used as outputs in this experiment, it would be easier and more efficient to have a method of addressing this group of bits. Notice that it takes four lines of code just to set up the bits as outputs, and then it takes 4 additional lines to individually set each bit. It’s not a big deal now, but as time goes on and you

Chapter 4: Basic Digital to Analog Conversion· Page 77

begin to create more complex programs, you may find yourself looking for ways to get the most out of the least code.

There are two registers that we need to set to control output on a specific group of I/O lines. The first register is called “direction”. The command “output” sets the direction as an “output”. Conversely, “input” sets the I/O line up as input. The second is the “data” register. If the I/O pin has been set up as an output, then this register’s “data” can be set to either 0 or 1. This in turn sends either a low or high signal to that pin, and the measured output will either be 0 or 5 volts.

Certain commands in PBASIC allow us to directly address its I/O lines as a word (16 individual bits), two bytes (two sets of 8 individual bits) or as 4 nibbles (four sets of four individual bits). To modify our code, we want to address a nibble at a time, and the four bits that we’re using are P4-P7. According to the BASIC Stamp Manual (which you should have a copy of by now - it’s free download at www.parallax.com and printed copies are not expensive) the group “P4-P7” is called nibble “b”. The next set of four (P8-P11) is nibble “c”, and so on.

Let’s try using this in a program and figure out how it works. Rewrite the DAC: subroutine in Program Listing 4.1 as follows:

DAC:

n = 11

DIRB = 15

OUTB = n

RETURN

Figure 4-5 shows the output is identical to the previous version of the DAC subroutine. We did the same job with two lines of code instead of eight.


Page 78 · Basic Analog and Digital

Figure 4-5

Debug Terminal Output for Program Listing 4.1, Revision 2.

Here is how to count from 0 to 15 using a nibble:

0 = 0000

4 = 0100

8 = 1000

12 = 1100

1 = 0001

5 = 0101

9 = 1001

13 = 1101

2 = 0010

6 = 0110

10 = 1010

14 = 1110

3 = 0011

7 = 0111

11 = 1011

15 = 1111

When nibble b is selected using DIRB, each bit in the number DIRB is set equal to a value corresponding to a data direction as shown below:

Bit in nibble B

3

2

1

0

I/O pin

P7

P6

P5

P4

If we used the command “DIRB = 4” the following direction register bits would be set like this:

Bit value

0

1

0

0

I/O pin

P7

P6

P5

P4

Chapter 4: Basic Digital to Analog Conversion· Page 79

This would result in I/O pin P6 being set as an output, and all the other pins (P0, P1, P3) set as inputs. Hence the command DIRB=15 (by virtue of the fact that all four bits are a “1”) sets up each of the I/O lines as outputs.

Sweeping the value of n from 0 to 15 should result in the same values as before.

A truly powerful aspect of using this method of addressing is that we can use PBASIC to count up and down or access values from a look up table to automatically address the I/O pins. The result is that we can program the BASIC Stamp to more effectively control the D/A converter output.

Modify the code labeled 'Start Display in Program Listing 4.1. First, modify the DEBUG CLS command, and then add a second line as shown.

'Start display

DEBUG CLS, "DAC Nibble Values", CR

DEBUG "Decimal

Binary DVM", CR

Modify the main subroutine as shown:

' -----[ Main Routine ]--------------------------------------

FOR n =

0 TO 15

GOSUB

DAC

GOSUB

ADC_Data

GOSUB

Calc_Volts

GOSUB

Display

NEXT

STOP

Delete the line that sets the value of n in the DAC subroutine. Once that's done it should look like this:

DAC:

DIRB = 15

OUTB = n

RETURN

Also modify the Display subroutine as shown.

Display:

DEBUG DEC2 n, " ", BIN4 n, " "

DEBUG DEC1 v, ".", DEC2 v2, " Volts", CR

RETURN


Page 80 · Basic Analog and Digital

Figure 4-6 shows the output. Try that with a hand held voltmeter and you’ll begin to see the usefulness of combining the BASIC Stamp with analog interfaces. Imagine trying to test all 4096 levels of a 12-bit DAC one at a time!

Figure 4-6

Debug Terminal

Sample Output for

Program Listing 4.1,

Revision 3.

This is a very efficient way to collect your voltage sweep data. Looking at the data for the voltage sweep brings several things to light. First, the voltage output for the D/A converter is always a little high. Second, the error increases as the output voltage


Chapter 4: Basic Digital to Analog Conversion· Page 81

increases. Third, the largest error is 0.1 volts. This kind of data can be exceedingly useful in electronics design, and the automated testing process is a huge time saver.

The Voltage Follower

Let's use the voltage sweep to analyze what happens when the output of the D/A converter is connected to another circuit. We'll use the output of the D/A converter to drive an LED circuit. First we'll connect the D/A converter output directly to the input of the LED circuit. Then we'll use the voltage follower as an intermediate step between the D/A converter output and the LED circuit input.

P0

P1

P2

Vdd

P7

LED

2 kΩ

1 kΩ

2 kΩ

8

5

P6

1

Vdd

Vref

2 kΩ

1 kΩ

270 Ω

/CS

Vin(+)

2

P5

7

CLK

GND

4

2 kΩ

1 kΩ

6

D0

3

Vin(-)

P4

ADC0831

2 kΩ

2 kΩ

Vss

Vss

Vss

Vss

Figure 4-7 D/A Circuit without a Buffer.


Page 82 · Basic Analog and Digital

Table 4-1:D/A Converter Output Without Buffer

Decimal

Binary

DVM (volts)

00

0000

___________

01

0001

___________

02

0010

___________

03

0011

___________

04

0100

___________

05

0101

___________

06

0110

___________

07

0111

___________

08

1000

___________

09

1001

___________

10

1010

___________

11

1011

___________

12

1100

___________

13

1101

___________

14

1110

___________

15

1111

___________

Figure 4-7 shows the D/A converter with an LED circuit added. The LED circuit is the "load" that the D/A converter must "drive". Run a voltage sweep on this circuit, and fill in the table below. Then try the voltage sweep using the output of the voltage follower in as shown in Figure 4-8. Fill out the same table of voltage sweep information for this second circuit, and compare the two. The voltage follower in Figure 4-7 is referred to as a buffer. The LED circuit has no voltage follower to separate it from the resistive ladder network. The LED is an "unbuffered" load for the D/A converter.