18.11.2014 Views

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Sensors to Detect and Measure<br />

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

10 REM clear flip-flop<br />

20 XBY(0E400H)=0<br />

30 XBY(0E400H)=1<br />

40 DO<br />

50 REM read port<br />

60 A=XBY(0E000H)<br />

70 REM see if bit 0 is set<br />

80 IF A.AND.1=1 THEN GOSUB 200<br />

90 WHILE 1=1<br />

100 END<br />

200 PRINT “vibration alarm”<br />

210 REM clear flip-flop<br />

220 XBY(0E400H)=0<br />

230 XBY(0E400H)=1<br />

240 RETI<br />

Many sensors have analog outputs, however. <strong>The</strong>y vary continuously in response to changes<br />

in the properties they sense. For example, the resistance of a Cadmium-sulfide (CdS)<br />

photocell varies with the intensity of light hitting it. If you want to use an analog sensor like<br />

this in an 8052-BASIC system, you need to add some components to convert the analog<br />

signal to digital.<br />

A comparator provides a way to detect a specific analog voltage. Figure 9-3 shows how to<br />

use a comparator to detect a specific light level on a photocell.<br />

A comparator is a special form of op amp with analog inputs, but a digital output. In Figure<br />

9-3, pin 4 is a reference voltage, and pin 5 is the input being sensed. When the sensed input<br />

is lower than the reference, the comparator’s output is low. When the sensed input is higher<br />

than the reference, the comparator’s output is high.<br />

R1 and the photocell form a voltage divider. As the light intensity hitting the photocell<br />

increases, its resistance decreases and pin 5’s voltage rises. To detect a specific light level,<br />

adjust R2 so that VOUT switches from low to high when the light reaches the desired intensity.<br />

You can read the logic state of VOUT at any input port pin.<br />

R4 is a pull-up resistor for the LM339’s open-collector output. R3 adds a small amount of<br />

hysteresis, which keeps the output from oscillating when the input is near the switching<br />

voltage.<br />

<strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong> 157

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!