ADC0831 scaling output to drive a servo 0-5V
Turnbull2112
Posts: 65
How would I scale the voltage reading from an ADC0831 0-5V to drive a servo open and closed with PULSOUT? I want the servo to maintain its position based on the voltage. I've completed the servo projects and the ADC projects but I'm having difficulty making the connection between the two. Any help/examples would be greatly appreciated!!
Thanks
Thanks
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$STAMP BS2}
' {$PBASIC 2.5}
CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 14
Cnts2Mv CON $139C 'x 19.6 (to mV)
result VAR Byte
mVolts VAR Word
Reset:
DEBUG CLS,
"ADC....", CR,
"volts..."
Main:
DO
GOSUB Read_0831
mVolts = result */ Cnts2Mv
mVolts = mVolts */185
mVolts = mVolts + 500
PULSOUT 14, mVolts
DEBUG HOME,
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts
PAUSE 100
LOOP
'
[noparse][[/noparse]subrtnes]
Read_0831:
LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [noparse][[/noparse]result\9]
HIGH CS
RETURN
If you look at most programs that drive servos, you'll see that they use a PAUSE 20 (or less) to get the 50 times a second rate.
' {$STAMP BS2}
' {$PBASIC 2.5}
CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 14
Cnts2Mv CON $139C 'x 19.6 (to mV)
result VAR Byte
mVolts VAR Word
Reset:
DEBUG CLS,
"ADC....", CR,
"volts..."
Main:
DO
GOSUB Read_0831
mVolts = result */ Cnts2Mv
DEBUG HOME,
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts
mVolts = mVolts */ 15
mVolts = mVolts + 700
PULSOUT 14, mVolts
PAUSE 10
LOOP
'
[noparse][[/noparse]subrtnes]
Read_0831:
LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [noparse][[/noparse]result\9]
HIGH CS
RETURN
The ADC0831 has a range of 0 to 255 for voltages from 0 to 5V (typically). Once you determine the PULSOUT value range, you can come up with a formula to express it and it's easy to convert that to a PBasic statement.
Say that your servo moves the way you want with a PULSOUT value range of 625 to 875. That's a range of 250, very close to the range of values returned by the ADC0831. You could simply add 625 to the value returned by the ADC and use that in the PULSOUT statement.
Post Edited (Mike Green) : 2/5/2010 8:01:21 PM GMT
' {$PBASIC 2.5}
CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 14
Cnts2Mv CON $139C 'x 19.6 (to mV)
result VAR Byte
mVolts VAR Word
Reset:
DEBUG CLS,
"ADC....", CR,
"volts..."
Main:
DO
GOSUB Read_0831
mVolts = result */ Cnts2Mv
DEBUG HOME,
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts
IF result < 50 THEN result = 50
result = result + 622
PULSOUT 14, result
PAUSE 10
LOOP
'
[noparse][[/noparse]subrtnes]
Read_0831:
LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [noparse][[/noparse]result\9]
HIGH CS
RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna