Monday, October 31, 2011

Sleep and Wake PIC Microcontrollers

Discription :
PIC microcontrollers SLEEP feature is an extremely useful mechanism to minimize power consumption in battery-powered applications.In sleep mode,the normal operation  of a pic microcontroller is suspended and clock oscillator is switched off.The power consumption is lowest in this state.The device can be woken up by an external reset,a watch-dog timer reset,an interrupt on INT0 pin,or port-on-change interrupt.In this experiment,we will discuss how to put PIC microcontroller into sleep mode and compare the current consumption during sleep mode and the normal operation mode .

Theory :
In sleep  (power-down) mode,a PIC microcontroller is placed in it's lowest current consumption state.The device goes into sleep mode by executing a SLEEP instruction.The device oscillator is turned off,so no system clocks are occurring in the device.However,the I/O ports maintain the status they had before the SLEEP instruction was executed.Therefore,in order to minimize the power consumption in sleep mode,the output ports must not be sourcing or sinking the current before going into sleep mode.Besides,all the unused I/O pins should be configured as inputs and pulled either high (Vdd)  or low (Vss).
Several events can make the device wake-up from the sleep mode :

  1. Any device reset.
  2. Watchdog timer wake-up (if WDT was enabled).
  3. Any peripheral module which can set its interrupt flag while in sleep, such as :
  • External INT pin
  • Change on port pin
  • Comparators
  • A/D conversion
  • Timer1
  • LCD
  • SSP
  • Capture;etc.
The first event (device reset) will reset the device upon wake-up. However the latter two events will wake the device and then resume program execution.When the SLEEP instruction is being executed,the next instruction (PC+1) is pre-fetched,so that on wake-up the processor could execute the next instruction after the SLEEP command.For the device to wake-up through an interrupt event,the corresponding interrupt enable bit must be set (enabled).Wake-up is regardless of the state of the GIE bit.If the GIE bit is clear (disabled),the device will just wake up from sleep and continues executing the program from the instruction right after the SLEEP command.If  the GIE  bit is (enabled),the processor will execute the instruction after the SLEEP instruction and then branches to the interrupt address (0004h).Therefore,if an interrupt is to be used just to wake up the PIC microcontroller,the GIE bit must be cleared before the sleep instruction.


Watchdog timer (WDT) :
The watchdog timer or WDT is an independent timer with its own clock source.It provides a way for the PIC processor to recover from a software error that obstruct from program continuation,such as an endless loop.It is a free-running timer which,if allowed to overflow, will automatically reset the PIC .The WDT is enabled/disabled by a device configuration bit,WDTE.If it is enabled,software execution may notdisable this function.To prevent a time-out condition the watchdog must be rest periodically via software.In 16f628A the watchdog timer has a nominal time-out period of 18ms,which can be extended up to 2.3s by using a prescaler with a division ratio of up to 1:128.The prescaler rate is selected through PS0-PS2 bits of the OPTION register.Note that the PSA bit must also be set to assign the prescaler to WDT,otherwise it will be used by timer0.

In normal operation,if the watchdog timer is enabled,a WDT reset instruction (CLRWDT° is placed in the main loop of the program,where it would normally be exepected to be executed often enough to prevent the WDT overflow.If the program hangs,and the CLRWDT instruction is not executed in time,the program counter is reset to 000 so that the program restarts.
However ,if the PIC microcontroller is in sleep mode, a WDT time-out will not reset the device ,but just causes it to wake up (know as WDT wake-up) and the microcontroller continues program execution from the instruction following the sleep instruction.
Note : When a sleep instruction is executed,the watchdog timer is cleard.But the WDT will keep running if it has been enabled .
Sleep mode is extremely useful in battery-powered data-loggers where the measurement samples are to be taken with some sampling interval.Between successive data samples,the microcontroller can be put into Sleep mode to prolong the battery life .
Experimental setup :
The setup for this experiment would be as shown in the circuit diagram below.The pic 16f628A runs at 4.MHz clock using an external crystal.An LED is connected to RB0 port pin,which glows for 5sec after every 4.3 second delay .During the first half of the delay (2.3 sec),the microcontroller  is put to slepp mode and WDT time-out wakes it up.The remaining 2 sec delay is created through a software routine using NOP instruction. An ammeter is connected in series between the power supply voltage and the microcontroller circuit to monitor the current consumption of the circuit.The MCLR pin is pulled high through a 10k resistor.
Software :
The following program is written in c and compiled with mikroC for pic. The OPTION register is configured to assign prescaler to WDT .The prescaler ratio of 1:128 creates the WDT time-out duration of approximately 2.3sec.An additional software delay of 2 sec is created using the delay_ms() library routine.The amount of current consumption during both the delay intervals is displayed on the digital meter.

/* Project name:
     Understanding sleep mode in PIC microcontrollers
 * Copyright:
     charaf zouhair
 * Test configuration:
     MCU:             PIC16F628A
     Oscillator:      XT, 4.0000 MHz
*/

sbit LED at RB0_bit;       // LED is connected to PORTB.0

void main() {
TRISB = 0b00000000;
TRISA = 0b00100000;
LED = 0;
OPTION_REG = 0b00001111;   // Assign 1:128 prescaler to WDT
do {
  asm sleep;               // Sleep mode, WDT is cleared,
                           // and time out occurs at Approx. 2.3 Sec
                           // Current is minimum in Sleep mode
  LED = 1;                 // After wake-up the LED is turned on
  Delay_ms(500);
  LED = 0;
  asm CLRWDT;              // Clear WDT
  Delay_ms(2000);          // Measure current here and compare with Sleep mode
 }while(1);                // current
}

The watchdog timer should be enabled in the configuration registers.In mikroC ,this can be done through the edit project window 
Output :
The current consumption during software delay routine (led is off) was found as high as 940 µA,whereas the sleep mode current was only 43 µA.This is a significant reduction in pic power consumption.


Summary :
PIC microcontrollers Sleep mode is a low current mode where the power consumption is minimum It is extermely useful in battery-powered applications to prolong the battery life.If you are making a battery-powered data logger,it would be a smart move to put the microcontroller into sleep mode during the time when it is not sampling the data value,and it can be waken-up by WDT time-out.In 16f628A ,the use od sleep mode WDT wake-up provides a maximum sleep duration of 2.3 sec .But if your data logger design requires a longer sampling interval between two successive data samples and you want to put the microcontroller into sleep mode during most of this interval,you can use multiple SLEEP instructions in sequence. When the microcontroller wakes up from the first sleep by WDT time-out; it resumes normal operation and executes the next instruction. If it finds another SLEEP instruction, it will reset the WDT and go back to sleep mode again,thus prolonging the sleep duration.The test circuit used in this experiment illustrated that the pic current consumption is significantly lower (more than 20 times less )in sleep mode than in normal execution mode .

for more information contacte me : charaf_zohair@hotmail.com

No comments: