Just in case anyone is looking at this, my original external A/D went from 0-4096. The PIC A/D goes from 0-65535! So a divide by 16 makes everything correct!
Just in case anyone is looking at this, my original external A/D went from 0-4096. The PIC A/D goes from 0-65535! So a divide by 16 makes everything correct!
Hi,
No it doesn't. The ADC in the 18F1320 (as in most PICs) are 10 bits, ie they return a value ranging from 0 to 1023.
However, since the result is 10bits it needs two 8bit registers (bytes) to store the result and depending on how you have the ADC configured it returns the result left or right justified within those two bytes. So, if the input to the ADC is saturated it returns the value 1023, looking at the two registers for the result (ADRESH and ADRESL) it'll will be either (00000011 11111111 = 1023) or (11111111 11000000 = 65472) depending on if you choose to have the result right or left justified.
Check the ADCON2 register.
/Henrik.
Hi Henrik!
Thanks! You are always a good friend and a very big help and I mean that sincerely. The PIC returns a "VOLTS" value of 5824 representing the .441 volts I see on a voltmeter. If I divide 5824 by 16 then the reading would be 364. I changed the code to be /16 rather than /10. Using a conversion chart where 5.00 volts equals 4096 then 364 is really close. The sensor does vary a little about every 28 second it will read either + or - 4 using the divide by 16 value or 5824 then 5888 without the divide. Not sure why this happens, it's not temperature related and the reference port and sensor port are connected together with a piece of tubing. I do not know how to display the value in the ADCON2 register to do a check.
Best, Ed
Hi Ed,
Your previous ADC had a resolution of 12 bits, ie a value ranging from 0 to 4095.
The PIC ADC has a resolution of 10 bits, ie a value ranging from 0 to 1023 in "steps" of 1 (0, 1 ,2 ,3, 4....) or from 0 to 65472 in "steps" of 64 (0, 64, 128, 192, 256...)
If you divide 65472 by 16 you'll get 4092 which is pretty close to 4095 - that's the reason it "works" for you. And if you divide the "step size" of 64 by 16 you'll get 4 so when you see you're value "bouncing" +/- 4 counts it's really only "bouncing" +/- 1 LSB which is kind of expected.
Try setting ADCON2.7 = 1 to right justify the result and see if you'll get a result from 0 to 1023 with "steps" of 1 instead. If needed, you can then get more resolution by oversampling. Take 16 readings, add them all up and divide by 4 to go from 10 bits to a "psuedo resolution" of 12 bits - for example.
I'm not saying what you're doing is wrong - as long as it works for you I'm happy :-)
I just wanted to clarify for you and others that might find your comment about the ADC is going from 0-65535 that the resolution of the ADC is NOT 16bits which you might think it is when reading your post.
/Henrik.
Interesting explanation Henrik. I was trying to work out how 0-65535 was achieved knowing that the PIC ADC was 10 bit. Reading the datasheet
I was puzzling over the last word "justification" and the effect it would have. Now I know.The module has five registers:
• A/D Result High Register (ADRESH)
• A/D Result Low Register (ADRESL)
• A/D Control Register 0 (ADCON0)
• A/D Control Register 1 (ADCON1)
• A/D Control Register 2 (ADCON2)
The ADCON0 register, shown in Register 17-1,
controls the operation of the A/D module. The
ADCON1 register, shown in Register 17-2, configures
the functions of the port pins. The ADCON2 register,
shown in Register 17-3, configures the A/D clock
source, programmed acquisition time and justification.
Steve Earl www.datageo.co.uk
Hi Steve, Ed,
Just to clarify further, it can never return 65535 no matter how you configure it.
It'll look like this, where x is the actual conversion result returned by the 10 bit ADC:With left justification the two most significant bits in ADRESL are the two least significant bits of the 10 bit result.Code:ADRESH ADRESL xxxxxxxx xx000000 Left justified, ADCON2.7 = 0 (default) 000000xx xxxxxxxx Right justified, ADCON2.7 = 1
With right justification the two least significant bits in ADRESH are the two most significant bits of the 10 bit result.
If you have the ADC set to left justification but look at the result as a "normal" 16 bit value each LSB of the ADC result will change the 16 bit value by 64 since the LSB of the ADC result is aligned with ADRESL.6.
So, with left justification and the ADC saturated the maximum value it can return is 65472 since the 6 least significant bits in ADRESL will be 0.
/Henrik.
Hi Henrik!
Again thank you! You are correct. I connected a 10K resistor from the +5v to the ADC input pin and got 65472! Probably should have done that first! Just wondering, any way to eliminate the "bounce"?
Ed
Bookmarks