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

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

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

Добавлен: 12.06.2025

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

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

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

Chapter 3: Basic Analog to Digital Conversion · Page 65

Why did I learn it?

There is a wide variety of electronic applications where analog signals are measured and digital devices are used to process the analog signal data. In this experiment, we used a BASIC Stamp and A/D converter combination to construct a digital DC voltmeter. We'll be using the BASIC Stamp DC-DVM in several of the remaining experiments. There is a surprising variety of uses for such a device as you will discover with each new experiment.

How can I apply this?

In developing the digital voltmeter, the process of sampling voltage, converting it, and processing it in digital form was introduced. An example of another use for an A/D interface is the digital sampling of the analog signal from a microphone for digital recording purposes. Another example that could make use of the circuit we built is a door sensor. Our potentiometer could be attached to a door hinge, and the analog information could be used to monitor how far the door is open. This circuit could in turn be incorporated into a larger system that controls how far open the door swings.

The field of analog to digital conversion is an industry in itself. There are semi-conductor manufacturing companies that specialize solely in creating A/D conversion chips and systems. Whether you’d like to design at the component level, or at the integrated circuit level, there will always be a need for creative analog interfacing – simply because the world isn’t black and white (binary), it’s all the colors in-between as well (analog).

Chapter 4: Basic Digital to Analog Conversion· Page 67

Chapter #4: Basic Digital to Analog Conversion

BUILD A RESISTIVE LADDER NETWORK

Digital to analog conversion (D/A conversion) is, for the most part, the reverse of A/D conversion. With A/D conversion, we started with a continuous voltage range at the converter's input. The A/D converter rounded to the nearest voltage step and sent a binary output indicating which step it measured.

D/A conversion starts with a binary number as the input, and the output is a voltage step. While the A/D process starts with an analog input and ends with a binary output, the D/A process starts with a binary input and ends with a voltage step for an output. It's not a true analog value that varies continuously; it's a discrete voltage that varies in steps.

The term resolution was introduced at the end of Chapter #3. Since a D/A converter's output always going gets rounded to a voltage step (a discrete voltage value) it's important to pick the right resolution for your D/A converter. Remember that with higher resolution comes higher precision, but it typically comes at the price of greater expense, more memory, and more processing steps.

The number of voltage levels a D/A converter can produce is given by how many counting numbers you can get from the number of binary bits in the resolution. We can use the combinations equation again to figure this out.

combinations = 2bits

The D/A converter we will use in this experiment has 4-bit resolution, so the number of output voltage levels for the converter will be:

combinations = 2bits = 24 = 16

In Chapter #3, we used an integrated circuit, which performed the A/D conversion. In this experiment, we'll build a D/A converter using resistors. It's called a resistive ladder network, and adding or removing resistors can be done to change the resolution of the converter. With a resistive ladder network, if you start with a 4-bit converter, and you want to increase the resolution by 1-bit, all it takes is two extra resistors added to the network.

In this chapter, we will build a resistive ladder network and program the BASIC Stamp to make the network do D/A conversion. PBASIC will be used to program the BASIC


Page 68 · Basic Analog and Digital

Stamp to send the resistive ladder network sets of binary voltage levels. These sets of binary voltages are converted by the resistive ladder network to discrete output voltages.

The DVM from Chapter #3 will be used to measure the converter's output voltages. Measuring all the D/A converter's output levels is called a voltage sweep. We will use PBASIC to automate our DVM to perform the entire voltage sweep. That way, the D/A converter's output voltages can be measured without manually repeating each measurement.

Parts Required

Gather these parts from your parts kit and let's get started:

(6) 2 k resistors

(3) 1 k resistors

(1) ADC0831 A/D Converter

(1) Red LED

(1) 270 resistor

(1) LM 358 op-amp

The resistive ladder network for this experiment is shown in shown in Figure 4-1. The name comes from the fact that the resistor network in the schematic resembles a ladder. It's definitely an inexpensive alternative compared to an integrated circuit digital to analog converter (D/A converter or DAC). A few resistors are a fraction of the cost of an integrated circuit.

DAC Output

P7

2 kΩ 1 kΩ 2 kΩ

P6

2 kΩ 1 kΩ

P5

2 kΩ 1 kΩ

P4

2 kΩ 2 kΩ

Vss Vss

Chapter 4: Basic Digital to Analog Conversion· Page 69

Figure 4-1

Resistive Ladder D/A Converter.

This resistive ladder network can be used as a D/A converter. The binary number input is sent in parallel as 4-bits across 4 separate data lines, P4 through P7. As long as the value of all four bits are present at the same time, the output of the D/A converter output will be the intended discrete voltage value.

This being the case, why doesn't everybody use resistive ladder networks for A/D and D/A conversion? The resistive ladder network is also used in many A/D and D/A integrated circuits, such as the ADC0831. The resistors used in integrated circuits are microscopic implants on the surface of a silicon wafer. One advantage of IC converters is that they have a high degree of accuracy. Another advantage an IC has is extra built in circuitry similar to the voltage follower we built in Chapter #1

Build It

Build the circuit as shown in Figure 4-2. Pay careful attention to the values of the resistors as well as what each resistor connects to. If your circuit from Chapter #3 is still intact, just remove the potentiometer and build the resistive ladder network near the sockets for pins P4 through P7. The input lead to the DVM, which was connected to the wiper terminal on the potentiometer, should now be connected to the output of the D/A converter. Be sure to pay close attention so that the resistors don't touch each other except where they're supposed to on the breadboard nodes.

Page 70 · Basic Analog and Digital

Voltmeter Input Lead

DAC Output

Figure 4-2

Schematic.

Vdd

P7

The DC

voltmeter

2 kΩ

1 kΩ

2 kΩ

from

8

5

P6

Chapter #3

1

Vdd

Vref

is

P0

2 kΩ

1 kΩ

/CS

Vin(+)

2

connected

P1

7

4

P5

to the

CLK

GND

2 kΩ

1 kΩ

output of

P2

6

D0

3

the

Vin(-)

P4

resistive

ADC0831

2 kΩ

2 kΩ

ladder

network

D/A

Vss

Vss

Vss

converter.

Program It

Not only can we use the DVM to measure the output voltage from the D/A converter, we can automate the testing process to measure all 16 the D/A converter's output voltage levels. This might not be a big deal for just 16 measurements, but just imagine trying to test all 4096 voltage steps on a 12-bit converter!

With some relatively simple additions to the code from Chapter #3, which was saved as file P3_1R3.bs2, we can control both devices. PBASIC can be used to instruct the BASIC Stamp to send an output signal to the D/A converter. The code for this will be added to the final version of Program Listing 3.1. This way we can use our DVM from this experiment to measure the D/A converter's output

Program Listing 4.1 is shown below. It's the final revision of Program Listing 3.1 with a subroutine labeled DAC added to send binary voltages to the D/A converter. There are also a few additional changes that are pointed out using comments such as 'Ƒ which means add this line and '¨ which shows the lines that have been changed.

If you saved the program listing from Chapter #3, add and modify the code for this experiment, and save the program under the name P4_1R0.bs2. If you do not have the code from Chapter #3, enter the entire program listing below using the BASIC Stamp


Chapter 4: Basic Digital to Analog Conversion· Page 71

Editor and make sure to save it for future use. When the circuit is built and the code is entered and saved, run Program Listing 4.1, and let's see how it works.

'-----[ Title ]-----------------------------------------------------------

'Basic Analog and Digital - PL4_1R0.bs2

'Digital Voltmeter (DVM). D/A Converter Added

'{$STAMP BS2}

'{$PBASIC 2.5}

'-----[ Declarations ]----------------------------------------------------

adcBits

VAR

Byte

v

VAR

Byte

r

VAR

Byte

v2

VAR

Byte

v3

VAR

Byte

n

VAR

Nib

' -----

[ Initialization

]--------------------------------------------------

CS

PIN

0

CLK

PIN

1

DataOutput

PIN

2

DEBUG CLS

'Start display.

'-----[ Main Routine ]

DO

GOSUB DAC GOSUB ADC_Data

GOSUB Calc_Volts GOSUB Display

LOOP

'-----[ Subroutines ]-----------------------------------------------------

DAC:

n = 11 ----------------------------------------------------

OUTPUT 7

OUTPUT 6

OUTPUT 5

OUTPUT 4

OUT7 = n.BIT3

OUT6 = n.BIT2

OUT5 = n.BIT1

OUT4 = n.BIT0

RETURN

ADC_Data:

HIGH CS

LOW CS


Page 72 · Basic Analog and Digital

LOW CLK

PULSOUT CLK, 210

SHIFTIN DataOutput,CLK,MSBPOST,[adcBits\8]

RETURN

Calc_Volts:

v = 5 * adcBits / 255 r = 5 * adcBits // 255 v2 = 100 * R / 255

v3 = 100 * R // 255

v3 = 10 * v3 / 255

IF (v3 >= 5) THEN v2 = v2 + 1 IF (v2 >= 100) THEN

v = v + 1 v2 = 0

ENDIF RETURN

Display:

DEBUG HOME, CR, CR, "Decimal value to DAC: ", DEC2 n DEBUG CR, CR, "Binary value to DAC: ", BIN4 n

DEBUG CR, CR, "DVM Reading: ", DEC1 v, ".", DEC2 v2, " Volts" RETURN

The Output

Given perfect resistor values, the output would be 3.00 volts. The resistors used in this sample have a 10% tolerance. This means that the measured resistance for each resistor should have a value within ±10% of what it’s supposed to be. Because of this, we can expect the output to be slightly different than what’s expected, such as the measured value in Figure 4-3. Given perfect resistor values, the measurement would be 2.20 volts.

Chapter 4: Basic Digital to Analog Conversion· Page 73

Figure 4-3

Debug Terminal

Output for Program

Listing 4.1.

About the Code

The comments are updated to indicate that this is Program Listing 4.1. In the third comment is included a reference to indicate that a function was added to the DVM that does D/A conversion.

' -----[ Title ]---------------------------------------------

'

Basic Analog and Digital - PL4_1R0.bs2

'

Digital Voltmeter (DVM). D/A Converter Added

'{$STAMP BS2}

'{$PBASIC 2.5}

A nibble size variable n is added to the declarations section, and it will be used to store the binary value for the D/A converter.

n

VAR

Nib

A GOSUB command is added to the main routine that sends the program to the DAC subroutine.

GOSUB DAC

Page 74 · Basic Analog and Digital

This is the start of the digital to analog conversion (DAC) subroutine, so it's descriptively labeled DAC. The value of n is set to 11. This means that the output should be n steps above 0 on an output scale of 0 to 16. The value of n can be changed to specify voltage.

DAC:

n = 11

The BASIC Stamp I/O pins connected to the D/A converter are set to output. These commands are normally found in the declarations section. If they were placed in the declarations section, the program would run faster because these commands would only be done once at the beginning of the program. Instead, they are executed each time the subroutine is run. The reason they were placed in the subroutine is to make it easier to present some new PBASIC techniques.

OUTPUT 7

OUTPUT 6

OUTPUT

5

OUTPUT

4

Next, the BASIC Stamp's parallel binary output is sent to the D/A converter. We are using the same command for sending outputs that was used in Chapters #1 and #2, but there is a new feature added. The variable n has an extension to indicate which bit in the nibble value is being used. For example, the command OUT7=n.BIT3 sets the output value of pin P7 equal to the value of bit 3 in the nibble variable n. Since we set the value of n to 11, the binary value of n is 1011. Bit 3 is the leftmost bit of the binary number, so it's 1, which means the output value of P7 is set high. Also, when n = 11, P6 is set low, P5 is set high, and P4 is set high.

OUT7 = n.BIT3

OUT6 = n.BIT2

OUT5

=

n.BIT1

OUT4

=

n.BIT0

That's all it takes for programming digital to analog conversion using a resistive ladder network. The RETURN command sends the program back to the command immediately following the GOSUB DAC command in the main: routine.

RETURN

The first two lines in the Display subroutine are modified to show the decimal and binary values of n, the 4-bit binary value used D/A output value.

DEBUG

HOME, CR, CR, "Decimal value to DAC: ", DEC2 n

DEBUG

CR, CR, "Binary value to DAC: ", BIN4 n