PDA

View Full Version : Binary to Decimal from ADC



RYTECH
- 16th July 2005, 00:35
Hi i need help with converting the Binary numbers coming as the result from the ADC onmy pic so i can display the result to 2 decimal places on my lcd...

I require a simple routine to convert 255 to a decimal (5.00000) and then multiply it by 3 (15.00) to two places...

im using a 16F767 clocked to 20mhz.

Thx :)

(and thanks all especially melanie for the many informative posts)

RYTECH
- 16th July 2005, 01:20
http://www.rentron.com/serial.htm

this page helped me solve my problems....

RYTECH
- 16th July 2005, 01:32
how do i define the constant for value 0.0195 ?

i get "bad modifier .0195" when i compile.....

RYTECH
- 16th July 2005, 02:50
New problem ... voltage is always 12

here's my program:

the part under full " voltage = (x * m / 10000) * 3 doesnt seem to work properly... what am i doin wrong...?


'************************************************* ***************
'* Name : F767_LCDBM *
'* Author : Ryan Barrett *
'* Notice : Copyright (c) 2005 Rytech Computing *
'* : All Rights Reserved *
'* Date : 6/24/2005 *
'* Version : 1.1 *
'* Notes : LCD BATTERY MONITOR PROGRAM * *
'* : *
'************************************************* ***************
define OSC 20
Define LOADER_USED 1
DEFINE LCD_DREG PORTC
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 5
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 4
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 8
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50
ADCON1 = 4
SLOW VAR BYTE
NORMAL VAR BYTE
FAST VAR BYTE
statusled var portb.6
statusled2 VAR portb.7
mspeedraw VAR Byte
x var byte
voltage var WORD
m con 195
' sets up the i/o ports on pic
TRISA = %00000000
TRISB = %00000011
TRISC = %00000000
ADCON1 = 7
' sets initial values to variables
SLOW = 45
NORMAL = 127
FAST = 254
' flashes statusled led 4 times to signal program start
HIGH statusled
PAUSE 1000
LOW statusled
PAUSE 500 'flash leD
HIGH statusled
PAUSE 1000
LOW statusled
PAUSE 500 'flash leD
HIGH statusled
PAUSE 1000
LOW statusled
PAUSE 500 'flash leD
HIGH statusled
PAUSE 1000
LOW statusled 'flash leD
PAUSE 5000
HIGH statusled
PAUSE 5000
LOW statusled
input portb.0
INPUT portb.1
Lcdout $fe, 1
LCDOUT $fe, $80,"RYTECH",$fe, $C0,"COMPUTING"
PAUSE 2000
GOTO loop
loop:

mspeedraw = 0
'GOSUB togglestatusleds


' ATTEMPT TO CHECK THE INPUT LEVELS AT 2 PINS on PORTB.0 and PORTB.1
' calculates the motor speed setting based on the 2 inputs
If portb.0 = 1 Then mspeedraw = (mspeedraw + 1)
IF portb.1 = 1 THEN mspeedraw = (mspeedraw + 2)
GOTO outputit
' ATTEMPT TO OUTPUT HPWM TO THE MOTOR DRIVER BOARD BASED ON mspeedraw
' DEBUG INDICATION ON STATUSLED & STATUSLED2
' if slow
GOTO LOOP
outputit:
IF mspeedraw = 1 THEN SLOWSPEED
' if normal speed
IF mspeedraw = 2 THEN NORMALSPEED
' if full speed
IF mspeedraw = 3 THEN FULLSPEED
IF mspeedraw = 0 ThEN NOSPEED
GOTO LOOP
SLOWSPEED:
'HPWM 1, SLOW, 1000
Lcdout $fe, 1, "slow"
pause 100
HIGH statusled
LOW statusled2
GOTO loop
NORMALSPEED:
Lcdout $fe, 1, "normal"
PAUSE 100
LOW statusled
HIGH statusled2
GOTO LOOP
FULLSPEED:
Lcdout $fe, 1, "full"
pause 100
HIGH statusled
HIGH statusled2
ADCON0 = $41
Pauseus 50 ' Wait for channel to setup
ADCON0.2 = 1 ' Start conversion
Pauseus 50 ' Wait for conversion
x = ADRESH
voltage = (x * m / 10000) * 3
Lcdout $fe, 1, "x=", #x
PAUSE 2000
LCDOUT $FE, $C0, #voltage
pause 3000
GOTO LOOP
NOSPEED:
Lcdout $fe, 1, "none"
pause 100
LOW statusled
LOW statusled2
GOTO loop

flashstatusled:
low statusled
pause 500
high statusled
pause 1000
Low statusled
RETURn
GOTO LOOP
'toggle status led test
togglestatusleds:
HIGH statusled
LOW statusled2
pause 400
HIGH statusled2
low statusled
pause 400
low statusled
LOW statusled2
pause 400
RETURN
GOTO LOOP
nostatusleds:
LOW statusled
LOW statusled2
pause 1000
RETURN
GOTO loop
bothstatusleds:
HIGH statusled
HIGH statusled2
pause 1000
RETURN
Goto loop

Bruce
- 16th July 2005, 03:04
PBP doesn't support floating point, but you don't really need it for A/D
conversions.

For 8-bit A/D you can do something like this;

Quanta CON 5000 ' set this to +Vref. @5V use 5000. @4.990V 4990, etc.

Read the A/D value

ADCIN 0,ADval ' read RA0 A/D into word var ADval

ADval = ADval */ Quanta ' use middle 16-bits of multiplication.

LCDOUT DEC ADval DIG 3,".",DEC3 ADval ' 5.000, 3.962, etc,,

For 10-bit;

Quanta CON 1251 ' If +Vref = 5V.

To find Quanta take +Vref / 1023.

If +Vref = 5V then 5 / 1023 = 4.887mV.
4.887mV * 256 = 1.251 or 1251 for Quanta.

You can tweak the Quanta value to improve results. It may vary slightly from
PIC to PIC, but you want a good solid +Vref.

RYTECH
- 16th July 2005, 05:29
thx for the quick reply Bruce :)

i'll give your suggestion a try.

RYTECH
- 16th July 2005, 06:42
That worked great :)

can you suggest a voltage divider schematic for using a range of 12 - 15v knocking it down to the 0-5v needed by the pic? I dont think im doing it right...when i multiply the voltage dividers output by 3 it doesnt equal my source input level ( currently from adjustable power, but eventually will be 12v battery)

Thanks for any additional suggestions.. :

would the solution mentioned here: http://www.picbasic.co.uk/forum/showthread.php?t=1828&highlight=voltage+divider be suitable?

Melanie
- 16th July 2005, 20:59
Consider the appended drawing.

Assuming a 3:1 ratio (ratio determined by R1+R2:R2), Resistor R1 has to drop 2 volts for every one volt appearing across R2. So, if we have 15v input, R1 has to drop 10v across it for R2 to drop 5v. Apply the same ratio for your Resistor Values... if we have 10K for R2, we will need 20K for R1.

The voltage the PIC will see will be Vin/(R1+R2)*R2... so if we have... R1=22K, R2=10K and Vin=12, we get...

12/(22000+10000)*10000 and the PIC sees 3.75v

10K is a good value for R2. Choose R1 so that the PIC never gets more than Vdd (most probably 5v) at the maximum possible applied input voltage to the divider.

vastator
- 27th July 2005, 18:42
Where can I find information on dec dig and dec3 modifiers? I searched through the PBP manual but came up empty.

Bruce
- 27th July 2005, 18:51
DIG is explained in the Math Operators section. DEC is a data modifier, and
is shown in sections of the manual for PBP commands that allow data modifiers like LCDOUT, SEROUT2, HSEROUT, etc,,.