Electronics > Microcontrollers

Combining ADC Result Over 2 Registers Into 1 Variable

(1/5) > >>

Lance:
I'm writing some functions to use the ADC module on a PIC16F917. It's an 8 bit device, and the ADC is 10 bits. So the result is split across two registers, 8 bits in one and two in the other. I want my read function to return one 10 bit value (I'm thinking of using a short integer but the compiler keeps complaining about that). My first idea was to create an addressable bitfield and tie it to the return variable using a union, however I would have to manually address each of the bits in the registers. As it is I can only directly go to the registers as a whole.

Is there a clean way for me to combine these two into a single 16 bit value (short integer)? Right now I'm going to try putting each register into a variable of the same size as my result, shifting the high register over the correct amount, and XORing them together.

Also, are there any tips or tricks anyone has for using the ADC module? Thanks.

scrat:
Which compiler/library are you using? The Microchip C16 ones?

Lance:
No, I'm using the Hi-Tech C compiler.

Psi:

--- Quote from: Lance on May 10, 2011, 06:26:24 am ---I'm writing some functions to use the ADC module on a PIC16F917. It's an 8 bit device, and the ADC is 10 bits. So the result is split across two registers, 8 bits in one and two in the other. I want my read function to return one 10 bit value (I'm thinking of using a short integer but the compiler keeps complaining about that).

--- End quote ---

Check if your environment already has a way to access the adc data as 16bits. I dunno about PICs, but with avrdude you can use ADCL for low bits, ADCH for high or ADCW for all 16 bits together. There may be something like that already available to you.

Lance:
I ran a search through the manual for the term ADC but didn't find anything other than a brief assembly code example. That would take all the fun out of this anyway.  ;D

I'm gonna try my idea of XORing the two registers after shifting one of them over. It makes sense in theory.

If I say had this in my upper and lower registers:

--- Code: ---00000011
11111111
--- End code ---

My plan is to stick them into a short unsigned int like so:

--- Code: ---0000000000000011
0000000011111111

--- End code ---

Shift the upper result register over:

--- Code: ---0000001100000000
0000000011111111

--- End code ---

And then XOR them:

--- Code: ---0000001111111111

--- End code ---

Seems solid to me.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version