View previous topic :: View next topic |
Author |
Message |
opvini
Joined: 27 Apr 2012 Posts: 50 Location: Brazil
|
ADC problem: cant read 0.5v |
Posted: Sat Apr 06, 2013 5:42 pm |
|
|
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
|
|
Posted: Sat Apr 06, 2013 5:44 pm |
|
|
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
|
|
Posted: Sat Apr 06, 2013 6:32 pm |
|
|
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
|
|
Posted: Sat Apr 06, 2013 6:36 pm |
|
|
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
|
|
Posted: Thu Apr 11, 2013 8:13 am |
|
|
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
|
|
Posted: Thu Apr 11, 2013 8:54 am |
|
|
Show us a schematic of what you are trying to do.
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Apr 11, 2013 12:26 pm |
|
|
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
|
|
Posted: Sat Apr 13, 2013 9:58 am |
|
|
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. |
|
|
|