Packing 10bit ADC results into PIC (not a question)

Discussion in 'Programmer's Corner' started by Markd77, Jul 5, 2011.

  1. Markd77

    Thread Starter Senior Member

    Sep 7, 2009
    2,803
    594
    I was just about to start naively storing all the bits sequentially, but thought of a better method (I'm probably not the first to think of this).
    Instead there is a pattern of 5 bytes, the first 4 contain exactly what was in the least significant bits of the first 4 analog to digital conversions. The 5th byte is packed with four sets of the extra 2 bits from each conversion.
    I'm pretty sure this is much faster than doing it the other way, both packing and unpacking.
    I haven't included the code for setting the FSR to the start point of the store, the loop or the readAD subroutine because they are more application specific.
    Code:
    Code ( (Unknown Language)):
    1.     call readAD                ;First byte
    2.     movf ADRESLtemp, W
    3.     movwf INDF
    4.     movlw 0x04
    5.     addwf FSR, F            ;FSR + 4
    6.     clrf INDF
    7.     btfsc ADRESHtemp, 1
    8.     bsf INDF, 7
    9.     btfsc ADRESHtemp, 0
    10.     bsf INDF, 6
    11.     movlw 0x03            ;FSR - 3
    12.     subwf FSR, F    
    13.  
    14.     call readAD                ;Second byte
    15.     movf ADRESLtemp, W
    16.     movwf INDF
    17.     movlw 0x03
    18.     addwf FSR, F            ;FSR + 3
    19.     btfsc ADRESHtemp, 1
    20.     bsf INDF, 5
    21.     btfsc ADRESHtemp, 0
    22.     bsf INDF, 4
    23.     movlw 0x02            ;FSR - 2
    24.     subwf FSR, F    
    25.  
    26.     call readAD                ;Third byte
    27.     movf ADRESLtemp, W
    28.     movwf INDF
    29.     movlw 0x02
    30.     addwf FSR, F            ;FSR + 2
    31.     btfsc ADRESHtemp, 1
    32.     bsf INDF, 3
    33.     btfsc ADRESHtemp, 0
    34.     bsf INDF, 2
    35.     movlw 0x01            ;FSR - 1
    36.     subwf FSR, F    
    37.  
    38.     call readAD                ;Fourth byte
    39.     movf ADRESLtemp, W
    40.     movwf INDF
    41.     movlw 0x01
    42.     addwf FSR, F            ;FSR + 1
    43.     btfsc ADRESHtemp, 1
    44.     bsf INDF, 1
    45.     btfsc ADRESHtemp, 0
    46.     bsf INDF, 0
    47.     movlw 0x1            ;FSR + 1
    48.     addwf FSR, F    
    49.  
     
    Last edited: Jul 5, 2011
    AfdhalAtiffTan likes this.
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,730
    Careful....if you don't clrf the 5th byte prior to your packing routine, your data is going to be corrupted for every pass following the first.
     
    AfdhalAtiffTan likes this.
  3. Markd77

    Thread Starter Senior Member

    Sep 7, 2009
    2,803
    594
    Good point, I've fixed it now.
     
  4. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    May I ask why you need to store four (4) ten bit ADC results?

    Regards, Mike
     
  5. Markd77

    Thread Starter Senior Member

    Sep 7, 2009
    2,803
    594
    Actually, l'll be storing a few hundred for later processing. Four 10bit samples fit into five bytes.
     
  6. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    If you're measuring a signal that changes relatively slowly over time, you might be able to store the first 10-bit reading and then store a signed 8-bit delta value for subsequent readings.

    Good luck on your project...

    Regards, Mike
     
Loading...