PIC Hangs/Doesnt Work

Discussion in 'Embedded Systems and Microcontrollers' started by ssm14293, Jan 14, 2012.

  1. ssm14293

    Thread Starter Member

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

    Code ( (Unknown Language)):
    1.  
    2.  
    3. void PIC_setting(void){
    4.  ADRESH = 0x00;
    5.  ADRESL = 0x00;
    6.  TRISA = 0x00;
    7.  TRISB = 0x00;
    8.  TRISD = 0x00;
    9.  TRISE = 0x00;
    10.  PORTA = 0;
    11.  }
    12. void main() {
    13.    PIC_setting();
    14.    while (1){
    15.          PORTA = ~PORTA;
    16.          delay_ms(1000);
    17.          PORTA = ~PORTA;
    18.    }
    19. }
    20.  
    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.
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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?
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.

    Code ( (Unknown Language)):
    1.  
    2.  while (1){
    3.         delay_ms(1000);
    4.          PORTA = ~PORTA;
    5.          delay_ms(1000);
    6.          PORTA = ~PORTA;
    7.    }
    8.  
    9.  
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    He is toggling the output port.
    Keep only one delay and one output instruction.
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    Ah yep. Now why didn't I think of that! :)

    My idea would still work but yours is more efficient.


    Code ( (Unknown Language)):
    1.  
    2.  while (1)
    3.  {          
    4.        PORTA = ~PORTA;          
    5.       delay_ms(1000);      
    6. }
    7.  
     
  6. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    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...
     
  7. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    new code:
    Code ( (Unknown Language)):
    1.  
    2.  
    3. void PIC_setting(void){
    4.  ADRESH = 0x00;
    5.  ADRESL = 0x00;
    6.  TRISA = 0x00;
    7.  TRISB = 0x00;
    8.  TRISD = 0x00;
    9.  TRISE = 0x00;
    10.  PORTA = 0;
    11.  }
    12. void main() {
    13.    PIC_setting();
    14.    while (1){
    15.          PORTA = ~PORTA;
    16.          delay_ms(1000);
    17.    }
    18. }
    19.  
    no difference... :(
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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: Jan 15, 2012
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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;
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,669
    804
    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 :)
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

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

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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
     
  14. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
    ssm14293 likes this.
  15. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    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).
     
  16. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    by any chance i forgot to write PIC name... its 18F452...
     
  17. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    ADCON thing worked... o_O :D
    doesnt make sense though... :p
    thank you all!! Love you guyz!!! :D
     
  18. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.

    How pins read is important when you use constructs such as:
    Code ( (Unknown Language)):
    1.    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)
     
  19. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    Lesson for All those Like me:
    ADCON register matters even in OUTPUT!!!
     
  20. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Remember it is not always ADCON to turn off analog. You need to check your datasheet. Sometimes it is ADCON, sometimes ANSEL, and there are other methods for disabling analog depending on the Pic.
     
    ssm14293 likes this.
Loading...