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 support@ccsinfo.com

ADC Problem

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



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

ADC Problem
PostPosted: Fri Jun 17, 2016 8:00 pm     Reply with quote

Hi, I'm trying to measure 25V.
but the measure is not good ... it has much error
for example 5V is 6.5V or 20V is 24V.
In the simulation it works fine, but not physically.
Anyone know why?

The hardware is simple:

AN0 <--------------------¬
Vcc (25V) <-----30K ----|----- 7.5K--> GND


Code:
#include  <18F26K22.h>
#Device  PASS_STRINGS=IN_RAM  //#ZERO_RAM
#define FASTER_BUT_MORE_ROM                                                               // Variables en memoria       
//#FUSES INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN, NOHFOFST
#fuses HSM, NOWDT, BROWNOUT, PUT, NOPBADEN, NOHFOFST
#fuses NOPLLEN        //HW PLL disabled, PLL enabled in software
#fuses MCLR           //Master Clear pin enabled
#fuses PROTECT        //Code protected from reads

#use delay(clock=12MHz))

int16 q;
float p;


void main(){

 setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_4);
 setup_adc_ports(sAN0|sAN1|sAN2, VSS_VDD);


while(TRUE){
   SET_ADC_CHANNEL(0);
 q= read_adc(); //
 p=25.0*q/1024; //
   
   printf("%03.3fV",p);

}
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Jun 17, 2016 9:11 pm     Reply with quote

with Vss Vdd set for A/D - what is your power supply voltage to the pic?
it matters in the "real"
is the power inlet pin decoupled with a capacitor?
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Fri Jun 17, 2016 9:17 pm     Reply with quote

asmboy wrote:
with Vss Vdd set for A/D - what is your power supply voltage to the pic?
it matters in the "real"
is the power inlet pin decoupled with a capacitor?


The power supply is 5V, the cap is necessary, because the error is big?
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Sat Jun 18, 2016 12:56 am     Reply with quote

The capacitor is necessary on any ADC input to clear ripples
0.1uF will to the job. Also 0.1uF close as you can to the PIC VDD
Check also your 5V supply for ripples
You need also delay between:
SET_ADC_CHANNEL(0);
read_adc();
With the same microcontroller I am getting very accurate readings:
Code:
set_adc_channel (0);
//code that takes 24us
analog0=read_adc(ADC_START_AND_READ);

Also your resistors division will give 5V for 25V, are they accurate?

Best wishes
Joe
guy



Joined: 21 Oct 2005
Posts: 291

View user's profile Send private message Visit poster's website

PostPosted: Sat Jun 18, 2016 2:38 am     Reply with quote

The resistance you are using is too high. The maximum resistance for A/D is 10K. You can divide both resistors by 10 (3K and 750 ohm).
Adding a capacitor between the analog pin and ground will help if you must use >10K resistance and also with the ripple, but it will slow down the response to a change in the signal and during power up.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Sat Jun 18, 2016 4:48 am     Reply with quote

I am using 39K on the up to 44 volts, 5K1 to the ground, both 1% accuracy, 0.1uF capacitor on the ADC pin and it works perfect.
And a 5.1V zener just in case
If an analog in is maximum 5V, using a serial resistor of 510 ohm and 0.1uF capacitor.
I burned a few PIC's without using the 510 ohm resistor.

James
guy



Joined: 21 Oct 2005
Posts: 291

View user's profile Send private message Visit poster's website

PostPosted: Sat Jun 18, 2016 5:28 am     Reply with quote

gjs_rsdi take care with the Zener diode. Different manufacturers have different reverse leakage currents and with a high 39K resistance you might find yourself reading lower voltages if you change the Zener one day.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

PostPosted: Sat Jun 18, 2016 5:49 am     Reply with quote

Thanks for the advice guy Smile
Alway using zeners in such cases not to violate the ADC input
I am always reading the data sheet

Best wishes
James
temtronic



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

View user's profile Send private message

PostPosted: Sat Jun 18, 2016 6:00 am     Reply with quote

comment:

When selecting resistors for your voltage divider, obviously use 1% ones, but be sure to measure them. Choose the BEST ones from say a batch of 10 of each value to get the closest match possible. This way you KNOW you've got 'good numbers' coming in and NOT have to 'tweak' the code to compensate for 'some analog funny stuff'....

Jay
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Sat Jun 18, 2016 9:25 am     Reply with quote

gjs_rsdi wrote:
The capacitor is necessary on any ADC input to clear ripples
0.1uF will to the job. Also 0.1uF close as you can to the PIC VDD
Check also your 5V supply for ripples
You need also delay between:
SET_ADC_CHANNEL(0);
read_adc();
With the same microcontroller I am getting very accurate readings:
Code:
set_adc_channel (0);
//code that takes 24us
analog0=read_adc(ADC_START_AND_READ);

Also your resistors division will give 5V for 25V, are they accurate?

Best wishes
Joe


Thank, why with 5V is very good. but with this not:
Code:
q= read_adc(); //
 p=25.0*q/1024; //


well, I put this code in one task. not in "while" maybe is by this?
The divisor is very good, I check with the voltmeter and work good.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sat Jun 18, 2016 11:50 am     Reply with quote

Quote:
39K on the up to 44 volts,


so is this 25v or 44v span or lucky 13 ???
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Sat Jun 18, 2016 7:15 pm     Reply with quote

Thank for all comment...I get the problem... the power supply was on 4.6V I had not seen... :/

now I fix to 5V with ADC 16bit and all is OK.
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