View previous topic :: View next topic |
Author |
Message |
Drone601
Joined: 16 Jan 2019 Posts: 8
|
ADC trouble |
Posted: Tue Jan 29, 2019 12:07 pm |
|
|
Hi all, and a preemptive thanks.
I'm new to ADC, and trying to use it on a PIC16F18345. The trouble seems to be that the pin I'm trying to use for ADC goes into a permanent pull-down state. Any voltage applied just gets sunk to near 0V and the ADC value doesn't change. But that pin works fine when I use it as a standard input. So the error seems to be me. Did try setup_adc_ports(sAN4, VSS_VDD), but that didn't help. Suggestions? Condemnations? Aspersions?
Code: |
#include <16F18345.h>
#fuses PUT,RSTOSC_HFINTRC_PLL,BROWNOUT,NOMCLR,NOCLKOUT,NOEXTOSC,PROTECT
#use delay(internal=4000000)
#use RS232(baud=9600, xmit=PIN_A0, ERRORS)
#define LCD_INIT 22
#define LCD_CLR_DSP 12
#define TX PIN_A0
void main()
{
set_tris_a(00111010);
set_tris_b(00000000);
set_tris_c(00000000);
int16 r;
setup_adc_ports(sAN4);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
output_high(TX); // LCD handling stuff
delay_ms(100); // ""
putc(LCD_INIT); // ""
putc(LCD_CLR_DSP); // ""
delay_ms(5); // ""
while(TRUE)
{
r =read_adc();
printf("AD is %4LD \r\n", r);
delay_ms(1000);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 29, 2019 12:32 pm |
|
|
Quote: | setup_adc_ports(sAN4);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0); |
You've set it up to use ch. 4 in the 1st line, but then you select channel 0.
You should select channel 4 in the 3rd line. |
|
|
Drone601
Joined: 16 Jan 2019 Posts: 8
|
|
Posted: Tue Jan 29, 2019 2:15 pm |
|
|
Ah, right. I had changed that in panicked random button pushing. Thanks!
And yes, that did help greatly. But...still have a problem with the pin pulling down the voltage too much. As soon as I connect to that pin...vooop. It's gone. I'd think I'd fried the port, but it works (or at least did last I checked) in non-ACD mode. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Tue Jan 29, 2019 2:43 pm |
|
|
What is the nature of the source feeding the ADC?.
The maximum recommended impedance for any source is 10KR. |
|
|
Drone601
Joined: 16 Jan 2019 Posts: 8
|
|
Posted: Tue Jan 29, 2019 3:05 pm |
|
|
It's a phototransistor. I *seem* to be connecting it as recommended by the manufacturer and some examples online. Surely it's just me being a dolt, trying to hammer it in with a rock or something. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Jan 29, 2019 3:11 pm |
|
|
Can you post a schematic please? |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue Jan 29, 2019 3:20 pm |
|
|
Ttelmah makes a good point. You can use a opamp buffer to ensure that you have a nice low impedance output to drive your ADC pin. If you are expecting voltages close to zero, make sure you get a negative power rail for your opamp, or use one with rail to rail output. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Jan 29, 2019 3:26 pm |
|
|
Most (all) photoransistors are a very nonlinear device (actually log ) so you'll need to calibrate the PIC program for each device. Mfrs post the graphs with the device info. As stated using a in/out rail-to-rail opamp will help.
Jay |
|
|
|