Bascom and AVR, Sleep and Wake-up, Watchdog


Sleep and Powerdown are typically used in battery powered applications when your controller has nothing to do and you want to save power.
The AT90S2313 can be made to sleep in two ways:
- The statement Idle stops the controller clock, but the UART, external interrupts and timer/counter interrupts keep functioning. All these interrupts can wake-up the controller.
- Powerdown stops the controller clock, but the external interrupts keep fuctioning and can wake-up the controller.

An example of Powerdown and wake-up on external interrupt:
powerdown.bas
$regfile = "2313def.dat"
$crystal = 4000000

Config Pind.6 = Output
Config Int0 = Low Level

On Int0 Button:
Enable Interrupts
Enable Int0

Do
  Set Portd.6
  Wait 1
  Reset Portd.6
  Cls
  Lcd "power-idle..."
  Lowerline
  Lcd "pushbtn to wake"
  Powerdown
Loop

Button:
  Cls
  Lcd "awake!"
  Wait 1
Return

End
After flashing the Led and writing a message to the lcd, Powerdown is used to stop the controller clock. Pressing the button will wake-up the controller.

Watchdog
Bascom can provide a watchdog function. A watchdog is a trick to reset your controller if the program somehow gets stuck or 'hangs'. A watchdog is a timer that will have to be reset every now and then. If the program gets stuck, it will not be able to reset the watchdog and when the watchdog times out, it will reset the controller. This is often used in critical applications where you have to be sure that the controller will restart whatever happens. A decent program must deal with a watchdog time-out in a sensible way. A watchdog time-out is a very serious situation, maybe caused by a hardware or software design error. In any case, when watchdogs are used, always include some sort of reporting mechanism, or else you will never know about these possible errors.
A watchdog has to be configured for a specific waiting time:
Config Watchdog 16|32|64|128|256|512|1024|2048
ranging from 16 to 2048 milliseconds.
The watchdog has to be reset before it times out:
Reset Watchdog
to prevent a controller reset.
The watchdog can be started and stopped with a
Start Watchdog
Stop Watchdog
but using the Stop Watchdog seems to me to be defeating the entire purpose of watchdogs.
The watchdog on the AT90S2313 uses a seperate 1Mhz on-chip oscillator. This oscillator is not very precise, so actual watchdog timings can vary a little.
An example of a forced watchdog resetting the controller:
watchdog.bas
$regfile = "2313def.dat"
$crystal = 4000000

Config Pind.6 = Output
Config Watchdog = 2048

Do
  Cls
  Set Portd.6
  Wait 2
  Reset Portd.6
  Lcd "wd running."
  Lowerline
  Lcd "wait 2 secs..."
  Start Watchdog
  Idle
Loop

End
After the Start Watchdog, the controller is put in Idle, stopping the controller clock. As the watchdog runs on an independant clock, in can and will reset the controller after app. two seconds.

TOC