AVR Hands On megaAVR picoPower by nikeborome

VIEWS: 18 PAGES: 30

									         AVR Hands-On: megaAVR picoPower


Prerequisites
• Required knowledge
  - Basic understanding of Integrated Development Environments                    Microcontrollers
  - Basic knowledge of Embedded MCU programming in ANSI C

• Software prerequisites
  - AVR Studio 4.18 SP3 or later
  - AVR GCC Toolchain 3.0 or later

• Hardware prerequisites
  - JTAGICE mkII
              ®
  - picoPower board
  - AT32UC3C-EK
  - Computer with at least 3 USB ports

• Estimated completion time:
  - 2h


Introduction
This hands-on session covers some of the power saving features of the Atmel
megaAVR® microcontrollers. The goal for this hands-on is to make users familiar
with the picoPower™ technology used in the megaAVR MCUs, and show how an
application can be modified to implement the power-saving features of the AVR.
The following items will be covered:
  Sleep modes
  Clock gating through the use of the Power Reduction Registers (PRR)




                                                                                      Rev. [doc nr]-Training-[mm]/[yy]
Document Overview
                    This hands-on consists of four assignments, split into several different steps.
                    Throughout this document you will find sections with icons. These icons are used to
                    identify different sections of assignments and to identify different types of information.

                          Information
                    Delivers contextual information about a specific topic

                           Tip
                    Highlights useful tips and techniques

                           To do
                    Highlights objectives to be completed in italic font

                           Result
                    Highlights the expected result of an assignment step

                           Warning
                    Indicates important information




2   AVR Hands-On
                                                                                          [doc nr]-Training-[mm]/[yy]
                                                                                                                                 AVR Hands-On

Table of Contents
                              AVR Hands-On: megaAVR picoPower .............................................. 1
                              Prerequisites ....................................................................................... 1
                              Introduction ......................................................................................... 1
                              Document Overview ........................................................................... 2
                              Table of Contents................................................................................ 3
                              1 Software Requirements ................................................................... 5
                                1.1 Development tools ............................................................................................... 5
                              2 Hardware Requirements .................................................................. 5
                                2.1 Evaluation Kits ..................................................................................................... 5
                                2.2 Emulator .............................................................................................................. 7
                              3 Hands-On Setup ............................................................................... 8
                                3.1 Objectives ............................................................................................................ 8
                                3.2 Setup ................................................................................................................... 8
                                   3.2.1 Board setup ............................................................................................................... 8
                                   3.2.2 Powering up the equipment ....................................................................................... 9

                              4 Hands-On Assignment 1 ............................................................... 12
                                4.1 Objectives .......................................................................................................... 12
                                4.2 Application description ...................................................................................... 12
                                4.3 Exercises ........................................................................................................... 13
                                   4.3.1 Step 1: Opening the Project .................................................................................... 13
                                   Step 2: Setup the clock Clock System and Brown Out Detector ...................................... 14
                                   4.3.2 Step 3: Disable the digital input buffers on analog inputs ........................................ 16
                                   4.3.3 Step 4: Compile and run the project ........................................................................ 19
                                   4.3.4 Step 5: Measure the power consumption ................................................................ 21

                              5 Hands-On Assignment 2 ............................................................... 22
                                5.1 Objectives .......................................................................................................... 22
                                5.2 Application description ...................................................................................... 22
                                5.3 Exercises ........................................................................................................... 23
                                   5.3.1 Step 1: Add IDLE sleep mode ................................................................................. 23
                                   5.3.2 Step 2: Measure the power consumption ................................................................ 24

                              6 Hands-On Assignment 3 ............................................................... 25
                                6.1 Objectives .......................................................................................................... 25
                                6.2 Exercises ........................................................................................................... 25
                                   6.2.1 Step 1: Disable the clocks to unused peripheral modules ....................................... 25
                                   6.2.2 Step 2: Measure the power consumption ................................................................ 26

                              7 Hands-On Assignment 4 ............................................................... 27
                                7.1 Objectives .......................................................................................................... 27
                                7.2 Application Introduction ..................................................................................... 27

                                                                                                                                                                    3
[doc nr]-Training-[mm]/[yy]
                     7.3 Exercises ........................................................................................................... 28
                        7.3.1 Step 1: Add Power Save sleep mode ...................................................................... 28
                        7.3.2 Step 2: Measure the power consumption ................................................................ 29

                   8 Summary......................................................................................... 30
                   9 Resources....................................................................................... 30
                   10 Atmel Technical Support Center ................................................ 30




4   AVR Hands-On
                                                                                                                     [doc nr]-Training-[mm]/[yy]
                                                                                                          AVR Hands-On

1 Software Requirements

1.1 Development tools

                                    As stated in the Prerequisites section, the installation of AVR® Studio as well
                                    as the AVR GCC Toolchain needs to be done prior to the hands-on.


2 Hardware Requirements

2.1 Evaluation Kits
                                picoPower board
                              The purpose of the picoPower board is to become familiar with the general power-
                              saving features of the AVR microcontrollers.




                              The picoPower board provides an easy way to measure the Microcontrollers actual
                              current consumption in different operation modes. The schematic of the picoPower
                              Board is shown in the figure below. You will also find this schematic printed on the flip
                              side of the board. Note that the switches may not be mounted on the boards we are
                              using for this Hands-On.

                                                ISP                        PDI                            JTAG
                                                          3.3V                       3.3V          1.8V            3.3V




                                          ATmega48PA                 ATxmega32A4                AT32UC3L064
                                                                                                          V_CORE
                                                    PD2                        PC2                   RESET


                                      Mode Switch                Mode Switch                Mode Switch




                                                                                                                          5
[doc nr]-Training-[mm]/[yy]
                   The break-away section of the picoPower board contains current sensors for each of
                   the AVRs, converting the current to a voltage that can be measured by the
                   AT32UC3C-EK kit.




                     AT32UC3C-EK
                   The AT32UC3C-EK is a 32-bit AVR UC3C series evaluation kit. In this Hands-On the
                   AT32UC3C-EK is used as a current measurement instrument, showing the average
                   current consumption measured from the picoPower board on the LCD.




6   AVR Hands-On
                                                                                   [doc nr]-Training-[mm]/[yy]
                                                                                                AVR Hands-On
2.2 Emulator
                                  JTAGICE mkII
                              The JTAGICE mkII is a programmer and emulator tool for all AVR Microcontrollers.




                                                                   JTAGICE mkII

                              Note: Other AVR programmers and debuggers like the AVRONE! or the AVR Dragon can be
                              used as well. However this hands-on is written with the JTAGICE mkII in mind so procedures
                              and descriptions outlined may differ slightly if you use one of the other tools.




                                                                                                                      7
[doc nr]-Training-[mm]/[yy]
3 Hands-On Setup

3.1 Objectives
                     The objective for this section is to ensure the hardware is connected properly and
                     functioning normally prior to starting the hands-on assignments.


3.2 Setup

                           When connecting boards and emulator, power should be turned off. Ensure the
                           JTAGICE mkII is powered off, and the evaluation kits are not plugged into the
                           PC before proceeding with the board setup.


3.2.1 Board setup

                          Configure the jumpers
                       Ensure that the jumpers on the UC3C-EK and picoPower boards are correctly set
                       according to the images below. Note: The jumper labeled “Warning 1.8V only”)
                       should be left open.




8     AVR Hands-On
                                                                                      [doc nr]-Training-[mm]/[yy]
                                                                                       AVR Hands-On

                                   Connect the picoPower board to the AT32UC3C-EK board
                                Connect the picoPower board expansion connector to the AT32UC3C-EK
                                expansion connectors (J32, J33 and J38).




                                    Connect the JTAGICE mkII to the megaAVR
                                Connect the JTAGICE mkII to the picoPower megaAVR ISP connector using the 6-
                                pin SPI adapter.

                              Be sure to use the correct orientation of the 6-pin header when connecting the
                              JTAGICE mkII to the picoPower board, see pictures below. The pin 1 marking is
                              located by the gray wire.




                                               Pin 1 mark




3.2.2 Powering up the equipment

                                    Connect the JTAGICE mkII to PC
                                  Plug the JTAGICE mkII USB cable to the PC, and turn on the JTAGICE mkII
                                  using the power switch on the back side of the emulator.



                                                                                                          9
[doc nr]-Training-[mm]/[yy]
                           The JTAGICE mkII emulator will install automatically.
                      A new hardware is found:




                      The “Found New Hardware” wizard pops up:
                        If prompted to check the web, select the “No, not this time” option
                        Click “next” 2 times to install the software automatically

                           When powering up the development boards the power up sequence is
                           important. The ATUC3C-EK must be powered up before powering up the
                           picoPower board.

                        Power up the AT32UC3C-EK:
                        Plug a USB cable from the USB_USER connector (J21) to one of your PCs
                         USB ports.

                           Power up the picoPower board:
                           Plug a USB cable from the USB connector to one of your PCs USB ports.
                           Note that dependent upon previous programming of the picoPower boards,
                            the LEDs might be blinking in random order.




                    Launch the metering application
                      Reset the AT32UC3C-EK board by pressing the reset button on the AT32UC3C-
                      EK board.




10   AVR Hands-On
                                                                                      [doc nr]-Training-[mm]/[yy]
                                                                                             AVR Hands-On

                                    The device selection screen is displayed.



                                                                 1. ATmega48PA
                                                                 2. ATXmega32A4
                                                                 3. UC3L064




                                   Device Selection
                                Touching   pad selects the ATmega48PA
                                Touching     pad selects the ATXmega32A4
                                Touching       pad selects the UC3L064
                              Press the RESET button in order to select a new device or to restart/clear the
                              averaging filter for a new measurement.

                                   Select the device
                                Touch   pad to select the ATMEGA48PA device.

                                    After approximately 5-10 seconds of measurements and averaging, the
                                    average current consumption of the selected device will be displayed.



                                                              Device: ATmega48
                                                              Current: 2.34 mA




                                     Current consumption
                              Note that dependent upon what firmware is programmed into the picoPower board,
                              the current consumption shown in the display might be different from the 2.34 mA
                              shown in the image above.




                                                                                                                 11
[doc nr]-Training-[mm]/[yy]
4 Hands-On Assignment 1

4.1 Objectives
                         The goal of this assignment is to set up an application running on an ATmega48PA
                         AVR device, and learn how we configure the clock fuses and how to use the Digital
                         Input Disable Registers to reduce system power consumption.
                         In this assignment you will:
                              Learn about the data logger application
                              Configure the ATmega48PA fuse settings
                              Program and run this application on the picoPower board
                              Measure the power consumption using the UC3C-EK board
                              Modify the code to reduce power consumption


4.2 Application description
                         This application is a simple data logger that measures an analog signal every five
                         milliseconds and calculates a 16-bit average of 32 measured samples. The
                         application uses the RTC to time and trigger the ADC sampling. Since the picoPower
                         board does not have any external components connected to the analog input pins, we
                         will sample a floating input pin.
                         To achieve the required functionality of the application the following peripherals and
                         functions are used:
                              clock system
                              real time counter (RTC)
                              Analog to Digital Converter (ADC)
                              RTC interrupt service routine
                              ADC interrupt service routine
                              Averaging filter algorithm

                                RTC setup
                         In our application the timer/counter2 module is used as a Real Time Counter (RTC) to
                         trigger a new ADC conversion at regular intervals. The time out period (time between
                         ADC samples) is set to approximately 5ms (32768Hz divided by 160). The timer is set
                         up in CTC mode (Clear Timer on Compare match). In this mode, the counter is
                         cleared automatically by the hardware when the compare match interrupt triggers.
                         The rtc_init() function is used to initialize the RTC operation.

                                Averaging filter algorithm
                         The averaging filter algorithm used calculates an average over 32 ADC samples.
                         Three data buffers are used, and while writing ADC data to one buffer the two other
                         buffers are used for the averaging operation.
                         The averaging algorithm has no other use in this Hands-On than to provide a
                         processing task that has to be performed with the CPU in active mode.
                         Understanding this algorithm is not vital for the object of this hands-on and therefore
                         we will not go into further details about this function.

12   AVR Hands-On
                                                                                             [doc nr]-Training-[mm]/[yy]
                                                                                               AVR Hands-On
4.3 Exercises

4.3.1 Step 1: Opening the Project

                                    Launch the AVR Studio Project:
                                Start AVR Studio. This can be done through the desktop “start” menu:
                                “All Programs” -> “Atmel AVR Tools” -> “AVR Studio 4”
                                When the pop-up dialog shows up, click the “Open” button to open an existing
                                project




                                    The Open Project menu item
                              If AVR Studio is already running, or you have disabled the welcome pop-up, go to the
                              AVR Studio menu and select Project -> Open Project




                                Browse to the folder on your hard drive where you placed the files for this training.
                                Select the “picoPower-megaAVR.aps” file in the <training files>\Hands-on\Hands-
                                On_1_megaAVR\Setup” folder and click the “Open” button.




                                                                                                                   13
[doc nr]-Training-[mm]/[yy]
Step 2: Setup the clock Clock System and Brown Out Detector
                            For this application the ATmega48PA will run at 8MHz. The ATmega48PA has a built
                            in internal 8MHz RC oscillator. The default setting is to run with a prescaler of 8. i.e.
                            running at 1MHz. This setting can be changed by clearing the CKDIV8 bit in the clock
                            setup fuse settings.

                                 Program the ATmega48PA clock and BOD fuses
                              Open the programming dialog from the Tools menu: Tools -> Program AVR ->
                              Connect




                              Select JTAGICE mkII as the platform, Auto port and click the Connect… button




14    AVR Hands-On
                                                                                                 [doc nr]-Training-[mm]/[yy]
                                                                                               AVR Hands-On

                                    Upgrading the JTAGICE mkII
                              If AVR studio detects that a newer firmware is available for the JTAGICE mkII it will
                              prompt you to upgrade your JTAGICE mkII.




                              Click OK and the JTAGICE mkII firmware will be automatically upgraded to the latest
                              version. AVR studio will shut down after the upgrade is completed, re-start AVR
                              studio as explained above.
                                   In the Main tab select ATmega48PA as device and ISP mode as the
                                   Programming Mode.
                                Click Read Signature to verify correct connection to the device




                                    The ISP frequency must be less than 1/4 of the main clock frequency of the
                                    target device. I.e. running at 1MHz the ISP frequency should be 250KHz or
                                    less. If a higher clock setting is selected click the Settings button to change the
                                    ISP frequency.




                                                                                                                      15
[doc nr]-Training-[mm]/[yy]
                                 In the Fuses tab, ensure that that the BODLEVEL is set to “Brown-out detection at
                                 Vcc = 2.7 V, if not then select this value.
                                 If the box besides CKDIV8 is checked, uncheck it.
                                 If you made any changes to the fuses, press the Program button to program the
                                 fuse settings into the ATmega48PA.
                                 Verify in the bottom status view that the fuses are set to 0xE2, 0xDD, 0xFF




                                   Close the programming dialog by pressing the

4.3.2 Step 3: Disable the digital input buffers on analog inputs
                               The ATmega48PA use some of its IO pins as both analog input and digital IO. When
                               used as analog inputs, and the analog input voltage is close to VCC/2, the digital
                               input buffers of the IO pin will be in a half-on/half-off state. This gives an added
                               current consumption through the digital input buffer. To avoid this, each IO that is
                               used as an analog input should have its digital input buffer disabled.
                               The ATmega48PA provides two dedicated registers to disable the digital input buffers
                               for pins used as analog input, the Digital Input Disable Registers, DIDR0 and DIDR1.
                               In our application we use the ADC0 pin (PC0) as analog input, and the corresponding
                               DIDR register for this pin is DIDR0.


16    AVR Hands-On
                                                                                                  [doc nr]-Training-[mm]/[yy]
                                                                                                  AVR Hands-On

                                     ATmega48PA DIDR0 register description:
                              The DIDR0 register description can be found in section 23.9.5 of the ATmega48PA’s
                              datasheet. The ATmega48PA datasheet can be found in the <training
                              files>\Resources folder.

                                     Bit operations
                              The coding style we have chosen for this hands-on use a set of pre defined bit-masks
                              to define the position of individual bits inside a register. This makes the source code
                              more readable and portable compared to directly writing the hex value to the register.
                              All the bit-masks are defined in the file bitmask.h. All bitmasks are defined as <bit
                              name>_bm.
                              Set one bit of a variable/register without affecting other bits in the register:
                                 reg |= bit_mask;
                              Clear one bit of a variable/register without affecting other bits in the register:
                                 reg &= ~bit_mask;
                              Toggle one bit of a variable/register without affecting other bits in the register:
                                 reg ^= bit_mask;
                              In which reg is a register or variable, and bit_mask is the bit mask defined in the
                              bitmask.h file for the corresponding bit to be changed.

                                     Finding the correct bit mask
                              As an example let’s see how we can find the correct bitmask to set the ADC0D bit in
                              the DIDR0 register. In the ATmega48PA datasheet section 23.9.5 you will find the
                              description of the DIDR0 register. The ATmega48PA datasheet can be found in the
                              <training files>\Resources folder.




                              Bit 0 is called ADC0D (ADC channel 0 Digital Input Disable) and is the correct bit for
                              disabling the digital input buffer for the ADC channel 0 pin (ADC0).
                              Expand the Header Files folder in the AVR Studio project tree and double click the
                              bitmask.h file to open this file in the AVR studio editor.




                              In the DIDR0 section of the bitmask.h file you will find the bit mask definitions for the
                              DIDR0 register.



                                                                                                                    17
[doc nr]-Training-[mm]/[yy]
                    You should use the ADC0D_bm to set the ADC0D bit of the DIDR0 register.
                    The following code will set the ADC0D bit of the DIDR0 register without affecting the
                    other bits in the register.
                       DIDR0 |= ADC0D_bm;         //Set the ADC0D bit of the DIDR0 register


                           Finding the ToDo bookmarks
                    For each task where you have to modify the source code we have added comments
                    in the code with a ToDo bookmark. In order to find the ToDo bookmarks in the code
                    you can use the search function in AVR Studio. Go to the Edit -> Find… menu in avr
                    Studio (keyboard shortcut Ctrl+f)




                    Type ToDo 1 in the Find what: field and Click Find Next to highlight the ToDo 1
                    bookmark.




18   AVR Hands-On
                                                                                      [doc nr]-Training-[mm]/[yy]
                                                                                                AVR Hands-On

                                    Disable the digital input buffer on ADC0
                              Go to the ToDo 1 bookmark in the main.c file and disable the digital input function on
                              ADC0 pin by writing the corresponding bit in the DIDR0 register.

4.3.3 Step 4: Compile and run the project
                              You are now ready to compile and run the project.

                                    Compile the project
                                Right click on the project file in the AVR Studio Project tree and click “Build Project”
                                (or press F7) to compile.




                                   Program the ATmega48PA with the compiled binary
                                Open the programming dialog as we did when programming the clock fuses.




                                                                                                                     19
[doc nr]-Training-[mm]/[yy]
                    Select JTAGICE mkII as the platform, Auto port and click the Connect… button




                    In the Program tab, select the compiled binary (hex) file. You can find this file in
                    “<TrainingFiles>\Hands-on\Hands-On_1_megaAVR\Setup\default\picoPower-
                    megaAVR.hex”. Press the Program button.




                                                            Step 1: Select target file




                                      Step 2: Program Device




                       As you might already know from reading the source code, the Active LED will
                       be toggled while performing the filter calculation. If the device was correctly
                       programmed you should now see the Active LED blinking.

20   AVR Hands-On
                                                                                     [doc nr]-Training-[mm]/[yy]
                                                                                         AVR Hands-On
4.3.4 Step 5: Measure the power consumption

                                 Measure the power consumption
                              Unplug the USB power cable from the picoPower board
                              Disconnect the JTAGICE mkII from the picoPower board
                              Plug the USB power cable back into the picoPower board
                              Press the Reset button on UC3C-EK board
                              Wait for the Active LED to start blinking
                              Touch the       pad to select the ATmega48PA as input to the current measurement
                              circuit of the UC3C-EK board
                              Wait approximately 5 seconds for the UC3C-EK to display the average current
                              Write down the current consumption value in the current consumption table

                                  You can now see the average current consumption of the ATmega48PA on the
                                  LCD display of UC3C-EK board. You should see a current of approximately
                                  2.9 mA

                                  JTAGICE mkII Connection
                                  In order to avoid any JTAG parasitic consumption, you have to disconnect the
                                  JTAGICE mkII before reading the current consumption.




                                                                                                             21
[doc nr]-Training-[mm]/[yy]
5 Hands-On Assignment 2

5.1 Objectives
                         The goal of this assignment is to use Idle sleep mode of the ATmega48PA to save
                         power.
                         In this assignment you will:
                              Enable Idle mode in the application
                              Measure power consumption to compare with the value from assignment 1


5.2 Application description
                         In our application the CPU is only needed to set up the peripherals, move the ADC
                         conversion data and to perform the filter function. In order to reduce power
                         consumption we should put the CPU in sleep whenever it is not in use. We will use
                         the Idle mode to do this. In Idle mode the CPU and Flash program memory is stopped,
                         while the peripheral clock and peripheral modules are kept running.
                         When an RTC interrupt occurs the CPU wakes up and executes the RTC interrupt
                         function, triggers an ADC conversion and then enters Idle mode.
                         When the ADC conversion is completed, the ADC interrupt wakes the CPU and the
                         ADC interrupt function stores the ADC conversion data into the data buffer. For
                         simplicity let’s call this operation a “measurement cycle”. After 32 measurement
                         cycles, when the last ADC conversion is complete, the CPU performs the averaging
                         filter function. Let’s call this cycle (32 measurement cycles plus the filtering function) a
                         “complete cycle”.
                         The current consumption waveform is illustrated below. The UC3C-EK current
                         measurement will average all current samples in a “complete cycle” and show this as
                         the average current consumption of the application.
                                                                            RTC ISR          ADC ISR


                                       current
                                                       Filter calculation
                                                                                                      Filter calculation
                                                 ISR                                                               ISR
                                Active level

                                  Idle level



                          Power-save level
                                   0 level
                                                                                                               time




22   AVR Hands-On
                                                                                                 [doc nr]-Training-[mm]/[yy]
                                                                                              AVR Hands-On

5.3 Exercises

5.3.1 Step 1: Add IDLE sleep mode
                              To enter Idle mode, the following procedures need to be performed:
                                Select the desired sleep mode
                                Enable sleep function
                                Execute sleep instruction
                                Disable sleep function
                              The AVR GCC Toolchain has predefined functions available to control the sleep
                              modes of the AVR. In our application we will use the following functions:
                                 set_sleep_mode()
                                 sleep_enable()
                                 sleep_cpu()
                                 sleep_disable()
                              To use these functions, you need to include the “avr/sleep.h” header file in your
                              project. We have already included this in the project.
                              Refer to “AVR Studio” -> menu -> Help -> “avr-libc Reference Manual” -> “Library
                              Reference” -> “<avr/sleep.h>: Power Management and Sleep Modes” for more
                              detailed information.

                                    Enabling/disabling sleep function
                              To avoid the MCU entering the sleep mode unless it is the programmer’s intention (i.e.
                              by runaway code due to software bugs), it is recommended to write the Sleep Enable
                              (SE) bit in the Sleep Mode Control Register (SMCR) to one just before the execution
                              of the SLEEP instruction and to clear it immediately after waking up. The
                              sleep_enable() function and sleep_disable() function are used to set/clear this bit.


                                     Add Idle mode
                                Go to the ToDo 2 bookmark in the main.c file and add the code required to enter
                                Idle mode (SLEEP_MODE_IDLE):
                                            o   Select the appropriate sleep mode using set_sleep_mode()
                                            o   Prepare the CPU to enter sleep mode using sleep_enable()
                                            o   Execute the sleep instruction using sleep_cpu()
                              After the CPU wakes up execution is continued from where the sleep instruction
                              where executed.
                                            o   Disable sleep mode using sleep_disable()



                                    Compile the project and program the ATmega48PA
                                If you do not remember how this is done, see section 4.3.3 for a detailed
                                description of the steps necessary.

                                    After programming the device, you can see the Active LED seems to be
                                    constantly on. It should be blinking. Why does it seem to be constantly lit?



                                                                                                                   23
[doc nr]-Training-[mm]/[yy]
                                  Automatic ADC triggering in Sleep mode
                            For megaAVR, when entering “Idle” or “ADC Noise Reduction” sleep modes while the
                            ADC is enabled, an ADC conversion will be automatically triggered. This feature
                            ensures that an ADC conversion is not started until the CPU is sleeping to avoid
                            noise from the CPU to affect the ADC conversion result. In our application, as the
                            ADC is enabled, a new ADC conversion will be triggered whenever the CPU enters
                            idle mode. As a result, the ADC is constantly re-triggered after each ADC interrupt
                            wakeup. This significantly shortens the complete cycle of our program, making the
                            LED blink very fast, and to the eye it looks like the LED is constant on.
                            To avoid this behavior, you need to make some small changes to the code to achieve
                            correct behavior.

                                  Modify the code to achieve correct ADC triggering
                              Go to the ToDo 3 bookmark in the main.c file and and modify the line of code to
                              disable the ADC module by clearing the ADEN bit of the ADCSRA register (the
                              existing line of code currently enables the ADC).
                              Go to the ToDo 4 bookmark in the main.c file and enable the ADC module by
                              setting the ADEN bit of the ADCSRA register.
                              Go to the ToDo 5 bookmark in the main.c file and remove (comment out) the ADC
                              triggering code
                              Go to the ToDo 6 bookmark in the main.c file and disable the ADC module by
                              clearing the ADEN bit of the ADCSRA register

                                  Compile the project and program the ATmega48PA
                              If you do not remember how this is done, see section 4.3.3 for a detailed
                              description of the steps necessary.

                                  After programming the device, you should see the Active LED blinking

5.3.2 Step 2: Measure the power consumption

                                 Measure the power consumption
                              Unplug the USB power cable from the picoPower board
                              Disconnect the JTAGICE mkII from the picoPower board.
                              Plug the USB power cable back into the picoPower board
                              Press the Reset button on UC3C-EK board
                              Wait for the Active LED to start blinking
                              Touch the       pad to select the ATmega48PA as input to the current measurement
                              circuit of the UC3C-EK board.
                              Wait approximately 5 seconds for the UC3C-EK to display the average current.
                              Write down the current consumption value in the current consumption table.
                            You will see a drop of current consumption compared to the first assignment, down to
                            approximately 1.0mA. This is because the CPU now spends most of its time sleeping
                            in IDLE mode. It sleeps when there is no task for it to be done i.e. while waiting for the
                            RTC and ADC to complete 32 samples.



24    AVR Hands-On
                                                                                                  [doc nr]-Training-[mm]/[yy]
                                                                                              AVR Hands-On
6 Hands-On Assignment 3

6.1 Objectives
                              The goal of this assignment is to show how disabling the clock tree to the unused
                              peripheral modules further decrease power consumption.
                              In this assignment you will:
                                Disable unused modules by configuring the Power Reduction registers (PRR)
                                Measure power consumption to compare with the one from assignment 2


6.2 Exercises

6.2.1 Step 1: Disable the clocks to unused peripheral modules
                              The megaAVR devices have a rich set of peripherals available, and each peripheral
                              will add a small contribution to the total current consumption as long as the module is
                              clocked. The clock domain of most peripherals can be disabled independently, and by
                              removing the clock signal to an unused peripheral, the dynamic power consumption
                              contribution of that peripheral will be removed.
                              We will now modify the code to disable the clock to all unused peripherals by writing
                              to the PRR registers.

                                    Register description
                              A description of the PRR register can be found in section 9.11.3 of the ATmega48PA
                              datasheet.

                                   Disable the unused modules
                                Go to the ToDo 7 bookmark in the main.c file and disable the unused peripheral
                                modules by writing to the appropriate bits in the PRR register. The following
                                modules can be disabled:
                                             o   TIM0 (Timer/Counter 0), TIM1 (Timer/Counter 1)
                                             o   TWI (I2C interface), SPI (SPI interface), USART0 (USART
                                                 interface)

                                    Bit masks
                              The bit masks for the PRR register is found in the bitmask.h file. Search for PRR

                                    Compile the project and program the ATmega48PA
                                If you do not remember how this is done, see section 4.3.3 for a detailed
                                description of the steps necessary.

                                    After programming the device, you should see the Active LED blinking.




                                                                                                                  25
[doc nr]-Training-[mm]/[yy]
6.2.2 Step 2: Measure the power consumption

                                 Measure the power consumption
                              Unplug the USB power cable from the picoPower board
                              Disconnect the JTAGICE mkII from the picoPower board
                              Plug the USB power cable back into the picoPower board
                              Press the Reset button on UC3C-EK board
                              Wait for the Active LED to start blinking
                              Touch the       pad to select the ATmega48PA as input to the current measurement
                              circuit of the UC3C-EK board
                              Wait approximately 5 seconds for the UC3C-EK to display the average current.
                              Write down the current consumption value in the current consumption table

                                  You should see a drop in the current consumption down to approximately
                                  0.7mA.




26    AVR Hands-On
                                                                                             [doc nr]-Training-[mm]/[yy]
                                                                                                AVR Hands-On

7 Hands-On Assignment 4

7.1 Objectives
                              The goal of this assignment is use the Power Save sleep mode of the ATmega48PA
                              to further reduce current consumption.
                              In this assignment you will:
                                Enable the power-save sleep mode
                                Measure the power consumption and compare it with the measurement from
                                assignment 3


7.2 Application Introduction
                              In this assignment the MCU will enter power-save mode when the CPU and the
                              synchronous peripherals are not used.
                              In power save mode the CPU, Flash, all synchronous peripherals, and all clocks are
                              disabled. Only the asynchronous peripherals are enabled:
                                Asynchronous Timer/Counter2
                                TWI address match compare function
                                Asynchronous external interrupts
                              Since the ADC is a synchronous peripheral module it cannot run in power-save mode,
                              so when the ADC is running the CPU will go to Idle mode instead of power-save
                              mode. The current consumption waveform of the application is illustrated below.
                                                                               RTC ISR   Idle   ADC ISR
                                                                                                          Power-save


                                          current
                                                          Filter calculation
                                                                                                      Filter calculation
                                                    ISR                                                            ISR
                                   Active level

                                     Idle level



                               Power-save level
                                        0 level
                                                                                                             time



                              We see from this figure that the ATmega48PA stays in power save mode between the
                              measurement cycles of the program, and in Idle mode during execution of the
                              measurement cycle. As described in section 5.2 a measurement cycle is defined as
                              an RTC wakeup triggering an ADC conversion, and an ADC conversion completion
                              triggering the ADC complete interrupt.




                                                                                                                     27
[doc nr]-Training-[mm]/[yy]
7.3 Exercises

7.3.1 Step 1: Add Power Save sleep mode
                            As with Idle mode (see section 5.3.1), the following procedures need to be performed
                            to enter sleep mode:
                              Select the desired sleep mode
                              Enable sleep function
                              Execute sleep instruction
                              After wakeup, disable the sleep function to avoid accidentally entering sleep mode


                            Again we will use the predefined functions defined by the AVR GCC Toolchain to
                            control the sleep modes.
                               set_sleep_mode()
                               sleep_enable()
                               sleep_cpu()
                               sleep_disable()


                                  Enter Power Save sleep mode
                              Go to the ToDo 8 bookmark in the main.c file and add the necessary function calls
                              to enter Power Save (SLEEP_MODE_PWR_SAVE).


                            The Brown Out Detection (BOD) is used to prevent the CPU from executing code if
                            the CPU voltage dips below the minimum voltage (BOD threshold). In power-save
                            mode, the BOD module’s power consumption becomes a noticeable contribution to
                            the total system power consumption. In Sleep mode the CPU does not execute any
                            instructions, and hence the BOD is not needed. The ATmega48PA is able to
                            automatically disable BOD when entering sleep modes to reduce power consumption.
                            As with the other sleep mode operations defined by the AVR GCC Toolchain, it also
                            provides a readymade function for the BOD disable feature:
                               sleep_bod_disable()


                                 BOD disable
                            For more information about the BOD Disable feature see section 9.2 in the
                            ATmega48PA datasheet.

                                  Enable BOD disable (Sleeping BOD) feature
                              Add a call to the sleep_bod_disable() function right before calling the sleep_cpu()
                              function.

                                  Compile the project and program the ATmega48PA
                              If you do not remember how this is done, see section          4.3.3 for a detailed
                              description of the steps necessary.

                                  After programming the device, you should see the Active LED blinking.




28    AVR Hands-On
                                                                                              [doc nr]-Training-[mm]/[yy]
                                                                                            AVR Hands-On
7.3.2 Step 2: Measure the power consumption

                                 Measure the power consumption
                              Unplug the USB power cable from the picoPower board
                              Disconnect the JTAGICE mkII from the picoPower board
                              Plug the USB power cable back into the picoPower board
                              Press the Reset button on UC3C-EK board
                              Wait for the Active LED to start blinking
                              Touch the       pad to select the ATmega48PA as input to the current measurement
                              circuit of the UC3C-EK board
                              Wait approximately 5 seconds for the UC3C-EK to display the average current
                              Write down the current consumption value in the current consumption table

                                  You should now see a significant drop in current consumption compared to the
                                  previous assignment, down to approximately 0.08mA. This is because the CPU
                                  now spends time sleeping in power save mode (RTC mode) when there is no
                                  specific task for it to do or no synchronous peripherals need to operate, i.e. in
                                  between all ADC conversions for the 32 measurements.




                                                                                                                29
[doc nr]-Training-[mm]/[yy]
8 Summary
                    In this training we have looked at some of the ways to reduce power consumption on
                    the megaAVR, including disabling digital inputs on analog pins, various sleep modes ,
                    and stopping the clock to unused peripheral modules.
                    Without changing the functionality of the application and by making only a few simple
                    code modifications, we have managed to reduce the average power consumption
                    from 3.6mA to 0.09mA.


9 Resources
                      megaAVR datasheets:
                                   o   http://www.atmel.com/avr
                      AVR Studio:
                                   o   http://www.atmel.com/products/AVR/


10 Atmel Technical Support Center
                    Atmel has several support channels available:
                        o   Web portal:     http://support.atmel.no/ All Atmel microcontrollers
                        o   Email:          avr@atmel.com            All AVR products
                        o   Email:          avr32@atmel.com          All 32-bit AVR products


                    Please register on the web portal to gain access to the following services:
                        o   Access to a rich FAQ database
                        o   Easy submission of technical support requests
                        o   History of all your past support requests
                        o   Register to receive Atmel microcontrollers’ newsletters
                        o   Get information about available trainings and training material




30   AVR Hands-On
                                                                                        [doc nr]-Training-[mm]/[yy]

								
To top