View previous topic :: View next topic |
Author |
Message |
picj1984
Joined: 01 Mar 2010 Posts: 73
|
analog inputs sourcing too much current? |
Posted: Mon Jun 07, 2010 4:44 pm |
|
|
Hi guys,
I'm using a PIC16f886, and I have 6 analog inputs (AN0, AN1, AN2, AN3, AN4, AN8). I'm using compiler version 4.105
Anyway, I'm just looking to get a voltage reading every once in awhile using 10k linear pots. Clockwise to 4.5V and counterclockwise to ground. Wiper to analog in.
Depending on what position the pots are in (i.e. if the pots are in all the way counterclockwise position), my circuit sources a TON of current.
Shouldn't the PIC prevent this from happening? Here is my main routine stuff. when I'm actually reading the analog ins I'm just doing a simple check_adcs function I wrote (at the bottom). let me know if anyone can shed any light on this issue. I'd really, really appreciate it.
Code: |
void main ()
{
in_c2 = input_state(PIN_C2);
if(in_c2 == lo)
setup_ccp1(CCP_CAPTURE_RE);
else if(in_c2 == hi)
setup_ccp1(CCP_CAPTURE_FE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DIV_BY_1,90,1);
setup_ccp2(CCP_PWM);
setup_adc_ports(sAN0);
setup_adc_ports(sAN1);
setup_adc_ports(sAN2);
setup_adc_ports(sAN3);
setup_adc_ports(sAN4);
setup_adc_ports(sAN8);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(10);
reload = read_adc();
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
|
here's that function I wrote that I was referring to:
Code: |
void check_adcs(){
if(adc_counter == 20)
{
set_adc_channel(0);
delay_us(12);
an0 = read_adc();
check = an0 >> 2;
if(check != check_last)
{
run = disable;
reload = an0;
if(reload >= 0xFC)
reload = 0xFC;
}
check_last = check;
}
else if(adc_counter == 40)
{
set_adc_channel(1);
delay_us(12);
dep = read_adc();
}
else if(adc_counter == 60)
{
set_adc_channel(2);
delay_us(12);
ratio = read_adc();
}
else if(adc_counter == 80)
{
set_adc_channel(3);
delay_us(12);
sine = read_adc();
}
else if(adc_counter == 100)
{
set_adc_channel(4);
delay_us(12);
tri = read_adc();
}
else if(adc_counter == 120)
{
set_adc_channel(8);
delay_us(12);
square = read_adc();
adc_counter = 0;
}
adc_counter++;
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 07, 2010 4:50 pm |
|
|
Quote: |
setup_adc_ports(sAN0);
setup_adc_ports(sAN1);
setup_adc_ports(sAN2);
setup_adc_ports(sAN3);
setup_adc_ports(sAN4);
setup_adc_ports(sAN8);
|
This is not correct. The setup function is not cumulative. You need to
change it to use one call to the function, and Bitwise OR (with the |
operator) the sANx values together. Example:
Code: | setup_adc_ports(sAN0 | sAN1 | etc. ); |
The reason for the high current is explained in this section of the
16F886 data sheet:
Quote: |
9.1.1 PORT CONFIGURATION
Note: Analog voltages on any pin that is defined
as a digital input may cause the input
buffer to conduct excess current.
|
Your code was only making sAN8 (the last one) into an analog input.
The rest of them were digital. |
|
|
picj1984
Joined: 01 Mar 2010 Posts: 73
|
|
Posted: Mon Jun 07, 2010 5:20 pm |
|
|
Thanks so much!!! |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Jun 07, 2010 5:37 pm |
|
|
And for correctness, analog (or any) inputs typically don't "source current".
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
picj1984
Joined: 01 Mar 2010 Posts: 73
|
|
Posted: Mon Jun 07, 2010 5:56 pm |
|
|
Hey guys,
They all work now except for sAN3. It's still doing the same thing. Is it possible I did some physical damage to the PIC? (although I do have a 50 mA resettable fuse protecting the circuit).
Here is the new code
Code: |
void main ()
{
in_c2 = input_state(PIN_C2);
if(in_c2 == lo)
setup_ccp1(CCP_CAPTURE_RE);
else if(in_c2 == hi)
setup_ccp1(CCP_CAPTURE_FE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DIV_BY_1,90,1);
setup_ccp2(CCP_PWM);
setup_adc_ports(sAN0 | sAN1 | sAN2 | sAN3 | sAN4 | sAN8);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(10);
reload = read_adc();
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 07, 2010 7:02 pm |
|
|
It's possible. Try a new PIC. |
|
|
picj1984
Joined: 01 Mar 2010 Posts: 73
|
|
Posted: Tue Jun 08, 2010 4:11 pm |
|
|
I had a solder bridge on a3. Everything works perfect now, thanks again. |
|
|
|