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

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

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

Добавлен: 12.06.2025

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

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

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

Page 38 · Basic Analog and Digital

Questions

1.Determine the decimal equivalents of these binary numbers: 1010, 1111, 0010, and 0100.

2.The command d = d<<1 was used to shift the bits in the variable d to the left by 1. What command do you think would be used to shift the bits to the right? What command would you use to shift the bits by, say, 3 to the left?

3.Explain the difference between serial and parallel data.

4.Explain the difference between synchronous and asynchronous data transmission.

Challenge!

1.Program Listing 2.1 is used to count to three. Write a program that uses three parallel bits (use pin P2 for the third bit) and counts to 7. You can check your work by connecting the input pin P2 to Vdd for a high signal and to Vss for a low signal (using a 1 k resistor).

2.Modify Program Listing 2.2 so that it displays the clock pulses you apply to the left pushbutton with the left LED.

3.Modify Program Listing 2.2 so that it shifts the bits back out, displaying them with the right LED.

Chapter 2: Introduction to Bit Crunching · Page 39

Why did I learn it?

The goal in this series of experiments is to demonstrate how a device such as the BASIC Stamp, which processes binary data, can be used to interface with the analog world. Binary numbers and states form the foundation for how microcontrollers, microprocessors and an entire class of binary circuits process data.

Analog data can be effectively processed with binary numbers using the techniques in this experiment. Learning the basics of processing binary data will help make a myriad of electronic circuits easier to understand. This is also the foundation for how the home computer processes data. Understanding data at the binary level ("bit crunching") also makes various programming languages easier to understand.

How can I apply this?

In some of the upcoming experiments, we will process analog data using serial and parallel data. We will also use synchronous and asynchronous communication. The BASIC Stamp can be connected to other integrated circuits and exchange binary data. The BASIC Stamp can also be programmed to convert the binary data to a meaningful decimal form.

We'll use these techniques to measure voltage, sound, light, etc.

The BASIC Stamp has commands which automate the serial data transmit and receive processes in both asynchronous and synchronous modes. We will encounter the serial, synchronous method in Chapter #3. The BASIC Stamp also has features available to simplify the sending and receiving of parallel data, as we'll discover in Chapter #4 where asynchronous parallel data is transmitted.


Chapter 3: Basic Analog to Digital Conversion · Page 41

Chapter #3: Basic Analog to Digital Conversion

BUILD YOUR OWN DIGITAL DC VOLTMETER

A digital DC voltmeter (DC DVM) is a handy tool for measuring voltage between two contact points. In this experiment, we will build a DVM for measuring DC voltage in the 0 to 5 volt range. A common use for a DC DVM is testing the voltage (potential) between the two terminals on a battery.

A digital voltmeter is so named because it displays its measurements with digits. The digits 0 through 9 and a decimal point are used to display the voltage measurements as decimal values. The digits 0 and 1 could be used. It would still be a "digital" voltmeter, but it would have binary display instead of a decimal display. Making sense out of each measurement would be time consuming. Since our DVM processes its measurements in binary, we'll start with a binary display and then modify it to the more conventional and easy to read decimal display.

In Chapter #1, we used an LED circuit to display changes in analog voltage level applied to a circuit. As a “continuously variable value”, analog voltage varies within a continuous range. We'll use the potentiometer as we did in Chapter #1 to make a range of voltages that can vary continuously between 0 and 5 volts on the Board of Education.

Although information about analog voltage can be processed efficiently with binary devices, the voltage has to be sampled and described using binary numbers first. The ADC0831 is a common integrated circuit that does this job. It describes the analog information with binary numbers for devices that process binary information, such as the BASIC Stamp.

In this experiment, we will make a DVM using the BASIC Stamp together with the ADC0831 integrated circuit. A pot will be wired to the Board of Education and adjusted to make analog output voltage. The DVM will then be used to measure samples from the pot’s continuous range of voltage outputs.

Continuous range: A minimum value, a maximum value, and everything in between. When a source of voltage varies over a continuous range, it is considered an analog voltage.

We will use our DVM to sample voltages over a continuous range, from 0 to 5 volts. So, the voltage we measure might be 1.234 volts or 3.857564… volts, or 4.9999… volts, etc.

Page 42 · Basic Analog and Digital

(1) ADC0831

(1) 100 k potentiometer

(10) Jumper wires, give or take a few

The Potentiometer, a Source of Variable Voltage

There is a reason why the voltage at the wiper terminal of a pot changes when you turn the knob. The wiper terminal makes the single resistive element in the pot work like two resistors in series. Figure 3-1 shows two resistors in series. When input voltage is applied and output voltage is measured as shown in Figure 3-1, the circuit is referred to as a voltage divider. R1 and R2 are the resistances between the wiper and the other two terminals on the pot, and their values change as the pot is adjusted. Since the pot causes the R1 and R2 to vary, we can call our wiper terminal the output of a variable voltage divider.

Vdd

Vinput

=Vdd

R1

Voutput

R2

Figure 3-1

Voltage Divider Circuit

A Voltage Divider Circuit shows how the wiper in a potentiometer makes the single resistive element look like two resistors in series.

Voutput is the voltage measured at the wiper terminal.

Vss

Resistor Values: When you know the value of the two resistors in Figure 3-1, you can predict the output voltage using this equation.

Voutput = Vinput ×

R 2

R1

+ R 2

Not surprisingly, it’s called the voltage divider equation, and this technique for scaling down an input voltage is commonly referred to as using a voltage divider.


Chapter 3: Basic Analog to Digital Conversion · Page 43

Resistors in Series: A chain of resistors connected end to end. Three resistors in series are shown below. The three resistors can be viewed as a single resistance whose value is:

Rseries=R1+R2+R3

R1 R2 R3

The ADC0831 Integrated Circuit - An 8-bit Analog to Digital Converter

The ADC0831 is an integrated circuit referred to as an 8-bit analog to digital converter (A/D converter) with synchronous serial output. Let's look at what each of these terms mean:

An integrated circuit (IC) is a circuit with microscopic components implanted on the surface of a silicon wafer. The Analog and Digital Parts Kit has three chips used in these experiments. Each chip is a black casing with eight pins. The black casing houses and protects an integrated circuit.

An A/D converter measures an analog voltage sample and returns a binary number that describes the sample.

8-bit is the number of binary digits the ADC0831 uses to describe the analog voltage it samples. 8-bit is also the resolution of the A/D converter. You can count from 0 to 255 (decimal) using an 8-bit binary number. This means that the ADC0831 can approximate the voltage it measures as one of 256 levels. A higher resolution converter, such as 12-bit, would break the same voltage range into 4096 levels because you can count from 0 to 4095 with 12 binary bits.

Synchronous and serial are terms we learned about in Chapter #2. We sent serial binary digits (bits) to the BASIC Stamp using one pushbutton and the bits were synchronized to a second pushbutton that was used to send a clock signal. The ADC0831 works in a similar way. The difference is that the ADC0831 depends on a clock signal sent by the BASIC Stamp to time the sending of each serial output bit.

The BASIC Stamp will be programmed to read and store the 8-serial-bits transmitted by the ADC0831. We’ll also program the BASIC Stamp to display the decimal equivalent of the binary output. Next, we’ll use this decimal equivalent to calculate and displays the

Page 44 · Basic Analog and Digital

measured voltage in decimal form (our DVM output). The BASIC Stamp must also be programmed to send binary control signals to make the ADC0831 do its job.

Figure 3-2 shows a pin map of the ADC0831. Each pin has a number and a label. The number is important for getting the wires connected to the right pins when constructing your circuit. The labels indicate the function of each pin.

Binary control signal: A voltage signal with two possible states, low or high, that is sent to tell a device how or when to do something. The ADC0831 requires control signals to activate it and a clock signal to synchronize the sending of each of the it's output bits.

8

5

ADC0831

1

Vdd

Vref

1

/CS

Vcc

8

/CS

Vin(+)

2

Vin (+)

CLK

7

2

7

4

CLK

GND

Vin (-)

D0

3

6

6

D0

Vin(-)

3

4

GND

Vref

5

ADC0831

Figure 3-2

ADC0831 Circuit Symbol and Pin Map.

The pin map on the right shows the pins and labels according to where they are on the chip. The circuit symbol on the left also shows the pins and their labels, but it's typically drawn in a way that most conveniently fits into the schematic.

The notation for the ADC0831's inputs and outputs works as follows: Vin(+) is the analog input, and D0 is the serial output. VREF and Vin(-) are used to bias the IC. Vcc and GND are used for supplying power to the IC. Vcc is essentially the same term as Vdd on the Board of Education, and GND corresponds to Vss. /CS stands for active low chip select, and CLK stands for clock. Both are inputs for binary control signals.

Bias: A method of applying specific voltage levels at certain places in a circuit to calibrate or tune it.

To prime the ADC0831 for taking a measurement, the /CS pin has to receive a signal from the BASIC Stamp that starts high, then goes low. This signal has to stay low for the duration of the conversion. Then the CLK input must receive a single clock pulse (a term introduced in Chapter #2, Figure 2-8) to signify that the conversion should start at the next clock pulse. For this IC, a clock pulse starts low, goes high, then goes low again. It takes 8 more clock pulses to complete the conversion. Each time a clock pulse is received by the CLK input, another of the serial bits is sent by the D0 output.


Chapter 3: Basic Analog to Digital Conversion · Page 45

Electronics designers use data sheets to find the kind of information just discussed. Each IC manufacturer publishes data sheets for the integrated circuits they make. The information just covered on the pin map and control signals was condensed from a data sheet published by National Semiconductor, the maker of the ADC0831. Of course all of the datasheets are available on the manufacturer's web sites.

Build It

Figure 3-3 shows the schematic for this experiment. This is a fairly simple circuit to build, so let's try it without the breadboard example. Hopefully you're getting the hang of the list of connections described by a schematic. Remember, when working with the connections to an IC, use the index mark on the chip along with the pin map to figure out the pin numbers!

Vdd

Vdd

8

5

P0

1

Vdd/CS

Vref

Vin(+)

2

100 kΩ

7

4

P1

CLK

GND

Pot

6

P2

D0

Vin(-)

3

ADC0831

Vss Vss

Figure 3-3

Schematic

List of connections made on this schematic:

Pin 1 on the ADC0831 is connected to pin P0 on the BASIC Stamp.

The wiper terminal of the pot is connected to pin 2 on the ADC0831.

Of the two remaining terminals on the pot, one is connected to Vdd on the Board of Education, and the other is connected to Vss.

Pins 3 and 4 on the ADC0831 are connected to Vss.

Pins 5 and 8 on the ADC0831 are connected to Vdd.

Pins 7 and 6 on the ADC0831 are connected BASIC Stamp pins P1 and P2 respectively.

Program It

Program Listing 3.1 is the first step to a functional DC voltmeter. This program displays the 8-bit serial output of the ADC0831. Enter the code and save it as P3_1R0.bs2. There will be three revisions of this program listing, so it will become important to keep track of your source code versions.


Page 46 · Basic Analog and Digital

We'll modify the code so that it also displays the decimal conversion of the 8-bit binary number. Next we'll add some more code to adjust the number to a 5 volt scale. Make sure your circuit is constructed correctly and your programming cable and power source are connected, then run the program.

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

'Basic Analog and Digital - PL3_1R0.bs2

'Program Listing 3.1 Revision 0.

'{$STAMP BS2}

'{$PBASIC 2.5}

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

adcBits

VAR

Byte

v

VAR

Byte

r

VAR

Byte

v2

VAR

Byte

v3

VAR

Byte

' -----

[ Initialization

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

CS

PIN

0

CLK

PIN

1

DataOutput

PIN

2

DEBUG CLS

'Start display.

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

DO

GOSUB ADC_Data GOSUB Calc_Volts GOSUB Display

LOOP

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

ADC_Data: HIGH CS LOW CS LOW CLK

PULSOUT CLK, 210

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

Calc_Volts:

RETURN

Display:

DEBUG HOME

DEBUG "8-bit binary value: ", BIN8 adcBits

RETURN

Chapter 3: Basic Analog to Digital Conversion · Page 47

The Output

If the pot is adjusted somewhere in the middle of its range, the output displayed in the Debug Terminal should look similar to Figure 3-4. As you adjust the pot, the zeros and ones should change rapidly. Each time you stop adjusting the pot, the output should settle, and a new pattern of eight zeros and ones should display.

Figure 3-4

Debug Terminal

Output for Program

Listing 3.1.

If your Debug Terminal responds this way, it’s likely your circuit and program are working right. If it doesn’t do this, check the wiring on your circuit. Also make sure code is entered correctly. Sometimes just one wrong letter will cause the program not to work properly. The Debug Terminal could also be hidden from view. It can be accessed from the menus by selecting Run/Debug/New Terminal in the BASIC Stamp Editor.

About the Code

The first few lines of text in this program are comments that begin with apostrophes, and they don't have any function in the program aside from explaining it to someone reading the code, and identifying the BASIC Stamp model and PBASIC version.

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

'Basic Analog and Digital - PL3_1R0.bs2

'Program Listing 3.1 Revision 0.

'{$STAMP BS2}

'{$PBASIC 2.5}