Вы находитесь на странице: 1из 4

This document is available online at www.ranchbots.com/club.

html Interfacing Analog to Digital Circuits (Comparators and A/D conversion): In the last section we discussed sensors and using the voltage divider to get a voltage that can be sensed out of the sensor for use in a robot. In this section we learn how to take that output voltage and use it to get a simple, digital output signal. We will examine two methods: the comparator circuit and the Analog to Digital (or A/D) converter. Comparator: As the name implies, a comparator circuit compares two values and sets an output depending on those values. A comparator is commonly available in IC form. The LM324 IC is a typical example. The LM324 is a general purpose operational amplifier circuit that can be used for many purposes. For now, we will only discuss its use as a comparator circuit. The comparator sets a reference voltage using a voltage divider. Then one sends the input signal to the comparator and it will compare the input voltage to the ref voltage. If the input voltage is equal to or greater than the ref voltage then the comparators output will be HIGH. A diagram of this setup is shown below.

The triangular symbol in the middle is the LM324. Note the numbers 1,2, etc. These are common pin numbers on an 8 pin IC for this device. But you should always check the pin out for the part you are using to be sure you are connecting it properly. Pin 4 is the supply voltage for the IC. Pin 1 is the comparator output. Pin 3 is the non-inverting input and pin 2 is the inverting input. The inverting and non-inverting name comes from the polarity of the input. The reference voltage is set by a voltage divider circuit to pin 2. Pin3 then goes to the output of the sensor. If we set a ref voltage of 3.75 V by choosing R1= 30K and R2 = 10K then the comparators output will be low while the sensors output voltage is between 0 and 3.74V. At 3.75V and above, the comparators output will be HIGH (1). We can graph this output response in terms of the input as below:

where Vref in this case is 3.75V. The opposite response can be had as well. That is, you can set up a comparator circuit so that the output is HIGH while the input is less than the ref voltage, and LOW when the input equals or exceeds the Vref. To do this you simply reverse the setup for the inverting/noninverting inputs. In other words, put the Vref generating voltage divider on the non-inverting input (pin 3 of the sample circuit above) and the output of the sensor on the inverting input (pin2). Many microcontrollers (The AVR 2313 on the AVR Botboard for instance) have a comparator built in. You just look at the spec for the part and determine what pin has the inverting input and which has the noninverting input. On the AVR BotBoard, pin 12 (accessible at connector J9) is equivalent to pin 3 in the figure above and is the non-inverting input, where the input from the analog circuit goes in. Pin 13 (accessed at J8) is the inverting input and is where you put your reference voltage divider (equivalent to pin 2 above). You could also use a pot in place of R1 and thus you could tweak the trigger value to be what you want it to be without having to replace R1. Analog to Digital Conversion (A/D): With A/D, the goal is not to have an output that is simply ON or OFF depending on the input analog voltage, but rather to have an output that tells the digital device we want to interface with WHAT THE ANALOG VOLTAGE IS exactly. A/D converters are specified by the number of digital bits of precision that they have. The most typical is an 8 bit A/D. Recall that with binary numbers the total range of values is calculated with the formula

range = 2 n
where n is the number of bits. So with 8 bits the range is 256 unique values. Remember that non-negative numbers start with 0 not 1, so this is a range of 0 to 255. A/D converters like a comparator are referenced to an input voltage that is normally equal to the full range value of output voltage. In our example, this would be 5V. This ref voltage is then divided equally among the range of the A/D. For this example, 256. Thus Increment scale = 5V / 256 = 0.02V Every increase of 0.02 VDC would result in the A/D converter output being increased by 1. By this scale then an input to the A/D converter from a sensor of 0Vwould result in an output of 0. If the sensor outputs 5V then the A/D output is 255. If the sensor outputs 3.75 V then the A/D converters output would be 187. 3.75 VDC / 0.02VDC = 187.5V Note that the .5 is dropped completely. An A/D converters output is an integer (whole number). Further note that it does not round the number. It simply truncates (cuts off) any remainder.

If youre thinking ahead, youll be asking but how does the A/D converter communicate 187 decimal in terms of a digital signal. The answer is it drives 8 output lines (bits 7 down to 0), each one with a state of ON or OFF depending on the output. Binary numbers: Binary numbers use a number base of 2. The numbers you work with every day are base 10. Each place to the left of a decimal is a power of 10 you may recall. The number 187 in decimal is represented by 7 in the ones place (100), 8 in the tens place (101) and 1 in the hundreds place (102). Binary numbers work the same way except instead of powers of ten, we have powers of 2. An 8 bit binary number has 8 places. The right most is the least significant bit (LSB) and the leftmost is the most significant bit (MSB) ___ Power Multiplier 27 128 ___ 26 64 ___ 25 32 ___ 24 16 ___ 23 8 ___ 22 4 ___ 21 2 ___ 20 1

Some examples:0 in binary is 0 just like in decimal. 1 is also just like in decimal, after that though 2 in decimal is 10 in binary (pronounced one, zero, not ten). 3 decimal or 3d is 11b (b for binary). Thats 1 times 20 + 1 times 21 or 1 plus 2 which is 3 (decimal). So what is 187? With 8 bits what is the largest place multiplier that is just below 187? 128 or 27. 187 128 is 59. Now, what base 2 place multiplier is just under 59? 32 or 25. Notice 64 or 26 then gets a zero in its place. Now we have 128 + 32 = 160. That leaves another 27 that has to be accounted for in our 8 bit number. What is just under 27? 16 or 24. 128 + 32 + 16 = 176. 187 176 = 11, so 8 (23) is next which leaves 3d, so both 2 and 1 (21 and 20) get filled in. This gives us a binary number of 187d = binary Power Multiplier _1_ 27 128 _0_ 26 64 _1_ 25 32 _1_ 24 16 _1_ 23 8 _1_ 22 4 _1_ 21 2 _1_ 20 1

187 in decimal is binary 10111111. 9 in decimal is 8 bit binary 00001001. Zeros that are to the left of the most significant digit are meaningless just like in decimal notation, so binary 00001001 can also be written as 1001b, but if we are specifying 8 bit binary you should put those zeros there for clarity. Remember the idea of binary numbers and logic. You were born into a computer age world and I assure you, these concepts are not going anywhere anytime soon. Okay, back to our A/D converter output. On a standard A/D IC like the ADC0804 there are 8 output lines, DB0 to DB7 as the symbol in the schematic below shows. DB0 is the least significant digit and so is the 20 line. DB7 is the 27 line.

So now you have 8 ON/OFF circuits (a data BUS) that represent the analog value that comes out of your sensor. The output of this chip could be ran to LEDs if you wanted a visual representation of the analog voltage or inputted into a microprocessor that would take the data and perform some desired operation. The RD and CS with bars over them are equivalent to the nREAD and nCS signals you saw in the RAM access synchronous example. NOTE: Just as microcontrollers frequently have comparators built into them, they also frequently have an A/D built into them. The AVR BotBoard has this feature built onto the board. The connectors marked AD0 and AD1 are inputs to an A/D chip that in turns communicates with the Atmel 2313 chip. There is a software routine to get this information for you. This routine starts on line 74 of the file Test2313.bas. The output of this function will be a value stored in an array called C in positions C(1) and C(2). This will be a 12 bit value, so you have quite a bit of resolution of measurement of an analog voltage with the AVR BotBoard. Other larger pin count micros will have dedicated A/D modules, 8 up to 12 pins or more each capable of being configured to be an A/D channel.

Вам также может понравиться