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).
Picture 2017-11-22 21_05_04
wdt config
wdt config 1
Picture 2017-11-23 21_04_37

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
}

Download Files

For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.

Comments

Popular posts from this blog

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program Interrupts in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)