PIC Hangs/Doesnt Work

Thread Starter

ssm14293

Joined Jul 14, 2011
90
Hi,
A problem has arose with my PIC18F452. Its is not working on a working circuit. I give the following code:

Rich (BB code):
void PIC_setting(void){
 ADRESH = 0x00;
 ADRESL = 0x00;
 TRISA = 0x00;
 TRISB = 0x00;
 TRISD = 0x00;
 TRISE = 0x00;
 PORTA = 0;
 }
void main() {
   PIC_setting();
   while (1){
         PORTA = ~PORTA;
         delay_ms(1000);
         PORTA = ~PORTA;
   }
}
the code is not working on both the pics i have. its was working a few days ago but now it isn't. What could be the reason, please help.
Im using MikroC for PIC 4.6, 8Mhz clock.
The PortA does give a high output but then it just stops there, no blinking LED.
Awaiting your reply, thanks in advance.
 

t06afre

Joined May 11, 2009
5,934
Then a PIC seems to be dead. It is most often something wrong with the configuration words. If it accept programming it is most likely OK. I would start with looking over your configuration words setting. Why is this left out in your sample code?
 

spinnaker

Joined Oct 29, 2009
7,830
Hi,
A problem has arose with my PIC18F452. Its is not working on a working circuit. I give the following code:

Rich (BB code):
void PIC_setting(void){
 ADRESH = 0x00;
 ADRESL = 0x00;
 TRISA = 0x00;
 TRISB = 0x00;
 TRISD = 0x00;
 TRISE = 0x00;
 PORTA = 0;
 }
void main() {
   PIC_setting();
   while (1){
         PORTA = ~PORTA;
         delay_ms(1000);
         PORTA = ~PORTA;
   }
}
the code is not working on both the pics i have. its was working a few days ago but now it isn't. What could be the reason, please help.
Im using MikroC for PIC 4.6, 8Mhz clock.
The PortA does give a high output but then it just stops there, no blinking LED.
Awaiting your reply, thanks in advance.
What happens when you debug the code? How are you certain the Pic is locked up and it is not your external circuit or your code? Have you use a scope or logic probe on your output pins to see in the pins are being toggled?

Have you done anything to diagnose the problem?

One thing I see is your eye will never see the light turn off.

You turn it on for 1000ms then turn it off for 500 nano seconds (if I did my math right) then turn it back on again. Unless you have a computer fast eye you will never see it turn off. You need a second delay as the last line in the while loop or the first line.

Rich (BB code):
 while (1){
        delay_ms(1000);
         PORTA = ~PORTA;
         delay_ms(1000);
         PORTA = ~PORTA;
   }
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
thanx... this is me being dumb...
circuit is fine as it was working before. the delay might be a reason. th config bit are set in a separate window...
OSC is external with HS confing, LVP enable, rest disabled...
i'll try the new code and tell you guyz bout it...
thanx again... :D
i dont have an CRO or Oscilloscope... not professional and its quiet expensive too...
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
new code:
Rich (BB code):
void PIC_setting(void){
 ADRESH = 0x00;
 ADRESL = 0x00;
 TRISA = 0x00;
 TRISB = 0x00;
 TRISD = 0x00;
 TRISE = 0x00;
 PORTA = 0;
 }
void main() {
   PIC_setting();
   while (1){
         PORTA = ~PORTA;
         delay_ms(1000);
   }
}
no difference... :(
 

spinnaker

Joined Oct 29, 2009
7,830
thanx... this is me being dumb...
circuit is fine as it was working before. the delay might be a reason. th config bit are set in a separate window...
OSC is external with HS confing, LVP enable, rest disabled...
i'll try the new code and tell you guyz bout it...
thanx again... :D
i dont have an CRO or Oscilloscope... not professional and its quiet expensive too...
You can make a logic probe for a few cents (very small amount of money).

If you want to be able to work with electronics then you will need at least some tools.

You could also have tried debugger which costs nothing assuming your programmer has a debugger. If it does not then you need to get one. It will be very difficult to trouble shoot more complex problems without one.

Oh and the PIC Kit II comes with a logic analyzer which you can use as a scope (for many digital needs) / logic probe.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
new code:
Rich (BB code):
void PIC_setting(void){
 ADRESH = 0x00;
 ADRESL = 0x00;
 TRISA = 0x00;
 TRISB = 0x00;
 TRISD = 0x00;
 TRISE = 0x00;
 PORTA = 0;
 }
void main() {
   PIC_setting();
   while (1){
         PORTA = ~PORTA;
         delay_ms(1000);
   }
}
no difference... :(
Try changing your config bits so it uses the internal osc if it has one.

What does PIC_setting() do?

Check your datasheet you should have an analog select register. Check PORTA to see if it has analog ports. If it does those ports should be set to digital. Most likely PORT A analog select resister is ANSEL. Set it to zero at the top of your code. ANSEL = 0;
 

ErnieM

Joined Apr 24, 2011
8,377
Pins on a PIC will default to their analog function if they have one, so first thing you want to do is turn off any analog control using the ADCON1 register. For your PIC you need to set ADCON1 to 0x07 to disable all analog pins.

Everyone looses time on this when beginning PICs so welcome to the club and remember the lesson.

Get to know the simulator inside MPLAB. It allows you to test your code without any hardware at all and will show you if you have a problem (though you still have to figure out WHY you have the problem).

What programmer do you use? Some are also able to do debugging with your hardware, which is incredibly valuable to troubleshoot code.
 

kubeek

Joined Sep 20, 2005
5,794
I don´t know PICs, but are you soure that PORTA is configured as ouptut? On AVRs it is input as default and I don´t see anything in the code that resembles configuring the output, but I am probably wrong :)
 

ErnieM

Joined Apr 24, 2011
8,377
I don´t know PICs, but are you soure that PORTA is configured as ouptut? On AVRs it is input as default and I don´t see anything in the code that resembles configuring the output, but I am probably wrong :)

Yeah he has that. The line "TRISA = 0;" sets all the port pins as outputs.
 

thatoneguy

Joined Feb 19, 2009
6,359
Make sure you have the watchdog timer disabled in the config, and use the internal oscillator.

Disable comparators.

Set compiler to debug mode and use MPLAB to Debug while running the code if you are using a PICKit 2 or PICKit 3
 

spinnaker

Joined Oct 29, 2009
7,830
Pins on a PIC will default to their analog function if they have one, so first thing you want to do is turn off any analog control using the ADCON1 register. For your PIC you need to set ADCON1 to 0x07 to disable all analog pins.

Everyone looses time on this when beginning PICs so welcome to the club and remember the lesson.

Get to know the simulator inside MPLAB. It allows you to test your code without any hardware at all and will show you if you have a problem (though you still have to figure out WHY you have the problem).

What programmer do you use? Some are also able to do debugging with your hardware, which is incredibly valuable to troubleshoot code.
I just noticed that he did list the chip. I sure wish Microchip woould standardize on their registers.

Actually I think you should only need to disable analog pins if they are inputs. But it does not hurt to do it for outputs.
 

Thread Starter

ssm14293

Joined Jul 14, 2011
90
thats what im thinking... if its output i wudnt matter wud it??
wudnt hurt to try though... PIC_settings call the settings... its written above :)
nd it doesnt have an internal osc, watchdog timer is off, what else??
oh yeah.. programmer... it has ICSP but no debugging i presume, its JDM Multi PIC programmer v5.2 (available on net).
 

ErnieM

Joined Apr 24, 2011
8,377
I just noticed that he did list the chip. I sure wish Microchip woould standardize on their registers.
I'm glad they didn't, or at least standardize like these are. For zero cents in extra transistors you should be able to use any I/O pin as an analog pin.

Actually I think you should only need to disable analog pins if they are inputs. But it does not hurt to do it for outputs.
PIC18FXX2 Data Sheet said:
Note: On a Power-on Reset, RA5 and RA3:RA0
are configured as analog inputs and read
as ‘0’.
How pins read is important when you use constructs such as:
Rich (BB code):
   PORTA = ~PORTA;
so always turn analog off if not used.

(Note: Even writing all 1's and all 0's to LATA with analog on produces possibly "unexpected" (incorrect) results in MPLAB)
 
Top