Shop OBEX P1 Docs P2 Docs Learn Events
need advice on code for a/d converter — Parallax Forums

need advice on code for a/d converter

ArchiverArchiver Posts: 46,084
edited 2003-08-09 15:10 in General Discussion
It's been years since I learned to program basic and I am working on
a program to control a solenoid. I took the code given to use the
a/d converter and changed it slightly to fit the needs of my
program. Only I don't completely understand all of the code given
for the converter so I don't know if I changed any critical
details. I will copy the short program below. Can anyone tell me
if the a/d converter will copy info to the variable pulse as the
code is written. If not can you please give me insight as to what
will need to be changed and why.


'This program is for a water injection solenoid driver. It
considers the input from the 'analog map sensor as it is converted
to digital signal by an ADC0831 analog-digital 'converter. Based on
this input, it will calculate a pulse, calulate a pause, and cycle
as 'a loop.

SYMBOL pulse = b0 '8 bit ADC result
SYMBOL mswait = b1 'derived by subtracting
pulse from 255
SYMBOL i = b2 'counter for 8 data bits ADC
clock cycle
SYMBOL CS = 0 'ADC chip select
Symbol AD = pin1
Symbol CLK = 2

loop: gosub conv 'Select ADC and get data
debug pulse
if pulse < 100 then goto loop 'skip pulse if map is vacuum
pulse = pulse / 5 'diving by fifths to figure

' # of 5ms cycles pwm
pwm 5,255,pulse 'output to pin 5 a 100%duty
' cycle, pulse x 5ms

pulse * 5 = mswait '
mswait = 255-mswait
pause mswait 'Wait completing 255 ms
debug mswait
goto loop 'Do it forever.

conv: low CLK 'Put clock line in starting
'state.
low CS 'Select ADC.
pulsout CLK, 1 '10 us clock pulse.
let pulse = 0 'Clear data.
for i = 1 to 8 'Eight data bits.
let pulse = pulse * 2 'Perform shift left.
pulsout CLK, 1 '10 us clock pulse.
let pulse = pulse + AD 'Put bit in LSB of data.
next 'Do it again.
high CS 'Deselect ADC when done.
return
Sign In or Register to comment.