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

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

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

Добавлен: 15.06.2025

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

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

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

Chapter 9

Figure 9-2. Two ways to read the state of a normally open switch/sensor: (A) basic input, (B) latched input.

Listing 9-1 assumes that in Figure 9-1B, the Q output connects to bit 0 of an input port at E000h, and the CLR input connects to bit 0 of an output port at E400h.The program clears the flip-flop, then reads the input port continuously until the bit in question goes high. It then displays a message, clears the flip-flop, and returns to the main program.

Analog Sensors

The above sensors have just two states: on and off, or open and closed. This makes them easy to use in digital circuits, which recognize only two logic states.

156

The Microcontroller Idea Book

Using Sensors to Detect and Measure

Listing 9-1. Reads and clears a flip-flop output connected to an input port pin.

10

REM clear flip-flop

20

XBY(0E400H)=0

30

XBY(0E400H)=1

40

DO

50

REM read port

60

A=XBY(0E000H)

70

REM see if bit 0 is set

80

IF A.AND.1=1 THEN GOSUB 200

90

WHILE 1=1

100

END

200

PRINT “vibration alarm”

210

REM clear flip-flop

220

XBY(0E400H)=0

230

XBY(0E400H)=1

240

RETI

Many sensors have analog outputs, however. They vary continuously in response to changes in the properties they sense. For example, the resistance of a Cadmium-sulfide (CdS) photocell varies with the intensity of light hitting it. If you want to use an analog sensor like this in an 8052-BASIC system, you need to add some components to convert the analog signal to digital.

A comparator provides a way to detect a specific analog voltage. Figure 9-3 shows how to use a comparator to detect a specific light level on a photocell.

A comparator is a special form of op amp with analog inputs, but a digital output. In Figure 9-3, pin 4 is a reference voltage, and pin 5 is the input being sensed. When the sensed input is lower than the reference, the comparator’s output is low. When the sensed input is higher than the reference, the comparator’s output is high.

R1 and the photocell form a voltage divider. As the light intensity hitting the photocell increases, its resistance decreases and pin 5’s voltage rises. To detect a specific light level, adjust R2 so that VOUT switches from low to high when the light reaches the desired intensity. You can read the logic state of VOUT at any input port pin.

R4 is a pull-up resistor for the LM339’s open-collector output. R3 adds a small amount of hysteresis, which keeps the output from oscillating when the input is near the switching voltage.

The Microcontroller Idea Book

157


Chapter 9

Figure 9-3. The comparator ’s output switches at the light level determined

You can use the same basic circuit with other sensors that vary in resistance. Replace the photocell with your sensor, and adjust R2 for the switching level you want. Connect VOUT to any input port pins.

Measuring Analog Signals

Sometimes you need something more sophisticated than a simple level detector. An analog-to-digital converter (ADC) enables you to measure the precise value of an analog voltage.

Some versions of the 8052 microcontroller, including Philips’ 80C562, include an on-chip ADC, but the 8052-BASIC doesn’t have this feature, so you have to add it externally. There are dozens of converters available, with varying resolution, accuracy, speed, method of conversion, number of analog inputs, and so on. Another option is to use an integrated sensor that contains its own ADC and has a digital output.

National Semiconductor’s ADC0848 is an easy-to-use, low-cost, general-purpose, eightchannel ADC. In many ways, the ADC0848 is similar to National’s long-popular ADC0808/9 A/D converters, but with some advantages. The ADC0848 does not require an external clock; its control signals interface directly to many microcontrollers; and it is faster, with a typical conversion time of 30 microseconds.

National’s data sheet for the ADC0848 has complete specifications, applications information, and example circuits. You’ll want a copy of the data sheet if you plan to use the chip.

158

The Microcontroller Idea Book

Using Sensors to Detect and Measure

Figure 9-4. The ADC0848 interfaces easily to an 8052-BASIC system, and can measure up to eight analog inputs.

Figure 9-4 shows an ADC0848 interfaced to Chapter 3’s 8052-BASIC system. The connections are similar to those used for RAM. WRITE and READ drive the converter’s WR and RD inputs. The converter is shown addressed at C000h, but you can use any unused chip-select

The Microcontroller Idea Book

159

Chapter 9

line. Digital outputs DB0-DB7 connect to the data bus (D0-D7). DB0-DB4 also function as control inputs MA0-MA4.

Up to eight analog inputs can connect to pins 2-9 on the ADC0848. The voltage at VREF determines the converter’s full-scale voltage, which is the input that results in an output of 11111111, or FFh. For maximum range, connect VREF to the +5V supply or to a more precise 5-volt reference like an LM336-5.0 reference diode. The analog inputs can then range from 0 to +5 volts.

Adjusting the Range

If your sensor’s output is much less than 5 volts, you can increase the resolution of the converter by connecting VREF to a voltage slightly larger than the highest voltage you expect to measure.

To illustrate, consider a sensor whose output ranges from 0 to 0.5 volt. The 8-bit digital output of the converter represents a number from 0 to 255. If VREF is 5 volts, each count equals 5/255, or 19.6 millivolts. A 0.2-volt analog input results in a count of 10, while a 0.5-volt input results in a count of 26. If your input goes no higher than 0.5 volt, your count will never go higher than 26, and the measured values will be accurate only to within 20 millivolts, or 1/255 of full-scale.

But if you adjust VREF down to 0.5 volts, each count now equals 0.5/255, or 2 millivolts. A 0.2-volt input gives a count of 102, a 0.5-volt input gives a count of 255, and the measured values can be accurate to within 2 millivolts.

However, if you decrease VREF as described above, you also increase the converter’s sensitivity to noise. With VREF at 5 volts, a 20-millivolt noise spike will cause at most a 1-bit error in the output. If you decrease VREF to 0.5 volt, the same spike can cause an error of 10 bits, since each bit now represents 2 millivolts, not 20.

Minimizing Noise

The rapid switching of digital circuits can cause voltage spikes in the ground lines, and these can cause errors in analog measurements. Good routing of ground wires or pc-board traces can minimize noise in circuits that mix analog and digital circuits.

To minimize noise, provide separate ground paths for analog and digital signals. In Figure 9-4, this means that AGND and any ground connections related to the analog inputs or VREF should be wired together, but kept separate from the ground connections for the digital circuits, including logic chips, the 8052-BASIC, and memory chips. The two grounds are tied together at one place only, as near to the power supply as possible. The schematic uses

160

The Microcontroller Idea Book


Using Sensors to Detect and Measure

different ground symbols for the two ground paths. Also be sure to include decoupling capacitors at pins 10 and 12.

Measuring Modes

To allow for different circuit requirements, the ADC0848 offers a choice of three softwareselectable modes of operation: single-ended, differential, and pseudo-differential. Figure 9-5 illustrates.

In single-ended mode, each analog input is referenced to AGND. This is the simplest mode and will work fine for many applications.

Listing 9-2 causes the ADC to convert each of the eight channels in turn and displays the results on the host computer. For testing the circuits, you can connect a the wiper of a potentiometer to each channel, with the other two leads connecting to AGND and VREF. Verify that the readings for each channel vary from 0 to 255 as you vary the input voltage.

The other modes are useful for more critical measurements where you need to reject background noise or offset voltages.

In differential mode, each channel is paired with an adjacent one, with the voltage on one channel referenced to the voltage on the other.

For example, you could connect an output from a sensor to channel 2 and a ground or other reference from that sensor to channel 1. With differential mode selected, channel 1 will read the difference between channel 1’s and channel 2’s voltage. This mode cancels out errors due to noise that is common to both channels in the pair, such as 60-cycle power-line interference. However,because each channel uses two analog inputs, this mode limits you to four channels.

The third mode of operation is pseudo-differential. In this mode, channels 1-7 are all referenced to channel 8. This allows you to make 7 measurements, all with the same reference. This mode is useful if you are have multiple sensors in the same location. Also, if you connect channel 8 to a voltage greater than AGND, the converter’s range will shift to match, with a 0 output occurring when an input equals channel 8’s voltage.

Reading the ADC

To begin a conversion on the ADC0848, the 8052-BASIC writes to the converter indicating the desired channel and mode. Bits 0-2 specify the channel (000=1, 001=2, 010=3, etc.), and bits 3-4 specify the mode of operation (00=differential, 01=single-ended, 11=pseudo differential). So, for example, in Figure 9-4’s circuit, to begin a single-ended conversion at channel 5, you would write 0000 1101, or 0Dh, to the converter’s address.

The Microcontroller Idea Book

161

Chapter 9

Figure 9-5. Measurement modes available with the ADC0848 are single-ended (A), differential (B), and pseudodifferential (C).

162

The Microcontroller Idea Book

Using Sensors to Detect and Measure

Listing 9-2. Displays measurements of channels 1 through 8 on the ADC0848.

10

REM use single-ended mode

20

REM set A to address of ADC

30

A=0C000H

40

FOR I=1 TO 8

50

XBY(A)=8+I-1

60

PRINT “Channel ”,I," = “, : PH0. XBY(A)

70

NEXT I

80

END

Writing to the converter causes the conversion to begin automatically. When the conversion is complete, a read operation to the converter’s address causes the the converted value to appear at DB0-DB7, where the 8052-BASIC reads it.

The INTR pin indicates when a conversion is complete, and can be used to trigger a read operation. INTR is low when a conversion has occurred that has not yet been read. It goes high after a read and remains high until the next conversion is completed. BASIC-52 is slow enough that you don’t have to worry about waiting the maximum 60 microseconds between requesting a conversion and reading the result, so you can ignore INTR and read the result any time after a write.

Packaging Options

The ADC0848 comes in a 24-pin “skinny” DIP, with the pin rows spaced 0.3" apart as on a 14-pin DIP. Sockets of this size, especially wire-wrap, can be hard to find, but in a pinch you can place a 16-pin and 8-pin socket end to end. If you need only four analog inputs, use the ADC0844, in a 20-pin skinny DIP.

Sensor Examples

Now let’s look a couple of examples of sensors that you can connect to the ADC0848.

Temperature

The first is an LM34 temperature sensor. Unlike many other temperature sensors, the LM34 requires no calibration. Its output is a simple 10 millivolts per degree Fahrenheit. As Figure 9-6 shows, it’s available in several versions. The ones with a narrower range or lower resolution are cheaper. If you prefer Celsius readings, use the LM35.

Figure 9-7 shows how to use an LM385-2.5 voltage reference to set the ADC’s VREF to 2.5V. The converter then can measure temperatures from 0 to 250 degrees, and each bit in the ADC0848’s output represents a 9.8 millivolt change in the sensor’s output.

The Microcontroller Idea Book

163


Chapter 9

Figure 9-6. The LM34 and LM35 temperature sensors have outputs of 10 millivolts per degree, and need no calibration.

Listing 9-3. Measures and displays temperature reading at ADC0848’s

Channel 2.

10

REM set A to address of ADC

20

A=0C000H

30

REM use single-ended mode

40

REM set C to channel to read (1-8)

50

C=2

60

XBY(A)=8+C-1

70

VREF=2.5

80

B=XBY(A)

90

T=INT(VREF*B*100/255+.5)

100

PRINT “Temperature = ”,T

110

PRINT “Press any key to take another measurement”

120

D=GET : IF D=0 THEN GOTO 120

130

GOTO 60

140

END

164

The Microcontroller Idea Book

Using Sensors to Detect and Measure

Figure 9-7. The LM385 series of voltage references includes 1.2V, 2.5V, and an adjustable version.

Listing 9-3 assumes that an LM34 connects to CH2 on the ADC0848, and that VREF is 2.5V. On request, it displays the current temperature.

For a smaller range, create a 1.2V reference with an LM385-1.2 and change line 70 in the program to match. Another option is the LM385 adjustable reference, which contains a reference diode and feedback amplifier. With the addition of a voltage source and resistors in a voltage divider, you can set the LM385’s output to the reference voltage you need. Use the formula shown to vary the resistors for different outputs.

Solar Energy

Figure 9-8 shows another sensor application, a solar cell that generates a current proportional to the intensity of the light hitting the cell. The output of the solar cell in the example varies

The Microcontroller Idea Book

165

Chapter 9

Figure 9-8. VOUT varies with the light intensity, and output current, of the solar cell.

from 0 in darkness to 300 milliamperes in full sun. The voltage across the cell is about 0.5 volt.

An LF353 dual op amp converts the solar cell’s current into a voltage that the ADC0848 can measure. Most of the solar cell’s current flows through the 1-ohm resistor to ground. Since the solar cell’s voltage is only about 0.5 volt, the power dissipated by the 1-ohm resistor is only about 0.15 watt in full sun.

About one percent of the solar cell’s output flows through the 100-ohm resistor. This same current flows through the 1.6K resistor, with the result that the voltage at pin 1 of the LF353 varies from 0 to about -4.75V. This voltage is proportional to the intensity of the light hitting the solar cell. The second op amp is an inverter that converts the voltage to positive levels that the ADC0848 can measure.

Listing 9-4 assumes that pin 7 of the LF353 connects to Channel 8 of the ADC0848. On request, the program converts the analog input and displays the result.

166

The Microcontroller Idea Book