CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

How to Wake-Up with WDT

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 191

View user's profile Send private message

How to Wake-Up with WDT
PostPosted: Tue Jun 14, 2022 10:59 am     Reply with quote

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: 21658

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 11:25 am     Reply with quote

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: 191

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 11:52 am     Reply with quote

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: 325
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 12:15 pm     Reply with quote

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: 21658

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 12:17 pm     Reply with quote

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: 325
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 12:30 pm     Reply with quote

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: 191

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 5:11 pm     Reply with quote

Smile Thanks everyone, i got what i needed
PrinceNai



Joined: 31 Oct 2016
Posts: 325
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Jun 14, 2022 7:25 pm     Reply with quote

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 Very Happy

Regards,
Samo
pilar



Joined: 30 Jan 2008
Posts: 191

View user's profile Send private message

PostPosted: Wed Jun 22, 2022 3:30 pm     Reply with quote

Thank you PrinceNai, your code worked for me Very Happy

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;
         }
      }
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group