View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
How to Wake-Up with WDT |
Posted: Tue Jun 14, 2022 10:59 am |
|
|
I'm using a PIC18LF4520 and I'm using it with CCS 4.074, I want to use a low power state and for that I send it to sleep and I want to wake it up with the WDT but it seems that it's not working, the pic doesn't wake up, can someone tell me how to solve this ?
Code: | #include <18F4520.h>
#fuses HS,WDT128,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
main() {
switch ( restart_cause() ) {
case WDT_TIMEOUT: {
printf("\r\nRestarted processor because of watchdog timeout!\r\n");
break;
}
case NORMAL_POWER_UP: {
printf("\r\nNormal power up!\r\n");
break;
}
}
setup_wdt(WDT_ON);
restart_wdt();
SLEEP();
while(TRUE) {
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 14, 2022 11:25 am |
|
|
If the 18F4520 is sleeping, a WDT timeout will cause the PIC to wake up
from sleep and continue running code after that point. If the PIC is sleeping,
the WDT will not cause a reset.
This is in the PIC datasheet, in section:
Quote: | 3.5.2 EXIT BY WDT TIME-OUT |
|
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jun 14, 2022 11:52 am |
|
|
Quote: | 3.5.2 EXIT BY WDT TIME-OUT |
Yes, I read it, but it is not clear to me, what would I have to modify in this code to get what I need, that the pic goes to sleep and that the WDT wakes it up? |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Tue Jun 14, 2022 12:15 pm |
|
|
Maybe this will do what you want:
Code: |
#include <18F4520.h>
#fuses HS,WDT128,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
main() {
switch ( restart_cause() ) {
case RESET_INSTRUCTION: {
printf("\r\nRestarted processor because of watchdog timeout!\r\n");
break;
}
case NORMAL_POWER_UP: {
printf("\r\nNormal power up!\r\n");
break;
}
}
setup_wdt(WDT_ON);
restart_wdt();
SLEEP();
reset_cpu();
while(TRUE) {
}
}
|
Mind that the code as it is will never reach while(true) loop, so it will basically just keep resetting the CPU. I suppose you want it to do something when the processor wakes up :-) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 14, 2022 12:17 pm |
|
|
Try something like this:
Code: | #include <18F4520.h>
#fuses HS,WDT512,NOPROTECT,NOLVP, PUT
#use delay(clock=20M)
#use rs232(baud=9600, UART1, ERRORS)
//================================
void main()
{
printf("Start\n\r");
restart_wdt();
sleep();
delay_cycles(1);
printf("Wake-up from sleep occurred\n\r");
while(TRUE)
{
restart_wdt();
}
} |
|
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Tue Jun 14, 2022 12:30 pm |
|
|
Or if you want something done after wake-up, something like this:
Code: |
#fuses HS,WDT128,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Working 1
#define Sleeping 2
int8 Go_To_Sleep = Working;
main() {
setup_wdt(WDT_ON);
restart_wdt();
while(true){
switch (Go_To_Sleep) {
case Sleeping: {
printf("\r\nSleeping!\r\n");
sleep();
Go_To_Sleep = Working;
break;
}
case Working: {
printf("\r\n!Working!\r\n");
Go_To_Sleep = Sleeping;
break;
}
}
}
}
|
Restart WDT as needed..
Last edited by PrinceNai on Tue Jun 14, 2022 7:52 pm; edited 1 time in total |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jun 14, 2022 5:11 pm |
|
|
Thanks everyone, i got what i needed |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Tue Jun 14, 2022 7:25 pm |
|
|
Nice to hear. For the posterity it would be cool if you posted which answer helped you. So that someone with a similar question can find a solution
Regards,
Samo |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Wed Jun 22, 2022 3:30 pm |
|
|
Thank you PrinceNai, your code worked for me
Code: | #fuses HS,WDT128,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define Working 1
#define Sleeping 2
int8 Go_To_Sleep = Working;
main() {
setup_wdt(WDT_ON);
restart_wdt();
while(true){
switch (Go_To_Sleep) {
case Sleeping: {
printf("\r\nSleeping!\r\n");
sleep();
Go_To_Sleep = Working;
break;
}
case Working: {
printf("\r\n!Working!\r\n");
Go_To_Sleep = Sleeping;
break;
}
}
}
} |
|
|
|
|