Binary to Decimal from ADC


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2005
    Posts
    93

    Default Binary to Decimal from ADC

    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)

  2. #2
    Join Date
    Jul 2005
    Posts
    93


    Did you find this post helpful? Yes | No

    Default

    http://www.rentron.com/serial.htm

    this page helped me solve my problems....

  3. #3
    Join Date
    Jul 2005
    Posts
    93


    Did you find this post helpful? Yes | No

    Default

    how do i define the constant for value 0.0195 ?

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

  4. #4
    Join Date
    Jul 2005
    Posts
    93


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Jul 2005
    Posts
    93


    Did you find this post helpful? Yes | No

    Smile

    thx for the quick reply Bruce

    i'll give your suggestion a try.

  7. #7
    Join Date
    Jul 2005
    Posts
    93


    Did you find this post helpful? Yes | No

    Wink

    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/show...oltage+divider be suitable?
    Last edited by RYTECH; - 16th July 2005 at 06:48.

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default Resistor Potential Divider

    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.
    Attached Images Attached Images  

  9. #9
    vastator's Avatar
    vastator Guest


    Did you find this post helpful? Yes | No

    Default info about dec dig and dec3 ?

    Where can I find information on dec dig and dec3 modifiers? I searched through the PBP manual but came up empty.

  10. #10
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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,,.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Convert long binary number to ASCII Decimal
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 9th July 2009, 13:58
  2. Binary Coded Decimal
    By -Dan- in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 25th May 2009, 09:23
  3. How to convert binary to decimal?
    By Cabomba in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 13th October 2007, 01:27
  4. Decimal to Binary Conversion
    By schlaray in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd December 2006, 14:58
  5. Decimal to binary conversion
    By Demon in forum Code Examples
    Replies: 9
    Last Post: - 25th February 2005, 20:05

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts