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 problem: cant read 0.5v

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



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

ADC problem: cant read 0.5v
PostPosted: Sat Apr 06, 2013 5:42 pm     Reply with quote

Hi everyone. I want to read an analog signal from a potentiometer. When it is on maximum position, it works fine (return 1023), but my problem is when it is on the minimum position (0,56v) it read 0 and not 115 as I expected.

The potentiometer is from a servo motor.
I really cant understand.
(sorry my english, I'm from Brazil).


Code:


#include <18f4550.h>
#device ADC=10
#fuses HS
#use delay(clock=4000000) 



#define use_portb_lcd true

// incluir depois do delay para poder usar o delay_ms ou delay_us
#include <aev_lcd.c>

void main(){

int16 potenciometro;


   lcd_init();
   
      // liga o conversor A/D
setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_INTERNAL);
   
   while(1){
     set_adc_channel(0); // seleciona o canal analogico
       delay_ms(1);
         potenciometro =  read_adc();

         delay_ms(1);
         printf(lcd_putc, "\f%ld",potenciometro);
         delay_ms(1000);
   }
}
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Sat Apr 06, 2013 5:44 pm     Reply with quote

sorry, the line "set_adc_channel(0);" is out of while(1) ok?!
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Apr 06, 2013 6:32 pm     Reply with quote

The code looks reasonable but I don't know if you are clocking the A/D too fast for your chip. How low a reading can it get before it goes bad? Are you sure it is not a hardware problem? What is the impedance (resistance) of the pot? Can you read with a voltmeter the voltage from the PIC GND pin to the PIC ADC pin? Is there some stray voltage between the pot and the PIC.
_________________
The search for better is endless. Instead simply find very good and get the job done.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 06, 2013 6:36 pm     Reply with quote

Quote:
setup_adc_ports(NO_ANALOGS);

What pin are you using for the analog input ? You must specify the pin
or group of pins for analog input. In the line above, you have selected
all pins as Digital. The A/D converter will not work correctly with this
setting. I suggest that you use pin AN0 for A/D input. Then use this:
Code:

setup_adc_ports(AN0);



Quote:
setup_adc (ADC_CLOCK_INTERNAL);

The 18F4550 says this divisor should be used with a 4MHz oscillator: ADC_CLOCK_DIV_4
opvini



Joined: 27 Apr 2012
Posts: 50
Location: Brazil

View user's profile Send private message

PostPosted: Thu Apr 11, 2013 8:13 am     Reply with quote

Thanks SherpaDoug and PCM programmer!
SherpaDoug, the problem is on the hardware. I will need one buffer circuit with operational amplifier, correct? Why do I need it (about the impedances)?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Apr 11, 2013 8:54 am     Reply with quote

Show us a schematic of what you are trying to do.

Mike
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Thu Apr 11, 2013 12:26 pm     Reply with quote

RE: potentiometers.

MANY cheap pots SHORT the wiper to the limit terminal BEFORE physical rotation is complete.

meaning with a low cost potentiometer, you can get a zero reading ( or fullscale) BEFORE the pot stops moving.
SuperDave



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

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

PostPosted: Sat Apr 13, 2013 9:58 am     Reply with quote

Impedance is very unlikely to be an issue since (a.) the input impedance of the PIC is very high and (b.) no one is changing the pot at millisecond speed especially when the measurement is made only once per second. The principle reason for a low impedance source is to improve settling time (and thus maintain bit resolution) which is not an issue here so the op amp is a waste of parts. This is a question of setting up the adc correctly including specifying the input pin you wish to measure. Make the software match the current hardware and you're set.
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