View previous topic :: View next topic |
Author |
Message |
Dean
Joined: 08 Jun 2017 Posts: 15
|
ADC in PIC18F4520 - fluctuation in reading |
Posted: Thu Dec 07, 2017 6:40 pm |
|
|
Hi everyone
I am using pic18F4520 ADC to read voltage which varies between 0 - 5 volts across 100 ohm resistor as a current sensor.
The reading I am gettting from the PIC fluctuated in High range while the AVOmeter reading is steady and exactly matches the calculations.
The code I am using is here:
Code: |
void TreatmentLoop()
{
setup_adc_ports(AN0); //sets Only AN0 pin to analog input
set_adc_channel(0); //the next read_adc call will read
while (True)
{
ReadCurrent() ;
lcd_gotoxy(1,1);
printf(lcd_putc "%lu \n\r", ANP1);
lcd_putc("Current Dose ");
delay_ms(1000) ;
}
}
|
Device is set as 16 bit
#device ADC=16
Reading I am getting varies between 0 to 320 (64, 128, 256, 320) in a random manner, while the reading is firmly steady on the AVOmeter. I can not understand why. Am I missing something here ????
Dean _________________ Find a way Or Make one . |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Thu Dec 07, 2017 7:34 pm |
|
|
hmm according to my datasheet, that PIC has a 10 bit ADC NOT a 16bit so I'd suggest:
#device ADC=10
The compiler should complain about it, though maybe it's smart and handles the configuration properly. Without a complete program we can't cut/paste/compile and dump the listing.
You should post your complete program as the ADC needs several options to be setup correctly.
The ADC is a single ended device. You mention a '100r load resistor' for current sensing, is one end tied to ground?
What is the actual load? If it's a motor, that will generate a LOT of EMI (noise) which the PIC's ADC will read unlike the DMM which only sample 3-4 times a second
Others will respond, these are just some quick comments I can think of.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 07, 2017 7:56 pm |
|
|
I don't see a line that calls the setup_adc() function. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19488
|
|
Posted: Fri Dec 08, 2017 2:39 am |
|
|
With ADC=16 selected the ADC is switched to 'left justified'. So a single count on the ADC will be 64 on the result....
The ADC will display steps of just 5mV, and quickly. An analog meter won't even see this, while conventional DVM's integrate the reading.
Remember also that the ADC is dependant on the reference being used. If this is the supply, there will be several mV of noise on this (and more on most prototype layouts). |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Fri Dec 08, 2017 1:03 pm |
|
|
The 'readcurrent()' function could be at fault. We have no idea what it actually does aside from a guess that maybe it stores something into a variable called ANP1 that main() printsout ?? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19488
|
|
Posted: Fri Dec 08, 2017 1:28 pm |
|
|
He is only seeing 5 counts on the ADC.
We haven't seen what clock rate is being used by the ADC?.
We haven't seen what reference is being used by the ADC?. |
|
|
Dean
Joined: 08 Jun 2017 Posts: 15
|
|
Posted: Fri Dec 08, 2017 4:20 pm |
|
|
Thank you guys Problem solved with your advice.
1. Software mistake #device ADC=10, NOT 16.
2. Hardware, extra capacitor is added 470Micro F at the power source.
Now it's working like a Swiss clock.
Thanks
Dean _________________ Find a way Or Make one . |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Fri Dec 08, 2017 4:40 pm |
|
|
Glad to hear it's 'up and running'.
Old school rule of thumb for power supplies
1) you need 1,000 MFD for every amp drawn
2) supply double the current you 'think' you need...
3) if motors or relays are involved... TRIPLE the supply !!
4) if it's 'marginal' , it will kinda fail, latch-up the product and cause you no end of grief...until you put a 10 AMP supply in...BTDT |
|
|
|