Introduction
PIC18F4550 has a power-down mode to reduce power consumption when the microcontroller is idle. Most of the applications like mobile phones, laptops rely on sleep mode which suspends most or all microcontroller operations to minimize power consumption. Sleep mode is necessary for such applications because these are battery-operated devices which help to consume less energy
In PIC18F4550 sleep mode, the device oscillator is turned off.
So no system clocks are occurring in the device. This keeps the PIC in a low power consumption state.
Enter PIC18F4550 in Sleep mode
To enter the PIC18F4550 in sleep mode,
- clear the IDLEN bit in the OSCCON register.
Bit IDLEN:
0 = enable sleep mode
1 = enable idle mode
2. And Execute the SLEEP instruction.
This halts all the clocks and oscillator as shown below
If WDT (Watchdog Timer) is enabled then INTRC Source having a frequency of 31 kHz is enabled.
If the Timer1 oscillator is enabled it will also continue to run.
Wake-up PIC18F4550 from Sleep Mode
PIC18F4550 can wake up from sleep mode by events like
- Interrupt
- Reset
- WDT time out
Interrupts are any interrupt sources like external interrupt, timer interrupt, usart interrupt, ADC interrupt, etc can cause the PIC microcontroller to exit from sleep mode.
IDLEN bit is not affected by the wake-up which means it will remain as 0.
Example
Let’s develop a simple application in which we force the PIC18F4550 to enter into sleep mode or exit from sleep mode by using an external interrupt.
Interfacing Diagram
- When the PIC microcontroller is in normal mode it will start blinking the LED and by giving external interrupt it will go into sleep mode and the LED will stop blinking.
- When the PIC microcontroller is in sleep mode, after providing external interrupt it will wake up and start blinking the LED again.
Program
/*
Enter PIC18F4550 in sleep and wake-up through external interrupt
www.electronicwings.com
*/
#include <pic18f4550.h>
#include "osc_config.h"
#define LED LATD
void MSdelay(unsigned int);
int flag;
void main()
{
TRISD=0;
TRISBbits.RB0=1; /* Set PORTB.0 as input*/
OSCCON=0x72; /* Internal osc. freq. 8MHz with IDLEN=0,
enabling IDLEN enables sleep mode */
INTCON2=0x00; /* INT0 negative edge interrupt selected */
INTCONbits.INT0IF=0; /* Clear INT0 interrupt flag */
INTCONbits.INT0IE=1; /* Enable INTOIF interrupt */
INTCONbits.GIE=1; /* Enable global interrupt */
flag = 0;
LED = 0; /* Turn OFF led initially */
while(!flag)
{
LED = ~LED; /* Blink LED continuously */
MSdelay(300);
}
SLEEP(); /* Enter in sleep mode */
}
void interrupt ISR()
{
flag = ~flag;
INTCONbits.INT0IF=0;
}
void MSdelay(unsigned int val)
{
unsigned int i,j;
for(i=0;i<val;i++)
for(j=0;j<165;j++);
}
Video
Components Used |
||
---|---|---|
PICKit 4 MPLAB PICKit 4 MPLAB |
X 1 | |
PIC18f4550 PIC18f4550 |
X 1 | |
Breadboard Breadboard |
X 1 | |
LED 5mm LED 5mm |
X 1 |