Scaling ADC ?

jlhooper

New Member
I have 0-5v feeding ADC pin.
Debug returns 0-255 steps OK.
I am feeding this to 2digit led display (595's).
I have the displays working OK.
I want to convert this to read 0-99.
I have tried dividing/multipling the ADC reading etc but to no avail.
Can anyone help me ?
Jeff
 

jlhooper

New Member
ADC code

This is what I'm using although this is for 3 led segments, I'm only using 2.
By changing the line :"for digitcounter = 1 to 3 'Convert reading to decimal"
1 to 2, I can get it working in a fashion !
Ta, Jeff


'Low resolution analog reading to 7 segment display 3 digits
'OPTIONS PICAXE-08

symbol dataout = 4
symbol clk = 0
symbol latchout = 2

symbol no = 0
symbol yes = 1

symbol decimalpoint = b1
symbol bitcounter = b2
symbol outbit = b3
symbol outbyte = b4
symbol areading = b5
symbol digitout = b6
symbol digitcounter = b7



decimalpoint = no 'Turn the decimal point off
loopa:
readadc 1, areading 'Read the analog value
for digitcounter = 1 to 3 'Convert reading to decimal
digitout = areading // 10
gosub outdigit
areading = areading / 10
next digitcounter
pulsout latchout, 1 'Display the reading
goto loopa

outdigit:
lookup digitout, ($BE, $82, $DC, $D6, $E2, $76, $7E, $92, $FE, $F2, $FA, $6E, $3C, $DE, $7C, $78), outbyte
if decimalpoint = no then nodec
outbyte = outbyte | $01 'Set bit 0
nodec:
for bitcounter = 0 to 7
outbit = outbyte & 1
if outbit = 1 then outhi
low dataout
goto clkout
outhi:
high dataout
clkout:
pulsout clk, 1
outbyte = outbyte / 2
next bitcounter
return
 

moxhamj

New Member
The picaxe can only do integer maths. In your case, you want 0-255 to turn into 0-99 so divide by 2.55. You can get the same result in integers by multiplying by 10 then divide by 25. Or multiply by 100 and divide by 255. Try a new simple program with just a few lines to test this out, then when you know it works, put it into your existing code.
 

Peter M

Senior Member
what DrAc says but don't forget when you multiply by 100 you will need to use word variables eg w1, for the result
 

hippy

Ex-Staff (retired)
@ Peter M : The PICAXE does all its internal maths as 16-bit so as long as the result comes in at 0 to 255 byte variables can be used ...

ReadAdc pin,b0
b1 = b0 * 100 / 255

Note this gives a reading of 255 for 5V analogue in, which converts to 100. For 0 to 99, multiply by 99, divide by 255.
 
Top