How to Program for Sleep and Walk Up PIC16F877A Using Watchdog Timer
The following code demonstrate how to put controller in sleep mode and wake up by using watchdog timer. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.
According to datasheet, watchdog timer overflow time is approximate 18 mSec (prescalar 1:1) to 2.304 Sec (prescalar 1:128).
Code in mikroC
/*
// watchdog overflow time calculation
// according to device datasheet
// at no prescalar (1:1), the overflow
// time is following.
// min typ max
// 7 18 33 (ms)
// WDT Overflow = Tcons * prescalar
// = 18(ms) * 128
// = 2.304 sec
*/
sbit LED_WDT_Dir at TRISC.B0;
sbit LED_WDT at PORTC.B0;
void WDTEnable(void);
void main(void)
{
LED_WDT_Dir = 0;
LED_WDT = 1;
Delay_ms(500);
WDTEnable();
while(1)
{
LED_WDT = 0;
asm sleep; // awake when WDT overflow approx. 2.3 Sec
LED_WDT = 1;
asm sleep; // awake when WDT overflow approx. 2.3 Sec
}
}
void WDTEnable(void)
{
OPTION_REG.PS0 = 1; //—————
OPTION_REG.PS1 = 1; // prescalar 128
OPTION_REG.PS2 = 1; //—————
OPTION_REG.PSA = 1; // Prescaler is assigned to the WDT
asm CLRWDT; // clear WDT
}
// watchdog overflow time calculation
// according to device datasheet
// at no prescalar (1:1), the overflow
// time is following.
// min typ max
// 7 18 33 (ms)
// WDT Overflow = Tcons * prescalar
// = 18(ms) * 128
// = 2.304 sec
*/
sbit LED_WDT_Dir at TRISC.B0;
sbit LED_WDT at PORTC.B0;
void WDTEnable(void);
void main(void)
{
LED_WDT_Dir = 0;
LED_WDT = 1;
Delay_ms(500);
WDTEnable();
while(1)
{
LED_WDT = 0;
asm sleep; // awake when WDT overflow approx. 2.3 Sec
LED_WDT = 1;
asm sleep; // awake when WDT overflow approx. 2.3 Sec
}
}
void WDTEnable(void)
{
OPTION_REG.PS0 = 1; //—————
OPTION_REG.PS1 = 1; // prescalar 128
OPTION_REG.PS2 = 1; //—————
OPTION_REG.PSA = 1; // Prescaler is assigned to the WDT
asm CLRWDT; // clear WDT
}
Download Files
For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.
Comments
Post a Comment