Is your value set to be left or right justified
right, which is normal afaik
how many bits is the ADC?
10bit, same as yours
Would I be correct in saying that an OR takes fewer operations than an XOR? Using an OR in my situation would be fine.
I dunno, ya would need to see how many asm instructions both ways translate into.
That's just the way i do it.
I doubt it will matter, ya shouldn't be that tight on instructions that you need to worry about it.
If you wanted to investigate you could check if there's a single XOR asm instruction for the pic cpu core your using.
My guess is that OR is going to be quicker.
This is what I came up with:
unsigned short int ADCREGU=0; //Holds upper ADRESH value
unsigned short int ADCREGL=0; //Holes lower ADRESL value
unsigned short int ADCRES=0; //Final Result
ADCREGU=ADRESH;
ADCREGL=ADRESL;
ADCREGU=ADCREGU << 8;
ADCRES=ADCREGU^ADCREGL;
return(ADCRES);
That looks like it should work fine but
- You might want to check the PIC datasheet as i'm not sure if this applies to PICs but... With AVRs you should always read the Low ADC byte first because it buffers the result. So reading the low byte locks the high byte from changing until you read it. If you read the High byte first there is a chance the low byte may get updated with new data before you can read it. In which case you end up with the high byte from sample 1 and the low byte from sample 2. (but it may not apply to PICs)
- Again, i'm going on my AVR experience but.. you don't really need the ADCREGL variable, you can use ADRESL directly and save the memory.
- You don't strictly need to zero those 3 variables, since they all get assigned to, but it's a good habit to get into. If you were tight on space you could safely remove the initialization to zero for those 3 lines.
I don't understand what the purpose of this is:
myvariable = (myvariable & 255) | ( (ADCH << 8 ) & 768 );
Some old cpus can do unusual or undefined things when you do bitwise shift operations. If you are paranoid about checking that the "seemingly" new bits that appear in a shift operation are zeros you can use a mask.
That is what those two AND operations do.
& 255 masks off all data but the bits your after 0000 0000 1111 1111 = 255
& 768 does the same 0000 0011 0000 0000 = 768
This isn't necessary if you know the shift behavior of the cpu your working on, but some people are paranoid