Menu

Converting 10-bit binary to decimal

Help
MBB
2018-10-18
2018-11-15
  • MBB

    MBB - 2018-10-18

    I'm using v0.98.02 with a 18F4550.

    I have a MPL115A1 barometer which outputs data as two 8-bit words. To get the Temperature and pressure coefficients you combine the two 8-bit words in to one 16-bit word BUT then use only the Right 10 binary digits.

    Here are examples from the data sheet:
    Pressure MSB = 67h
    Pressure LSB = C0h Pressure = 67C0h = 0110 0111 11 00 0000
    = 415 ADC counts
    First 10 digits are 0110 0111 11 which = 415
    Temperature MSB = 80h
    Temperature LSB = 40h Temperature = 8040h = 1000 0000 01 00 0000
    = 513 ADC counts
    First 10 digits are 1000 0000 01 which = 513.

    When I use the GCB "VAL" command to convert the first example to decimal I get 41703 which is wrong. It should be 415.

    Does anyone know how to convert a 10-bit binary to decimal?

     
  • Anobium

    Anobium - 2018-10-18

    :-)

    BitsOut = FnLSR(BitsIn, NumBits)

    So, 513 = FnLSR( 0x8040 , 6 ).... so use valout = FnLSR( valin, 6

     
  • Chris Roper

    Chris Roper - 2018-10-18

    If you only wish to work on the 10 Most Significant Bits you need to Right Shift 6 times.
    Considering that a Right Shift is the equivalent of Divide by 2, then 6 Right shifts are a divide by 64.
    So concatenate the MSB and LSB into a Word Variable then divide the Word by 64
    VAL will then return your expected value of 415.

    Cheers
    Chris

    Edit Anobium hit send whilst I was running the Spell Check :)
    Same Maths just a different explination...........

     

    Last edit: Chris Roper 2018-10-18
  • MBB

    MBB - 2018-10-18

    Thanks!

     
  • William Roth

    William Roth - 2018-11-15

    Both methods will give the same result. However as a general rule shifting bits is faster and more code efficient than division.

    Consider the following code.

     #chip 18F25K42, 16
    
    Pressure = 0x67C0
    
    Pressure = Pressure / 64         '// This takes ~ 92 us to complete
    '                                '// Uses 78 Words of Program memory  
    '                                '// Uses 1 Byte of RAM
    
    Pressure = FnLSR(Pressure, 6)   '//  This takes ~ 14 us to complete
    '                               '//  Uses 29 Words of Program memory
    '                               '//  Uses 6 bytes of RAM
    
     

    Last edit: William Roth 2018-11-15

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.