CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADC in PIC18F4520 - fluctuation in reading

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Dean



Joined: 08 Jun 2017
Posts: 15

View user's profile Send private message Send e-mail

ADC in PIC18F4520 - fluctuation in reading
PostPosted: Thu Dec 07, 2017 6:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 07, 2017 7:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 07, 2017 7:56 pm     Reply with quote

I don't see a line that calls the setup_adc() function.
Ttelmah



Joined: 11 Mar 2010
Posts: 19488

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 2:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 1:03 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 1:28 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 08, 2017 4:20 pm     Reply with quote

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. Laughing

Thanks

Dean
_________________
Find a way Or Make one .
temtronic



Joined: 01 Jul 2010
Posts: 9220
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 4:40 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group