A/D Programming - 1

<= Back to Co-Processor page

A stamp can figure out analog voltages by various means. They are, however, not as quick or as accurate as a true Analog to Digital converter, or A/D. Our Co-Processor has 5 channels of 10 bit A/D. This means the analog input voltage is broken up into 1024 discrete steps for measuring. That is 0.1% resolution. The range goes from 0 volts minimum to a maximum of the Co-Processor power supply voltage which is nominally 5.0 volts. Our Controller board has a LM2940 Low Drop Regulator as its logic regulator. The regulator isn't guaranteed accurate to anywhere near 0.1%. The Worst Case, over temperature (0 to 125°  C) spec is 4.75 to 5.25 volts. If our robot, Libby, is anywhere near 100° C, the A/D being off by 5% will probably not be foremost on your mind! At room temperature the tolerance tightens to 4.85 to 5.15 volts. In practice, the regulators are much tighter than this. In our lab, with a randomly chosen unit the regulator measured 4.99 volts. Whatever the exact voltage on your regulator, since they tend to be stable, you can easily calibrate the Co-Processor output value to get highly accurate voltage readings.

Often you only need an 8 bit value. If you want only the most significant 8 bits, you do a read to the desired channel for the first 8 bits.

Here's the code to get it -



A2D_val  VAR    BYTE'returned 8 bit A/D value
 AUXIO'2p40 only
 SEROUT 10, 1021, [116]
 SEROUT 10, 1021, [116]'Reset Co-Processor
  ' only needed once for initialization
 
read_A2D_8b:'Read only the Most Significant 8 Bits of the A/D
 
 SEROUT 10, 1021, [122]'Read first byte of A/D channel 2
  ' command = 120 + channel number
 SERIN 9, 1021, A2D_val'Get the Most Significant 8 bits.
  ' into A2D_val. 0 = 0 volts, FF = 5 volts *(255/256)
  'that's it, all done!


But we have a 10 bit A/D

A byte only holds 8 bits so the value has to be broken into 2 bytes. You just saw how to get the most significant 8 bits. Ideally, the second byte will contain the least significant 2 bits such that they easily go into a WORD variable. The cleanest way to do that is to put those bits in the most significant bit positions of the least significant byte. When the A/D channel is read you first get the most significant byte as shown above. To get the second byte containing the last 2 LS bits, you just send the read command = 85 (decimal) or $55 (hex). That will get the second byte of whatever A/D channel you read last. In this next code, let's try using the channel and serial command as variables. They could, of course, continue to be constants like the 8 bit code above. The only real differences needed for 10 bits are the second read command and using a word to hold the result.


chanVAR    NIB'variable used for channel number
SerCmdOutVAR    BYTE'Serial Command to the Co-Processor
adc_valueVAR    WORD'10 bit A/D value left justified
 AUXIO'2p40 only
 SEROUT 10, 1021, [116]
 SEROUT 10, 1021, [116]'Reset Co-Processor
  ' only needed once for initialization
 
read_A2D_10b:'Read all 10 Bits of the A/D from the
  ' channel number in chan and load the
  ' data into a word named adc_value.
 chan = 3'set channel number (0->4 are valid)
 SerCmdOut = 120 + chan'command to read first byte of A/D command
  ' = 120 + channel number
 SEROUT 10, 1021, [SerCmdOut]'Read first byte of A/D
 SERIN 9, 1021, [adc_value.HIGHBYTE]
  'Get the Most Significant 8 bits.
 SEROUT 10, 1021, [85]'Now read 2nd byte of A/D
' command = 85 (decimal) or $55 (hex)
' independent of which A/D channel
 SERIN 9, 1021, [adc_value.LOWBYTE]
  'the data coming back is the least significant
  ' 2 bits but left justified - that is
  ' they are in most significant 2 bit positions.
  ' The bottom 6 bits are 0.


After executing the code above, adc_value = 0 for 0 volts input, or 1111 1111 1100 0000 for Processor power supply voltage*(1023/1024) = 5*(1023/1024) volts. Voltages in between will give values in between.

With a step size of only 5 millivolts (0.005 volts) you can make a super datalogger. You probably want the program to output decimal values instead of binary or hexadecimal. Here is a datalogger program using our controller board with an attached PC as the display.

It is given as a text file for a Datalogger but it can be copied into the Stamp editor as a BS2p program.

<= Back to Co-Processor page

Copyright © 2002 Blue Bell Design, Incorporated. All rights reserved.