View previous topic :: View next topic |
Author |
Message |
surajka
Joined: 04 Oct 2015 Posts: 9
|
Wrong ADC value |
Posted: Sun Oct 04, 2015 4:43 am |
|
|
I am getting wrong ADC values.
compiler v4.130
My output is getting like this
BAT = 768
BAT = 0
BAT = 453
BAT = 377
BAT = 0
BAT = 593
BAT = 0
BAT = 768
BAT = 0
BAT = 428
BAT = 432
BAT = 0
BAT = 624
BAT = 0
BAT = 768
BAT = 0
BAT = 373
BAT = 433
I have connected 1.4V input to B4 pin, which is reliable and noise free.
I checked all other adc channels and found no problem except this one. The funny thing is that I get different values measured in 2ms gap.
I checked the voltage at the pin. It's OK and not floating.
Somebody please help me.
My code is
Code: | #include <16F886.h>
#device adc=10
#FUSES WDT //Watch Dog Timer
#FUSES PUT //Power Up Timer
//!#FUSES PROTECT //Code protected from reads
//!#FUSES BROWNOUT_SW //Brownout controlled by configuration bit in special file register
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES BORV40
#FUSES NOBROWNOUT //No brownout reset
//!#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOIESO //Internal External Switch Over mode disabled
#use delay(internal=4000000,restart_wdt)
//#use STANDARD_IO( B )
#use FIXED_IO( B_outputs=PIN_B7 )
#use FIXED_IO( A_outputs=PIN_A7,PIN_A6,PIN_A4 )
#use FIXED_IO( C_outputs=PIN_C5,PIN_C2,PIN_C1,PIN_C0 )
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)
void main()
{
setup_adc_ports(san11);
setup_adc(ADC_CLOCK_INTERNAL);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(OSC_4MHZ);
SETUP_CCP1(CCP_OFF);
while(TRUE)
{
set_adc_channel(11);
delay_ms(2);
printf("\r\n BAT = %lu", read_adc());
delay_ms(2);
printf("\r\n BAT = %lu", read_adc());
delay_ms(1000);
//TODO: User Code
}
} |
_________________ suraj |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Sun Oct 04, 2015 5:15 am |
|
|
this ...
setup_adc(ADC_CLOCK_INTERNAL);
probably is the problem.
Check the PIC datasheet, ADC section and review the text and charts as to what ADC clocks are valid for a 4MHz processor speed.
Jay |
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Sun Oct 04, 2015 5:23 am |
|
|
temtronic wrote: | this ...
setup_adc(ADC_CLOCK_INTERNAL);
probably is the problem.
Check the PIC datasheet, ADC section and review the text and charts as to what ADC clocks are valid for a 4MHz processor speed.
Jay |
Thank you Jay.
I changed it to all possible values div32, div8 and div2. No change. I am getting correct values on all the other 10 adc channels. Still not getting it working.
I also tried setting tris and Ansel registers using #byte. No change in the result. _________________ suraj |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Sun Oct 04, 2015 6:06 am |
|
|
hmm...
1) According to the datasheet I have , table 0-1, Fosc/8 is the only valid selection unless the PIC is in SLEEP.
2) You should disable the PWM peripheral as it uses that pin,though you say the other ADC inputs are fine and 3 of them are the other PWM pins.
3) Use Standard I/O and #FUSES NOLVP.
4) I'd change the prints to 'BAT1' and 'BAT2' just to show which reading is 1st and 2nd
recode/recompile.retest,repost program and report back what happens....
it does seem odd that only ADC11 is bad....
Jay |
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Sun Oct 04, 2015 6:44 am |
|
|
temtronic wrote: | hmm...
1) According to the datasheet I have , table 0-1, Fosc/8 is the only valid selection unless the PIC is in SLEEP.
2) You should disable the PWM peripheral as it uses that pin,though you say the other ADC inputs are fine and 3 of them are the other PWM pins.
3) Use Standard I/O and #FUSES NOLVP.
4) I'd change the prints to 'BAT1' and 'BAT2' just to show which reading is 1st and 2nd
recode/recompile.retest,repost program and report back what happens....
it does seem odd that only ADC11 is bad....
Jay |
Thanks Jay.
I will check it and post tomorrow morning.
How can I disable just pwm? Tried disabling ccp.
In another code with a delay 1 sec in between I got values increasing from 0 to ~ 768 and then ~768 to 0, just like a sine wave. I will post the results tomorrow. Has it any connection with mclr as I/o pin? I got correct result with another circuit with mclr enabled. Tried changing the pic, no results. _________________ suraj |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 04, 2015 7:25 am |
|
|
What do you think the line in bold does ?
Quote: | #include <16F886.h>
#device adc=10
#FUSES WDT //Watch Dog Timer
#FUSES PUT //Power Up Timer
//!#FUSES PROTECT //Code protected from reads
//!#FUSES BROWNOUT_SW //Brownout controlled by configuration bit in special file register |
|
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Sun Oct 04, 2015 7:40 am |
|
|
PCM programmer wrote: | What do you think the line in bold does ?
Quote: | #include <16F886.h>
#device adc=10
#FUSES WDT //Watch Dog Timer
#FUSES PUT //Power Up Timer
//!#FUSES PROTECT //Code protected from reads
//!#FUSES BROWNOUT_SW //Brownout controlled by configuration bit in special file register |
|
Watchdog timer. I used restart_wdt . can it be causing the problem? I think the circuit is not restarting. It worked well with other channels. I can check the code only tomorrow. I will update you. Thank you pcm programmer. _________________ suraj |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 04, 2015 7:42 am |
|
|
It could be an issue. The watchdog timer's timeout period is variable
with temperature. It can be as low as 10 ms.
Try using NOWDT and see what happens. |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Sun Oct 04, 2015 3:50 pm |
|
|
Try checking that the register setup is correct -
ADCON0 ADCON1 ANSELH TRISB etc.
If it's a compiler bug then it could affect only the specific channel.
Also I suggest to read_adc() into an int16 variable and only then printf() it. Maybe watching the variable will show something?
The 16F886 silicon is well-debugged I assume. Maybe a newer compiler version will fix it? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Sun Oct 04, 2015 4:36 pm |
|
|
I still can't figure out what ONLY affects channel 11, as he says all the others are fine.
hmm... maybe a bad connection ?
Jay |
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Mon Oct 05, 2015 3:24 am |
|
|
I changed to the code. No results.
Code: | #include <16F886.h>
#device adc=10
#FUSES NOWDT //Watch Dog Timer
#FUSES PUT //Power Up Timer
//!#FUSES PROTECT //Code protected from reads
//!#FUSES BROWNOUT_SW //Brownout controlled by configuration bit in special file register
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES BORV40
#FUSES NOBROWNOUT //No brownout reset
//!#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOIESO //Internal External Switch Over mode disabled
#use delay(internal=4000000,restart_wdt)
#use STANDARD_IO( B )
//!#use FIXED_IO( B_outputs=PIN_B7 )
//!#use FIXED_IO( A_outputs=PIN_A7,PIN_A6,PIN_A4 )
//!#use FIXED_IO( C_outputs=PIN_C5,PIN_C2,PIN_C1,PIN_C0 )
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)
void main()
{
unsigned long adc_read_value=0;
setup_adc_ports(san11);
setup_adc(ADC_CLOCK_DIV_8);
//! setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(OSC_4MHZ);
SETUP_CCP1(CCP_OFF);
set_tris_b(0x7F);
#byte ANSELH = 0x3F;
ANSELH = 0x3F;
while(TRUE)
{
set_adc_channel(11);
delay_ms(2);
adc_read_value = read_adc();
while(!adc_done());
printf("\r\n BAT1 = %lu", adc_read_value);
adc_read_value = read_adc();
while(!adc_done());
printf("\r\n BAT2 = %lu", adc_read_value);
delay_ms(1000);
//TODO: User Code
}
} |
The output when the input at the pin is 55mV is below
BAT1 = 23
BAT2 = 0
BAT1 = 1
BAT2 = 0
BAT1 = 4
BAT2 = 0
BAT1 = 0
BAT2 = 12
BAT1 = 0
BAT2 = 38
BAT1 = 0
BAT2 = 5
BAT1 = 0
BAT2 = 0
BAT1 = 36
BAT2 = 0
BAT1 = 29
BAT2 = 0
BAT1 = 14
BAT2 = 0
BAT1 = 1
BAT2 = 4
BAT1 = 0
BAT2 = 38
BAT1 = 0
BAT2 = 11
BAT1 = 0
BAT2 = 12
BAT1 = 24
BAT2 = 0
BAT1 = 36
BAT2 = 0
BAT1 = 35
BAT2 = 0
BAT1 = 1
BAT2 = 4
BAT1 = 0
BAT2 = 35
BAT1 = 0
The output when the input is 1.4V is below
BAT2 = 614
BAT1 = 625
BAT2 = 0
BAT1 = 582
BAT2 = 0
BAT1 = 0
BAT2 = 509
BAT1 = 0
BAT2 = 768
BAT1 = 565
BAT2 = 0
BAT1 = 732
BAT2 = 0
BAT1 = 0
BAT2 = 461
BAT1 = 0
BAT2 = 768
BAT1 = 497
BAT2 = 0
BAT1 = 768
BAT2 = 0
BAT1 = 0
BAT2 = 395
BAT1 = 0
BAT2 = 768
BAT1 = 430
BAT2 = 0
BAT1 = 768
BAT2 = 0
BAT1 = 0
BAT2 = 277
BAT1 = 0
BAT2 = 768
BAT1 = 312
BAT2 = 0
BAT1 = 768
Does it have anything to do with the PWM? _________________ suraj |
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Mon Oct 05, 2015 3:33 am |
|
|
I am getting more stable values in the floating pins.
The simulation in proteus is giving the right values. _________________ suraj |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Oct 05, 2015 3:46 am |
|
|
Quote: | Does it have anything to do with the PWM? |
Don't think so. Probably has more to do with this:
Code: |
setup_adc_ports(san11);
...
#byte ANSELH = 0x3F;
ANSELH = 0x3F;
|
AN11 is enabled in ANSELH. setup_adc_ports() sets ANSEL for you. You then disable it by setting ANSELH to 0x3F! So, no, it's not going to work.
You do similar confusing things with TRIS. I'd remove all your ANSEL and TRIS settings, leave everything as default and let the compiler set them for you.
It really doesn't matter, nor tell you much, if the code works in Proteus simulation. There's way too many problem with that simulation. You have to ge it working in hardware - that's where it really matters. |
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Mon Oct 05, 2015 3:58 am |
|
|
RF Developer, I changed the code as you suggested. I included a floating input AN5(no such pin in the IC). Stable voltage is connected to AN10.
I am posting the code and results below. Strange thing getting correct value at AN10(is 1.82V) without setting up the input pin as analog.
Code: |
#include <16F886.h>
#device adc=10
#FUSES NOWDT //Watch Dog Timer
#FUSES PUT //Power Up Timer
//!#FUSES PROTECT //Code protected from reads
//!#FUSES BROWNOUT_SW //Brownout controlled by configuration bit in special file register
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES BORV40
#FUSES NOBROWNOUT //No brownout reset
//!#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOIESO //Internal External Switch Over mode disabled
#use delay(internal=4000000,restart_wdt)
#use STANDARD_IO( B )
//!#use FIXED_IO( B_outputs=PIN_B7 )
//!#use FIXED_IO( A_outputs=PIN_A7,PIN_A6,PIN_A4 )
//!#use FIXED_IO( C_outputs=PIN_C5,PIN_C2,PIN_C1,PIN_C0 )
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)
void main()
{
unsigned long adc_read_value1=0,adc_read_value2=0;
setup_adc_ports(sAN11);
setup_adc(ADC_CLOCK_DIV_8);
//! setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(OSC_4MHZ);
SETUP_CCP1(CCP_OFF);
//! set_tris_b(0x7F);
//! #byte ANSELH = 0x3F;
//! ANSELH = 0x3F;
while(TRUE)
{
set_adc_channel(11);
delay_ms(2);
adc_read_value1 = read_adc();
while(!adc_done());
printf("\r\n BAT1 = %04lu", adc_read_value1);
adc_read_value2 = read_adc();
while(!adc_done());
printf(" BAT2 = %04lu", adc_read_value2);
set_adc_channel(10);
delay_ms(2);
adc_read_value2 = read_adc();
while(!adc_done());
printf(" BAT3 = %04lu", adc_read_value2);
set_adc_channel(5);
delay_ms(2);
adc_read_value2 = read_adc();
while(!adc_done());
printf(" BAT4 = %04lu", adc_read_value2);
delay_ms(1000);
//TODO: User Code
}
} |
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0324
BAT1 = 0768 BAT2 = 0000 BAT3 = 0372 BAT4 = 0356
BAT1 = 0768 BAT2 = 0000 BAT3 = 0372 BAT4 = 0369
BAT1 = 0768 BAT2 = 0000 BAT3 = 0372 BAT4 = 0370
BAT1 = 0768 BAT2 = 0000 BAT3 = 0372 BAT4 = 0371
BAT1 = 0761 BAT2 = 0000 BAT3 = 0372 BAT4 = 0371
BAT1 = 0703 BAT2 = 0106 BAT3 = 0372 BAT4 = 0371
BAT1 = 0636 BAT2 = 0210 BAT3 = 0372 BAT4 = 0371
BAT1 = 0564 BAT2 = 0306 BAT3 = 0372 BAT4 = 0371
BAT1 = 0506 BAT2 = 0372 BAT3 = 0372 BAT4 = 0371
BAT1 = 0446 BAT2 = 0425 BAT3 = 0372 BAT4 = 0371
BAT1 = 0376 BAT2 = 0458 BAT3 = 0372 BAT4 = 0371
BAT1 = 0313 BAT2 = 0506 BAT3 = 0372 BAT4 = 0371
BAT1 = 0206 BAT2 = 0509 BAT3 = 0372 BAT4 = 0371
BAT1 = 0085 BAT2 = 0558 BAT3 = 0372 BAT4 = 0371
BAT1 = 0010 BAT2 = 0585 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0604 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0614 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0726 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0768 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0742 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0717 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0702 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0665 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0628 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0617 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0595 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0563 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0558 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0530 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0504 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0489 BAT3 = 0372 BAT4 = 0371
BAT1 = 0000 BAT2 = 0468 BAT3 = 0372 BAT4 = 0371 _________________ suraj |
|
|
surajka
Joined: 04 Oct 2015 Posts: 9
|
|
Posted: Mon Oct 05, 2015 4:37 am |
|
|
The output was from an opamp instrumentation amplifier with a 4.7k impedance added. There is no negative supply rail involved. I removed the connection and added another voltage source to the pin. Now I am getting correct values. The problem is not a software issue. Thank you all for the support. I am sorry for not troubleshooting the hardware earlier and wasting your time. The thread is closed. Still dont understand why such strange values I get.
BAT1 = 0400 BAT2 = 0402 BAT3 = 0371 BAT4 = 0370
BAT1 = 0403 BAT2 = 0399 BAT3 = 0372 BAT4 = 0370
BAT1 = 0401 BAT2 = 0399 BAT3 = 0371 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0371 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0371 BAT4 = 0370
BAT1 = 0399 BAT2 = 0398 BAT3 = 0371 BAT4 = 0370
BAT1 = 0404 BAT2 = 0401 BAT3 = 0371 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0372 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0371 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0371 BAT4 = 0370
BAT1 = 0401 BAT2 = 0400 BAT3 = 0372 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0372 BAT4 = 0370
BAT1 = 0401 BAT2 = 0401 BAT3 = 0372 BAT4 = 0370
BAT1 = 0400 BAT2 = 0401 BAT3 = 0372 BAT4 = 0370
BAT1 = 0400 BAT2 = 0401 BAT3 = 0372 BAT4 = 0371
BAT1 = 0400 BAT2 = 0400 BAT3 = 0371 BAT4 = 0370
BAT1 = 0400 BAT2 = 0400 BAT3 = 0371 BAT4 = 0370
BAT1 = 0400 BAT2 = 0401 BAT3 = 0372 BAT4 = 0370
BAT1 = 0401 BAT2 = 0404 BAT3 = 0371 BAT4 = 0370 _________________ suraj |
|
|
|