Shop OBEX P1 Docs P2 Docs Learn Events
making a 10-bit value from 2, 8-bit values — Parallax Forums

making a 10-bit value from 2, 8-bit values

TCTC Posts: 1,019
edited 2016-09-22 12:53 in Propeller 1
Hello all,

Sorry if this question is very simple, I am out of time to try and figure it out myself... And trust me, I dont like it.

The part I am working with has registers that store the calibration data for 8 sensor inputs. Each sensor has a 10-bit value for the calibration value that is split between 2, 8-bit registers. Does anyone have a easy way to combine 2, 8-bit values? I have included the part's register map to show what I am working with. From my own thinking I would shift the values to get what I wanted. But I dont know for sure.

Thanks
TC

[img][/img]4684a52f03ab60df892f28917e8ad3.jpg
1181 x 660 - 274K

Comments

  • PaulRowntreePaulRowntree Posts: 150
    edited 2016-09-22 12:47
    The implementation depends on the language, but you would shift the high byte to the left by 8 bits (or multiply by 256) , and add it to the low byte. For safety, I would do an AND of the result with $3FF (%11_1111_1111) to ensure that unwanted bits are cleared.


    EDIT ... just saw the table. Weird layout! I would mask off the unwanted bits of the least-significant register (the mask pattern depends on which sensor is being used), shift the most significant byte left by 2 bits, then combine.

    is this what you mean?

  • TCTC Posts: 1,019

    EDIT ... just saw the table. Weird layout! I would mask off the unwanted bits of the least-significant register (the mask pattern depends on which sensor is being used), shift the most significant byte left by 2 bits, then combine.

    I am using SPIN

    It is defiantly a weird layout, that is why I am getting overwhelmed on the best way to do it. I figured they would of put bits 7..0 in one register, and bits 8..9 in another. That would make my life a little easier.



  • TC wrote: »

    EDIT ... just saw the table. Weird layout! I would mask off the unwanted bits of the least-significant register (the mask pattern depends on which sensor is being used), shift the most significant byte left by 2 bits, then combine.

    I am using SPIN

    It is defiantly a weird layout, that is why I am getting overwhelmed on the best way to do it. I figured they would of put bits 7..0 in one register, and bits 8..9 in another. That would make my life a little easier.


    They may have done it that way so you could just ignore the low order bits if you don't need that much precision.

  • Dave HeinDave Hein Posts: 6,347
    edited 2016-09-22 14:05
    I think this code will do what you need. inptr points to a byte buffer containing the 10 raw bytes. outptr points to a word buffer that will hold the 8 10-bit results.
    pub MergeBits(inptr, outptr) | lsbs
      lsbs := (byte[inptr][9] << 8)  | byte[inptr][8]
      repeat 8
        word[outptr] := (byte[inptr++] << 2) | (lsbs & 3)
        lsbs >>= 2
        outptr += 2
    
  • TCTC Posts: 1,019
    edited 2016-09-22 14:14
    David Betz wrote: »

    They may have done it that way so you could just ignore the low order bits if you don't need that much precision.

    That very well could be the reason. I just found out exactly what the registers are for. They are the value of the base capacitance.

    quote from application note I just found
    Base capacitance can be
    determined from register B1h (sensor CS1). One LSB of the calibration register equals 0.196pF (i.e.,
    a value of 0xE5 indicates 45pF of base capacitance).

    The note doesn't say anything about the 2 LSB in the other registers. I am assuming they are not needed. If it is the case, this is as easy as doing...

    (8-bit value* 196) / 100

    so ($E5 * 196) / 100) = 448 if I add a "." I would have 44.8
  • TCTC Posts: 1,019
    Dave Hein wrote: »
    I think this code will do what you need. inptr points to a byte buffer containing the 10 raw bytes. outptr points to a word buffer that will hold the 8 10-bit results.
    pub MergeBits(inptr, outptr) | lsbs
      lsbs := (byte[inptr][9] << 8)  | byte[inptr][8]
      repeat 8
        word[outptr] := (byte[inptr++] << 2) | (lsbs & 3)
        lsbs >>= 2
        outptr += 2
    

    Thank you. I will have to try it out when I have the chance later on.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2016-09-23 04:49
    As we say in Hollywood... for your consideration:
    pub get_cal(device, ch)
    
    '' Read 10-bit channel calibration from a device
    
      if ((device < 0) or (device > 124))
        return 0
    
      if ((ch < 1) or (ch > 8))
        return 0
    
      case ch
        1 : return (read_reg(device, $B1) << 2) | ((read_reg(device, $B9) >> 0) & %11)
        2 : return (read_reg(device, $B2) << 2) | ((read_reg(device, $B9) >> 2) & %11)     
        3 : return (read_reg(device, $B3) << 2) | ((read_reg(device, $B9) >> 4) & %11)     
        4 : return (read_reg(device, $B4) << 2) | ((read_reg(device, $B9) >> 6) & %11)     
        5 : return (read_reg(device, $B5) << 2) | ((read_reg(device, $BA) >> 0) & %11)     
        6 : return (read_reg(device, $B6) << 2) | ((read_reg(device, $BA) >> 2) & %11)     
        7 : return (read_reg(device, $B7) << 2) | ((read_reg(device, $BA) >> 4) & %11)     
        8 : return (read_reg(device, $B8) << 2) | ((read_reg(device, $BA) >> 6) & %11)   
    
    
    pub read_reg(device, reg)
    
      ' Read register from selected device
    
    I find that when I'm under pressure, coding in the most obvious way is the cleanest -- as above. The table could be easily reconstructed from this code. BTW, I time tested it against another version (not so obvious) -- both run between 150-200us (with test code for read_reg); speed of the code above dependent on the channel that is selected.

    Here's the not-so-obvious code:
    pub get_cal(device, ch) | msb, lsb, adj
    
    '' Read 10-bit channel calibration from a device
    
      if ((device < 0) or (device > 124))
        return 0
    
      if ((ch < 1) or (ch > 8))
        return 0
      else
        --ch                                                        ' z-align channel
    
      msb := $B1 + ch                                               ' $B1..$B8
      lsb := $B9 + (ch >> 2)                                        ' $B9 + ch.bit2  ($B9..$BA)
      adj := (ch & %011) << 1                                       ' (ch // 4) x 2  (0, 2, 4, 6)
    
      return (read_reg(device, msb) << 2) | ((read_reg(device, lsb) >> adj) & %11)
    
  • Jon,

    I always like to read your code. You should teach people how to do it.

    Enjoy!

    Mike
  • JonnyMacJonnyMac Posts: 9,102
    edited 2016-09-23 05:46
    I always like to read your code. You should teach people how to do it.
    Thanks! And I do -- every other month in Nuts & Volts magazine.
  • TCTC Posts: 1,019
    I want to thank everyone for there help and input. Yesterday my friend and I were finally able to get together to run a test with a few of the faces. Sadly, after about 6 hours of trying, we could not get the hardware to work consistently. The actual hardware or code were not the main problem. The main problem was the sensor design. We tried various sensor designs. First they would work, then they wouldn't. I think it was because the chip I am using will self adjust, and it was adjusting the wrong way.

    At least now, I have a lot more time to figure out the best option. The next possible chance to show off the tech would be around December.

    Thank you everyone for all your help.
  • Hey Jon, I scrutinized the Propeller more thoroughly than any micro that I have used. I downloaded Prop Tool probably a full year before I ever even bought one.

    Your column had more to do with my decision than any other consideration.

    In fact, your column is pretty much the only reason I subscribe to the magazine as well. There is also DipTrace, and then DipTrace introduced me to Bay Area Circuits, and the list goes on. Your writings did all that.

    I have used what I have learned from you and turned it into food on my table. You have given me a gift which, try as I may, I couldn't possibly repay.

    If you ever find yourself feeling underappreciated, you aren't.

    As honestly as I can say it, THANK YOU, and by all means, keep on Spinning (and winning) :-)

    [/SAPPYCRAP]
  • JonnyMacJonnyMac Posts: 9,102
    edited 2016-09-25 13:12
    @bte2: Wow, thank you. I really don't feel under-appreciated; in fact, at times I'm overwhelmed by the kind things people say to me vis-a-vis my column and the posts I make here. I'm in year 22 of my relationship with Parallax, and they treat me incredibly well. I consider Ken Gracey one of my closest friends, and it was through him that I met Rick (paintball mini gun) Galinson who has become like a brother to me.

    I sincerely appreciate the kind comments, but we've kind of gone off track here with TC's thread. It is my great pleasure to write for N&V, and to share my experiences here. TC often posts interesting questions -- as in this thread -- and I enjoy developing ideas that may be useful because doing so helps me become a better programmer.
    [/SAPPYCRAP]
    It took a second for that to register, and it made me chuckle.
  • bte2 wrote: »
    Hey Jon, I scrutinized the Propeller more thoroughly than any micro that I have used. I downloaded Prop Tool probably a full year before I ever even bought one.

    Your column had more to do with my decision than any other consideration.

    In fact, your column is pretty much the only reason I subscribe to the magazine as well. There is also DipTrace, and then DipTrace introduced me to Bay Area Circuits, and the list goes on. Your writings did all that.

    I have used what I have learned from you and turned it into food on my table. You have given me a gift which, try as I may, I couldn't possibly repay.

    If you ever find yourself feeling underappreciated, you aren't.

    As honestly as I can say it, THANK YOU, and by all means, keep on Spinning (and winning) :-)

    [/SAPPYCRAP]
    +1 Jon! You are an inspirational guy for people like me that struggles to understanding coding. You always try to make it easy. Kudos!
Sign In or Register to comment.