2 bytes to word array variable - fastest way


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33

    Default 2 bytes to word array variable - fastest way

    Hello All,
    I would like an "expert check" on this one....
    I need to put 2 ADC bytes into a word variable thats part of an array.

    This is what I have:
    adcData var word[16]
    adcData[index] = (ADRESH << 8) + ADRESL

    Is there a better way (faster) ?

    thanks,
    Mike

    P.S.
    I am modernizing an old dehumidifier whose humidity sensor (tape strip) has
    gone the way of the dodo. I figure get an electronic humidity sensor and I
    would have another excuse for a PIC project.

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    Have you tested that? it seems like it will shift ADRESH 8 times, then assign it to the word. DOing it like that would be better I think like this:
    Code:
    adcdata[index] = ADRESH
    adcdata[index] = adcdata[index]<<8
    adcdata[index] = adcdata[index]+ADRESL
    Not sure about that either.

    I think you can do this:
    Code:
    adcdata.highbyte[index] = ADRESH
    adcdata.lowbyte[index] = ADRESL
    I am not certain on this, so my reply is as much a question as an answer. Hmmm, that sounds a little deep

    OK, this I am certain about:
    Code:
    tempword var word
     
    tempword.highbyte = ADRESH
    tempword.lowbyte = ADRESL
    adcdata[index] = tempword
    Last edited by cncmachineguy; - 8th June 2011 at 00:45.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    I have tested the code with VB.Net, and it works, but I haven't tested on the PIC yet.

    I actually tried this code first but it did not compile:
    adcData.highbyte[index] = ADRESH
    adcData.lowbyte[index] = ADRESL
    I guess you can't use the .lowbyte/.highbyte syntax on arrays.

    Using the temp variable is probably the norm but I figure there might be some trick to doing
    the same thing all nice and clean on one line.

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    In your VB test, Was ADRESH a byte or a word?
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    Thanks Bert. I see what your getting at. ADRESH was a word.
    I just tried ADRESH as a byte and had an overflow. oops, I missed that one.
    I guess I'll use the temp variable way.

    There is another way I used before:
    adcData[index] = (ADRESH * 256) + ADRESL
    But I would put money on this way is slower than the temp variable way.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    Hi,
    Have you tried:
    Code:
    adcData.lowbyte[index] = ADRESL
    adcData.lowbyte[index+1] = ADRESH
    Take a look at Melanies writeup, specifically section 5, for further details.

    /Henrik.

  7. #7
    Join Date
    Jun 2010
    Location
    Venezuela
    Posts
    12


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    hello, i use this and work:

    read I,b1.HIGHBYTE ' read byte type var from EEprom
    read I+1,b1.LOWBYTE 'read byte type var from EEprom
    TEmpDs18(L) = B1 'word array

  8. #8
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    Well it turns out the more intuitive way is also a little faster. That is if I
    have done this correctly.

    I used [view] [program memory] in MPLab and counted the number of
    instructions between the start and end labels.

    This code was 18 lines:
    Code:
     
    StartTest:
      adcData.lowbyte[Index] = ADRESL
      adcData.lowbyte[Index+1] = ADRESH
    EndTest:
    This code was 16 lines:
    Code:
     
    StartTest:
      tempWord.highbyte = ADRESH
      tempWord.lowbyte = ADRESL
      adcdata[index] = tempword
    EndTest:
    So as long as I can afford the extra word variable, I will be using the
    temp variable way...... unless someone else reveals a clever trick.

    thanks all for your replies,
    Mike -

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    If ADRESH & ADRESL are consecutive SFR... then you could use

    Code:
        @ADRESULT = ADRESL
        ADRESULT VAR WORD EXT
    
        MyArray VAR WORD [8]
    
    Start:
        MyArray[0] = ADRESULT
    But I doubt ADRESL/ADRESH are consecutive in many PIC.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: 2 bytes to word array variable - fastest way

    mister_e,
    Thats a good trick. One to remember. And it only produces 9 lines of
    instruction. But your right, the PIC I'm using (16F684), ADRESH/L are not
    next to each other.

    Actually, ACCON0 is after ADRESH. That would give you an interesting result.

    thanks,
    Mike -

Members who have read this thread : 0

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