Connect with us

Crashing after 145 seconds

Discussion in 'Microcontrollers, Programming and IoT' started by LordSputnik, Jun 6, 2012.

  1. LordSputnik

    LordSputnik

    45
    0
    Aug 11, 2011
    [SOLVED] Crashing after 145 seconds

    Hello Everyone!

    I've just started programming PICs today. Started out with a PIC18F25K22, as I've used the chip when working with PICAXE28-X2s, and I wanted to start out with something similar to what I've already used.

    I can write working programs for the device in C (and compile using Microchip's C18 compiler and MPLAB X) and program it fine (using usbpicprog).

    However, when I start the PIC, after approx. 145 seconds, it resets itself. I've reduced the code down to a tiny main function, which still reproduces the problem for me:

    Code:
    #include <delays.h>
    #include <p18f25k22.h>
    
    void main()
    {
        
        TRISB = 0x00; //Set all Port B pins to outputs.
    
        PORTBbits.RB0 = 1; //Set B0 High.
        Delay10KTCYx(7); //Delay for 70k cycles.
        PORTBbits.RB0 = 0; //Set B0 Low.
    
        while(1)
            PORTB = 0; //Do something in this "infinite" loop.
        
        return;
    }
    When the code runs, the LED on B0 lights up briefly. After 145 seconds, the LED lights again - therefore the execution must have restarted. This happens with the earlier, more complex programs I tried - all of them did a lot more, but still reset after 145 seconds.

    I'm using a 3.3V regulator to provide a stable supply to the PIC. I have a 0.1uF ceramic capacitor between 3.3V and 0V to further stabilize the supply voltage.

    I've also tied !MCLR to the positive rail using a 4.7k resistor.

    I can't at all see why it's reseting itself, unless all PICs automatically do this for some strange reason, which I really doubt! I don't think it's random, as it happens after 145 seconds every time, and I've repeated this about 20 times...

    Any thoughts/tips/ideas would be appreciated!

    Thanks,
    Ben

    P.S. I haven't been able to test the program on another PIC yet, as I'm waiting for some more stable firmware for my programmer. Will do this and see if the problem is the same as soon as I can.
     
    Last edited: Jun 7, 2012
  2. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    Is the Watch Dog Timer enabled or disabled? If it's enabled, disable it and see if that resolves the problem...
     
  3. LordSputnik

    LordSputnik

    45
    0
    Aug 11, 2011
    I saw something about that somewhere... What exactly is the watchdog timer? Is there a C function in the C18 Library to disable it, do you know?

    I'll have a look around, thanks for such a quick reply!
     
  4. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    The watchdog timer will is a constantly counting loop (duration set by the prescaler) the chip counts from $00 to $FF if it gets past $FF it reboots the chip... So you need to constantly reset this timer throughout the program (before it overflows) or disable it...

    Most compilers litter the final assembly with resets so it never overflows, but...

    Anyway, you disable the WDT when you program the chip, some compilers and programmers will allow the fuses to be set in the hex (set by the compiler, read by the programmer) others compiler and/or programers will ignore the included fuse settings in the hex... Best to always verify your programmers configuration settings before you burn the chip...

    Edit Google '#pragma config' if your programmer supports included settings in the hex that is the way you implement them in C18
     
    Last edited: Jun 7, 2012
  5. LordSputnik

    LordSputnik

    45
    0
    Aug 11, 2011
    Ok, my Programmer allows me to set the configuration as far as I can see...

    Disabling the watchdog timer appears to fix the problem :) However, I noticed the WDTPS config setting was 1:32768...

    I believe the default clock frequency is 1MHz, and the CPU f is this value divided by 4 - 250kHz. So the period would be 4uS.

    The prescalar (or is it a postscalar? I don't even know the difference) then multiplies this period by 32768, which would give ~131ms.

    If I understand right, I then need to multiply this by 256 to get the reset time - which is 33.5 seconds.

    So, my question is, why was it taking 145s instead of 33.5s to reset? Have I done the calculation right?
     
  6. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    As I stated it's an overflow, it is likely being reset in the code just not often enough... Just a guess but possibly there is a collision during your one of your 'delays' that just happened to sync up to when it needed a reset and thus misses that reset...

    http://www.hobbyprojects.com/pic_tutorials/tutorial13.html
     
    Last edited: Jun 7, 2012
  7. LordSputnik

    LordSputnik

    45
    0
    Aug 11, 2011
    Thanks for your help! :)

    Do you have any other advice for working with PICs that would be good for a beginner to know?
     
  8. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    Learn the basics, this is a perfect example of something that many overlook once you understand it it's simple and just become 2nd nature... For probably 99% of my programming I have a default few dozen lines of code I start with (varies by chip), setting the fuses, setting some registers to what I normally use and even setting up a bank of a half dozen generic variables that I can use without going back and defining them as I write...
     
  9. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    One other thing I just thought of that most newbies miss, unless you need a reset you can disable the MCLR pin as well the same way and use it as a normal I/O pin, no need to tie it to VCC...
     
  10. BobK

    BobK

    7,609
    1,648
    Jan 5, 2010
    The watchdog timer does not run off the system clock, it runs of the 31KHz low frequency oscillator.

    And contrary to what CocaCola said, the C compiler does not insert watchdog timer resets. And if it did, it would render the watchdog timer useless.

    Bob
     
  11. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    I never said that every compiler did, nor did I say that the C18 one did as I have never used it... I did say most compilers do any maybe I should have said some... I primarily use PicBasic pro and it does in fact litter the code with watchdog timer resets unless you specifically tell it not to... It automatically includes clears in common instructions that would generally time out like a PAUSE instruction, and I suspect most compilers do the same or else commands like that couldn't be used without resets...
     

    Attached Files:

    • wdt.jpg
      wdt.jpg
      File size:
      90.1 KB
      Views:
      155
    Last edited: Jun 7, 2012
  12. BobK

    BobK

    7,609
    1,648
    Jan 5, 2010
    Most compilers don't have instructions that would time out. I can see someone putting a clear in a delay function, but that would not be the compiler doing it. I don't use basic, but I suspect this PAUSE instruction is a delay, which would be a function call in C.

    Bob
     
  13. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    But they can very well have built in macros that can...

    delay_ms(50000); //or equivalent will indeed time out a WDT unless it has build it clears, and some compilers do put them in the built in macros...

    That depends on the compiler, again a blanket statement like that simply isn't true, I included a copy and paste of the PicBasic Pro user manual above and it clear states...

    "By default, PBP automatically clears and restarts the watchdog timer periodically so that the watchdog reset never happens."

    And if your compiler includeds a delay_ms() function that happens to have the clears in it, then again it's all done behind the scenes by the compiler...

    This really has to be taken on a case by case basis and the programmer needs to know their compiler...
     
  14. BobK

    BobK

    7,609
    1,648
    Jan 5, 2010
    This is a matter of semantics. Since I have been working on compilers professionally for over 40 years, I may use a different language than you do. To me, the compiler is only that piece of code that translates source code to object code. It does not include the library functions that might be provided in a compiler package. It would be wrong for a C compiler to insert code to clear the WDT, and I have not seen a C compiler that does this.

    Bob
     
  15. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    As primarily do my micro stuff in BASIC, I know PicBasic Pro and Proton Basic both reset the timer automatically... In Proton you have to use DECLARE WATCHDOG=ON in the code for it to do it automatically... I don't have much experience with C, but I do have some experience with CodevisionAVR (a C compiler for AVR) and know it automatically inserts them as well into the delay_ms() command...

    I believe the best advice it to simply not assume anything and do a little snooping in the asm output of the compiler if unsure, basically know your tools...

    Also there are also the pros and cons of even using the watchdog, in many applications (most of the designs I have done) an automatic unexpected reset can be just as annoying or troublesome as it getting stuck in an endless loop, so I rarely enable it... There are of course other instances when it's beneficial to have on...
     
  16. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    But why the need to reset the timer when disabling it seems the more logical approach?
     
  17. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    Not, using it is what a lot of people do, as it's a whole lot easier to not deal with it in many instance and basically the watchdog is the last chance fail safe that isn't necessary in all instance... If the chip glitches or gets stuck in a loop for whatever reason it will automatically reboot if enabled, if not it will simply sit there in the dead state until the user reboots it...

    In some cases like say for example an unmanned remote weather station that is relaying a stream of data, the watchdog in such an application might be useful... Lets say it has a less than stellar power source and said power source hiccups and causes the micro to freeze up... With the watchdog timer active the micro will automatically reboot itself in a few ms and be right back to work... On the flip side if it wasn't enabled someone would have to manually go there and reboot it...

    But, in other cases like say if you had a micro based watch that played a similar 'ta-da' sound (like Windows) upon every start up, can you imagine how annoying it might be if it was unexpectedly rebooting while you were at the library vs just locking up ;)

    And one the last note, we all have come to 'expect' a micro to just do what it's supposed to and most of the time they do, but the reality is that sometimes they just get lost and for whatever unknown reason stop responding, thus the reason the watchdog was implemented...
     
    Last edited: Jun 10, 2012
  18. jackorocko

    jackorocko

    1,284
    1
    Apr 4, 2010
    I know what it does. But as bob says, if it is something you need then you should be capable of coding it yourself. I have always used C, so the basic nature that something needs to be implied and included is ass backwards to me.
     
  19. gorgon

    gorgon

    603
    23
    Jun 6, 2011
    The whole idea with the watchdog system is to let it time out if your code is taking a stroll in the park, and you're not aware of it.
    You'll only reset it at certain points in your program, and never inside interrupt service routines.
    If you need to take big delays in your code, a more structured timer system should be implemented.
    If you run functions/routines that you know may take more time than the watchdog time-out delay, you should divide it up in chuncks. It's always bad practice to reset watchdogs inside functions. The same goes for automatic watchdog resets. I must confess that this is the first time I've ever heard of 'automatic watchdog control' after 35+ years of programming different microcontroller brands.

    If you run a BASIC based system, I can understand it since this is normally an interpreted code running on top of a functional kernel. The kernel necessarily will have need for a watchdog control, and you can't really compare this with a barebone C or assembler program.

    TOK ;)
     
    Last edited: Jun 10, 2012
  20. CocaCola

    CocaCola

    3,635
    4
    Apr 7, 2012
    Maybe on the BASIC stamp, most other BASIC compilers are just that compilers not interpreters they are just like a C compiler that compile to native assembly/hex they are not run on top of anything they produce native micro code... So to say you can't compare it to a C or assembler program is false...
     
    Last edited: Jun 10, 2012
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-