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

analog inputs sourcing too much current?

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



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

analog inputs sourcing too much current?
PostPosted: Mon Jun 07, 2010 4:44 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 07, 2010 4:50 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 07, 2010 5:20 pm     Reply with quote

Thanks so much!!!
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Jun 07, 2010 5:37 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 07, 2010 5:56 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 07, 2010 7:02 pm     Reply with quote

It's possible. Try a new PIC.
picj1984



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

PostPosted: Tue Jun 08, 2010 4:11 pm     Reply with quote

I had a solder bridge on a3. Everything works perfect now, thanks again.
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