View previous topic :: View next topic |
Author |
Message |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
ADC causing PIC to freeze up |
Posted: Tue Mar 22, 2011 4:46 pm |
|
|
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
|
|
Posted: Tue Mar 22, 2011 10:10 pm |
|
|
SP is not defined in the posted code.
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: 19548
|
|
Posted: Wed Mar 23, 2011 2:59 am |
|
|
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: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Mar 23, 2011 5:53 am |
|
|
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
|
|
Posted: Wed Mar 23, 2011 11:17 am |
|
|
PCM programmer wrote: |
SP is not defined in the posted code.
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 |
|
|
|