Simple A to D
TechnoRobbo
Posts: 323
This is a real simple A to D made from a Radio Shack Quad Comparator compatible to the LM339 and a BS2.
It uses an PWM command and RC circuit to create the reference voltage and a short and very fast successive approximation routine to read the input voltage. The result is very linear yielding a value from 0 (0v)·to 255 (5V).
the schematics and code are attached below.
' {$STAMP BS2}
' {$PBASIC 2.5}
IDX······ VAR Nib
X········ VAR Byte
LOW 7
INPUT 15
Main:···· 'Successive Aproximation Routine
DO
··· x=128
··· FOR idx=7 TO 0
····· X=X | DCD idx
····· PWM 7, X, 5
····· IF IN15=0 THEN
········ X=X ^ DCD idx
····· ENDIF
··· NEXT
··· DEBUG HOME,DEC3 X
LOOP
END
·····
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 12/30/2006 4:29:45 PM GMT
It uses an PWM command and RC circuit to create the reference voltage and a short and very fast successive approximation routine to read the input voltage. The result is very linear yielding a value from 0 (0v)·to 255 (5V).
the schematics and code are attached below.
' {$STAMP BS2}
' {$PBASIC 2.5}
IDX······ VAR Nib
X········ VAR Byte
LOW 7
INPUT 15
Main:···· 'Successive Aproximation Routine
DO
··· x=128
··· FOR idx=7 TO 0
····· X=X | DCD idx
····· PWM 7, X, 5
····· IF IN15=0 THEN
········ X=X ^ DCD idx
····· ENDIF
··· NEXT
··· DEBUG HOME,DEC3 X
LOOP
END
·····
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 12/30/2006 4:29:45 PM GMT
Comments
http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv25.pdf
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Utilizing the other comparators on the chip you can increase the A/D resolution by using pull up and pulldown resistors in voltage dividing combinations - In the example below on comparator handles 0v to 2.5v and the other handles 2.5v to 5v.· I sum them together to double the resolution. This allows the stamp to read 1/100th of a volt (9 bit resolution). Sometimes the range of the sensor your using is to large and impractical -Using this technique to·adjust the your A/D reference voltage may yield even higher resolutions without the need for more pins or components.
' {$STAMP BS2}
' {$PBASIC 2.5}
IDX······ VAR Nib
X········ VAR Byte
stdout··· VAR Word
INPUT 15
INPUT 14
Main:···· 'Successive Aproximation Routine
DO
··· x=0
··· FOR idx=7 TO 0
····· X=X | DCD idx
····· PWM 7, X, 5
····· IF IN15=0 THEN
········ X=X ^ DCD idx
····· ENDIF
··· NEXT
··· stdout=x
··· 'double it
··· x=0
··· FOR idx=7 TO 0
····· X=X | DCD idx
····· PWM 6, X, 5
····· IF IN14=0 THEN
········ X=X ^ DCD idx
····· ENDIF
··· NEXT
··· stdout=stdout + x
··· DEBUG HOME,DEC3 stdout
LOOP
EN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 1/2/2007 3:51:59 AM GMT
Utilizing the other comparators on the chip you can increase the A/D resolution by using pull up and pulldown resistors in voltage dividing combinations
Can you give some·more details on how to do this what is in red
What i would like to do is have the following
comparator 1······· ·0 to 5 volts
comparator 2·········5 to 10 volts···· Any· ··s
comparator 3········ 10 to 15 volts
Please tell me if this can be done and how you go about doing this
·Have tried this part of what i am talking about before
Thank You for any ··that you have on how to do this
Sam
use PWM 7, X, 10· instead of PWM 7, X, 5 (replace 7 with the pin your using)
·················
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 1/5/2007 2:14:37 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Thanks for your post and your ·
·If I have time this weeken I will· built this and try it and let you how it works
I just want to make sure of one thing and that is that all "vdd"'s are 5 volts
Please let know if I sm right about this
Thank again for help and you time
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
Sam
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Thanks for your reply
Thanks for telling about pin 3 on the LM339 and all of the VDD"s
·I will try to have some fun··········· ·· ·But these Guys are having way to much fun
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
Sam
How would you write the code for pins 13 and pin 5
' {$STAMP BS2}
' {$PBASIC 2.5}
IDX VAR Nib
X VAR Byte
stdout VAR Word
INPUT 15
INPUT 14
INPUT 13
Main: 'Successive Aproximation Routine
DO
x=0
FOR idx=7 TO 0
X=X | DCD idx
PWM 7, X, 5
IF IN15=0 THEN
X=X ^ DCD idx
ENDIF
NEXT
stdout=x··········· · '........................................
'double it
x=0
FOR idx=7 TO 0
X=X | DCD idx
PWM 6, X, 5
IF IN14=0 THEN
X=X ^ DCD idx
ENDIF
NEXT
' I am not sure what·is suppost to be here·...........>>>>>>>>
This
'stdout=stdout + x·········· ·'.......................................
·Or this
My thinking is that this is what go here
'stdout=x
'double it
'x=0
·
FOR idx=7 TO 0
X=X | DCD idx
PWM 5, X, 5
IF IN13=0 THEN
X=X ^ DCD idx
ENDIF
NEXT
stdout=stdout + x ......................................' Is this right
DEBUG HOME,DEC3 stdout
LOOP
LOOP
END
Thanks for all of the help and that·you given to me in this matter
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
Sam
Post Edited (sam_sam_sam) : 1/10/2007 3:03:45 AM GMT
' {$STAMP BS2}
' {$PBASIC 2.5}
IDX VAR Nib
X VAR Byte
stdout VAR Word
INPUT 15
INPUT 14
INPUT 13
Main: 'Successive Aproximation Routine
DO
x=0
FOR idx=7 TO 0
X=X | DCD idx
PWM 7, X,10
IF IN15=0 THEN
X=X ^ DCD idx
ENDIF
NEXT
stdout=x·············
x=0
·
FOR idx=7 TO 0
X=X | DCD idx
PWM 5, X, 10
IF IN13=0 THEN
X=X ^ DCD idx
ENDIF
NEXT
stdout=stdout + x
x=0
FOR idx=7 TO 0
X=X | DCD idx
PWM 6, X, 10
IF IN14=0 THEN
X=X ^ DCD idx
ENDIF
NEXT
stdout=stdout + x··········
DEBUG HOME,DEC5 stdout
LOOP
END
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 1/27/2007 4:44:53 AM GMT
Thanks so very much reply this will help alot
···························
··························
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Here is the code for the Battery Tester
For some reason the Attachment Manager is not working very good to night
I put this together today the Simple A to D·this works very good thanks for posting it··I have not built the other one yet· but i will when i have some more time
·this what i came up with a battery tester and this is the little routine that i wrote for it
I will need use this routine when i built a project that i have in mind builting
· 'This Routine Displays The type of battery that is hook up
I add this to your routine
··· IF x > 50 AND x < 80 THEN
··· DEBUG "1.2 TO 1.5 Volts", CR
··· PAUSE 500···················· 'This is to clear the screen·· I am going to put leds instead of the debug statements this just to test it
··· EXIT
··· ELSEIF x > 120 AND x < 160 THEN
··· DEBUG " 2.5 TO 3.0 Volts", CR
··· PAUSE 500····················· 'This is to clear the screen
··· EXIT
··· ELSEIF x = 238 THEN
··· DEBUG "4.5 Volts", CR······· ' The range is N/A at this time will come latter
··· PAUSE 500························ ·'This is to clear the screen
··· EXIT
··· ENDIF
LOOP
DEBUG CLS, CR····················· 'This is the routine to clear screen
GOTO main
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 1/16/2007 3:59:49 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
I am going thorugh your conversations and I'm really amazed by your work... I have some similar question to continue if you guys can help me too... well suppose I have a battery outside and if I want to measure its voltage with the use of basic stamp, how can I go with it...??
again appriciated,
Regards,
Darshan
a 12 bit A to D will give you 0 to 5 volt with 4096 step resolution
multiply your input x and multiply by */3125
volt=x*/3125
5 volts will read out as 49987
use the DIG command to format the output and add the decimal point
DEBUG DEC1 x DIG 4,".",DEC1 x DIG 3,DEC1 x DIG 2,DEC1 x DIG 1,DEC1 x DIG 0
http://www.parallax.com/detail.asp?product_id=604-00026
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
I do not remenber how much speed this VCO work,but its good to obtain voltage meter or low sample rate audio,sampling.
I've built and used the simple A to D converter using an LM 339, it works.· Thanks for the information.
Can I store the results of the A to D output in EEprom?· The results are shown as numbers up to 255, which indicates 5v, on the debug screen.· Can these results be stored as the same numbers in EEPROM?
Thanks
Patrick
Read the Stamp Basic Manual sections on the WRITE, READ, and DATA statements to learn about the use of EEPROM for data storage.