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 causing PIC to freeze up

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



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

ADC causing PIC to freeze up
PostPosted: Tue Mar 22, 2011 4:46 pm     Reply with quote

I've run into a problem with my PIC12f1822, the following code blinks an LED for a random number of times then the PIC freezes up, but it always freezes in the 'adc()' function because the LED is always on (low) when it freezes. My ADC ports are connected to phototransistors with 2.2K resistors pulling them to ground. Is this possibly causing an error in my ADC hardware?

Compiler V4.028

Code:


#include "12f1822.h"
#device adc=10
#use delay(clock=4000000)
#Fuses NOFCMEN,NOCLKOUT,NOSTVREN,NODEBUG,NOPROTECT,NOBROWNOUT,NOCPD,NOPUT,NOIESO,INTRC_IO,NOWDT,NOMCLR
#define LED pin_a5

int16 LS1=0;
int16 LS2=0;
int16 LS3=0;

void adc()
{
   set_adc_channel(0);
   delay_us(20);
   LS1=read_adc();
   
   set_adc_channel(1);
   delay_us(20);
   LS2=read_adc();
   
   set_adc_channel(2);
   delay_us(20);
   LS3=read_adc();
}

void main()
{
   setup_oscillator(OSC_4MHZ);

   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(sAN0 | sAN1 | sAN2 | VSS_VDD);
   output_low(LED);
   output_low(SP);
   
   while(1)
   {
      output_low(LED);
      delay_ms(200);
     
      adc();
     
      output_high(LED);
      delay_ms(200);
   }
}

_________________
Vinnie Ryan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 22, 2011 10:10 pm     Reply with quote

Quote:
output_low(SP);

SP is not defined in the posted code.

Quote:
Compiler V4.028

Can you re-check and confirm your compiler version. Look at the top
of the .LST file after a successful compilation. You will see it there.
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Wed Mar 23, 2011 2:59 am     Reply with quote

Also, how is the LED connected?.
That it hangs with the LED 'on', doesn't imply that it hangs in the ADC function. It could be hanging at the act of switching the LED on. Much more likely, given the currents involved.
What is connected to the MCLR pin?. Though you have 'NOMCLR' selected, you can still get a reset/hang, if this pin is allowed to float.
How is your supply generated?. Again commonest reason for a hang, is a glitch on the supply, possibly caused by the surge in current as the LED is turned on, or by other circuitry nearby.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Mar 23, 2011 5:53 am     Reply with quote

You do have a current limiting resistor for the LED, based on VDD ?!
Random could mean insufficient power supply design or noise.
Be sure to have decoupling caps AT the PIC power pins.

Comment out the ADC routine and run, if it still crashes you KNOW it's not the adc() !
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Wed Mar 23, 2011 11:17 am     Reply with quote

PCM programmer wrote:
Quote:
output_low(SP);

SP is not defined in the posted code.

Quote:
Compiler V4.028

Can you re-check and confirm your compiler version. Look at the top
of the .LST file after a successful compilation. You will see it there.


The "output_low(SP)" line was supposed to be commented out. Also you're right, my compiler is 4.119. I was looking at CCS loader version.

The led is connected from V+ -> LED -> 1K resistor -> pin_a5. MCLR isn't connected to anything, though I even tried grounding it through a 10K resistor and had the same result. The power supply is 3 LR-44 1.5V batteries (4.5V) but I've tried using a bench power supply as well, same result either way. I have a .1uf cap at VDD - VSS. When I comment out the ADC routine, the code runs perfectly fine without crashing. I'll add serial communication and try to find out which specific ADC line it's crashing at. Maybe one of my ADC ports has a bad connection..?
_________________
Vinnie Ryan
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