Embedded Systems Design

## Low Power and Embedded Systems - Workbook 4

### Introduction

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.

### Supporting material

atmega644p.pdf

Data sheet for the Atmel ATMEGA644P 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 8011Q of the datasheet dated 02/13.

hall_effect.pdf

Data sheet for the A1104 hall effect switch.

avr-libc documentation

Documentation for the libraries available

An on-line version of this guide is available at:

### Exercise 1 - Measuring the interval between events

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 hall effect switches, which change state in response to a change in 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 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 13.6 of the datasheet) which is specifically designed for functions like this.

### Exercise 2 - sleep

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 7 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 to resetting the microcontroller 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 specific sequence of instructions, and these must be completed within a maximum number of clock cycles. This is described in section 8.3.2 of the datasheet.

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.

Copyright Ian Wassell and Brian Jones 2009-2013