Positron Compiler Documentation

Sleep

Source: Positron8 Compiler User Manual, PDF page 356

Syntax

Sleep { Length }

Overview

Places the microcontroller into low power mode for approx n seconds. i.e. power down but leaves the port pins in their previous states.

Parameters

Length is an optional variable or constant (1-65535) that specifies the duration of sleep in seconds. If length is omitted, then the Sleep command is assumed to be the assembler mnemonic, which means the microcontroller will sleep continuously, or until the Watchdog timer wakes it up.

Example

Symbol LED = PORTA.0
Do
  High LED
                    ' Turn LED on.
  DelayMs 1000
                    ' Wait 1 second.
  Low LED
                    ' Turn LED off.
  Sleep 60
                    ' Sleep for 1 minute.
Loop

Notes

Sleep will place the device into a low power mode for the specified period of seconds. Period is 16 bits, so delays of up to 65,535 seconds are the limit (a little over 18 hours) Sleep uses the Watchdog Timer so it is independent of the oscillator frequency. The smallest units is about 2.3 seconds and may vary depending on specific environmental conditions and the device used.

The Sleep command is used to put the microcontroller in a low power mode without resetting the registers. Allowing continual program execution upon waking up from the Sleep period.

Waking a 14-bit core device from Sleep

All the PICmicro™ range have the ability to be placed into a low power mode, consuming micro Amps of current.

The command for doing this is Sleep. The compiler's Sleep command or the assembler's Sleep instruction may be used. The compiler's Sleep command differs somewhat to the assembler's in that the compiler's version will place the device into low power mode for approx n seconds (where n is a value from 0 to 65535). The assembler's version still places the device into low power mode, however, it does this forever, or until an internal or external source wakes it. This same source also wakes the device when using the compiler's command.

Many things can wake the device from its sleep, the WatchDog Timer is the main cause and is what the compiler's Sleep command uses.

Another method of waking the PICmicro™ is an external one, a change on one of the port pins. We will examine more closely the use of an external source. There are several ways of waking the microcontroller using an external source. One is a change on bits 4..7 of PORTB. Another is a change on bit-0 of PORTB. We shall first look at the wake up on change of PORTB,bits-4..7.

As its name suggests, any change on these pins either high to low or low to high will wake the device. However, to setup this mode of operation several bits within registers INTCON and OPTION_REG need to be manipulated. One of the first things required is to enable the weak PORTB pull-up resistors. This is accomplished by clearing the RBPU bit of OPTION_REG (OPTION_REG.7). If this was not done, then the pins would be floating and random input states would occur waking the microcontroller up prematurely. Although technically we are enabling a form of interrupt, we are not interested in actually running an interrupt handler. Therefore, we must make sure that Global interrupts are disabled, or the device will jump to an interrupt handler every time a change occurs on PORTB. This is done by clearing the GIE bit of INTCON (INTCON.7).

The interrupt we are concerned with is the RB port change type. This is enabled by setting the RBIE bit of the INTCON register (INTCON.3). All this will do is set a flag whenever a change occurs (and of course wake up the PICmicro™). The flag in question is RBIF, which is bit-0 of the INTCON register. For now we are not particularly interested in this flag, however, if global interrupts were enabled, this flag could be examined to see if it was the cause of the interrupt. The RBIF flag is not cleared by hardware so before entering Sleep it should be cleared. It must also be cleared before an interrupt handler is exited.

The Sleep command itself is then used. Upon a change of PORTB, bits 4..7 the device will wake up and perform the next instruction (or command) after the Sleep command was issued. A second external source for waking the device is a pulse applied to PORTB.0. This interrupt is triggered by the edge of the pulse, high to low or low to high. The INTEDG bit of OPTION_REG (OPTION_REG.6) determines what type of pulse will trigger the interrupt. If it is set, then a low to high pulse will trigger it, and if it is cleared then a high to low pulse will trigger it.

To allow the PORTB.0 interrupt to wake the PICmicro™ the INTE bit must be set, this is bit-4 of the INTCON register. This will allow the flag INTF (INTCON.1) to be set when a pulse with the right edge is sensed. This flag is only of any importance when determining what caused the interrupt. However, it is not cleared by hardware and should be cleared before the Sleep command is used (or the interrupt handler is exited). The program below will wake the microcontroller when a change occurs on PORTB, bits 4-7.

  Symbol LED = PORTB.0
                                ' Assign the LED's pin
Main:
  INTCONbits_GIE = 0
                                ' Turn Off global interrupts
  Input PORTB.4
                                ' Make PORTB.4 an Input
  OPTION_REGbits_RBPU = 0
                                ' Enable PORTB Pull-up Resistors
  INTCONbits_RBIE = 1
                                ' Enable PORTB[4..7] interrupt
  Do
     DelayMs 100
     Low LED
                                ' Turn off the LED
     INTCONbits_RBIF = 0
                                ' Clear the PORTB[4..7] interrupt flag
     Sleep
                                ' Put the microcontroller to sleep
     DelayMs 100
                                ' When it wakes up, delay for 100ms
     High LED
                                ' Then light the LED
  Loop
                                ' Do it forever