Decimal value


Closed Thread
Results 1 to 6 of 6

Thread: Decimal value

  1. #1
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79

    Default Decimal value

    If I have one ADC value of 819 thatīs one value of 15.9 in my corrent whatīs the code to measure with one decimal my corrent. Iīm having a little difficulties to measure with one decimal. Iīm using PIC16F872 and 10 ADC bits.

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


    Did you find this post helpful? Yes | No

    Default

    If ADC value of 819 equals say 15.9mA then just do some math to convert 819 into 159 and remember there is one decimal place in your answer which needs attention whenever you display...

    819*1942/10000=159

    so...

    The variables...

    ADCValue var Word
    TempA var Word
    TempB var Word
    Result var Word

    The Calculation (see PBP manual section 4.17.8 DIV32 command)...

    TempA=1942
    TempB=ADCValue*TempA
    Result=DIV32 10000

    The display (see PBP manual 4.17.7 DIG command)...

    LCDOut $FE,1
    If Result>99 then LCDOut #Result DIG 2
    LCDout #Result DIG 1,".",#Result DIG 0,"mA"

    Your display will show... 15.9mA

  3. #3
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Smile Thankīs

    Ok thankīs a lot.
    I was forgetting DIG command. I was using / and // to give me the values, but itīs much simple DIG command.
    Regards
    Leonel Monteiro

  4. #4
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default 32 bits multiplication

    I have one problem:
    I achieve my best performance in low corrents doing this code:
    ADCIN 0, ADC_corrente
    pause 10
    x = ADC_corrente * 2360
    res = DIV32 10000
    if res > 99 then
    unid_corrente = res DIG 0
    dec_corrente = res / 10
    else
    unid_corrente = res DIG 1
    dec_corrente = res DIG 0
    endif

    but when ADC value * 2360 is biggest then 65535 i lost my upper 16bits, because the operator * returns the lower 16 bits of a 32 bits result.
    What i have to do to doesnīt lost bits?
    I know ** returns the upper 16 bits but there are any way to have the result in one variable?

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


    Did you find this post helpful? Yes | No

    Default

    >> any way to have the result in one variable?

    Yes, the ONE VARIABLE answer is in RES.

    Look...

    The maximum value for your 10-bit ADC is 1024...

    Multiply this by 2360 gives 2416640...

    divide this by 10,000 gives 241

    everything is within range of PBP's capabilities...

    The dummy WORD value 'x' when used as x=ADC*2360 is irrelevant, you follow that immediately with your DIV32 statement so the correct value of 241 ends up in RES.

    Remember that RES is a factor of TEN greater... ie RES=241 is actually 24.1 so just do the math to extract what you need.

    You can't have floating points in integer math, sometimes you have to "think outside of the box" in order to achieve what you want.

  6. #6
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Talking Wrong code

    Your right. I had a wrong code, so i though it was because multiplication has an 32 bits result.
    The correct code is:
    ADCIN 0, ADC_corrente
    pause 10
    x = ADC_corrente * 2360
    res = DIV32 10000
    if res > 99 then
    dec_corrente = res DIG 0
    unid_corrente = res / 10
    else
    unid_corrente = res DIG 1
    dec_corrente = res DIG 0
    endif

Similar Threads

  1. variable + decimal
    By savnik in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 18th August 2009, 14:21
  2. Binary Coded Decimal
    By -Dan- in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 25th May 2009, 09:23
  3. hex ascii help please
    By ffr58kk90 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th December 2006, 21:09
  4. 16bit variable and degrees conversion
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd May 2005, 17:27
  5. Convert a word variable to decimal
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 4th December 2004, 20:02

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