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

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

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

Добавлен: 15.06.2025

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

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

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

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

Using Sensors to Detect and Measure

Listing 9-4. Measures and displays solar energy detected by solar-cell circuits at Channel 8 of ADC0848.

10

REM set A to address of ADC

20

A=0C000H

30

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

40

C=8

50

REM use single-ended mode, select channel, start convert

60

XBY(A)=8+C-1

70

REM FS=full-scale voltage (5V)/full-sun output (4.75V)

80

FS=1.05

90

B=XBY(A)

100

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

110

PRINT “Solar energy = ”,T," percent of full sun"

120

PRINT “press any key to take another measurement”

130

D=GET : IF D=0 THEN GOTO 130

140

GOTO 60

150

END

Level Translating

As you can see, not every sensor has an output that can connect directly to the ADC0848’s inputs. A sensor’s output may vary from -2 to -1V, from -0.5 to +0.5V, or from -12 to +12V. In all of these cases, you need to shift the signal levels and sometimes adjust the signal range to be compatible with a converter that requires inputs between 0 and 5 volts.

Figure 9-9 shows a general-purpose circuit that can amplify or reduce input levels, and can also raise or lower the entire signal by adding or subtracting a voltage. Separate, independent adjustments control the gain and offset. The circuit is a series of three op amps: a buffer, a level shifter, and an amplifier. The example circuit uses three of the devices in an LF347 quad JFET-input op amp. The LF347 has fast response and high input impedance. You may use a different op amp if you prefer.

The first op amp is a noninverting amplifier whose output at pin 1 equals VIN. The op amp presents a high-impedance input to VIN, to minimize loading effects.

The second op amp is an inverting summing amplifier that shifts pin 1’s voltage up or down as R5 is adjusted. Adjusting R5 raises and lowers the voltage at pin 7, but the signal’s shape and peak-to-peak amplitude remain constant.

The third op amp is an inverting amplifier whose gain is adjusted by R4. This amplifier increases or decreases the peak-to-peak amplitude of its input.

The Microcontroller Idea Book

167


Chapter 9

Figure 9-9. With this circuit, you can adjust the level and amplitude of an analog signal so that it varies from 0 to +5V.

As an example of how to use the circuit, if VIN varies from +12V to -12V, adjust R4 for a ±2.5-volt swing at VOUT, then adjust R5 to raise VOUT to achieve the desired 0-to-+5V swing.

Resistor R4 can increase the gain as well as decrease it. If you need to shift the signal level down instead of up, connect R5 to +15V instead of -15V. If you don’t need level shifting, you can remove R5 and connect pin 6 only to R1 and R2.

168

The Microcontroller Idea Book


Using Sensors to Detect and Measure

Choosing a Converter

The ADC0848 is a good, general-purpose chip, but you may want to look at other converters, depending on your application. Below are some things to consider when choosing an A/D converter. Example answers describe the ADC0848, using information from its data sheet:

What is the analog input range? (0V to V+)

How many analog channels are there? (8)

What is the converter’s resolution? (8 bits)

How fast is the conversion? (30 microseconds typical, 60 microseceonds maximum)

How accurate is the conversion? (±.1 LSB (least significant bit), 1/2 LSB version available)

What are the power-supply requirements and power consumption? (+4.5 to +6V, 15 milliwatts)

What input modes are available? (single-ended, differential, pseudo-differential)

How is the converter controlled and interfaced? (control signals are WE, OE, CS

Are there any special features on-chip (sample-and-hold, voltage reference, etc.)? (an internal clock times the conversions)

What package types are available? (24-pin 0.3" DIP, 28-lead chip carrier)

Sample and Hold Circuits

An additional component that you may need for rapidly changing analog inputs is a sample-and-hold circuit. To ensure correct conversions, the analog input must not change in value while the conversion is taking place.

A sample-and-hold circuit ensures that the analog signal is stable by sampling the signal at the desired measurement time and storing it, usually as a charge on a capacitor. The converter uses this stored signal as the input to be converted.

When do you need a sample-and-hold? The ADC0848 requires 60 microseconds or less to convert, so you should get good results with inputs that do not vary more than 1 bit in this amount of time. When a rapidly changing input does require one, sample-and-hold ICs like the LF398 are available, or you can use a converter like the ADC0820, which has the sample-and-hold on-chip.

The Microcontroller Idea Book

169

170

The Microcontroller Idea Book

Clocks and Calendars

10

Clocks and Calendars

Many 8052-BASIC systems can make use of a real-time clock that keeps track of seconds, minutes, hours, and even days, months, and years. You can use the clock to trigger operations at specified intervals, such as every five minutes, hourly, daily, on the first of the month, or whatever. Or, a data logger might record the time and date of each measurement it takes, or the times when it detects selected events.

BASIC-52 includes its own real-time clock that counts in 5-millisecond increments. For many timing tasks, this is all you’ll need. Another approach is to add a timekeeping chip that automatically keeps track of time and calendar information. Many clocks perform functions beyond simple timekeeping, such as generating periodic interrupts or acting as a watchdog that resets the microprocessor in case of program crashes. Plus, using a separate timekeeping chip means that you don’t have to devote any of the 8052-BASIC’s resources to the task.

This chapter describes how to use both BASIC-52’s real-time clock and Dallas Semiconductor’s DS1286 Watchdog Timekeeper chip.

BASIC-52’s Real-time Clock

The 8052-BASIC, like other computers, has a timing crystal or another frequency source connected to its XTAL pins. In fact, the chip will do nothing at all without this input, since it is what clocks instructions into the chip’s CPU for execution. While this clock provides

The Microcontroller Idea Book

171


Chapter 10

an essential timing reference, by itself it doesn’t keep track of real-world time measured in seconds, minutes, and hours. But if you know the crystal’s frequency, you can measure seconds by counting the oscillations of the crystal. This is what BASIC-52’s real-time clock does.

A CLOCK1 statement starts the real-time clock, which causes the TIME operator to increment every 5 milliseconds. Reading the TIME operator tells you the number of seconds that have passed since the clock was enabled. CLOCK0 stops the clock and freezes TIME at its current value. TIME resets to 0 when the count reaches 65536 seconds (18 hours, 12.3 minutes), or when the statement TIME=0 executes. If you stop the clock and then then restart it, TIME will continue counting from where it left off, unless you first reset it to 0.

The ONTIME instruction jumps to a subroutine whenever TIME reaches the value you specify. Because the ONTIME subroutine is an interrupt routine, you use RETI, not RETURN, to end it.

Listing 10-1 is a program that counts seconds, minutes, and hours, and displays the current reading once per second. For accurate timekeeping, the XTAL operator must match the value of your timing crystal.

You can also use ONTIME to trigger periodic operations. Listing 10-2 is a program that toggles bit 7 of Port 1 once per second and displays the logic state of the bit after each toggle.

Clock Accuracy

The more accurate your timing reference, the more accurate your clock will be. You can tune the frequency of a crystal slightly by varying the value of one of the capacitors that connects from the crystal to ground.

Temperature variations will cause a crystal’s frequency to drift. Crystal accuracy is rated in parts per million per degree Celsius (often shortened to ppm). Over time, a crystal rated at ±10 ppm should vary no more than 0.001 percent per degree Celsius, or 0.86 seconds per day, if the temperature varies no more than ±1 degree Celsius. If your clock must be super-accurate, choose the most stable crystal you can find and and avoid temperature fluctuations.

You might think that you can get a more accurate real-time clock by adjusting XTAL to match your crystal’s actual frequency, rather than its rated value. You could measure the crystal’s frequency with a frequency counter, or experiment by varying the value of XTAL and monitoring the real-time clock to find the best match. For example, if your 12Mhz crystal actually oscillates at 11.97 Mhz, you could set XTAL equal to 11970000.

172

The Microcontroller Idea Book

Clocks and Calendars

Listing 10-1. Uses BASIC-52’s real-time clock to count seconds, minutes,

and hours.

10

REM set XTAL to match your crystal’s frequency

20

XTAL=12000000

30

REM set and initialize clock

40

GOSUB 200

50

REM increment clock variables once per minute

60

DO

70

ONTIME 60,500

80

WHILE 1=1

90

END

200

PRINT “Please enter the current time:”

210

INPUT “AM (0) or PM (1)? ”,AP

220

INPUT “Hour (1-12)? ”,H

230

INPUT “Minutes (0-59)? ”,M

240

INPUT “Seconds (0-59)? ”,S

250

REM initialize clock to current seconds

260

TIME=S

270

REM start clock

280

CLOCK 1

290

RETURN

500

REM increment and display time once per minute

510

REM reset seconds

520

TIME=0

530

REM increment minutes

540

M=M+1

550

IF M=60 THEN

GOSUB 700

560

REM display current time

570

PRINT “the time is :”

580

PRINT H,"hours"

590

PRINT M,"minutes"

600

IF AP=0 THEN

PRINT “ AM” ELSE

PRINT “ PM”

610

RETI

700

REM once/hour timekeeping

710

REM reset minutes

720

M=0

730

REM increment hours

740

H=H+1

750

REM at 12:00, toggle am/pm

760

IF H=12 THEN AP=A

770

REM at 1:00, reset hours

780

IF H=13 THEN H=1

790

RETURN

The Microcontroller Idea Book

173