Embedded Systems Design
In this workbook we will accurately time the interval between two input events present at the microcontroller, investigate the sleep mode of the microcontroller and look at other techniques to reduce power consumption.
Data sheet for the Atmel ATMEGA168P used in these exercises. You will need to refer to this frequently. Within these workbooks this will be referred to as 'the datasheet'. The section numbers referred to in these workbooks refer to revision 8161C of the datasheet dated 05/09.
Documentation for the libraries available on the PCs
An on-line version of this guide is available at:
This exercise builds upon the work undertaken in Workbook3, where you connected an LCD to the microcontroller. The objective in this task is to accurately time the interval between two switch change events. In this exercise the switches used are reed switches, which close in the presence of a magnetic field. The microcontroller can be programmed to generate interrupts when inputs change from low to high, high to low, or at either transition. Using two such interrupts and a timer adjusted to count in multiples of 1us it is possible to make an accurate event interval timer, and display the result on the LCD. To get timer ticks at exacly 1us intervals, the device will be run from an 8MHz master clock, and using a timer prescale value P of 8 (note just the timer presale, not the master clock divide which is set in the fuses). The 16-bit timer takes a maximum time of 65ms before it overflows. However we will use a count match interrupt to limit the counter to a maximum period of 10ms, and by counting these interrupts using a 16-bit word this will allow timing of intervals up to approximately 650 seconds to be performed.
There are alternative ways of achieving this result. Any IO pins can be used to create pin change interrupts, and also there is an Input Capture unit (see section 15.6 of the datasheet) which is specifically designed for functions like this.
In this exercise we will see how we can reduce the current consumption of the microcontroller utilising the sleep modes.
Getting the microcontroller to sleep is one half of the problem. Getting it to wake again can be a source of frustration unless care is taken when selecting the sleep mode and the wake-up event. You must carefully consider which events will be used to trigger a wakeup, and make sure that the clocks required to cause those triggers continue to run in the chosen sleep mode.
Refer to section 9 of the datasheet for details.
This exercise will use the Watchdog timer as the wake-up source. It allows wake up from all sleep modes. The watchdog timer's normal purpose is to take control of a device which is not running correctly. In normal use the watchdog receives a 'kick' regularly. Each kick clears a counter which counts up in response to a clock source. If the counter overflows, the watchdog normally issues a reset command to re-initialise the device. An alternative is to issue an interrupt. This exercise will deliberately allow the watchdog timer to overflow and use the resulting interrupt to wake from sleep mode.
To minimise the chances that runaway code will make changes to the watchdog, any change to the watchdog must use a timed sequence. This is described in section 10.8.2
Apart from a reduction in power consumption, this should behave exactly as per worksheet1 exercise2.
Remember that as the clock source has changed, you must issue the
make fuses command followed by the
make program command.
To reduce power consumption still further, it is possible to disable parts of the microcontroller which are not in use.