View previous topic :: View next topic |
Author |
Message |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
adc resolution setup 16f88 |
Posted: Mon Oct 19, 2009 5:55 am |
|
|
16F88 have a 10 bit ADC, so, 1023 must be the maximum value.
I have a program with this instructions, that do adc and lcd update each second (through interrupt):
Code: |
int16 adc_value;
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
adc_value = read_adc();
printf(lcd_putc, "volt=%lu", adc_value);
|
But when I give +5V to the ADC input, I read just "volt=250" on the lcd.
Maybe the adc is working at 8 bit, so the maximum value is 255.
How can I change the ADC bit resolution? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1937 Location: Norman, OK
|
|
Posted: Mon Oct 19, 2009 6:11 am |
|
|
#DEVICE ADC=10 _________________ Google and Forum Search are some of your best tools!!!! |
|
|
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
|
Posted: Mon Oct 19, 2009 1:22 pm |
|
|
I imagined that was something simple.
Now the higher value is 65472.... like 2^16.
adc_value is an int16 variable.
why I get 2^16 as max?... How can I get 2^10=1023 as max?
I observe also that the lcd can show just some values, for example from 21632 go directly to 21760. I supose because there are 2^10 levels, but into 2^16 range. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 19, 2009 2:29 pm |
|
|
That's because vs. 3.160 is buggy with the 16F88. Normally, the compiler
sets the ADFM bit = 1, to right-justify the 10-bit A/D result. But it doesn't
do that in your version. To fix it, you need to do it manually with code.
Add the lines shown in bold below:
Quote: | #include <16F88.h>
#device ADC=10
#fuses XT, NOWDT, NOLVP
#use delay(clock=4000000)
#byte ADCON1 = 0x9F
#bit ADFM_BIT = ADCON1.7
//=====================================
void main(void)
{
int16 result;
// Put A/D setup code here.
ADFM_BIT = 1; // Right-justify 10-bit A/D result
result = read_adc();
while(1);
}
|
I didn't show the A/D setup code. I only showed how to set the ADFM
bit. |
|
|
|