How to extend the battery life of your microcontroller based design - Embedded.com

How to extend the battery life of your microcontroller based design

Whether it is an electric toothbrush, shaver, cell phone, PDA, MP3player or the remote control for anything not within arm's reach,battery-powered devices are an everyday part of life. As such, powermanagement is a significant issue for embedded designers today.

The ubiquitous microcontroller offers designers a multitude of waysto manage the power requirements in many applications. The differentMCUs themselves offer a range of current consumption and a variety ofpower saving features. However, power management in an MCU-based designis more than just selecting the right microcontroller.

It also means coming up with a development strategy that mosteffectively uses these features. And at the system level, independentof the MCU that you have chosen, there are also a number of strategiesthat can be employed to further extend the battery life of yourapplication.

Example Application: WirelessBicycle Computer
To demonstrate effective power management, we will use a wirelessbicycle computer as an example. This computer is composed of threemodules, a control panel on the handle bars, a speed sensor on a wheeland a display on the rider's helmet.

The speed sensor tells the control panel how fast it is turning. Thecontrol panel calculates information such as speed, distance covered,elapsed time and calories burned. The control panel sends this to thedisplay. Figure 1, below, showsa block diagram of a bike computer control panel.

Figure1. Block diagram for a wireless bicycle computer control panel,demonstrating the enhanced power management features on today's MCUs.

Increased number of low power modes.
MCU's are moving into smaller geometries to reduce die size, whichresults in transistors that cannot tolerate direct application of 3 ormore volts. So, voltage regulators are used to drop the voltage to theinternal logic.

Unfortunately, these regulators add to the MCU's current draw.However, since power is equal to voltage times current, a 1.8 volt to 3volt system with a regulator may still be lower power than a 5 voltsystem without one.

MCU's rely heavily on power management modes to keep the overalloperating current down while still supporting regulated power suppliesand increased clock speeds. New MCU's are providing multiple low powermodes to address these needs and yet still maintain system flexibility.Freescale's MC9S08GB60 MCU has four low power modes: stop1, stop2,stop3 and wait.

In wait mode, power is reduced by turning off the CPU clock, butleaving the clocks enabled to other MCU peripherals such as A-to-Dconverters, timers or serial communication modules. This mode is usefulfor saving power when these peripherals need to function, but the CPUhas nothing to do until the peripheral completes its task.

In our example, wait mode could be used while the SPI is used tocommunicate with the RF transceiver.

To reduce power consumption further, three stop modes can be used. Stop1, stop2, and stop3 eachprovide different levels of operation that will reduce powerconsumption.

Stop3 provides the most functionality of the three. In stop3, the on-chipvoltage regulator is put into a power saving mode that still providesminimum regulation for retaining RAM and I/O register contents. Severalinterrupt sources and the reset pin can wake the MCU up from stop3.Stop3 is the only stop mode in which the low voltage inhibit (LVI)module can be enabled. It is also the only stop mode in which thecrystal oscillator can remain enabled

In our example, stop3 can be used when the MCU is waiting betweenspeed readings from the speed sensor. The RTI function that runs instop3 can be used to wake the MCU in time for the next reading.

Stop2 providesslightly less functionality, but reduces power consumption further. Instop2, the voltage regulator is powered down. However, the RAM contentsare still retained. The I/O registers are powered down in this mode andneed to be reconfigured upon wakeup from stop. In stop2, fewerinterrupt sources are available to wake the MCU up, but the RTIfunction is still available. Back to our example, stop2 can replacestop3 to reduce power consumption slightly more. Since the RTI functionand RAM both still work in stop2, the time between speed readings canstill be measured.

Stop1 is the lowest power mode of the MCU. It powers the voltage regulatordown completely along with all peripherals, the CPU, RAM and I/O. Onlythe reset or IRQ pins can wake the MCU up. Stop1 is used when the MCUcan be put into a powered down state, but still needs to respond to anexternal stimulus such as the press of a button.

In the bike computer example, stop1 would be entered when thecomputer is powered down. Powering down with stop1 mode puts the MCU inthe lowest possible power mode without actually removing power from thechip. Why not remove power from the chip? Removing power requires amore expensive toggle switch to disable power to the chip.

Also, using a push button switch tied to an interrupt pin, allowsthe MCU to use the switch for multiple purposes, depending on thecurrent state of the system. So stop1 mode is perfect for keeping thedesign simple, inexpensive and yet still consumes almost no current.

Clock Management
Many designers equate low power to slow clock frequencies. However,depending on what the MCU is doing and what low power modes areavailable on the MCU, running at top speed can actually save power.

If the MCU has an efficient low power mode, then you will save themost power by spending the most time in this mode. So if the CPU needsto execute code before returning to sleep, running at the fastestpossible speed to complete code execution and return to low power modecan burn less current than running constantly at slow speeds.

Let's use our bike computer example. Assume the control panel getsa speed update once every 1 second and it takes 16,000 bus cycles tocompute the data to be displayed. Running with a typical 32 kHz crystaland assuming a common divide-by-two bus clock, we have a 16 kHz bus, inwhich case it takes 1 full second to complete the calculations.

Now, if we can run at 8 MHz bus clock, it only takes 2 millisecondsto complete the calculations and 998 milliseconds can be spent in lowpower mode.

Of course, not every task the MCU has to perform will benefit fromtop speed performance. In our example, the time required for thewireless communication would probably not require an 8 MHz bus rate ifthe data is fairly slow. So in this case, to minimize powerconsumption, we want to run the MCU as slow as possible until thewireless communication is complete.

So we need an MCU with flexible clocks, such as Freescale's MC9S08GB60MCU. With this device, you can use high or low frequency crystalsor an internal oscillator.

With any of these clock sources, you can optionally use the on-chipfrequency-locked loop (FLL) to multiply or divide the bus speed to meetthe needs of the task and minimize power consumption. Figure 2, below, shows how the powerconsumption changes during the different modes of operation of our bikecomputer example.

Figure2: Chart of how power is managed in the bicycle computer example byalternating between short bursts of high activity and longer periods ofinactive low power modes.

System hardware strategies to extendbattery life
In addition to low power modes and clock management, there are manyother design considerations, both hardware and software, that canminimize power consumption. From a hardware standpoint, controllingpower to peripherals , both internal to the MCU and external, cansaveconsiderable power.

It is straight forward enough to disable on-chip peripherals usingthe MCU's control registers. But it may not be as obvious to disableperipherals external to the MCU. Using general I/O pins, power can becontrolled to external circuits.

Our example uses a speed sensor to measure the speed of the bicyclewheels. This can be implemented as and LED and photosensor attached tothe bike frame and a slotted disc attached to the wheel. Keeping theLED and photosensor on continuously will burn considerable current.Using I/O pins to enable the LED and photosensor only when taking aspeed measurement greatly reduces this current.

Now discreet components such as LEDs and photosensors may be obviouscandidates for controlling with I/O, but just about any circuit can becontrolled in a similar fashion. If the component requires more currentthan the MCU can provide directly, buffers can be used as powerswitches to these circuits. In some cases, simply connecting severalI/O pins in parallel can provide enough current.

The speed sensor also demonstrates another opportunity to reducecurrent. If the photosensor output is continuously read to determine ifthe light is shining through the disc, then the MCU has to be left in ahigher current run mode. Since all we really care about is thetransition point from light to dark and vice versa, we can useinterrupts instead of constant polling. Interrupts allow the MCU toenter a lower power wait mode. The MCU's timer can continue to countand by using an input capture feature tied to the photosensor output,we can easily measure the light/dark timing of the speed sensor todetermine the RPM.

The MCU's I/O pins themselves can be a source of excess current ifnot configured properly. Unused pins need to be terminated to prevent afloating input from creating a high current path. This is frequentlyoverlooked when using an MCU that comes in a variety of packages.

We tend to forget that the pins available in the highest pin countversion are still on the silicon in the lower pin count versions. Anyinput pin left floating will become a frustrating source of excessivecurrent, many times only under certain circumstances such astemperature variations. In these cases, either an internal pull-upshould be enabled or if the pin is an I/O, it can be configured as anoutput (data is irrelevant if it is driving an open circuit).

System software strategies to extendbattery life
From a software perspective, there are some less obvious tricks toreduce power consumption. As stated previously, the best way toconserve power is to stay in thelowest power state for as long aspossible .

Since run mode with the CPU actively executing instructions is neverthe lowest power state, we want to minimize the amount of work the CPUmust perform. This allows the CPU to complete tasks faster and allowsthe MCU to return to a low power mode.

Here are some tips for reducing the CPU execution time. Always usethe smallest data type possible . When writing C code, it is easytoforget that the common integer is usually defined as 16 or 32 bitnumber, even on compilers for 8-bit MCUs.

For 8-bit devices, always defaultto using the 8-bit char type unless a larger size is really necessary. Even then, you may be able toreduce code size by breaking a 16-bit or 32-bit number into 8-bitslices and only concatenate at the end of the data manipulation.

Avoid using short loops orsubroutine calls if you can afford theextra memory to use in-line code. Every loop and subroutine usesadditional CPU cycles to either determine if the loop is complete or tostack and unstack the program counter.

If you know a short loop will always be executed four times, justwrite the same code four times in arow rather than using a for-next orwhile loop . If a subroutine only has 10 or 20 bytes of code, considerin-lining it instead of keeping it a subroutine. This willeliminate afair amount of CPU overhead for simple tasks.

Pre-calculate values whenappropriate . Getting back to our bikecomputer example, assume the RF link uses two baud rates depending ifthe main control panel is talking to the display or to the speedsensor. When writing C code it may be tempting to pass the actual baudrate to the serial interface setup routine rather than the actualprescaler value the serial interface requires.

After all, this makes the code more readable. However, this alsoforces the serial interface routine to calculate the prescaler valuefrom the baud rate every time the baud rate is changed. Calculating theprescaler values beforehand and passing these to the serial setuproutine will save CPU cycles and code space.

Consider using look-up tablesrather than complex calculations . MCUssuch as Freescale's HCS08family have very efficient instructions and address modes foraccessing tabled data. Depending on the complexity of the calculation,this can save quite a bit of CPU effort. If calculations are stillrequired, always check for early exits before starting the routine. Asimple example is looking for multiplication by '1' or '0'.

Conclusion
Today's feature rich microcontrollers can offer designers ofbattery-powered applications many options for extending thebattery-life of these products. Multiple low power modes and flexibleclock sources give the designer the ability to manage powerconservation versus performance as necessary to meet design targets.Run fast when the CPU demand is high and slow down when it is not.Always switch to low power modes when ever possible.

In addition to managing the power of the MCU itself, foresight insystem planning allows the MCU to manage the power consumption of theentire system. The MCU can enable and disable devices and circuitswithin the system as needed almost as easily as it can manage itson-chip peripherals.

And not to be left out, the software engineers can add theircontribution to battery-life by establishing a CPU cycle awareness. Thefewer instructions the CPU has to execute, the sooner the MCU can entera lower power mode. Creating a lot of small functions may reduce codesize, but can end up reducing battery-life. On the other hand, alwaysusing the smallest data type possible will reduce both code size andincrease battery-life.

So for your next battery-powered system design, remember to chooseyour MCU wisely and to use everything the MCU offers to manage theoverall power of your system.

ScottPape is Systems Engineer in the Microcontroller Division, FreescaleSemiconductor, Austin, Texas

For more information on thegeneral subject of power management in a variety of designs, go to the Power ManagementDesign Line.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.