Hi! I have a project with a standalone atmega329p. For some reason after a few hours, it get stuck and due the watchdog it restart itself, but then get stuck in Setup() function, restarting again again and again due watchdog. The point is that if I disconnect the board and connect it again, it works perfectly again for hours.
What is the difference between a restart and a power loss? Because with one it is still stuck and with the another one the problem is solved. I need to know what is the difference to find the problem source.
The MCU Status Register (MCUSR) provides information on MCU reset cause.
Bit 0 – PORF: Power-on Reset Flag
Bit 3 – WDRF: Watchdog Reset Flag
See the datasheet for details.
Thanks, but what I'm looking for is not the cause of the restart (i know it, it's due the watchdog). What I wanna know is the difference between it restart by watchdog or by power loss, because the behaviour is different, when is by watchdog I must keep some variables, memory or whatever that even restarting like that doesn't run again the program, and when I make a restart cutting of the board, then the program runs perfectly for hours.
It is probably something in your app. Start of MCU should be the same. Truly, I do not studied 329P in details but e.g. 328P there is no difference between any reset except of this register, maybe in timing. You should read the section about reset in the datasheet for details and maybe some tests. Simplify your app as possible. Could you provide the code?
Are you using a bootloader?
After a WDT reset, it resets with the WDT enabled with minimum timeout, you need to clear the WDRF flag and reconfigure the WDT immediately in setup to handle this case. This is described in the datasheet, its intended behavior. Ypu dont see this on the uno/mega or new bootloader nano because the bootloader does this for you (on old bootloader nano and pro mini bootloader, it doesnt, and these have the same problem)