Features * High Performance, Low Power AVR(R) 8-bit Microcontroller * Advanced RISC Architecture * * * * * * * * * - 124 Powerful Instructions - Most Single Clock Cycle Execution - 32 x 8 General Purpose Working Registers - Fully Static Operation - Up to 1 MIPS Throughput at 1 MHz Nonvolatile Program and Data Memories - 40K Bytes of In-System Self-Programmable Flash, Endurance: 10,000 Write/Erase Cycles - Optional Boot Code Section with Independent Lock Bits In-System Programming by On-chip Boot Program True Read-While-Write Operation - 512 bytes EEPROM, Endurance: 100,000 Write/Erase Cycles - 2K Bytes Internal SRAM - Programming Lock for Software Security On-chip Debugging - Extensive On-chip Debug Support - Available through JTAG interface Battery Management Features - Two, Three, or Four Cells in Series - Deep Under-voltage Protection - Over-current Protection (Charge and Discharge) - Short-circuit Protection (Discharge) - Integrated Cell Balancing FETs - High Voltage Outputs to Drive Charge/Precharge/Discharge FETs Peripheral Features - One 8-bit Timer/Counter with Separate Prescaler, Compare Mode, and PWM - One 16-bit Timer/Counter with Separate Prescaler and Compare Mode - 12-bit Voltage ADC, Eight External and Two Internal ADC Inputs - High Resolution Coulomb Counter ADC for Current Measurements - TWI Serial Interface for SM-Bus - Programmable Wake-up Timer - Programmable Watchdog Timer Special Microcontroller Features - Power-on Reset - On-chip Voltage Regulator - External and Internal Interrupt Sources - Four Sleep Modes: Idle, Power-save, Power-down, and Power-off Packages - 48-pin LQFP Operating Voltage: 4.0 - 25V Maximum Withstand Voltage (High-voltage pins): 28V Temperature Range: -30C to 85C - Speed Grade: 1 MHz 8-bit Microcontroller with 40K Bytes In-System Programmable Flash ATmega406 Preliminary 2548F-AVR-03/2013 1. Pin Configurations Figure 1-1. Pinout ATmega406. 48 47 46 45 44 43 42 41 40 39 38 37 NNI NI PI PPI VREFGND VREF NV PV1 PV2 PV3 PV4 GND Top View 36 35 34 33 32 31 30 29 28 27 26 25 1 2 3 4 5 6 7 8 9 10 11 12 PVT OD VFET OC OPC BATT PC0 GND PD1 PD0 (T0) PB7 (OC0B/PCINT15) PB6 (OC0A/PCINT14) RESET XTAL1 XTAL2 GND (TDO/PCINT8) PB0 (TDI/PCINT9) PB1 (TMS/PCINT10) PB2 (TCK/PCINT11) PB3 (PCINT12) PB4 (PCINT13) PB5 SCL SDA 13 14 15 16 17 18 19 20 21 22 23 24 SGND (ADC0/PCINT0) PA0 (ADC1/PCINT1) PA1 (ADC2/PCINT2) PA2 (ADC3/PCINT3) PA3 VREG VCC GND (ADC4/INT0/PCINT4) PA4 (INT1/PCINT5) PA5 (INT2/PCINT6) PA6 (INT3/PCINT7) PA7 1.1 Disclaimer Typical values contained in this datasheet are based on simulations and characterization of other AVR microcontrollers manufactured on the same process technology. Min and Max values will be available after the device is characterized. 2 ATmega406 2548F-AVR-03/2013 ATmega406 2. Overview The ATmega406 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATmega406 achieves throughputs approaching 1 MIPS at 1 MHz. 2.1 Block Diagram Figure 2-1. Block Diagram PD1..0 PB7..0 PORTD (2) PORTB (8) XTAL1 Oscillator Circuits / Clock Generation XTAL2 Watchdog Oscillator VCC RESET OPC OC OD FET Control Battery Protection PPI NNI PVT PV4 PV3 PV2 PV1 NV Wake-Up Timer JTAG 8 bit T/C0 Cell Balancing Flash SRAM 16 bit T/C1 Voltage ADC EEPROM Voltage Reference Watchdog Timer Power Supervision POR & RESET CPU SGND VREF VREFGND GND BATT VFET VREG Coulumb Counter ADC Charger Detect PI NI DATA BUS Voltage Regulator TWI PORTC (1) PORTA (8) PA3..0 SCL SCA PC0 PA7..0 The ATmega406 provides the following features: a Voltage Regulator, dedicated Battery Protection Circuitry, integrated cell balancing FETs, high-voltage analog front-end, and an MCU with two ADCs with On-chip voltage reference for battery fuel gauging. The voltage regulator operates at a wide range of voltages, 4.0 - 25 volts. This voltage is regulated to a constant supply voltage of nominally 3.3 volts for the integrated logic and analog functions. The battery protection monitors the battery voltage and charge/discharge current to detect illegal conditions and protect the battery from these when required. The illegal conditions are deep under-voltage during discharging, short-circuit during discharging and over-current during charging and discharging. 3 2548F-AVR-03/2013 The integrated cell balancing FETs allow cell balancing algorithms to be implemented in software. The MCU provides the following features: 40K bytes of In-System Programmable Flash with Read-While-Write capabilities, 512 bytes EEPROM, 2K byte SRAM, 32 general purpose working registers, 18 general purpose I/O lines, 11 high-voltage I/O lines, a JTAG Interface for On-chip Debugging support and programming, two flexible Timer/Counters with PWM and compare modes, one Wake-up Timer, an SM-Bus compliant TWI module, internal and external interrupts, a 12-bit Sigma Delta ADC for voltage and temperature measurements, a high resolution Sigma Delta ADC for Coulomb Counting and instantaneous current measurements, a programmable Watchdog Timer with internal Oscillator, and four software selectable power saving modes. The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers are directly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. The resulting architecture is more code efficient while achieving throughputs up to ten times faster than conventional CISC microcontrollers. The Idle mode stops the CPU while allowing the other chip function to continue functioning. The Power-down mode allows the voltage regulator, battery protection, regulator current detection, Watchdog Timer, and Wake-up Timer to operate, while disabling all other chip functions until the next Interrupt or Hardware Reset. In Power-save mode, the Wake-up Timer and Coulomb Counter ADC continues to run. The device is manufactured using Atmel's high voltage high density non-volatile memory technology. The On-chip ISP Flash allows the program memory to be reprogrammed In-System, by a conventional non-volatile memory programmer or by an On-chip Boot program running on the AVR core. The Boot program can use any interface to download the application program in the Application Flash memory. Software in the Boot Flash section will continue to run while the Application Flash section is updated, providing true Read-While-Write operation. By combining an 8-bit RISC CPU with In-System Self-Programmable Flash, fuel gauging ADCs, dedicated battery protection circuitry, Cell Balancing FETs, and a voltage regulator on a monolithic chip, the Atmel ATmega406 is a powerful microcontroller that provides a highly flexible and cost effective solution for Li-ion Smart Battery applications. The ATmega406 AVR is supported with a full suite of program and system development tools including: C Compilers, Macro Assemblers, Program Debugger/Simulators, and On-chip Debugger. 4 ATmega406 2548F-AVR-03/2013 ATmega406 2.2 2.2.1 Pin Descriptions VFET High voltage supply pin. This pin is used as supply for the internal voltage regulator, described in "Voltage Regulator" on page 114. In addition the voltage level on this pin is monitored by the battery protection circuit, for deep-under-voltage protection. For details, see "Battery Protection" on page 125. 2.2.2 VCC Digital supply voltage. Normally connected to VREG. 2.2.3 VREG Output from the internal Voltage Regulator. Used for external decoupling to ensure stable regulator operation. For details, see "Voltage Regulator" on page 114. 2.2.4 VREF Internal Voltage Reference for external decoupling. For details, see "Voltage Reference and Temperature Sensor" on page 121. 2.2.5 VREFGND Ground for decoupling of Internal Voltage Reference. For details, see "Voltage Reference and Temperature Sensor" on page 121. 2.2.6 GND Ground 2.2.7 SGND Signal ground pin, used as reference for Voltage-ADC conversions. For details, see "Voltage ADC - 10-channel General Purpose 12-bit Sigma-Delta ADC" on page 116. 2.2.8 Port A (PA7:PA0) PA3:PA0 serves as the analog inputs to the Voltage A/D Converter. Port A also serves as a low-voltage 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). As inputs, Port A pins that are externally pulled low will source current if the pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port A also serves the functions of various special features of the ATmega406 as listed in "Alternate Functions of Port A" on page 68. 2.2.9 Port B (PB7:PB0) Port B is a low-voltage 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B also serves the functions of various special features of the ATmega406 as listed in "Alternate Functions of Port B" on page 70. 5 2548F-AVR-03/2013 2.2.10 Port C (PC0) Port C is a high voltage Open Drain output port. 2.2.11 Port D (PD1:PD0) Port D is a low-voltage 2-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port D also serves the functions of various special features of the ATmega406 as listed in "Alternate Functions of Port D" on page 72. 2.2.12 SCL SMBUS clock, Open Drain bidirectional pin. 2.2.13 SDA SMBUS data, Open Drain bidirectional pin. 2.2.14 OC/OD/OPC High voltage output to drive external Charge/Discharge/Pre-charge FETs. For details, see "FET Control" on page 133. 2.2.15 PPI/NNI Unfiltered positive/negative input from external current sense resistor, used by the battery protection circuit, for over-current and short-circuit detection. For details, see "Battery Protection" on page 125. 2.2.16 PI/NI Filtered positive/negative input from external current sense resistor, used to by the Coulomb Counter ADC to measure charge/discharge currents flowing in the battery pack. For details, see "Coulomb Counter - Dedicated Fuel Gauging Sigma-delta ADC" on page 106. 2.2.17 NV/PV1/PV2/PV3/PV4 NV, PV1, PV2, PV3, and PV4 are the inputs for battery cells 1, 2, 3 and 4, used by the Voltage ADC to measure each cell voltage. For details, see "Voltage ADC - 10-channel General Purpose 12-bit Sigma-Delta ADC" on page 116. 2.2.18 PVT PVT defines the pull-up level for the OD output. 2.2.19 BATT Input for detecting when a charger is connected. This pin also defines the pull-up level for OC and OPC outputs. 2.2.20 RESET Reset input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running. The minimum pulse length is given in Table 11 on page 38. Shorter pulses are not guaranteed to generate a reset. 6 ATmega406 2548F-AVR-03/2013 ATmega406 2.2.21 XTAL1 Input to the inverting Oscillator amplifier. 2.2.22 XTAL2 Output from the inverting Oscillator amplifier. 3. Resources A comprehensive set of development tools, application notes and datasheets are available for download on http://www.atmel.com/avr. 4. About Code Examples This documentation contains simple code examples that briefly show how to use various parts of the device. These code examples assume that the part specific header file is included before compilation. Be aware that not all C compiler vendors include bit definitions in the header files and interrupt handling in C is compiler dependent. Please confirm with the C compiler documentation for more details. For I/O Registers located in extended I/O map, "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" instructions must be replaced with instructions that allow access to extended I/O. Typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR". 7 2548F-AVR-03/2013 5. AVR CPU Core 5.1 Introduction This section discusses the AVR core architecture in general. The main function of the CPU core is to ensure correct program execution. The CPU must therefore be able to access memories, perform calculations, control peripherals, and handle interrupts. 5.2 Architectural Overview Figure 5-1. Block Diagram of the AVR Architecture Data Bus 8-bit Flash Program Memory Program Counter Status and Control 32 x 8 General Purpose Registrers Control Lines Direct Addressing Instruction Decoder Indirect Addressing Instruction Register Interrupt Unit Watchdog Timer ALU I/O Module1 I/O Module 2 Data SRAM I/O Module n EEPROM I/O Lines In order to maximize performance and parallelism, the AVR uses a Harvard architecture - with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program memory. This concept enables instructions to be executed in every clock cycle. The program memory is In-System Reprogrammable Flash memory. 8 ATmega406 2548F-AVR-03/2013 ATmega406 The fast-access Register File contains 32 x 8-bit general purpose working registers with a single clock cycle access time. This allows single-cycle Arithmetic Logic Unit (ALU) operation. In a typical ALU operation, two operands are output from the Register File, the operation is executed, and the result is stored back in the Register File - in one clock cycle. Six of the 32 registers can be used as three 16-bit indirect address register pointers for Data Space addressing - enabling efficient address calculations. One of the these address pointers can also be used as an address pointer for look up tables in Flash program memory. These added function registers are the 16-bit X-, Y-, and Z-register, described later in this section. The ALU supports arithmetic and logic operations between registers or between a constant and a register. Single register operations can also be executed in the ALU. After an arithmetic operation, the Status Register is updated to reflect information about the result of the operation. Program flow is provided by conditional and unconditional jump and call instructions, able to directly address the whole address space. Most AVR instructions have a single 16-bit word format. Every program memory address contains a 16- or 32-bit instruction. Program Flash memory space is divided in two sections, the Boot Program section and the Application Program section. Both sections have dedicated Lock bits for write and read/write protection. The SPM instruction that writes into the Application Flash memory section must reside in the Boot Program section. During interrupts and subroutine calls, the return address Program Counter (PC) is stored on the Stack. The Stack is effectively allocated in the general data SRAM, and consequently the Stack size is only limited by the total SRAM size and the usage of the SRAM. All user programs must initialize the SP in the Reset routine (before subroutines or interrupts are executed). The Stack Pointer (SP) is read/write accessible in the I/O space. The data SRAM can easily be accessed through the five different addressing modes supported in the AVR architecture. The memory spaces in the AVR architecture are all linear and regular memory maps. A flexible interrupt module has its control registers in the I/O space with an additional Global Interrupt Enable bit in the Status Register. All interrupts have a separate Interrupt Vector in the Interrupt Vector table. The interrupts have priority in accordance with their Interrupt Vector position. The lower the Interrupt Vector address, the higher the priority. The I/O memory space contains 64 addresses for CPU peripheral functions as Control Registers, SPI, and other I/O functions. The I/O Memory can be accessed directly, or as the Data Space locations following those of the Register File, 0x20 - 0x5F. In addition, the ATmega406 has Extended I/O space from 0x60 - 0xFF in SRAM where only the ST/STS/STD and LD/LDS/LDD instructions can be used. 5.3 ALU - Arithmetic Logic Unit The high-performance AVR ALU operates in direct connection with all the 32 general purpose working registers. Within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immediate are executed. The ALU operations are divided into three main categories - arithmetic, logical, and bit-functions. Some implementations of the architecture also provide a powerful multiplier supporting both signed/unsigned multiplication and fractional format. See the "Instruction Set" section for a detailed description. 9 2548F-AVR-03/2013 5.4 Status Register The Status Register contains information about the result of the most recently executed arithmetic instruction. This information can be used for altering program flow in order to perform conditional operations. Note that the Status Register is updated after all ALU operations, as specified in the "AVR Instruction Set" description. This will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. The Status Register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software. 5.4.1 SREG - AVR Status Register The AVR Status Register - SREG - is defined as: Bit 7 6 5 4 3 2 1 0 0x3F (0x5F) I T H S V N Z C Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 SREG * Bit 7 - I: Global Interrupt Enable The Global Interrupt Enable bit must be set for the interrupts to be enabled. The individual interrupt enable control is then performed in separate control registers. If the Global Interrupt Enable Register is cleared, none of the interrupts are enabled independent of the individual interrupt enable settings. The I-bit is cleared by hardware after an interrupt has occurred, and is set by the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared by the application with the SEI and CLI instructions, as described in the "AVR Instruction Set" description. * Bit 6 - T: Bit Copy Storage The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or destination for the operated bit. A bit from a register in the Register File can be copied into T by the BST instruction, and a bit in T can be copied into a bit in a register in the Register File by the BLD instruction. * Bit 5 - H: Half Carry Flag The Half Carry Flag H indicates a Half Carry in some arithmetic operations. Half Carry Is useful in BCD arithmetic. See the "AVR Instruction Set" for detailed information. * Bit 4 - S: Sign Bit, S = N V The S-bit is always an exclusive or between the negative flag N and the Two's Complement Overflow Flag V. See the "AVR Instruction Set" for detailed information. * Bit 3 - V: Two's Complement Overflow Flag The Two's Complement Overflow Flag V supports two's complement arithmetics. See the "AVR Instruction Set" for detailed information. * Bit 2 - N: Negative Flag The Negative Flag N indicates a negative result in an arithmetic or logic operation. See the "AVR Instruction Set" for detailed information. 10 ATmega406 2548F-AVR-03/2013 ATmega406 * Bit 1 - Z: Zero Flag The Zero Flag Z indicates a zero result in an arithmetic or logic operation. See the "AVR Instruction Set" for detailed information. * Bit 0 - C: Carry Flag The Carry Flag C indicates a carry in an arithmetic or logic operation. See the "AVR Instruction Set" for detailed information. 5.5 General Purpose Register File The Register File is optimized for the AVR Enhanced RISC instruction set. In order to achieve the required performance and flexibility, the following input/output schemes are supported by the Register File: * One 8-bit output operand and one 8-bit result input * Two 8-bit output operands and one 8-bit result input * Two 8-bit output operands and one 16-bit result input * One 16-bit output operand and one 16-bit result input Figure 5-2 shows the structure of the 32 general purpose working registers in the CPU. Figure 5-2. AVR CPU General Purpose Working Registers 7 0 Addr. R0 0x00 R1 0x01 R2 0x02 ... R13 0x0D General R14 0x0E Purpose R15 0x0F Working R16 0x10 Registers R17 0x11 ... R26 0x1A X-register Low Byte R27 0x1B X-register High Byte R28 0x1C Y-register Low Byte R29 0x1D Y-register High Byte R30 0x1E Z-register Low Byte R31 0x1F Z-register High Byte Most of the instructions operating on the Register File have direct access to all registers, and most of them are single cycle instructions. As shown in Figure 5-2, each register is also assigned a data memory address, mapping them directly into the first 32 locations of the user Data Space. Although not being physically implemented as SRAM locations, this memory organization provides great flexibility in access of the registers, as the X-, Y- and Z-pointer registers can be set to index any register in the file. 11 2548F-AVR-03/2013 5.5.1 The X-register, Y-register, and Z-register The registers R26:R31 have some added functions to their general purpose usage. These registers are 16-bit address pointers for indirect addressing of the data space. The three indirect address registers X, Y, and Z are defined as described in Figure 5-3. Figure 5-3. The X-, Y-, and Z-registers 15 X-register XH 7 XL 0 R27 (0x1B) 15 Y-register YH 7 YL 0 0 7 0 R28 (0x1C) 15 ZH 7 0 R31 (0x1F) 0 R26 (0x1A) R29 (0x1D) Z-register 0 7 ZL 7 0 0 R30 (0x1E) In the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (see the "AVR Instruction Set" description for details). 5.6 Stack Pointer The Stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. The Stack Pointer Register always points to the top of the Stack. Note that the Stack is implemented as growing from higher memory locations to lower memory locations. This implies that a Stack PUSH command decreases the Stack Pointer. The Stack Pointer points to the data SRAM Stack area where the Subroutine and Interrupt Stacks are located. This Stack space in the data SRAM must be defined by the program before any subroutine calls are executed or interrupts are enabled. The Stack Pointer must be set to point above 0x100. The Stack Pointer is decremented by one when data is pushed onto the Stack with the PUSH instruction, and it is decremented by two when the return address is pushed onto the Stack with subroutine call or interrupt. The Stack Pointer is incremented by one when data is popped from the Stack with the POP instruction, and it is incremented by two when data is popped from the Stack with return from subroutine RET or return from interrupt RETI. The AVR Stack Pointer is implemented as two 8-bit registers in the I/O space. The number of bits actually used is implementation dependent. Note that the data space in some implementations of the AVR architecture is so small that only SPL is needed. In this case, the SPH Register will not be present. 12 ATmega406 2548F-AVR-03/2013 ATmega406 5.6.1 SPH and SPL - Stack Pointer Register Bit 15 14 13 12 11 10 9 8 0x3E (0x5E) SP15 SP14 SP13 SP12 SP11 SP10 SP9 SP8 SPH 0x3D (0x5D) SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 SPL 7 6 5 4 3 2 1 0 Read/Write Initial Value 5.7 R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Instruction Execution Timing This section describes the general access timing concepts for instruction execution. The AVR CPU is driven by the CPU clock clkCPU, directly generated from the selected clock source for the chip. No internal clock division is used. Figure 5-4 shows the parallel instruction fetches and instruction executions enabled by the Harvard architecture and the fast-access Register File concept. This is the basic pipelining concept to obtain up to 1 MIPS per MHz with the corresponding unique results for functions per cost, functions per clocks, and functions per power-unit. Figure 5-4. The Parallel Instruction Fetches and Instruction Executions T1 T2 T3 T4 clkCPU 1st Instruction Fetch 1st Instruction Execute 2nd Instruction Fetch 2nd Instruction Execute 3rd Instruction Fetch 3rd Instruction Execute 4th Instruction Fetch Figure 5-5 shows the internal timing concept for the Register File. In a single clock cycle an ALU operation using two register operands is executed, and the result is stored back to the destination register. Figure 5-5. Single Cycle ALU Operation T1 T2 T3 T4 clkCPU Total Execution Time Register Operands Fetch ALU Operation Execute Result Write Back 13 2548F-AVR-03/2013 5.8 Reset and Interrupt Handling The AVR provides several different interrupt sources. These interrupts and the separate Reset Vector each have a separate program vector in the program memory space. All interrupts are assigned individual enable bits which must be written logic one together with the Global Interrupt Enable bit in the Status Register in order to enable the interrupt. Depending on the Program Counter value, interrupts may be automatically disabled when Boot Lock bits BLB02 or BLB12 are programmed. This feature improves software security. See the section "Memory Programming" on page 195 for details. The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors. The complete list of vectors is shown in "Interrupts" on page 51. The list also determines the priority levels of the different interrupts. The lower the address the higher is the priority level. RESET has the highest priority. The Interrupt Vectors can be moved to the start of the Boot Flash section by setting the IVSEL bit in the MCU Control Register (MCUCR). Refer to "Interrupts" on page 51 for more information. The Reset Vector can also be moved to the start of the Boot Flash section by programming the BOOTRST Fuse, see "Boot Loader Support - ReadWhile-Write Self-Programming" on page 178. When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a Return from Interrupt instruction - RETI - is executed. There are basically two types of interrupts. The first type is triggered by an event that sets the interrupt flag. For these interrupts, the Program Counter is vectored to the actual Interrupt Vector in order to execute the interrupt handling routine, and hardware clears the corresponding interrupt flag. Interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. If an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the interrupt flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software. Similarly, if one or more interrupt conditions occur while the Global Interrupt Enable bit is cleared, the corresponding interrupt flag(s) will be set and remembered until the Global Interrupt Enable bit is set, and will then be executed by order of priority. The second type of interrupts will trigger as long as the interrupt condition is present. These interrupts do not necessarily have interrupt flags. If the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered. When the AVR exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served. Note that the Status Register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. This must be handled by software. When using the CLI instruction to disable interrupts, the interrupts will be immediately disabled. No interrupt will be executed after the CLI instruction, even if it occurs simultaneously with the 14 ATmega406 2548F-AVR-03/2013 ATmega406 CLI instruction. The following example shows how this can be used to avoid interrupts during the timed EEPROM write sequence. Assembly Code Example in r16, SREG cli ; store SREG value ; disable interrupts during timed sequence sbi EECR, EEMWE ; start EEPROM write sbi EECR, EEWE out SREG, r16 ; restore SREG value (I-bit) C Code Example char cSREG; cSREG = SREG; /* store SREG value */ /* disable interrupts during timed sequence */ _CLI(); EECR |= (1< xxx ... ... 0x0034 RESET: ... ; Enable interrupts ; 52 ATmega406 2548F-AVR-03/2013 ATmega406 When the BOOTRST Fuse is unprogrammed, the Boot section size set to 2K bytes and the IVSEL bit in the MCUCR Register is set before any interrupts are enabled, the most typical and general program setup for the Reset and Interrupt Vector Addresses is: Address Labels 0x0000 RESET: Code Comments ldi r16,high(RAMEND); Main program start 0x0001 out SPH,r16 0x0002 ldi r16,low(RAMEND) 0x0003 0x0004 out sei SPL,r16 0x0005 ; Set Stack Pointer to top of RAM ; Enable interrupts xxx ; .org 0x4C02 0x4C02 jmp BPINT ; Battery Protection Interrupt Handler 0x4C04 jmp EXT_INT0 ; External Interrupt Request 0 Handler ... ... ... ; 0x4C2C jmp SPM_RDY ; Store Program Memory Ready Handler When the BOOTRST Fuse is programmed and the Boot section size set to 2K bytes, the most typical and general program setup for the Reset and Interrupt Vector Addresses is: Address Labels Code Comments .org 0x0002 0x0002 jmp BPINT ; Battery Protection Interrupt Handler 0x0004 jmp EXT_INT0 ; External Interrupt Request 0 Handler ... ... ... ; 0x002C jmp SPM_RDY ; Store Program Memory Ready Handler ; .org 0x4C00 0x4C00 RESET: ldi r16,high(RAMEND); Main program start 0x4C01 out SPH,r16 0x4C02 ldi r16,low(RAMEND) 0x4C03 0x4C04 out sei SPL,r16 0x4C05 ; Set Stack Pointer to top of RAM ; Enable interrupts xxx When the BOOTRST Fuse is programmed, the Boot section size set to 2K bytes and the IVSEL bit in the MCUCR Register is set before any interrupts are enabled, the most typical and general program setup for the Reset and Interrupt Vector Addresses is: Address Labels Code Comments ; .org 0x4C00 0x4C00 0x4C02 jmp jmp RESET BPINT ; Reset handler ; Battery Protection Interrupt Handler 0x4C04 jmp EXT_INT0 ; External Interrupt Request 0 Handler ... ... ... ; 0x4C2C jmp SPM_RDY ; Store Program Memory Ready Handler ldi r16,high(RAMEND); Main program start ; 0x4C2E RESET: 53 2548F-AVR-03/2013 0x4C2F out SPH,r16 0x4C30 ldi r16,low(RAMEND) 0x4C31 0x4C32 out sei SPL,r16 0x4C33 11.2 ; Set Stack Pointer to top of RAM ; Enable interrupts xxx Moving Interrupts Between Application and Boot Space The General Interrupt Control Register controls the placement of the Interrupt Vector table. Assembly Code Example Move_interrupts: ; Enable change of Interrupt Vectors ldi r16, (1< CSn2:0 > 1). The number of system clock cycles from when the timer is enabled to the first count occurs can be from 1 to N+1 system clock cycles, where N equals the prescaler divisor. It is possible to use the prescaler reset for synchronizing the Timer/Counter to program execution. However, care must be taken if the other Timer/Counter that shares the same prescaler also uses prescaling. A prescaler reset will affect the prescaler period for all Timer/Counters it is connected to. 17.3 External Clock Source An external clock source applied to the T0 pin can be used as Timer/Counter0 clock (clkT0). The T0 pin is sampled once every system clock cycle by the pin synchronization logic. The synchronized (sampled) signal is then passed through the edge detector. Figure 17-1 shows a functional equivalent block diagram of the T0 synchronization and edge detector logic. The registers are clocked at the positive edge of the internal system clock (clkI/O). The latch is transparent in the high period of the internal system clock. The edge detector generates one clkT0 pulse for each positive (CSn2:0 = 7) or negative (CSn2:0 = 6) edge it detects. Figure 17-1. T1/T0 Pin Sampling Tn D Q D Q D Tn_sync (To Clock Select Logic) Q LE clk I/O Synchronization Edge Detector 103 2548F-AVR-03/2013 The synchronization and edge detector logic introduces a delay of 2.5 to 3.5 system clock cycles from an edge has been applied to the T0 pin to the counter is updated. Enabling and disabling of the clock input must be done when T0 has been stable for at least one system clock cycle, otherwise it is a risk that a false Timer/Counter clock pulse is generated. Each half period of the external clock applied must be longer than one system clock cycle to ensure correct sampling. The external clock must be guaranteed to have less than half the system clock frequency (fExtClk < fclk_I/O/2) given a 50/50% duty cycle. Since the edge detector uses sampling, the maximum frequency of an external clock it can detect is half the sampling frequency (Nyquist sampling theorem). However, due to variation of the system clock frequency and duty cycle caused by Oscillator source (crystal, resonator, and capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than fclk_I/O/2.5. An external clock source can not be prescaled. Figure 17-2. Prescaler for Timer/Counter0 and Timer/Counter1(1) PSRSYNC CK/1024 CK/256 CK/128 CK/64 CK/8 CK/32 Clear clk I/O T0 Synchronization clkT1 Note: 104 clkT0 1. The synchronization logic on the input pins (T1/T0) is shown in Figure 17-1. ATmega406 2548F-AVR-03/2013 ATmega406 17.4 17.4.1 Register Description GTCCR - General Timer/Counter Control Register Bit 7 6 5 4 3 2 1 0 0x23 (0x43) TSM - - - - - - PSRSYNC Read/Write R/W R R R R R R R/W Initial Value 0 0 0 0 0 0 0 0 GTCCR * Bit 7 - TSM: Timer/Counter Synchronization Mode Writing the TSM bit to one activates the Timer/Counter Synchronization mode. In this mode, the value that is written to the PSRSYNC bit is kept, hence keeping the corresponding prescaler reset signals asserted. This ensures that the corresponding Timer/Counters are halted and can be configured to the same value without the risk of one of them advancing during configuration. When the TSM bit is written to zero, the PSRSYNC bit is cleared by hardware, and the Timer/Counters start counting simultaneously. * Bit 0 - PSRSYNC: Prescaler Reset When this bit is one, Timer/Counter1 and Timer/Counter0 prescaler will be Reset. This bit is normally cleared immediately by hardware, except if the TSM bit is set. Note that Timer/Counter1 and Timer/Counter0 share the same prescaler and a reset of this prescaler will affect both timers. 105 2548F-AVR-03/2013 18. Coulomb Counter - Dedicated Fuel Gauging Sigma-delta ADC 18.1 Features * * * * * * * * * * * Sampled System Coulomb Counter Low Power Sigma-Delta ADC Optimized for Coulomb Counting Instantaneous Current Output with 3.9 ms Conversion Time Accumulate Current Output with Programmable Conversion Time: 125/250/500/1000 ms Input voltage Range Larger than 0.15V, Allowing Measurement of more than 30A @ 5 m 13-bit Resolution (including sign) corresponding to 53.7 V (10.7 mA @ 5 m) for Instantaneous Current Output 18-bit Resolution (including sign) corresponding to 1.68 V (0.335 A @ 5 m) for Accumulate Current Output Input Offset Less than 10 V for the ADC Interrupt on Instantaneous Current Conversion Complete Interrupt on Accumulate Current Conversion Complete Interrupt on Regular Current with Programmable Compare Level and Programmable Sampling Interval: 250/500/1000/2000 ms ATmega406 features a dedicated Sigma-Delta ADC (CC-ADC) optimized for Coulomb Counting to sample the charge or discharge current flowing through the external sense resistor RSENSE. Two different output values are provided, Instantaneous Current and Accumulate Current. The Instantaneous Current Output has a short conversion time at the cost of lower resolution. The Accumulate Current Output provides a highly accurate current measurement for Coulomb Counting. The sampling Coulomb Counter provides a highly accurate and flexible solution. Accuracy can easily be traded against conversion time. It also provides Regular Current detection. This allows ultra-low power operation in Power-save mode when small charge or discharge currents are flowing. Figure 18-1. Coulomb Counter Block Diagram INSTANTANEOUS CURRENT ACCUMULATE CURRENT 8-BIT DATABUS Regular Current IRQ Level Control & Status Register Current Comparator IRQ IRQ PI Sigma Delta modulator R SENSE Decimation IRQ Decimation NI 106 ATmega406 2548F-AVR-03/2013 ATmega406 18.2 Operation When enabled, the CC-ADC continuously measures the voltage over the external sense resistor RSENSE. The Instantaneous Current conversion time is fixed to 3.9 ms (typical value) allowing the output value to closely follow the input. After each Instantaneous Current conversion an interrupt is generate if the interrupt is enabled. Data from conversion will be updated in the Instantaneous Current registers - CADICL and CADICH simultaneously as the interrupt is given. To avoid losing conversion data, both the low and high byte must be read within a 3,9 ms timing window after the corresponding interrupt is given. When the low byte register is read, updating of the Instantaneous Current registers and interrupts will be stopped until the high byte is read. Figure 18-2 shows an Instantaneous Current conversion diagram, where DATA4 will be lost because DATA3 reading is not completed within the limited period. Figure 18-2. Instantaneous Current Conversion Enable ~12 ms settling 3.9 ms 3.9 ms Setting of Digital Filters DATA1 DATA2 7.8 ms Instantaneous Interrupt Instantaneous Data DATA 3 DATA5 Read low byte Read high byte The Accumulate Current output is a high-resolution, high accuracy output with programmable conversion time selected by the CADAS bits in CADCSRA. The converted value is an accurate measurement of the average current flow during one conversion period. The CC-ADC generates an interrupt each time a new Accumulate Current conversion has finished if the interrupt is enabled. Data from conversion will be updated in the Accumulation Current registers - CADAC0, CADAC1, CADAC2 and CADAC3 simultaneously as the interrupt is given. To avoid losing conversion data, all bytes must be read within the selected conversion period. When the lower byte registers are read, updating of the Accumulation Current registers and interrupts will be stopped until the highest byte is read. Figure 18-3 on page 108 shows an Accumulation Current conversion example, where DATA4 will be lost because DATA3 reading is not completed within the limited period. 107 2548F-AVR-03/2013 Figure 18-3. Accumulation Current Conversion Enable 125, 250, 500, or 1000 ms 125, 250, 500, or 1000 ms 250, 500, 1000, or 2000 ms Accumulation Interrupt Accumulation Data Setting of Digital Filters DATA1 DATA2 DATA 3 DATA5 Read byte 1 Read byte 2 Read byte 3 Read byte 4 While the CC-ADC is converting, the CPU can enter sleep mode and wait for an interrupt from the Accumulate Current conversion. After adding the new Accumulate Current value for Coulomb Counting, the CPU can go back to sleep again. This reduces the CPU workload, and allows more time spent in low power modes, reducing power consumption. The CC-ADC can generate an interrupt if the result of an Instantaneous Current conversion is greater than a programmable threshold. This allows the detection of a Regular Current condition. This function is available in Active mode and all sleep modes except Power-down and Power-off mode. This allows an ultra-low power operation in Power-save, where the CC-ADC can be configured to enter a Regular Current detection mode with a programmable current sampling interval. By setting the CADSE bit in CADCSRA, the Coulomb Counter will repeatedly do one Instantaneous Current conversion, before it is being turned off for a timing interval specified by the CADSI bits in CADCSRA. This allows operating the Regular Current detection while keeping the Coulomb Counter off most of the time. The Coulomb Counter is halted in Power-down mode. In this mode, time measurements and the battery self-discharge characteristics should be used to estimate the charge flow. When waking up from Power-down mode, the CC-ADC will automatically resume continuous operation. The CC-ADC is enabled by setting the CC-ADC Enable bit, CADEN, in CADCSRA. Note that the bandgap voltage reference must be enabled separately, see "BGCCR - Bandgap Calibration C Register" on page 123. The CC-ADC will not consume power when CADEN is cleared. It is therefore recommended to switch off the CC-ADC whenever the Coulomb Counter or Regular Current Detection functions are not used. The CC-ADC is automatically disabled in Power-down and Power-off mode. After the CC-ADC is enabled, either by setting the CADEN bit or leaving Power-down with CADEN already set, the first four conversions do not contain useful data and should be ignored. This also applies after clearing the CADSE bit. In-system offset voltage for the CC-ADC is typically in the range 0 - 100 V. To compensate for this offset error, a CC-ADC offset value should be stored in EEPROM and subtracted from each Accumulate Current conversions before the resulting value is added for Coloumb Counting. The CC-ADC offset value can be found by performing a CC-ADC conversion at typical temperature with zero current flowing through RSENSE. When the battery is not used or the current level stays very low for a long time, it is recommended to estimate the charge flow instead of using the CC-ADC for Coloumb Counting. The 108 ATmega406 2548F-AVR-03/2013 ATmega406 charge flow estimation should be based on the self-discharge rate of the battery and the standby current of the battery system. 18.2.1 CADCSRA - CC-ADC Control and Status Register A Bit 7 6 5 4 3 2 1 0 CADEN - CADUB CADAS1 CADAS0 CADSI1 CADSI0 CADSE Read/Write R/W R R R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 (0xE4) CADCSRA * Bit 7 - CADEN: CC-ADC Enable When the CADEN bit is cleared (zero), the CC-ADC is disabled, and any ongoing conversions will be terminated. When the CADEN bit is set (one), the CC-ADC will continuously measure the voltage drop over the external sense resistor RSENSE. In Power-off, the CC-ADC is always disabled. Note that the bandgap voltage reference must be enabled separately, see "BGCCR - Bandgap Calibration C Register" on page 123. * Bit 6 - Res: Reserved This bit is reserved bit in the ATmega406 and will always read as zero. * Bit 5 - CADUB: CC-ADC Update Busy The CC-ADC operates in a different clock domain than the CPU. Whenever a new value is written to CADCSRA, CADRCC or CADRDC, this value must be synchronized to the CC-ADC clock domain. Subsequent writes to these registers will be blocked during this synchronization. Synchronization of one of the registers, will block updating of all the others. The CADUB bit will be read as one while any of these registers is being synchronized, and will be read as zero when neither register is being synchronized. * Bits 4:3 - CADAS1:0: CC-ADC Accumulate Current Select The CADAS bits select the conversion time for the Accumulate Current output as shown in Table 18-1. Table 18-1. CC-ADC Accumulate Current Conversion Time CADAS1:0 CC-ADC Accumulate Current Conversion Time 00 125 ms 01 250 ms 10 500 ms 11 1s 109 2548F-AVR-03/2013 * Bits 2:1 - CADSI1:0: CC-ADC Current Sampling Interval The CADSI bits determine the current sampling interval for the Regular Current detection as shown in Table 18-2. The current sampling interval is only used if the CADSE bit is set. Table 18-2. Notes: CC-ADC Regular Current Sampling Interval CADSI1:0 CC-ADC Regular Current Sampling Interval(1)(2) 00 250 ms (+ sampling time) 01 500 ms (+ sampling time) 10 1 s (+ sampling time) 11 2 s (+ sampling time) 1. The actual value of depends on the actual frequency of the "Slow RC Oscillator" on page 27. See "Electrical Characteristics" on page 225. 2. Sampling time ~ 12 ms. * Bit 0 - CADSE: CC-ADC Current Sampling Enable When the CADSE bit is written to one, the ongoing CC-ADC conversion is aborted, and the CCADC enters Regular Current detection mode. 18.2.2 CADCSRB - CC-ADC Control and Status Register B Bit 7 6 5 4 3 2 1 0 (0xE5) - CADACIE CADRCIE CADICIE - CADACIF CADRCIF CADICIF Read/Write R R/W R/W R/W R R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 CADCSRB * Bits 7, 3 - Res: Reserved These bits are reserved bits in the ATmega406 and will always read as zero. * Bit 6 - CADACIE: CC-ADC Accumulate Current Interrupt Enable When the CADACIE bit is set (one), and the I-bit in the Status Register is set (one), the CC-ADC Accumulate Current Interrupt is enabled. * Bit 5 - CADRCIE: CC-ADC Regular Current Interrupt Enable When the CADRCIE bit is set (one), and the I-bit in the Status Register is set (one), the CC-ADC Regular Current Interrupt is enabled. * Bit 4 - CADICIE: CC-ADC Instantaneous Current Interrupt Enable When the CADICIE bit is set (one), and the I-bit in the Status Register is set (one), the CC-ADC Instantaneous Current Interrupt is enabled. * Bit 2 - CADACIF: CC-ADC Accumulate Current Interrupt Flag The CADACIF bit is set (one) after the Accumulate Current conversion has completed. The CCADC Accumulate Current Interrupt is executed if the CADACIE bit and the I-bit in SREG are set (one). CADACIF is cleared by hardware when executing the corresponding Interrupt Handling Vector. Alternatively, CADACIF is cleared by writing a logic one to the flag. * Bit 1 - CADRCIF: CC-ADC Regular Current Interrupt Flag 110 ATmega406 2548F-AVR-03/2013 ATmega406 The CADRCIF bit is set (one) when the absolute value of the result of the last CC-ADC conversion is greater than, or equal to, the compare values set by the CC-ADC Regular Charge/Discharge Current Level Registers. A positive value is compared to the Regular Charge Current Level, and a negative value is compared to the Regular Discharge Current Level. The CC-ADC Regular Current Interrupt is executed if the CADRCIE bit and the I-bit in SREG are set (one). CADRCIF is cleared by hardware when executing the corresponding Interrupt Handling vector. Alternatively, CADRCIF is cleared by writing a logic one to the flag. * Bit 0 - CADICIF: CC-ADC Instantaneous Current Interrupt Flag The CADICIF bit is set (one) when a CC-ADC Instantaneous Current conversion is completed. The CC-ADC Instantaneous Current Interrupt is executed if the CADICIE bit and the I-bit in SREG are set (one). CADICIF is cleared by hardware when executing the corresponding Interrupt Handling vector. Alternatively, CADICIF is cleared by writing a logic one to the flag. 18.2.3 CADICH and CADICL - CC-ADC Instantaneous Current Bit 15 14 13 12 11 (0xE9) CADIC[15:8] (0xE8) CADIC[7:0] 10 9 8 CADICH CADICL Bit 7 6 5 4 3 2 1 0 Read/Write R R R R R R R R R R R R R R R R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Initial Value When a CC-ADC Instantaneous Current conversion is complete, the result is found in these two registers. CADIC15:0 represents the converted result in 2's complement format, sign extended to 16 bits. When CADICL is read, the CC-ADC Instantaneous Current register is not updated until CADCH is read. Reading the registers in the sequence CADICL, CADICH will ensure that consistent values are read. 18.2.4 CADAC3, CADAC2, CADAC1 and CADAC0 - CC-ADC Accumulate Current Bit 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 (0xE3) CADAC[31:24] CADAC3 (0xE2) CADAC[23:16] CADAC2 (0xE1) CADAC[15:8] CADAC1 (0xE0) CADAC[7:0] CADAC0 Read/Write Initial Value R R R R R R R R R R R R R R R R R R R R R R R R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The CADAC3, CADAC2, CADAC1 and CADAC0 Registers contain the Accumulate Current measurements in 2's complement format, sign extended to 32 bits. 111 2548F-AVR-03/2013 When CADAC0 is read, the CC-ADC Accumulate Current register is not updated until CADAC3 is read. Reading the registers in the sequence CADAC0, CADAC1, CADAC2, CADAC3 will ensure that consistent values are read. 18.2.5 CADRCC - CC-ADC Regular Charge Current Bit 7 6 5 4 (0xE6) 3 2 1 0 CADRCC[7:0] CADRCC Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 The CC-ADC Regular Charge Current Register determines the threshold level for the Regular Charge Current detection. When the result of a CC-ADC Instantaneous Current conversion is positive with a value greater than, or equal to, the Regular Charge Current level, the CC-ADC Regular Current Interrupt Flag is set. The value in this register is specified in 2's complement format, and it defines the eight least significant bits of the Regular Charge Current level. The most significant bits of the Regular Charge Current level are always zero. The programmable range for the Regular Charge Current level is given in Table 18-3. Table 18-3. Programmable Range for the Regular Charge Current Level Minimum Maximum Step Size 0 13700 53.7 RSENSE = 5 m 0 2740 10.7 RSENSE = 7 m 0 1957 7.7 Voltage (V) Current (mA) The CC-ADC Regular Charge Current Register does not affect the setting of the CC-ADC Conversion Complete Interrupt Flag. 18.2.6 CADRDC - CC-ADC Regular Discharge Current Bit 7 6 5 (0xE7) 4 3 2 1 0 CADRDC[7:0] CADRDC Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 The CC-ADC Regular Discharge Current Register determines the threshold level for the Regular Discharge Current detection. When the result of a CC-ADC Instantaneous Current conversion is negative with an absolute value greater than, or equal to, the Regular Discharge Current level, the CC-ADC Regular Current Interrupt Flag is set. The value in this register is specified in 2's complement format, and it defines the eight least significant bits of the Regular Discharge Current level. The most significant bits of the Regular 112 ATmega406 2548F-AVR-03/2013 ATmega406 Charge Current level are always one. The programmable range for the Regular Discharge Current level is given in Table 18-4. Table 18-4. Programmable Range for the Regular Discharge Current Level Minimum Maximum Step Size 0 13700 53.7 RSENSE = 5 m 0 2740 10.7 RSENSE = 7 m 0 1957 7.7 Voltage (V) Current (mA) The CC-ADC Regular Discharge Current Register does not affect the setting of the CC-ADC Conversion Complete Interrupt Flag. 113 2548F-AVR-03/2013 19. Voltage Regulator 19.1 Features * Linear Regulation. * Operating Voltage Range 4.0 - 25V. * Fixed Output Voltage at 3.3V. ATmega406 is supplied by the VFET terminal. Operating voltage range at the VFET terminal is 4.0 - 25V. The Internal Voltage Regulator regulates this voltage down to 3.3V, which is a suitable supply voltage for the internal logic, I/O lines, and analog circuitry. An external decoupling capacitor of 1 F or larger is required for stable operation of the Voltage Regulator. A larger capacitor will allow larger load currents and increase start-up time. The block diagram of the Voltage Regulator is shown in Figure 19-1. Figure 19-1. Voltage Regulator Block Diagram VFET Voltage Regulator RN PVT PV1 Regulator Control LDO_ON VREG LDO Regulator Creg > 1 uF RP POWER_OFF Power Distributor Rsense Digital Supply 19.2 Analog Supply Operation The Regulator will operate in all sleep modes, including Power-off. In this mode the regulator will automatically reduce the ATmega406's power consumption by turning off supply for all peripheral modules, allowing only the Charger Detect module and the Voltage Regulator itself to operate. The Regulator will automatically ensure that it has stable work conditions before allowing itself to start regulating the VFET terminal. If the voltage at the VFET pin is below the Regulator-on Threshold voltage, VROT, the LDO will be switched off. Powering-up the regulator is either done from the battery side when the smart battery controller is assembled with the battery pack and there is no charger present, or from the charger side when a deep discharge has occurred (0V charging). When powering- up with a charger present, the voltage between the VFET and the PVT pin must be above a Charge-Threshold voltage, VCHT. 114 ATmega406 2548F-AVR-03/2013 ATmega406 When powering-up without a charger present, the voltage on Cell1, VPV1, must be above the Cell1-Threshold voltage, VPV1T. After powering-up the regulator the chip will enter Power-off sleep mode (lowest power consumption). Until a charger is detected, the chip will stay in this mode. For details on Charger Detect, see "Power-on Reset and Charger Connect" on page 40. Table 30-2 on page 230 shows the characteristics for powering-up the LDO. 115 2548F-AVR-03/2013 20. Voltage ADC - 10-channel General Purpose 12-bit Sigma-Delta ADC 20.1 Features * * * * * * * * 12-bit Resolution 1 LSB Accuracy 519s Conversion Time Four Differential Input Channels for Cell Voltage Measurements Six Single Ended Input Channels 0 to 0.9 x VREF Input Voltage Range 0.2x Pre-scaling of Cell Voltages and VREG Interrupt on V-ADC Conversion Complete The ATmega406 features a 12-bit Sigma-Delta ADC. Automatic offset cancellation technique reduces the input offset voltage to less than 0.5 mV. The Voltage ADC (V-ADC) is connected to ten different sources through the Input Multiplexer. There are four differential channels for Cell Voltage measurements. These channels are scaled 0.2x to comply with the Full Scale range of the V-ADC. In addition there are six single ended channels referenced to SGND. One channel is for measuring the internal temperature sensor VPTAT and five channels for measuring the ADC input pins at Port A. ADC3:0 are not scaled, meaning that full-scale reading corresponds to 1.1 V. ADC4 is scaled by 0.2x, meaning that fullscale reading corresponds to 5.5 V. The ADC4 input can be used to measure the voltage at the PA4 pin when this pin is used to supply an external thermistor, see Figure 29-1 on page 223. To obtain a total absolute accuracy better than 0.25% for the cell voltage measurements, calibration registers for the individual cell voltage gain in the analog front-end is provided. A factory calibration value is stored in the signature row, see Section 27.7.10 "Reading the Signature Row from Software" on page 189. The V-ADC conversion of a cell voltage must be scaled with the corresponding calibration value by software to correct for gain error in the analog front-end. The PRVADC bit in "PRR0 - Power Reduction Register 0" on page 36 must be written to zero to enable V-ADC module. 116 ATmega406 2548F-AVR-03/2013 ATmega406 Figure 20-1. Voltage ADC Block Schematic V-ADC CONVERSION COMPLETE IRQ V-ADC MULTIPLEXER SEL. REG (VADMUX) ADC3 VADCCIE VADCCIF 8-BIT DATA BUS V-ADC CONTROL AND STATUS REG (VADCSR) ADC2 ADC1 ADC0 V-ADC CONTROL VTEMP INPUT MUX ADC4 PV4 V-ADC DATA REGISTER (VADCL/ADCH) 12-BIT SIGMA-DELTA ADC PV3 PV2 PV1 NV VREF SGND Note: The shaded signals are scaled by 0.2, other signals are scaled by 1.0 20.2 Operation To enable V-ADC conversions, the V-ADC Enable bit, VADEN, in V-ADC Control and Status Register - VADCSR must be set. If this bit is cleared, the V-ADC will be switched off, and any ongoing conversions will be terminated. The V-ADC is automatically halted in Power-save, Power-down and Power-off mode. Note that the bandgap voltage reference must be enabled and disabled separately, see "BGCCR - Bandgap Calibration C Register" on page 123. Figure 20-2. Voltage ADC Conversion Diagram 519 us Start Conversion Interrupt Conversion Result OLD DATA INVALID DATA VA L I D D ATA INVALID DATA To perform a V-ADC conversion, the analog input channel must first be selected by writing to the VADMUX bits in VADMUX. When a logical one is written to the V-ADC Start Conversion bit VADSC, a conversion of the selected channel will start. The VADSC bit stays high as long as the conversion is in progress and will be cleared by hardware when the conversion is completed. If a different data channel is selected while a conversion is in progress, the ADC will finish the current conversion before performing the channel change. When a conversion is finished the V- 117 2548F-AVR-03/2013 ADC Conversion Complete Interrupt Flag - VADCCIF is set. One 12-bit conversion takes 519 s to complete from the start bit is set to the interrupt flag is set. To ensure that correct data is read, both high and low byte data registers should be read before starting a new conversion. 20.3 20.3.1 Register Description VADMUX - V-ADC Multiplexer Selection Register Bit 7 6 5 4 3 2 1 0 (0x7C) - - - - VADMUX3 VADMUX2 VADMUX1 VADMUX0 Read/Write R R R R R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 VADMUX * Bit 7:4 - RES: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bit 3:0 - VADMUX3:0: V-ADC Channel Selection Bits The VADMUX bits determine the V-ADC channel selection. See Table 20-1 on page 118. Table 20-1. VADMUX channel selection VADMUX3:0 20.3.2 Channel Selected Scale 0001 CELL 1 0.2 0010 CELL 2 0.2 0011 CELL 3 0.2 0100 CELL 4 0.2 0101 ADC4 0.2 0110 VTEMP 1.0 0111 ADC0 1.0 1000 ADC1 1.0 1001 ADC2 1.0 1010 ADC3 1.0 VADCSR - V-ADC Control and Status Register Bit 7 6 5 4 3 2 1 0 (0x7A) - - - - VADEN VADSC VADCCIF VADCCIE Read/Write R R R R R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 VADCSR * Bit 7:4 - RES: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bit 3 - VADEN: V-ADC Enable Writing this bit to one enables V-ADC conversion. By writing it to zero, the V-ADC is turned off. Turning the V-ADC off while a conversion is in progress will terminate this conversion. Note that the bandgap voltage reference must be enabled separately, see "BGCCR - Bandgap Calibration C Register" on page 123. 118 ATmega406 2548F-AVR-03/2013 ATmega406 * Bit 2 - VADSC: Voltage ADC Start Conversion Write this bit to one to start a new conversion of the selected channel. VADSC will read as one as long as the conversion is not finished. When the conversion is complete, it returns to zero. Writing zero to this bit has no effect. VADSC will automatically be cleared when the VADEN bit is written to zero. * Bit 1 - VADCCIF: V-ADC Conversion Complete Interrupt Flag This bit is set when a V-ADC conversion completes and the data registers are updated. The VADC Conversion Complete Interrupt is executed if the VADCCIE bit and the I-bit in SREG are set. VADCCIF is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, VADCCIF is cleared by writing a logical one to the flag. Beware that if doing a Read-Modify-Write on VADCSR, a pending interrupt can be disabled. * Bit 0 - VADCCIE: V-ADC Conversion Complete Interrupt Enable When this bit is written to one and the I-bit in SREG is set, the V-ADC Conversion Complete Interrupt is activated. 20.3.3 VADCL and VADCH - The V-ADC Data Register Bit 15 14 13 12 (0x79) - - - - (0x78) 11 10 9 8 VADC[11:8] VADCH VADC[7:0] Read/Write Initial Value VADCL 7 6 5 4 3 2 1 0 R R R R R R R R R R R R R R R R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 When a V-ADC conversion is complete, the result is found in these two registers. To ensure that correct data is read, the data registers must be read before starting a new conversion. * VADC11:0: V-ADC Conversion Result These bits represent the result from the conversion. To obtain the best absolute accuracy for the cell voltage measurements, gain and offset compensation is required. Factory calibration values are stored in the device signature row, refer to section "Reading the Signature Row from Software" on page 189 for details. The cell voltage in mV is given by: cell n result cell n gain calibration word Cell n voltage mV = --------------------------------------------------------------------------------------------------- - cell n offset calibration word TBD When performing a Vtemp conversion, the result must be adjusted by the factory calibration value stored in the signature row, refer to section "Reading the Signature Row from Software" on page 189 for details. The absolute temperature in Kelvin is given by: V temp result VPTAT calibration word T(K) = -----------------------------------------------------------------------------------------------TBD 119 2548F-AVR-03/2013 20.3.4 DIDR0 - Digital Input Disable Register 0 Bit 7 6 5 4 3 2 1 0 (0x7E) - - - - VADC3D VADC2D VADC1D VADC0D Read/Write R R R R R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 DIDR0 * Bits 7:4 - Res: Reserved Bits These bits are reserved for future use. To ensure compatibility with future devices, these bits must be written to zero when DIDR0 is written. * Bit 3:0 - VADC3D:VADC0D: V-ADC3:0 Digital Input Disable When this bit is written logic one, the digital input buffer on the corresponding V-ADC pin is disabled. The corresponding PIN Register bit will always read as zero when this bit is set. When an analog signal is applied to the VADC3:0 pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer. 120 ATmega406 2548F-AVR-03/2013 ATmega406 21. Voltage Reference and Temperature Sensor 21.1 Features * * * * * * Accurate Voltage Reference of 1.100V 0.1% Accuracy After Calibration (2 mV Calibration Steps) Temperature Drift Less than 80 ppm/C after Calibration Alternate Low Power Voltage Reference for Voltage Regulator Internal Temperature Sensor Possibility for Runtime Compensation of Temperature Drift in Both Voltage Reference and Onchip Oscillators * External Decoupling for Optimum Noise Performance * Low Power Consumption A low power band-gap reference provides ATmega406 with an accurate On-chip voltage reference V REF of 1.100V. This reference voltage is used as reference for the On-chip Voltage Regulator, the V-ADC and the CC-ADC. The reference to the ADCs uses a buffer with external decoupling capacitor to enable excellent noise performance with minimum power consumption. The reference voltage VREF_P/VREF_N to the CC-ADC is scaled to match the full scale requirement at the current sense input pins. This configuration also enables concurrent operation of both V-ADC and CC-ADC. To guaranty ultra low temperature drift after factory calibration, ATmega406 features a two-step calibration algorithm. The first step is performed at 85C and the second at room temperature. By default, Atmel factory calibration is performed at 85C, and the result is stored in Flash. The customer can easily implement the second calibration step in their test flow. This requires an accurate input voltage and a stable room temperature. Temperature drift after this calibration is guarantied by design and characterization to be less than 80 ppm/C from 0C to 60C and 100 ppm/C from 0C to 85C. The BG Calibration C Register can also be altered runtime to implement temperature compensation in software. Very high accuracy for any temperature inside the temperature range can thus be achieved at the cost of extra calibration steps. A lower power, less accurate voltage reference source exists. This voltage reference source is chosen as reference for the voltage regulator whenever the band-gap voltage reference is disabled. This voltage reference source is not available for the V-ADC and CC-ADC. ATmega406 has an On-chip temperature sensor for monitoring the die temperature. A voltage Proportional-To-Absolute-Temperature, VPTAT, is generated in the voltage reference circuit and connected to the multiplexer at the V-ADC input. This temperature sensor can be used for runtime compensation of temperature drift in both the voltage reference and the On-chip Oscillator. To get the absolute temperature in degrees Kelvin, the measured VPTAT voltage must be scaled with the VPTAT factory calibration value stored in the signature row. See "Reading the Signature Row from Software" on page 189 for details. 121 2548F-AVR-03/2013 Figure 21-1. Reference Circuitry 1.1V VREF BG Reference VREF_P VPTAT 0.22V CREF VREF_N VREF_GND 21.2 Writing to Bandgap Calibration Registers When the calibration registers are changed it will affect both the Voltage Regulator output and BOD-level. The BOD will react quickly to new detection levels, while the regulator will adjust the voltage more slowly, depending on the size of the external decoupling capacitor. To avoid that a BOD-reset is issued when calibration is done, it is recommended to change the values of the BGCC and BGCR bits stepwise, with a step size of 1, and with a hold-off time between each step. The hold-off time depends on the size of the voltage regulators external decoupling capacitor. For details, see Table 21-1. Table 21-1. Hold-off Times depending on CREG. Regulator Cap 122 Hold-off Time BGCCR Hold-off Time BGCRR 1 F 1.2 s 3.0 s 2 F 2.4 s 6.0 s 3 F 3.6 s 9.0 s 4 F 4.8 s 12.0 s 5 F 6.0 s 15.0 s 6 F 7.2 s 18.0 s 7 F 8.4 s 21.0 s 8 F 9.6 s 24.0 s 9 F 10.8 s 27.0 s 10 F 12.0 s 30.0 s ATmega406 2548F-AVR-03/2013 ATmega406 21.3 21.3.1 Register Description for Voltage Reference and Temperature Sensor BGCCR - Bandgap Calibration C Register Bit 7 6 5 4 3 2 1 0 BGEN - BGCC5 BGCC4 BGCC3 BGCC2 BGCC1 BGCC0 Read/Write R R R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 (0xD0) BGCCR * Bit 7 - BGEN This bit is not available from revision E and on of the ATmega406. A complete description is found in the revision A of this document. * Bit 6 - Res: Reserved Bit This bit is reserved for future use. * Bit 5:0 - BGCC5:0: BG Calibration of PTAT Current These bits are used for trimming of the nominal value of the bandgap reference voltage. These bits are binary coded. Minimum VREF: 000000, maximum VREF: 111111. Step size approximately 2 mV. 21.3.2 BGCRR - Bandgap Calibration R Register Bit 7 6 5 4 3 2 1 0 BGCR7 BGCR6 BGCR5 BGCR4 BGCR3 BGCR2 BGCR1 BGCR0 Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 (0xD1) BGCRR * Bit 7:0 - BGCR7:0: BG Calibration of Resistor ladder These bits are used for temperature gradient adjustment of the bandgap reference. Figure 21-2 illustrates VREF as a function of temperature. VREF has a positive temperature coefficient at low temperatures and negative temperature coefficient at high temperatures. Depending on the process variations, the top of the VREF curve may be located at higher or lower temperatures. To minimize the temperature drift in the temperature range of interest, BGCRR is used to adjust the top of the curve towards the centre of the temperature range of interest. The BGCRR bits are temperature coded resulting in 9 possible settings: 00000000, 00000001, 00000011, 00000111, ... , 11111111. The value 00000000 shifts the top of the VREF curve to the highest possible temperature, and the value 11111111 shifts the top of the VREF curve to the lowest possible temperature. 123 2548F-AVR-03/2013 Figure 21-2. Illustration of VREF as a function of temperature. 1.5 BGCRR is used to move the top of the VREF curve to the center of the tempearture range of interest. Temperature range of interest VREF [V] 1 0.5 0 -40 -20 0 20 40 60 80 100 Temperature [o C] 124 ATmega406 2548F-AVR-03/2013 ATmega406 22. Battery Protection 22.1 Features * * * * * * Deep Under-voltage Protection Charge Over-current Protection Discharge Over-current Protection Short-circuit Protection Programmable and Lockable Detection Levels and Reaction Times Autonomous Operation Independent of CPU If the voltage at the VFET pin falls below the programmable Deep Under-voltage detection level, C-FET, PC-FET, and D-FET are disabled and the chip is set in Power-off mode to reduce power consumption to a minimum. The Current Battery Protection circuitry (CBP) monitors the charge and discharge current and disables C-FET, PC-FET, and D-FET if an over-current or short-circuit condition is detected. There are three different programmable detection levels: Discharge Over-current Detection Level, Charge Over-current Detection Level and Short-circuit Detection Level. The external filter at the PI/NI input pins will cause too large delay for short-circuit detection. Therefore the separate PPI/NNI inputs are used for Current Battery Protection. There are two different programmable delays for activating Current Battery Protection: Short-circuit Reaction Time and Over-current Reaction Time. After Current Battery Protection has been activated, the application software must re-enable the FETs. The Battery Protection hardware provides a hold-off time of 1 second before software can re-enable the discharge FET. This provides safety in case the application software should unintentionally re-enable the discharge FET too early. The activation of a protection also issues an interrupt to the CPU. The battery protection interrupts can be individually enabled and disabled by the CPU. The effect of the various battery protection types is given in Table 22-1. Table 22-1. Effect of Battery Protection Types Battery Protection Type Interrupt Requests C-FET D-FET PC-FET Cell Balancing FETs MCU Deep Under-voltage Detected CPU Reset on exit Disabled Disabled Disabled Disabled Power-off Discharge Over-current Protection Entry and exit Disabled Disabled Disabled Operational Operational Charge Over-current Protection Entry and exit Disabled Disabled Disabled Operational Operational Short-circuit Protection Entry and exit Disabled Disabled Disabled Operational Operational In order to reduce power consumption, both Short-circuit and Discharge Over-current Protection are automatically deactivated when the D-FET is disabled. The Charge Over-current Protection is disabled when both the C-FET and the PC-FET are disabled. Note however that Charge Overcurrent Protection is never automatically disabled when any of the C-FET or PC-FETs are controlled by PWM. 125 2548F-AVR-03/2013 22.2 Deep Under-voltage Protection The Deep Under-voltage Protection ensures that the battery cells will not be discharged deeper than the programmable Deep Under-voltage detection level. If the voltage at the VFET pin is below this level for a time longer than the programmable delay time, C-FET, PC-FET and D-FET are automatically switched off and the chip enters Power-off mode. The Deep Under-voltage Early Warning interrupt flag (DUVIF) will be set 250 ms before the chip enters Power-off. This will give the CPU a chance to take necessary actions before the power is switched off. The device will remain in the Power-off mode until a charger is connected. When a charger is detected, a normal power-up sequence is started and the chip initializes to default state. The Deep Under-voltage delay time and Deep Under-voltage detection level are set in the Battery Protection Deep Under-voltage Register (BPDUV). The Parameter Registers can be locked after the initial configuration, prohibiting any further updates until the next Hardware Reset. Refer to "Register Description for Battery Protection" on page 128 for register descriptions. 22.3 Discharge Over-current Protection The Current Battery Protection (CBP) monitors the cell current by sampling the shunt resistor voltage at the PPI/NNI input pins. A differential operational amplifier amplifies the voltage with a suitable gain. The output from the operational amplifier is compared to an accurate, programmable On-chip voltage reference by an Analog Comparator. If the shunt resistor voltage is above the Discharge Over-current Detection level for a time longer than Over-current Protection Reaction Time, the chip activates Discharge Over-current Protection. A sampled system clocked by the internal ULP Oscillator is used for Over-current and Short-circuit Protection. This ensures a reliable clock source, off-set cancellation and low power consumption. When the Discharge Over-current Protection is activated, the external D-FET, PC-FET, and CFET are disabled and a Current Protection Timer is started. This timer ensures that the FETs are disabled for at least one second. The application software must then set the DFE and CFE bits in the FET Control and Status Register to re-enable normal operation. If the D-FET is re-enabled while the loading of the battery still is too large, the Discharge Over-current Protection will be activated again. 22.4 Charge Over-current Protection If the voltage at the PPI/NNI pins is above the Charge Over-current Detection level for a time longer than Over-current Protection Reaction Time, the chip activates Charge Over-current Protection. When the Charge Over-current Protection is activated, the external D-FET, PC-FET, and C-FET are disabled and a Current Protection Timer is started. This timer ensures that the FETs are disabled for at least one second. The application software must then set the DFE and CFE bits in the FET Control and Status Register to re-enable normal operation. If the C-FET is re-enabled and the charger continues to supply too high currents, the Charge Over-current Protection will be activated again. 126 ATmega406 2548F-AVR-03/2013 ATmega406 22.5 Short-circuit Protection A second level of high current detection is provided to enable a faster response time to very large discharge currents. If a discharge current larger than the Short-circuit Detection Level is present for a period longer than Short-circuit Reaction Time, the Short-circuit Protection is activated. When the Short-circuit Protection is activated, the external D-FET, PC-FET, and C-FET are disabled and a Current Protection Timer is started. This timer ensures that the D-FET, PC-FET, and C-FET are disabled for at least one second. The application software must then set the DFE and CFE bits in the FET Control and Status Register to re-enable normal operation. If the D-FET is re-enabled before the cause of the short-circuit condition is removed, the Short-circuit Protection will be activated again. The Over-current and Short-circuit Protection parameters are programmable to adapt to different types of batteries. The parameters are set by writing to I/O Registers. The Parameter Registers can be locked after the initial configuration, prohibiting any further updates until the next Hardware Reset. Refer to "Register Description for Battery Protection" on page 128 for register descriptions. 22.6 Battery Protection CPU Interface The Battery Protection CPU Interface is illustrated in Figure 22-1. Figure 22-1. Battery Protection CPU Interface 8-BIT DATA BUS Battery Protection Parameter Lock Register LOCK? LOCK? LOCK? 8 / Interrupt Request Battery Protection Level Register Battery Protection Timing Register Battery Protection Control Register Interrupt Acknowledge 4 / PPI NNI Current Battery Protection VFET Voltage Battery Protection Battery Protection Interrupt Register 4 / Current Protection FET Control Deep Under-voltage Power-off Each protection has an Interrupt Flag. Each Flag can be read and cleared by the CPU, and each flag has an individual interrupt enable. All enabled flags are combined into a single battery protection interrupt request to the CPU. This interrupt can wake up the CPU from any operation mode, except Power-off. The interrupt flags are cleared by writing a logic `1' to their bit locations from the CPU. Note that there are neither flags nor status bits indicating that the chip has entered the Power Off mode. This is because the CPU is powered down in this mode. The CPU will, however be able 127 2548F-AVR-03/2013 to detect that it came from a Power-off situation by monitoring CPU reset flags when it resumes operation. 22.7 Register Description for Battery Protection The Battery Protection module operates in a different clock domain than the CPU. Whenever a new value is written to BPCR, BPDUV, BPOCD, BPSCD, or CPBTR, the value must be synchronized to the Battery Protection clock domain. Subsequent writes to this register should not be made during this synchronization. Therefore, after writing to one of these registers, the same register should not be re-written within the next 8 CPU clock periods. Note that each register is synchronized independently of the others. 22.7.1 BPPLR - Battery Protection Parameter Lock Register Bit 7 6 5 4 3 2 1 0 (0xF8) - - - - - - BPPLE BPPL Read/Write R R R R R R R/W R/W Initial Value 0 0 0 0 0 0 0 0 BPPLR * Bit 7:2 - Res: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bit 1 - BPPLE: Battery Protection Parameter Lock Enable * Bit 0 - BPPL: Battery Protection Parameter Lock The Battery Protection parameters set in the Battery Protection Parameter Registers and the disable function set in the Battery Protection Disable Register can be locked from any further software updates. Once locked, these registers cannot be accessed until the next hardware reset. This provides a safe method for protecting these registers from unintentional modification by software runaway. It is recommended that software sets these registers shortly after reset, and then protects these registers from any further updates. To lock these registers, the following algorithm must be followed: 1. In the same operation, write a logic one to BPPLE and BPPL. 2. Within the next four clock cycles, in the same operation. write a logic zero to BPPLE and a logic one to BPPL. The Battery Protection Parameter Registers are BPCR, CBPTR, BPOCP, BPSCD and BPDUV. 22.7.2 BPCR - Battery Protection Control Register Bit 7 6 5 4 3 2 1 0 (0xF7) - - - - DUVD SCD DCD CCD Read/Write R R R R R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 BPCR * Bit 7:4 - Res: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bit 3 - DUVD: Deep Under-voltage Protection Disable 128 ATmega406 2548F-AVR-03/2013 ATmega406 When the DUVD bit is set, the Deep Under-voltage Protection is disabled. The Deep Under-voltage Detection will be disabled, and any Deep Under-voltage condition will be ignored * Bit 2 - SCD: Short Circuit Protection Disabled When the SCD bit is set, the Short-circuit Protection is disabled. The Short-circuit Detection will be disabled, and any Short-circuit condition will be ignored. * Bit 1 - DCD: Discharge Over-current Protection Disable When the DCD bit is set, the Discharge Over-current Protection is disabled. The Discharge Over-current Detection will be disabled, and any Discharge Over-current condition will be ignored. * Bit 0 - CCD: Charge Over-current Protection Disable When the CCD bit is set, the Charge Over-current Protection is disabled. The Charge Over-current Detection will be disabled, and any Charge Over-current condition will be ignored. 22.7.3 CBPTR - Current Battery Protection Timing Register Bit 7 6 (0xF6) 5 4 3 2 SCPT[3:0] 1 0 OCPT[3:0] CBPTR Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 * Bit 7:4 - SCPT3:0: Short-circuit Protection Timing These bits control the delay of the Short-circuit Protection. See Table 22-2. Table 22-2. SCPT[3:0] with Corresponding Short-circuit Delay Time Short-circuit Protection Reaction Time SCPT[3:0] Typ SCPT[3:0] Typ SCPT[3:0] Typ SCPT[3:0] Typ 0000 61 s 0100 305 s 1000 610 s 1100 1098 s 0001 122 s 0101 366 s 1001 732 s 1101 1220 s 0010 183 s 0110 427 s 1010 854 s 1110 1342 s 0011 244 s 0111 488 s 1011 976 s 1111 1464 s * Bit 3:0 - OCPT3:0: Over-current Protection Timing These bits control the delay of the Charge and Discharge Current Protection. See Table 22-3. Note that the same setting applies to both types of over-current protection. Table 22-3. OCPT[3:0] with Corresponding Over-current Delay Time Over-current Protection Reaction Time OCPT[3:0] Typ OCPT[3:0] Typ OCPT[3:0] Typ OCPT[3:0] Typ 0000 1 ms 0100 8 ms 1000 16 ms 1100 24 ms 0001 2 ms 0101 10 ms 1001 18 ms 1101 26 ms 0010 4 ms 0110 12 ms 1010 20 ms 1110 28 ms 0011 6 ms 0111 14 ms 1011 22 ms 1111 30 ms 129 2548F-AVR-03/2013 22.7.4 BPOCD - Battery Protection Over-current Detection Level Register Bit 7 6 (0xF5) 5 4 3 2 DCDL[3:0] 1 0 CCDL[3:0] BPOCD Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 * Bits 7:4 - DCDL3:0: Discharge Over-current Detection Level These bits set the RSENSE voltage level for detection of Discharge Over-current, as defined in Table 22-4. Table 22-4. DCDL[3:0] with Corresponding RSENSE Voltage for Discharge Over-current Detection Level Discharge Over-current Protection Detection Level DCDL[3:0] Typ DCDL[3:0] Typ DCDL[3:0] Typ DCDL[3:0] Typ 0000 0.050V 0100 0.070V 1000 0.110V 1100 0.160V 0001 0.055V 0101 0.080V 1001 0.120V 1101 0.180V 0010 0.060V 0110 0.090V 1010 0.130V 1110 0.200V 0011 0.065V 0111 0.100V 1011 0.140V 1111 0.220V * Bits 3:0 - CCDL3:0: Charge Over-current Detection Level These bits set the RSENSE voltage level for detection of Charge Over-current, as defined in Table 22-5. Table 22-5. CCDL[3:0] with Corresponding RSENSE Voltage for Charge Over-current Detection Level Charge Over-current Protection Detection Level 22.7.5 CCDL[3:0] Typ CCDL[3:0] Typ CCDL[3:0] Typ CCDL[3:0] Typ 0000 0.050V 0100 0.070V 1000 0.110V 1100 0.160V 0001 0.055V 0101 0.080V 1001 0.120V 1101 0.180V 0010 0.060V 0110 0.090V 1010 0.130V 1110 0.200V 0011 0.065V 0111 0.100V 1011 0.140V 1111 0.220V BPSCD - Battery Protection Short-circuit Detection Level Register Bit 7 6 5 4 (0xF4) - - - - 3 Read/Write R R R R R/W Initial Value 0 0 0 0 0 2 1 0 R/W R/W R/W 0 0 0 SCDL[3:0] BPSCD * Bit 7:4 - Res: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bits 3:0 - SCDL3:0: Short-circuit Detection Level These bits set the RSENSE voltage level for detection of Short-circuit in the discharge direction, as defined in Table 22-6 on page 131. 130 ATmega406 2548F-AVR-03/2013 ATmega406 Table 22-6. SCDL[3:0] with Corresponding RSENSE Voltage for Short-circuit Detection Level Short-circuit Protection Detection Level 22.7.6 SCDL[3:0] Typ SCDL[3:0] Typ SCDL[3:0] Typ SCDL[3:0] Typ 0000 0.100V 0100 0.140V 1000 0.220V 1100 0.320V 0001 0.110V 0101 0.160V 1001 0.240V 1101 0.360V 0010 0.120V 0110 0.180V 1010 0.260V 1110 0.400V 0011 0.130V 0111 0.200V 1011 0.280V 1111 0.440V BPDUV - Battery Protection Deep Under Voltage Register Bit 7 6 (0xF3) - - 5 4 3 Read/Write R R R/W R/W R/W Initial Value 0 0 0 0 0 2 1 0 R/W R/W R/W 0 0 0 DUVT[1:0] DUDL[3:0] BPDUV * Bit 7:6 - Res: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bits 5:4 - DUVT1:0: Deep Under-voltage Timing These bits set the Deep Under-voltage Protection delay. Table 22-7. DUVT[1:0] with Corresponding Deep Under-voltage Delay DUVT1:0 Deep Under-voltage Delay 00 750 ms 01 1000 ms 10 1250 ms 11 1500 ms * Bits 3:0 - DUDL3:0: Deep Under-voltage Detection Level These bits set the Deep Under-voltage detection level. Table 22-8. DUDL[3:0] with Corresponding Deep Under-voltage Detection Level DUDL[3:0] Typ DUDL[3:0] Typ 0000 4.71V 1000 7.23V 0001 5.03V 1001 7.54V 0010 5.34V 1010 7.86V 0011 5.66V 1011 8.17V 0100 5.97V 1100 8.49V 0101 6.29V 1101 8.80V 0110 6.60V 1110 9.11V 0111 6.91V 1111 9.43V 131 2548F-AVR-03/2013 22.7.7 BPIR - Battery Protection Interrupt Register Bit 7 6 5 4 3 2 1 0 DUVIF COCIF DOCIF SCIF DUVIE COCIE DOCIE SCIE Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 (0xF2) BPIR * Bit 7 - DUVIF: Deep Under-voltage Early Warning Interrupt Flag If the voltage at VFET pin is below the Deep Under-voltage detection level and only 250 ms is left of the Deep Under-voltage delay, DUVIF becomes set. The flag must be cleared by writing a logical one to it. * Bit 6 - COCIF: Charge Over-current Protection Activated Interrupt Flag When the Charge Over-current Protection is activated, COCIF becomes set. The flag must be cleared by writing a logical one to it. * Bit 5 - DOCIF: Discharge Over-current Protection Activated Interrupt Flag When the Discharge Over-current Protection is activated, DOCIF becomes set. The flag must be cleared by writing a logical one to it. * Bit 4 - SCIF: Short-circuit Protection Activated Interrupt Flag When the Short-circuit Protection is activated, SCIF becomes set. The flag must be cleared by writing a logical one to it. * Bit 3 - DUVIE: Deep Under-voltage Early Warning Interrupt Enable The DUVIE bit enables interrupt caused by the Deep Under-voltage Early Warning Interrupt Flag * Bit 2 - COCIE: Charge Over-current Protection Activated Interrupt Enable The COCIE bit enables interrupt caused by the Charge Over-current Protection Activated Interrupt Flag. * Bit 1 - DOCIE: Discharge Over-current Protection Activated Interrupt Enable The DOCIE bit enables interrupt caused by the Discharge Over-current Protection Activated Interrupt Flag. * Bit 0 - SCIE: Short-circuit Protection Activated Interrupt Enable The SCIE bit enables interrupt caused by the Short-circuit Protection Activated Interrupt Flag. If one of the Battery Protection Interrupt Flags is set, and the corresponding Interrupt Enable bit and the I-bit in the Status Register (SREG) are set, the MCU will jump to the Battery Protection interrupt vector. The application software must read the Battery Protection Interrupt Register to determine the cause of the interrupt. The interrupt flags will not be cleared when the interrupt routine is executed, they must be cleared by writing a logical one to them. 132 ATmega406 2548F-AVR-03/2013 ATmega406 23. FET Control In addition to the FET disable control signals from the battery protection circuitry, the CPU may disable the Charge FET (C-FET), the Discharge FET (D-FET), or both, by writing to the FET Control Register. Note that the CPU is never allowed to enable a FET that is disabled by the battery protection circuitry. The FET control is shown in Figure 23-1 on page 133. The PWM output from the 8-bit Timer/Counter0, OC0B, can be configured to drive the C-FET, Precharge FET (PC-FET) or both directly. This can be useful for controlling the charging of the battery cells. The PWM is configured by the COM0B1:0 and WGM02:0 bits in the TCCR0A/TCCR0B registers. Note that the OC0B pins does not need to be configured as an output. This means that the PWM output can be used to drive the C-FET and/or the PC-FET without occupying the OC0B-pin. If C-FET is disabled and D-FET enabled, discharge current will run through the body-drain diode of the C-FET and vice versa. To avoid the potential heat problem from this situation, software must ensure that D-FET is not disabled when a charge current is flowing, and that C-FET is not disabled when a discharge current is flowing. If the battery has been deeply discharged, large surge currents may result when a charger is connected. In this case, it is recommended to first pre charge the battery through a current limiting resistor. For this purpose, ATmega406 provides a Precharge FET (PC-FET) control output. This output is default enabled. If ATmega406 has entered the Power-off mode, all FET control outputs will be disabled. When a charger is connected, the CPU will wake up. When waking up from Power-off mode, the C-FET and D-FET control outputs will remain disabled while PC-FET is default enabled. When the CPU detects that the cell voltages have risen enough to allow normal charging, it should enable the C-FET and D-FET control outputs and disable the PC-FET control output. If the Current Battery Protection has been activated, the Current Protection Timer will ensure a hold-off time of 1 second before software can re-enable the external FETs. Figure 23-1. FET Control Block Diagram Power-off Mode CURRENT_PROTECTION Current Protection Timer OC0B PWMOC 1 8-BIT D ATA BU S CFE FET Control and Status Register 0 FET Driver OC FET Driver OPC FET Driver OD PWMOPC 1 PFD DFE 0 133 2548F-AVR-03/2013 23.1 FET Driver Figure 23-2. Connection of external FETs Rpc Rpf + Rdf Rcf RN PVT OD OC OPC BATT The connection of external FETs to OD, OC, and OPC is shown in Figure 23-2. When switching on an FET, the output pulls the gate quickly low to avoid heating of the FET. When the FET is switched completely on, the output changes operation mode in order to reduce current consumption. The gate-source voltage for the FET when switched on, |VGS_ON|, is limited to 13V 15%. When disabling an external FET, the FET Driver output quickly pushes the gate voltage to the source pin potential, making the gate-source voltage of the FET close to zero. This disables the FET, and the FET Driver output switches operation mode to high impedance in order to reduce current consumption. The external resistor will keep the gate-source voltage at zero until the FET is enabled again and its gate is pulled low as explained above. 23.2 Register Description for FET Control The FET Controller operates in a different clock domain than the CPU. Whenever a new value is written to the FCSR, the value must be synchronized to the FET Controller clock domain. Subsequent writes to this register should not be made during this synchronization. Therefore, after writing to this register, a guard time of 3 ULP Oscillator cycles + 3 CPU clock cycles is required. It is recommended that software only reads the FCSR when handling a Battery Protection Interrupt (BPINT). 23.2.1 FCSR - FET Control and Status Register Bit 7 6 5 4 3 2 1 0 (0xF0) - - PWMOC PWMOPC CPS DFE CFE PFD Read/Write R R R/W R/W R R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 FCSR * Bits 7:6 - Res: Reserved Bits These bits are reserved bits in the ATmega406, and will always read as zero. * Bit 5 - PWMOC: Pulse Width Modulation of OC output 134 ATmega406 2548F-AVR-03/2013 ATmega406 When the PWMOC is cleared (zero), the CFE bit and the battery protection circuitry controls the OC output. When this bit is set (one), the OC output will be the logical AND of the PWM output from the 8-bit Timer/Counter0 and the inverse of CURRENT_PROTECTION from the Battery Protection circuitry. * Bit 4 - PWMOPC: Pulse Width Modulation of OPC output When the PWMOPC is cleared (zero), the PFD bit and the battery protection circuitry controls the OPC output. When this bit is set (one), the OPC output will be the logical AND of the PWM output from the 8-bit Timer/Counter0 and the inverse of CURRENT_PROTECTION from the Battery Protection circuitry. * Bit 3 - CPS: Current Protection Status The CPS bit shows the status of the Current Protection. This bit is set (one) when the Current Protection Timer is activated, and is cleared (zero) when the hold-off time has elapsed. * Bit 2 - DFE: Discharge FET Enable When the DFE bit is cleared (zero), the Discharge FET will be disabled regardless of the state of the Battery Protection circuitry. When this bit is set (one), the Discharge FET state is determined by the Battery Protection circuitry. This bit will be cleared when CURRENT_PROTECTION is set (one). * Bit 1 - CFE: Charge FET Enable When the CFE bit is cleared (zero), the Charge FET will be disabled regardless of the state of the Battery Protection circuitry. When this bit is set (one), the Charge FET state is determined by the Battery Protection circuitry. This bit will be cleared when CURRENT_PROTECTION is set (one). * Bit 0 - PFD: Precharge FET Disable The PFD bit provides complete control of the Precharge FET. When the PFD bit is cleared (zero), the Precharge FET will be enabled. When the PFD bit is cleared, the Precharge FET will be enabled. When the PFD bit is set (one), the Precharge FET will be disabled. This bit will be cleared when the CURRENT_PROTECTION is set (one) 135 2548F-AVR-03/2013 24. Cell Balancing ATmega406 incorporates cell balancing FETs. The chip provides one cell balancing FET for each battery cell in series. The FETs are directly controlled by the application software, allowing the cell balancing algorithms to be implemented in software. The FETs are connected in parallel with the individual battery cells. The cell balancing is illustrated in Figure 24-1. The figure shows a four-cell configuration. The cell balancing FETs are disabled in the Power-off mode. Typical current through the Cell Balancing FETs (TCB) is 2 mA. The Cell Balancing FETs are controlled by the CBCR. Neighbouring FETs cannot be simultaneously enabled. If trying to enable two neighbouring FETs, both will be disabled. Figure 24-1. Cell Balancing PV4 RP Level Shift 8-BIT DATA BUS TCB PV3 RP TCB Level Shift Cell Balancing Control Register PV2 RP TCB Level Shift PV1 RP Level Shift TCB RP 136 NV ATmega406 2548F-AVR-03/2013 ATmega406 24.1 24.1.1 Register Description CBCR - Cell Balancing Control Register Bit 7 6 5 4 3 2 1 0 (0xF1) - - - - CBE4 CBE3 CBE2 CBE1 Read/Write R R R R R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 CBCR * Bit 7:4 - Res: Reserved Bits These bits are reserved bits in the ATmega406 and will always read as zero. * Bit 3 - CBE4: Cell Balancing Enable 4 When this bit is set, the integrated Cell Balancing FET between terminals PV4 and PV3 will be enabled. When the bit is cleared, the Cell Balancing FET will be disabled. The Cell Balancing FETs are always disabled in Power-off mode. CBE4 cannot be set if CBE3 is set. * Bit 2 - CBE3: Cell Balancing Enable 3 When this bit is set, the integrated Cell Balancing FET between terminals PV3 and PV2 will be enabled. When the bit is cleared, the Cell Balancing FET will be disabled. The Cell Balancing FETs are always disabled in Power-off mode. CBE3 cannot be set if CBE2 or CBE4 is set. * Bit 1 - CBE2: Cell Balancing Enable 2 When this bit is set, the integrated Cell Balancing FET between terminals PV2 and PV1 will be enabled. When the bit is cleared, the Cell Balancing FET will be disabled. The Cell Balancing FETs are always disabled in Power-off mode. CBE2 cannot be set if CBE1 or CBE3 is set. * Bit 0 - CBE1: Cell Balancing Enable 1 When this bit is set (one), the integrated Cell Balancing FET between terminals PV1 and NV will be enabled. When the bit is cleared (zero), the Cell Balancing FET will be disabled. The Cell Balancing FETs are always disabled in Power-off mode. CBE1 cannot be set if CBE2 is set. 137 2548F-AVR-03/2013 25. 2-wire Serial Interface 25.1 Features * * * * * * * * * * 25.2 Simple yet Powerful and Flexible Communication Interface, Only Two Bus Lines Needed Both Master and Slave Operation Supported Device can Operate as Transmitter or Receiver 7-bit Address Space allows up to 128 Different Slave Addresses Multi-master Arbitration Support Operates on 4 MHz Clock, achieving up to 100 kHz Data Transfer Speed Slew-rate Limited Output Drivers Noise Suppression Circuitry Rejects Spikes on Bus Lines Fully Programmable Slave Address with General Call Support Address Recognition Causes Wake-up when AVR is in Sleep Mode Two-wire Serial Interface Bus Definition The Two-wire Serial Interface (TWI) is ideally suited for typical microcontroller applications. The TWI protocol allows the systems designer to interconnect up to 128 different devices using only two bi-directional bus lines, one for clock (SCL) and one for data (SDA). The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol. The PRTWI bit in "PRR0 - Power Reduction Register 0" on page 36 must be written to zero to enable TWI module. Figure 25-1. TWI Bus Interconnection VBUS Device 1 Device 2 Device 3 ........ Device n R1 R2 SDA SCL 138 ATmega406 2548F-AVR-03/2013 ATmega406 25.2.1 TWI Terminology The following definitions are frequently encountered in this section. Table 25-1. 25.2.2 TWI Terminology Term Description Master The device that initiates and terminates a transmission. The Master also generates the SCL clock. Slave The device addressed by a Master. Transmitter The device placing data on the bus. Receiver The device reading data from the bus. Electrical Interconnection As depicted in Figure 25-1, both bus lines are connected to the positive supply voltage through pull-up resistors. The bus drivers of all TWI-compliant devices are open-drain or open-collector. This implements a wired-AND function which is essential to the operation of the interface. A low level on a TWI bus line is generated when one or more TWI devices output a zero. A high level is output when all TWI devices tri-state their outputs, allowing the pull-up resistors to pull the line high. Note that all AVR devices connected to the TWI bus must be powered in order to allow any bus operation. The number of devices that can be connected to the bus is only limited by the bus capacitance limit of 400 pF and the 7-bit slave address space. A detailed specification of the electrical characteristics of the TWI is given in "2-wire Serial Interface Characteristics" on page 229. 25.3 25.3.1 Data Transfer and Frame Format Transferring Bits Each data bit transferred on the TWI bus is accompanied by a pulse on the clock line. The level of the data line must be stable when the clock line is high. The only exception to this rule is for generating start and stop conditions. Figure 25-2. Data Validity SDA SCL Data Stable Data Stable Data Change 139 2548F-AVR-03/2013 25.3.2 START and STOP Conditions The Master initiates and terminates a data transmission. The transmission is initiated when the Master issues a START condition on the bus, and it is terminated when the Master issues a STOP condition. Between a START and a STOP condition, the bus is considered busy, and no other Master should try to seize control of the bus. A special case occurs when a new START condition is issued between a START and STOP condition. This is referred to as a REPEATED START condition, and is used when the Master wishes to initiate a new transfer without relinquishing control of the bus. After a REPEATED START, the bus is considered busy until the next STOP. This is identical to the START behavior, and therefore START is used to describe both START and REPEATED START for the remainder of this datasheet, unless otherwise noted. As depicted below, START and STOP conditions are signalled by changing the level of the SDA line when the SCL line is high. Figure 25-3. START, REPEATED START, and STOP Conditions SDA SCL START 25.3.3 STOP START REPEATED START STOP Address Packet Format All address packets transmitted on the TWI bus are nine bits long, consisting of seven address bits, one READ/WRITE control bit and an acknowledge bit. If the READ/WRITE bit is set, a read operation is to be performed, otherwise a write operation should be performed. When a slave recognizes that it is being addressed, it should acknowledge by pulling SDA low in the ninth SCL (ACK) cycle. If the addressed Slave is busy, or for some other reason can not service the Master's request, the SDA line should be left high in the ACK clock cycle. The Master can then transmit a STOP condition, or a REPEATED START condition to initiate a new transmission. An address packet consisting of a slave address and a READ or a WRITE bit is called SLA+R or SLA+W, respectively. The MSB of the address byte is transmitted first. Slave addresses can freely be allocated by the designer, but the address 0000 000 is reserved for a general call. When a general call is issued, all slaves should respond by pulling the SDA line low in the ACK cycle. A general call is used when a Master wishes to transmit the same message to several slaves in the system. When the general call address followed by a write bit is transmitted on the bus, all slaves set up to acknowledge the general call will pull the SDA line low in the ack cycle. The following data packets will then be received by all the slaves that acknowledged the general call. Note that transmitting the general call address followed by a Read bit is meaningless, as this would cause contention if several slaves started transmitting different data. All addresses of the format 1111 xxx should be reserved for future purposes. 140 ATmega406 2548F-AVR-03/2013 ATmega406 Figure 25-4. Address Packet Format Addr MSB Addr LSB R/W ACK 7 8 9 SDA SCL 1 2 START 25.3.4 Data Packet Format All data packets transmitted on the TWI bus are nine bits long, consisting of one data byte and an acknowledge bit. During a data transfer, the Master generates the clock and the START and STOP conditions, while the Receiver is responsible for acknowledging the reception. An Acknowledge (ACK) is signalled by the Receiver pulling the SDA line low during the ninth SCL cycle. If the Receiver leaves the SDA line high, a NACK is signalled. When the Receiver has received the last byte, or for some reason cannot receive any more bytes, it should inform the Transmitter by sending a NACK after the final byte. The MSB of the data byte is transmitted first. Figure 25-5. Data Packet Format Data MSB Data LSB ACK 8 9 Aggregate SDA SDA from Transmitter SDA from Receiver SCL from Master 1 SLA+R/W 25.3.5 2 7 Data Byte STOP, REPEATED START, or Next Data Byte Combining Address and Data Packets Into a Transmission A transmission basically consists of a START condition, a SLA+R/W, one or more data packets and a STOP condition. An empty message, consisting of a START followed by a STOP condition, is illegal. Note that the wired-ANDing of the SCL line can be used to implement handshaking between the Master and the Slave. The Slave can extend the SCL low period by pulling the SCL line low. This is useful if the clock speed set up by the Master is too fast for the Slave, or the Slave needs extra time for processing between the data transmissions. The Slave extending the SCL low period will not affect the SCL high period, which is determined by the Master. As a consequence, the Slave can reduce the TWI data transfer speed by prolonging the SCL duty cycle. Figure 25-6 shows a typical data transmission. Note that several data bytes can be transmitted between the SLA+R/W and the STOP condition, depending on the software protocol implemented by the application software. 141 2548F-AVR-03/2013 Figure 25-6. Typical Data Transmission Addr MSB Addr LSB R/W ACK Data MSB 7 8 9 1 Data LSB ACK 8 9 SDA SCL 1 START 25.4 2 SLA+R/W 2 7 Data Byte STOP Multi-master Bus Systems, Arbitration and Synchronization The TWI protocol allows bus systems with several masters. Special concerns have been taken in order to ensure that transmissions will proceed as normal, even if two or more masters initiate a transmission at the same time. Two problems arise in multi-master systems: * An algorithm must be implemented allowing only one of the masters to complete the transmission. All other masters should cease transmission when they discover that they have lost the selection process. This selection process is called arbitration. When a contending master discovers that it has lost the arbitration process, it should immediately switch to Slave mode to check whether it is being addressed by the winning master. The fact that multiple masters have started transmission at the same time should not be detectable to the slaves (i.e., the data being transferred on the bus must not be corrupted). * Different masters may use different SCL frequencies. A scheme must be devised to synchronize the serial clocks from all masters, in order to let the transmission proceed in a lockstep fashion. This will facilitate the arbitration process. The wired-ANDing of the bus lines is used to solve both these problems. The serial clocks from all masters will be wired-ANDed, yielding a combined clock with a high period equal to the one from the master with the shortest high period. The low period of the combined clock is equal to the low period of the master with the longest low period. Note that all masters listen to the SCL line, effectively starting to count their SCL high and low Time-out periods when the combined SCL line goes high or low, respectively. 142 ATmega406 2548F-AVR-03/2013 ATmega406 Figure 25-7. SCL Synchronization between Multiple Masters TA low TA high SCL from Master A SCL from Master B SCL bus Line TB low Masters Start Counting Low Period TB high Masters Start Counting High Period Arbitration is carried out by all masters continuously monitoring the SDA line after outputting data. If the value read from the SDA line does not match the value the master had output, it has lost the arbitration. Note that a master can only lose arbitration when it outputs a high SDA value while another master outputs a low value. The losing master should immediately go to Slave mode, checking if it is being addressed by the winning master. The SDA line should be left high, but losing masters are allowed to generate a clock signal until the end of the current data or address packet. Arbitration will continue until only one master remains, and this may take many bits. If several masters are trying to address the same slave, arbitration will continue into the data packet. Figure 25-8. Arbitration between Two Masters START SDA from Master A Master A Loses Arbitration, SDA A SDA SDA from Master B SDA Line Synchronized SCL Line Note that arbitration is not allowed between: * A REPEATED START condition and a data bit. * A STOP condition and a data bit. * A REPEATED START and a STOP condition. 143 2548F-AVR-03/2013 It is the user software's responsibility to ensure that these illegal arbitration conditions never occur. This implies that in multi-master systems, all data transfers must use the same composition of SLA+R/W and data packets. In other words: All transmissions must contain the same number of data packets, otherwise the result of the arbitration is undefined. 25.5 Overview of the TWI Module The TWI module is comprised of several submodules, as shown in Figure 25-9. The shaded registers are accessible through the AVR data bus. Figure 25-9. Overview of the TWI Module Slew-rate Control SDA Spike Filter Slew-rate Control Spike Filter Bus Interface Unit START / STOP Control Spike Suppression Arbitration Detection Address/Data Shift Register (TWDR) Address Match Unit Address Register (TWAR) Address Comparator 25.5.1 144 Bit Rate Generator Prescaler Bit Rate Register (TWBR) Ack Control Unit Status Register (TWSR) Control Register (TWCR) TWI Unit SCL State Machine and Status Control SCL and SDA Pins These pins interface the AVR TWI with the rest of the MCU system. The output drivers contain a slew-rate limiter in order to conform to the TWI specification. The input stages contain a spike suppression unit removing spikes shorter than 50 ns. ATmega406 2548F-AVR-03/2013 ATmega406 25.5.2 Bit Rate Generator Unit This unit controls the period of SCL when operating in a Master mode. The SCL period is controlled by settings in the TWI Bit Rate Register (TWBR) and the Prescaler bits in the TWI Status Register (TWSR). Slave operation does not depend on Bit Rate or Prescaler settings, but the CPU clock frequency in the slave must be at least 16 times higher than the SCL frequency. Note that slaves may prolong the SCL low period, thereby reducing the average TWI bus clock period. The SCL frequency is generated according to the following equation: TWI Clock frequency SCL frequency = ----------------------------------------------------------TWPS 16 + 2(TWBR) 4 * TWBR = Value of the TWI Bit Rate Register. * TWPS = Value of the prescaler bits in the TWI Status Register. Notes: 25.5.3 1. TWBR should be 10 or higher if the TWI operates in Master mode. If TWBR is lower than 10, the master may produce an incorrect output on SDA and SCL for the reminder of the byte. The problem occurs when operating the TWI in Master mode, sending Start + SLA + R/W to a slave (a slave does not need to be connected to the bus for the condition to happen). 2. The TWI clock is 4 MHz, see "Calibrated Fast RC Oscillator" on page 26. Bus Interface Unit This unit contains the Data and Address Shift Register (TWDR), a START/STOP Controller and Arbitration detection hardware. The TWDR contains the address or data bytes to be transmitted, or the address or data bytes received. In addition to the 8-bit TWDR, the Bus Interface Unit also contains a register containing the (N)ACK bit to be transmitted or received. This (N)ACK Register is not directly accessible by the application software. However, when receiving, it can be set or cleared by manipulating the TWI Control Register (TWCR). When in Transmitter mode, the value of the received (N)ACK bit can be determined by the value in the TWSR. The START/STOP Controller is responsible for generation and detection of START, REPEATED START, and STOP conditions. The START/STOP controller is able to detect START and STOP conditions even when the AVR MCU is in one of the sleep modes, enabling the MCU to wake up if addressed by a Master. If the TWI has initiated a transmission as Master, the Arbitration Detection hardware continuously monitors the transmission trying to determine if arbitration is in process. If the TWI has lost an arbitration, the Control Unit is informed. Correct action can then be taken and appropriate status codes generated. 25.5.4 Address Match Unit The Address Match unit checks if received address bytes match the 7-bit address in the TWI Address Register (TWAR). If the TWI General Call Recognition Enable (TWGCE) bit in the TWAR is written to one, all incoming address bits will also be compared against the General Call address. Upon an address match, the Control unit is informed, allowing correct action to be taken. The TWI may or may not acknowledge its address, depending on settings in the TWCR. The Address Match unit is able to compare addresses even when the AVR MCU is in sleep mode, enabling the MCU to wake-up if addressed by a Master. 145 2548F-AVR-03/2013 25.5.5 Control Unit The Control unit monitors the TWI bus and generates responses corresponding to settings in the TWI Control Register (TWCR). When an event requiring the attention of the application occurs on the TWI bus, the TWI Interrupt Flag (TWINT) is asserted. In the next clock cycle, the TWI Status Register (TWSR) is updated with a status code identifying the event. The TWSR only contains relevant status information when the TWI interrupt flag is asserted. At all other times, the TWSR contains a special status code indicating that no relevant status information is available. As long as the TWINT flag is set, the SCL line is held low. This allows the application software to complete its tasks before allowing the TWI transmission to continue. The TWINT flag is set in the following situations: * After the TWI has transmitted a START/REPEATED START condition. * After the TWI has transmitted SLA+R/W. * After the TWI has transmitted an address byte. * After the TWI has lost arbitration. * After the TWI has been addressed by own slave address or general call. * After the TWI has received a data byte. * After a STOP or REPEATED START has been received while still addressed as a Slave. * When a bus error has occurred due to an illegal START or STOP condition. 146 ATmega406 2548F-AVR-03/2013 ATmega406 25.6 25.6.1 TWI Register Description TWBR - TWI Bit Rate Register Bit 7 6 5 4 3 2 1 0 TWBR7 TWBR6 TWBR5 TWBR4 TWBR3 TWBR2 TWBR1 TWBR0 Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 0 0 0 0 0 0 0 0 (0xB8) TWBR * Bits 7:0 - TWI Bit Rate Register TWBR selects the division factor for the bit rate generator. The bit rate generator is a frequency divider which generates the SCL clock frequency in the Master modes. See "Bit Rate Generator Unit" on page 145 for calculating bit rates. 25.6.2 TWCR - TWI Control Register Bit 7 6 5 4 3 2 1 0 TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE Read/Write R/W R/W R/W R/W R R/W R R/W Initial Value 0 0 0 0 0 0 0 0 (0xBC) TWCR The TWCR is used to control the operation of the TWI. It is used to enable the TWI, to initiate a Master access by applying a START condition to the bus, to generate a Receiver acknowledge, to generate a stop condition, and to control halting of the bus while the data to be written to the bus are written to the TWDR. It also indicates a write collision if data is attempted written to TWDR while the register is inaccessible. * Bit 7 - TWINT: TWI Interrupt Flag This bit is set by hardware when the TWI has finished its current job and expects application software response. If the I-bit in SREG and TWIE in TWCR are set, the MCU will jump to the TWI Interrupt Vector. While the TWINT flag is set, the SCL low period is stretched. The TWINT flag must be cleared by software by writing a logic one to it. Note that this flag is not automatically cleared by hardware when executing the interrupt routine. Also note that clearing this flag starts the operation of the TWI, so all accesses to the TWI Address Register (TWAR), TWI Status Register (TWSR), and TWI Data Register (TWDR) must be complete before clearing this flag. * Bit 6 - TWEA: TWI Enable Acknowledge Bit The TWEA bit controls the generation of the acknowledge pulse. If the TWEA bit is written to one, the ACK pulse is generated on the TWI bus if the following conditions are met: 1. The device's own slave address has been received. 2. A general call has been received, while the TWGCE bit in the TWAR is set. 3. A data byte has been received in Master Receiver or Slave Receiver mode. By writing the TWEA bit to zero, the device can be virtually disconnected from the Two-wire Serial Bus temporarily. Address recognition can then be resumed by writing the TWEA bit to one again. * Bit 5 - TWSTA: TWI START Condition Bit The application writes the TWSTA bit to one when it desires to become a Master on the Twowire Serial Bus. The TWI hardware checks if the bus is available, and generates a START con- 147 2548F-AVR-03/2013 dition on the bus if it is free. However, if the bus is not free, the TWI waits until a STOP condition is detected, and then generates a new START condition to claim the Bus Master status. TWSTA is cleared by the TWI hardware when the START condition has been transmitted. * Bit 4 - TWSTO: TWI STOP Condition Bit Writing the TWSTO bit to one in Master mode will generate a STOP condition on the Two-wire Serial Bus. When the STOP condition is executed on the bus, the TWSTO bit is cleared automatically. In Slave mode, setting the TWSTO bit can be used to recover from an error condition. This will not generate a STOP condition, but the TWI returns to a well-defined unaddressed Slave mode and releases the SCL and SDA lines to a high impedance state. * Bit 3 - TWWC: TWI Write Collision Flag The TWWC bit is set when attempting to write to the TWI Data Register - TWDR when TWINT is low. This flag is cleared by writing the TWDR Register when TWINT is high. * Bit 2 - TWEN: TWI Enable Bit The TWEN bit enables TWI operation and activates the TWI interface. When TWEN is written to one, the TWI takes control over the I/O pins connected to the SCL and SDA pins, enabling the slew-rate limiters and spike filters. If this bit is written to zero, the TWI is switched off and all TWI transmissions are terminated, regardless of any ongoing operation. * Bit 1 - Res: Reserved Bit This bit is a reserved bit and will always read as zero. * Bit 0 - TWIE: TWI Interrupt Enable When this bit is written to one, and the I-bit in SREG is set, the TWI interrupt request will be activated for as long as the TWINT flag is high. 25.6.3 TWSR - TWI Status Register Bit 7 6 5 4 3 2 1 0 TWS7 TWS6 TWS5 TWS4 TWS3 - TWPS1 TWPS0 Read/Write R R R R R R R/W R/W Initial Value 1 1 1 1 1 0 0 0 (0xB9) TWSR * Bits 7:3 - TWS: TWI Status These five bits reflect the status of the TWI logic and the Two-wire Serial Bus. The different status codes are described in Table 25-3 on page 156 through Table 25-6 on page 165. Note that the value read from TWSR contains both the 5-bit status value and the 2-bit prescaler value. The application designer should mask the prescaler bits to zero when checking the status bits. This makes status checking independent of prescaler setting. This approach is used in this datasheet, unless otherwise noted. * Bit 2 - Res: Reserved Bit This bit is reserved and will always read as zero. * Bits 1:0 - TWPS: TWI Prescaler Bits These bits can be read and written, and control the bit rate prescaler. 148 ATmega406 2548F-AVR-03/2013 ATmega406 Table 25-2. TWI Bit Rate Prescaler TWPS1 TWPS0 Prescaler Value 0 0 1 0 1 4 1 0 16 1 1 64 To calculate bit rates, see "Bit Rate Generator Unit" on page 145. The value of TWPS1:0 is used in the equation. 25.6.4 TWDR - TWI Data Register Bit 7 6 5 4 3 2 1 0 TWD7 TWD6 TWD5 TWD4 TWD3 TWD2 TWD1 TWD0 Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 1 1 1 1 1 1 1 1 (0xBB) TWDR In Transmit mode, TWDR contains the next byte to be transmitted. In Receive mode, the TWDR contains the last byte received. It is writable while the TWI is not in the process of shifting a byte. This occurs when the TWI Interrupt Flag (TWINT) is set by hardware. Note that the data register cannot be initialized by the user before the first interrupt occurs. The data in TWDR remains stable as long as TWINT is set. While data is shifted out, data on the bus is simultaneously shifted in. TWDR always contains the last byte present on the bus, except after a wake-up from a sleep mode by the TWI interrupt. In this case, the contents of TWDR is undefined. In the case of a lost bus arbitration, no data is lost in the transition from Master to Slave. Handling of the ACK bit is controlled automatically by the TWI logic, the CPU cannot access the ACK bit directly. * Bits 7:0 - TWD: TWI Data Register These eight bits constitute the next data byte to be transmitted, or the latest data byte received on the Two-wire Serial Bus. 25.6.5 TWAR - TWI (Slave) Address Register Bit 7 6 5 4 3 2 1 0 TWA6 TWA5 TWA4 TWA3 TWA2 TWA1 TWA0 TWGCE Read/Write R/W R/W R/W R/W R/W R/W R/W R/W Initial Value 1 1 1 1 1 1 1 0 (0xBA) TWAR The TWAR should be loaded with the 7-bit slave address (in the seven most significant bits of TWAR) to which the TWI will respond when programmed as a slave transmitter or Receiver, and not needed in the Master modes. In multi-master systems, TWAR must be set in masters which can be addressed as slaves by other masters. The LSB of TWAR is used to enable recognition of the general call address (0x00). There is an associated address comparator that looks for the slave address (or general call address if enabled) in the received serial address. If a match is found, an interrupt request is generated. * Bits 7:1 - TWA: TWI (Slave) Address Register These seven bits constitute the slave address of the TWI unit. 149 2548F-AVR-03/2013 * Bit 0 - TWGCE: TWI General Call Recognition Enable Bit If set, this bit enables the recognition of a General Call given over the Two-wire Serial Bus. 25.6.6 TWAMR - TWI (Slave) Address Mask Register Bit 7 6 5 (0xBD) 4 3 2 1 0 - TWAM[6:0] Read/Write R/W R/W R/W R/W R/W R/W R/W R Initial Value 0 0 0 0 0 0 0 0 TWAMR * Bits 7:1 - TWAM: TWI Address Mask The TWAMR can be loaded with a 7-bit Slave Address mask. Each of the bits in TWAMR can mask (disable) the corresponding address bits in the TWI Address Register (TWAR). If the mask bit is set to one then the address match logic ignores the compare between the incoming address bit and the corresponding bit in TWAR. Figure 25-10 shown the address match logic in detail. Figure 25-10. TWI Address Match Logic, Block Diagram TWAR0 Address Match Address Bit 0 TWAMR0 Address Bit Comparator 0 Address Bit Comparator 6..1 * Bit 0 - Res: Reserved Bit This bit is an unused bit in the ATmega406, and will always read as zero. 25.7 Using the TWI The AVR TWI is byte-oriented and interrupt based. Interrupts are issued after all bus events, like reception of a byte or transmission of a START condition. Because the TWI is interrupt-based, the application software is free to carry on other operations during a TWI byte transfer. Note that the TWI Interrupt Enable (TWIE) bit in TWCR together with the Global Interrupt Enable bit in SREG allow the application to decide whether or not assertion of the TWINT flag should generate an interrupt request. If the TWIE bit is cleared, the application must poll the TWINT flag in order to detect actions on the TWI bus. When the TWINT flag is asserted, the TWI has finished an operation and awaits application response. In this case, the TWI Status Register (TWSR) contains a value indicating the current state of the TWI bus. The application software can then decide how the TWI should behave in the next TWI bus cycle by manipulating the TWCR and TWDR registers. Figure 25-11 is a simple example of how the application can interface to the TWI hardware. In this example, a Master wishes to transmit a single data byte to a Slave. This description is quite abstract, a more detailed explanation follows later in this section. A simple code example implementing the desired behavior is also presented. 150 ATmega406 2548F-AVR-03/2013 ATmega406 Application Action Figure 25-11. Interfacing the Application to the TWI in a Typical Transmission 1. Application writes to TWCR to initiate transmission of START TWI Hardware Action TWI bus 3. Check TWSR to see if START was sent. Application loads SLA+W into TWDR, and loads appropriate control signals into TWCR, making sure that TWINT is written to one START SLA+W 2. TWINT set. Status code indicates START condition sent 5. Check TWSR to see if SLA+W was sent and ACK received. Application loads data into TWDR, and loads appropriate control signals into TWCR, making sure that TWINT is written to one A 4. TWINT set. Status code indicates SLA+W sent, ACK received Data 7. Check TWSR to see if data was sent and ACK received. Application loads appropriate control signals to send STOP into TWCR, making sure that TWINT is written to one A 6. TWINT set. Status code indicates data sent, ACK received STOP Indicates TWINT set 1. The first step in a TWI transmission is to transmit a START condition. This is done by writing a specific value into TWCR, instructing the TWI hardware to transmit a START condition. Which value to write is described later on. However, it is important that the TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI will not start any operation as long as the TWINT bit in TWCR is set. Immediately after the application has cleared TWINT, the TWI will initiate transmission of the START condition. 2. When the START condition has been transmitted, the TWINT flag in TWCR is set, and TWSR is updated with a status code indicating that the START condition has successfully been sent. 3. The application software should now examine the value of TWSR, to make sure that the START condition was successfully transmitted. If TWSR indicates otherwise, the application software might take some special action, like calling an error routine. Assuming that the status code is as expected, the application must load SLA+W into TWDR. Remember that TWDR is used both for address and data. After TWDR has been loaded with the desired SLA+W, a specific value must be written to TWCR, instructing the TWI hardware to transmit the SLA+W present in TWDR. Which value to write is described later on. However, it is important that the TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI will not start any operation as long as the TWINT bit in TWCR is set. Immediately after the application has cleared TWINT, the TWI will initiate transmission of the address packet. 4. When the address packet has been transmitted, the TWINT flag in TWCR is set, and TWSR is updated with a status code indicating that the address packet has successfully been sent. The status code will also reflect whether a slave acknowledged the packet or not. 5. The application software should now examine the value of TWSR, to make sure that the address packet was successfully transmitted, and that the value of the ACK bit was as expected. If TWSR indicates otherwise, the application software might take some special action, like calling an error routine. Assuming that the status code is as expected, the application must load a data packet into TWDR. Subsequently, a specific value must be written to TWCR, instructing the TWI hardware to transmit the data packet present in TWDR. Which value to write is described later on. However, it is important that the TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI will 151 2548F-AVR-03/2013 not start any operation as long as the TWINT bit in TWCR is set. Immediately after the application has cleared TWINT, the TWI will initiate transmission of the data packet. 6. When the data packet has been transmitted, the TWINT flag in TWCR is set, and TWSR is updated with a status code indicating that the data packet has successfully been sent. The status code will also reflect whether a slave acknowledged the packet or not. 7. The application software should now examine the value of TWSR, to make sure that the data packet was successfully transmitted, and that the value of the ACK bit was as expected. If TWSR indicates otherwise, the application software might take some special action, like calling an error routine. Assuming that the status code is as expected, the application must write a specific value to TWCR, instructing the TWI hardware to transmit a STOP condition. Which value to write is described later on. However, it is important that the TWINT bit is set in the value written. Writing a one to TWINT clears the flag. The TWI will not start any operation as long as the TWINT bit in TWCR is set. Immediately after the application has cleared TWINT, the TWI will initiate transmission of the STOP condition. Note that TWINT is NOT set after a STOP condition has been sent. Even though this example is simple, it shows the principles involved in all TWI transmissions. These can be summarized as follows: * When the TWI has finished an operation and expects application response, the TWINT flag is set. The SCL line is pulled low until TWINT is cleared. * When the TWINT flag is set, the user must update all TWI registers with the value relevant for the next TWI bus cycle. As an example, TWDR must be loaded with the value to be transmitted in the next bus cycle. * After all TWI Register updates and other pending application software tasks have been completed, TWCR is written. When writing TWCR, the TWINT bit should be set. Writing a one to TWINT clears the flag. The TWI will then commence executing whatever operation was specified by the TWCR setting. In the following an assembly and C implementation of the example is given. Note that the code below assumes that several definitions have been made for example by using include-files. Assembly code example(1) ldi r16, (1< max(16fSCL, 450 kHz)(4) 0 100 kHz fSCL 100 kHz V BUS - 0,4V ------------------------------350A V BUS - 0,4V ------------------------------100A Output Low-voltage (1) Condition 350 A sink current Rise Time for both SDA and SCL (2) (1) tof Output Fall Time from VIHmin to VILmax (1) tSP Spikes Suppressed by Input Filter Ii Input Current each I/O Pin Ci(1) Capacitance for each I/O Pin fSCL SCL Clock Frequency Rp Value of Pull-up resistor tHD;STA Hold Time (repeated) START Condition fSCL 100 kHz 4.0 - s tLOW Low Period of the SCL Clock fSCL 100 kHz 4.7 - s tHIGH High period of the SCL clock fSCL 100 kHz 4.0 - s tSU;STA Set-up time for a repeated START condition fSCL 100 kHz 4.7 - s tHD;DAT Data hold time fSCL 100 kHz 0.3 3.45 s tSU;DAT Data setup time fSCL 100 kHz 250 - ns tSU;STO Setup time for STOP condition fSCL 100 kHz 4.0 - s tBUF Bus free time between a STOP and START condition fSCL 100 kHz 4.7 - s Notes: 1. 2. 3. 4. Cb < 400 pF 0.1VBUS < Vi < 0.9VBUS In ATmega406, this parameter is characterized and not tested. Cb = capacitance of one bus line in pF. fCK = CPU clock frequency This requirement applies to all ATmega406 Two-wire Serial Interface operation. Other devices connected to the Two-wire Serial Bus need only obey the general fSCL requirement. 229 2548F-AVR-03/2013 Figure 30-3. Two-wire Serial Bus Timing tof tHIGH tr tLOW tLOW SCL tSU;STA tHD;STA tHD;DAT tSU;DAT tSU;STO SDA tBUF 30.5 Reset Characteristics Table 30-2. Symbol(2) Characteristics for Powering-up the LDO(1) Parameter Min Typ Max Units 4.0 V Charger Present VROT Regulator Power-on Threshold VCHT Charge Voltage Threshold 3.0 1.0 V No Charger Present VROT Regulator Power-on Threshold VPVIT Voltage Threshold on Battery Cell 1 Notes: Symbol VCOT 2.0 V Parameter Condition Min Typ Max Units Regulator must operate 6 7 8 V Condition Min Typ Max Units VREG = 3.3V 0.66 2.8 V Internal Voltage Regulator must be on. Symbol 230 V Power-on Reset Characteristics Charger-on Thresholt Voltage Table 30-4. Note: 4.0 1. Power-on Reset is issued when a charger is connected and the regulator has stable work conditions. 2. Values based on characterization. Table 30-3. Note: 3.0 External Reset Characteristics Parameter VRST RESET Pin Threshold Voltage tRST Minimum pulse width on RESET Pin 900 ns Internal Voltage Regulator must be on. ATmega406 2548F-AVR-03/2013 ATmega406 30.6 Supply Current of I/O Modules Table 30-5 on page 231 is showing the additional current consumption compared to ICC Active and ICC Idle for every I/O module controlled by the Power Reduction Register, see "PRR0 - Power Reduction Register 0" on page 36 for details. The tables and formulas below can be used to calculate the additional current consumption for the different I/O modules in Active and Idle mode. Table 30-5. Additional Current Consumption at VCC = 3.3V, F = 1 MHZ [A] Additional Current Consumption compared to Active mode [%] Additional Current Consumption compared to Idle mode [%] PRTWI 68.0 5.6 25.2 PRTIM1 4.5 0.4 1.7 PRTIM0 6.0 0.5 2.2 PRVADC 5.0 4.2 1.9 PRR0 bit 30.6.0.1 Additional Current Consumption for the different I/O modules Example 1 Calculate the expected current consumption in idle mode with TIMER1, V-ADC and Battery Protection enabled at VCC = 3.3V and F = 1MHz. From Table 30-5, fourth column, we see that we need to add 1.7% for the TIMER1, 1.9% for the V-ADC, and 25.2% for the TWI module. Reading from "DC Characteristics" on page 225, we find that the idle current consumption is typically 1.2 mA at VCC = 3.3V and F = 1MHz. The total current consumption in idle mode with USART0, TIMER1, and SPI enabled, gives: I CC total 1.2mA 1 + 0.017 + 0.019 + 0.252 1.55mA 231 2548F-AVR-03/2013 31. Typical Characteristics - Preliminary The following charts are tested on a few microcontrollers only. These figures are not tested during manufacturing, and are added for illustration purpose. 31.1 Pin Pull-up Figure 31-1. I/O Pin Pull-Up Resistor Current vs. Input Voltage (VCC = 3.3V) I/O PIN PULL-UP RESISTOR CURRENT vs. INPUT VOLTAGE VCC = 3.3V 90 80 70 IOP (uA) 60 50 40 30 20 25 C 10 85 C -30 C 0 0 0,5 1 1,5 2 2,5 3 3,5 V I (V) Figure 31-2. Reset Pull-Up Resistor Current vs. Input Voltage (VCC = 3.3V) RESET PULL-UP RESISTOR CURRENT vs. RESET PIN VOLTAGE VCC = 3.3V 80 70 IRESET(uA) 60 50 40 30 20 -30 C 10 25 C 85 C 0 0 0,5 1 1,5 2 2,5 3 3,5 VRESET(V) 232 ATmega406 2548F-AVR-03/2013 ATmega406 31.2 Pin Driver Strength Figure 31-3. I/O Pin Putput Voltage vs. Sink Current (VCC = 3.3V) I/O PIN OUTPUT VOLTAGE vs. SINK CURRENT VCC = 3.3V 1.3 85 C 1.2 1.1 25 C 1 VOL (V) -30 C 0.9 0.8 0.7 0.6 0.5 0.4 0 5 10 15 20 25 IOL (mA) Figure 31-4. I/O Pin output Voltage vs. Source Current (VCC = 3.3V) I/O PIN OUTPUT VOLTAGE vs. SOURCE CURRENT VCC = 3.3V 2.9 2.7 VOH (V) 2.5 2.3 -30 C 25 C 85 C 2.1 1.9 1.7 1.5 0 5 10 15 20 25 IOH (mA) 233 2548F-AVR-03/2013 31.3 Internal Oscillator Speed Figure 31-5. Watchdog Oscillator Frequency vs. Temperature (VCC = 3.3V) WATCHDOG OSCILLATOR FREQUENCY vs. TEMPERATURE VCC = 3.3 V 126 125 124 FRC (kHz) 123 122 121 120 119 118 117 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 Temperature Figure 31-6. Calibrated 1 MHz RC Oscillator Frequency vs. Temperature (VCC = 3.3V) CALIBRATED 1 MHz RC OSCILLATOR FREQUENCY vs. TEMPERATURE VCC = 3.3 V 1.025 1.02 1.015 FRC (MHz) 1.01 1.005 1 0.995 0.99 0.985 0.98 0.975 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 Temperature 234 ATmega406 2548F-AVR-03/2013 ATmega406 Figure 31-7. Calibrated 1 MHz RC Oscillator Frequency vs. OSCCAL Value (VCC = 3.3V) CALIBRATED 1 MHz RC OSCILLATOR FREQUENCY vs. OSCCAL VALUE 2.5 85 C 2 25 C FRC (MHz) -30 C 1.5 1 0.5 0 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 OSCCAL (X1) Figure 31-8. Slow RC Oscillator Frequency vs. Temperature (VCC = 3.3V) SLOW RC OSCILLATOR FREQUENCY vs. TEMPERATURE VCC = 3.3 V 156.5 156 FRC (kHz) 155.5 155 154.5 154 153.5 153 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 Temperature 235 2548F-AVR-03/2013 32. Register Summary Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0xFF) Reserved - - - - - - - - 236 Page (0xFE) Reserved - - - - - - - - (0xFD) Reserved - - - - - - - - (0xFC) Reserved - - - - - - - - (0xFB) Reserved - - - - - - - - (0xFA) Reserved - - - - - - - - (0xF9) Reserved - - - - - - - - (0xF8) BPPLR - - - - - - BPPLE BPPL 128 (0xF7) BPCR - - - - DUVD SCD DCD CCD 128 (0xF6) CBPTR SCPT[3:0] OCPT[3:0] 129 (0xF5) BPOCD DCDL[3:0] CCDL[3:0] 130 (0xF4) BPSCD - - - - SCDL[3:0] 130 (0xF3) BPDUV - - DUVT1 DUVT0 DUDL[3:0] 131 (0xF2) BPIR DUVIF COCIF DOCIF SCIF DUVIE COCIE DOCIE SCIE 132 (0xF1) CBCR - - - - CBE4 CBE3 CBE2 CBE1 137 (0xF0) FCSR - - PWMOC PWMOPC CPS DFE CFE PFD 134 (0xEF) Reserved - - - - - - - - (0xEE) Reserved - - - - - - - - (0xED) Reserved - - - - - - - - (0xEC) Reserved - - - - - - - - (0xEB) Reserved - - - - - - - - (0xEA) Reserved - - - - - - - - (0xE9) CADICH CADIC[15:8] 111 (0xE8) CADICL CADIC[7:0] 111 (0xE7) CADRDC CADRDC[7:0] 112 (0xE6) CADRCC CADRCC[7:0] (0xE5) CADCSRB - CADACIE CADRCIE CADICIE - CADACIF CADRCIF CADICIF 110 (0xE4) CADCSRA CADEN - CADUB CADAS1 CADAS0 CADSI1 CADSI0 CADSE 109 (0xE3) CADAC3 CADAC[31:24] 111 (0xE2) CADAC2 CADAC[23:16] 111 (0xE1) CADAC1 CADAC[15:8] 111 (0xE0) CADAC0 CADAC[7:0] (0xDF) Reserved - - - - - - - - 112 111 (0xDE) Reserved - - - - - - - - (0xDD) Reserved - - - - - - - - (0xDC) Reserved - - - - - - - - (0xDB) Reserved - - - - - - - - (0xDA) Reserved - - - - - - - - (0xD9) Reserved - - - - - - - - (0xD8) Reserved - - - - - - - - (0xD7) Reserved - - - - - - - - (0xD6) Reserved - - - - - - - - (0xD5) Reserved - - - - - - - - (0xD4) Reserved - - - - - - - - (0xD3) Reserved - - - - - - - - (0xD2) Reserved - - - - - - - - (0xD1) BGCRR BGCR7 BGCR6 BGCR5 BGCR4 BGCR3 BGCR2 BGCR1 BGCR0 123 (0xD0) BGCCR BGEN - BGCC5 BGCC4 BGCC3 BGCC2 BGCC1 BGCC0 123 (0xCF) Reserved - - - - - - - - (0xCE) Reserved - - - - - - - - (0xCD) Reserved - - - - - - - - (0xCC) Reserved - - - - - - - - (0xCB) Reserved - - - - - - - - (0xCA) Reserved - - - - - - - - (0xC9) Reserved - - - - - - - - (0xC8) Reserved - - - - - - - - (0xC7) Reserved - - - - - - - - (0xC6) Reserved - - - - - - - - (0xC5) Reserved - - - - - - - - (0xC4) Reserved - - - - - - - - (0xC3) Reserved - - - - - - - - (0xC2) Reserved - - - - - - - - (0xC1) Reserved - - - - - - - - (0xC0) CCSR - - - - - - XOE ACS 29 ATmega406 2548F-AVR-03/2013 ATmega406 Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0xBF) Reserved - - - - - - - - TWBCIF TWBCIE - - - TWBDT1 TWBDT0 TWBCIP 169 - 150 (0xBE) TWBCSR (0xBD) TWAMR (0xBC) TWCR (0xBB) TWDR TWAM[6:0] TWINT TWEA TWSTA TWSTO TWWC TWEN - TWIE 2-wire Serial Interface Data Register Page 147 149 (0xBA) TWAR (0xB9) TWSR TWA[6:0] TWGCE (0xB8) TWBR (0xB7) Reserved - - - - - - - (0xB6) Reserved - - - - - - - - (0xB5) Reserved - - - - - - - - (0xB4) Reserved - - - - - - - - (0xB3) Reserved - - - - - - - - (0xB2) Reserved - - - - - - - - (0xB1) Reserved - - - - - - - - (0xB0) Reserved - - - - - - - - (0xAF) Reserved - - - - - - - - (0xAE) Reserved - - - - - - - - (0xAD) Reserved - - - - - - - - (0xAC) Reserved - - - - - - - - (0xAB) Reserved - - - - - - - - (0xAA) Reserved - - - - - - - - (0xA9) Reserved - - - - - - - - (0xA8) Reserved - - - - - - - - (0xA7) Reserved - - - - - - - - (0xA6) Reserved - - - - - - - - (0xA5) Reserved - - - - - - - - (0xA4) Reserved - - - - - - - - (0xA3) Reserved - - - - - - - - (0xA2) Reserved - - - - - - - - (0xA1) Reserved - - - - - - - - (0xA0) Reserved - - - - - - - - (0x9F) Reserved - - - - - - - - (0x9E) Reserved - - - - - - - - (0x9D) Reserved - - - - - - - - (0x9C) Reserved - - - - - - - - (0x9B) Reserved - - - - - - - - (0x9A) Reserved - - - - - - - - (0x99) Reserved - - - - - - - - (0x98) Reserved - - - - - - - - (0x97) Reserved - - - - - - - - (0x96) Reserved - - - - - - - - (0x95) Reserved - - - - - - - - (0x94) Reserved - - - - - - - - (0x93) Reserved - - - - - - - - (0x92) Reserved - - - - - - - - (0x91) Reserved - - - - - - - - (0x90) Reserved - - - - - - - - (0x8F) Reserved - - - - - - - - (0x8E) Reserved - - - - - - - - (0x8D) Reserved - - - - - - - - (0x8C) Reserved - - - - - - - - (0x8B) Reserved - - - - - - - - (0x8A) Reserved - - - - - - - - (0x89) OCR1AH Timer/Counter1 - Output Compare Register A High Byte 101 (0x88) OCR1AL Timer/Counter1 - Output Compare Register A Low Byte 101 (0x87) Reserved - - - - - - - - (0x86) Reserved - - - - - - - - (0x85) TCNT1H Timer/Counter1 - Counter Register High Byte 101 (0x84) TCNT1L Timer/Counter1 - Counter Register Low Byte 101 (0x83) Reserved - - - - - - - (0x82) Reserved - - - - - - - - (0x81) TCCR1B - - - - CTC1 CS12 CS11 CS10 (0x80) Reserved - - - - - - - - (0x7F) Reserved - - - - - - - - (0x7E) DIDR0 - - - - VADC3D VADC2D VADC1D VADC0D TWS[7:3] - TWPS1 TWPS0 2-wire Serial Interface Bit Rate Register 149 148 147 - 100 120 237 2548F-AVR-03/2013 Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0x7D) Reserved - - - - - - - - (0x7C) VADMUX - - - - VADMUX3 VADMUX2 VADMUX1 VADMUX0 (0x7B) Reserved - - - - - - - - (0x7A) VADCSR - - - - VADEN VADSC VADCCIF VADCCIE (0x79) VADCH - - - - (0x78) VADCL (0x77) Reserved - - - - - - - - (0x76) Reserved - - - - - - - - (0x75) Reserved - - - - - - - - (0x74) Reserved - - - - - - - - (0x73) Reserved - - - - - - - - (0x72) Reserved - - - - - - - - (0x71) Reserved - - - - - - - - (0x70) Reserved - - - - - - - - (0x6F) TIMSK1 - - - - - - OCIE1A TOIE1 102 (0x6E) TIMSK0 - - - - - OCIE0B OCIE0A TOIE0 93 (0x6D) Reserved - - - - - - - - VADC Data Register High byte Page 118 118 119 VADC Data Register Low byte 119 (0x6C) PCMSK1 PCINT[15:8] (0x6B) PCMSK0 PCINT[7:0] 59 (0x6A) Reserved - - - - - - (0x69) EICRA ISC31 ISC30 ISC21 ISC20 ISC11 ISC10 ISC01 ISC00 56 (0x68) PCICR - - - - - - PCIE1 PCIE0 58 (0x67) Reserved - - - - - - - - (0x66) FOSCCAL (0x65) Reserved - - - - - - - - (0x64) PRR0 - - - - PRTWI PRTIM1 PRTIM0 PRVADC (0x63) Reserved - - - - - - - - (0x62) WUTCSR WUTIF WUTIE WUTCF WUTR WUTE WUTP2 WUTP1 WUTP0 (0x61) Reserved - - - - - - - - 59 - - Fast Oscillator Calibration Register 29 36 49 (0x60) WDTCSR WDIF WDIE WDP3 WDCE WDE WDP2 WDP1 WDP0 0x3F (0x5F) SREG I T H S V N Z C 47 10 0x3E (0x5E) SPH SP15 SP14 SP13 SP12 SP11 SP10 SP9 SP8 12 0x3D (0x5D) SPL SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0 12 0x3C (0x5C) Reserved - - - - - - - - 0x3B (0x5B) Reserved - - - - - - - - 0x3A (0x5A) Reserved - - - - - - - - 0x39 (0x59) Reserved - - - - - - - - 0x38 (0x58) Reserved - - - - - - - - 0x37 (0x57) SPMCSR SPMIE RWWSB SIGRD RWWSRE BLBSET PGWRT PGERS SPMEN 0x36 (0x56) Reserved - - - - - - - - 183 0x35 (0x55) MCUCR JTD - - PUD - - IVSEL IVCE 0x34 (0x54) MCUSR - - - JTRF WDRF BODRF EXTRF PORF 46 0x33 (0x53) SMCR - - - - SM2 SM1 SM0 SE 31 - - - - - - - - 0x32 (0x52) Reserved 0x31 (0x51) OCDR 0x30 (0x50) Reserved - - - - - - - - On-Chip Debug Register 55/73/176 176 0x2F (0x4F) Reserved - - - - - - - - 0x2E (0x4E) Reserved - - - - - - - - 0x2D (0x4D) Reserved - - - - - - - - 0x2C (0x4C) Reserved - - - - - - - - 0x2B (0x4B) GPIOR2 General Purpose I/O Register 2 0x2A (0x4A) GPIOR1 General Purpose I/O Register 1 0x29 (0x49) Reserved 0x28 (0x48) OCR0B Timer/Counter0 Output Compare Register B 92 0x27 (0x47) OCR0A Timer/Counter0 Output Compare Register A 92 0x26 (0x46) TCNT0 Timer/Counter0 (8 Bit) 0x25 (0x45) TCCR0B FOC0A FOC0B - - WGM02 CS02 CS01 CS00 0x24 (0x44) TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00 88 0x23 (0x43) GTCCR TSM - - - - - - PSRSYNC 105 0x22 (0x42) EEARH - - - - - - - High Byte 19 0x21 (0x41) EEARL EEPROM Address Register Low Byte 19 0x20 (0x40) EEDR EEPROM Data Register 19 - - - EEPM1 - EEPM0 24 - EERIE - - - 92 EEMPE EEPE EERE 91 0x1F (0x3F) EECR 0x1E (0x3E) GPIOR0 0x1D (0x3D) EIMSK - - - - INT3 INT2 INT1 INT0 57 0x1C (0x3C) EIFR - - - - INTF3 INTF2 INTF1 INTF0 57 238 - - 24 General Purpose I/O Register 0 19 24 ATmega406 2548F-AVR-03/2013 ATmega406 Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0x1B (0x3B) PCIFR - - - - - - PCIF1 PCIF0 Page 0x1A (0x3A) Reserved - - - - - - - - 0x19 (0x39) Reserved - - - - - - - - 0x18 (0x38) Reserved - - - - - - - - 0x17 (0x37) Reserved - - - - - - - - 0x16 (0x36) TIFR1 - - - - - - OCF1A TOV1 102 0x15 (0x35) TIFR0 - - - - - OCF0B OCF0A TOV0 94 0x14 (0x34) Reserved - - - - - - - - 0x13 (0x33) Reserved - - - - - - - - 0x12 (0x32) Reserved - - - - - - - - 0x11 (0x31) Reserved - - - - - - - - 0x10 (0x30) Reserved - - - - - - - - 0x0F (0x2F) Reserved - - - - - - - - 0x0E (0x2E) Reserved - - - - - - - - 0x0D (0x2D) Reserved - - - - - - - - 0x0C (0x2C) Reserved - - - - - - - - 0x0B (0x2B) PORTD - - - - - - PORTD1 PORTD0 74 0x0A (0x2A) DDRD - - - - - - DDD1 DDD0 74 0x09 (0x29) PIND - - - - - - PIND1 PIND0 74 0x08 (0x28) PORTC - - - - - - - PORTC0 76 0x07 (0x27) Reserved - - - - - - - - 0x06 (0x26) Reserved - - - - - - - - 0x05 (0x25) PORTB PORTB7 PORTB6 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0 74 0x04 (0x24) DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0 74 74 0x03 (0x23) PINB PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0 0x02 (0x22) PORTA PORTA7 PORTA6 PORTA5 PORTA4 PORTB3 PORTA2 PORTA1 PORTA0 73 0x01 (0x21) DDRA DDA7 DDA6 DDA5 DDA4 DDA3 DDA2 DDA1 DDA0 73 0x00 (0x20) PINA PINA7 PINA6 PINA5 PINA4 PINA3 PINA2 PINA1 PINA0 73 Notes: 1. For compatibility with future devices, reserved bits should be written to zero if accessed. Reserved I/O memory addresses should never be written. 2. I/O registers within the address range $00 - $1F are directly bit-accessible using the SBI and CBI instructions. In these registers, the value of single bits can be checked by using the SBIS and SBIC instructions. 3. Some of the status flags are cleared by writing a logical one to them. Note that the CBI and SBI instructions will operate on all bits in the I/O register, writing a one back into any flag read as set, thus clearing the flag. The CBI and SBI instructions work with registers 0x00 to 0x1F only. 4. When using the I/O specific commands IN and OUT, the I/O addresses $00 - $3F must be used. When addressing I/O registers as data space using LD and ST instructions, $20 must be added to these addresses. The ATmega406 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in Opcode for the IN and OUT instructions. For the Extended I/O space from $60 - $FF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used. 239 2548F-AVR-03/2013 33. Instruction Set Summary Mnemonics Operands Description Operation Flags #Clocks ARITHMETIC AND LOGIC INSTRUCTIONS ADD Rd, Rr Add two Registers Rd Rd + Rr Z,C,N,V,H ADC Rd, Rr Add with Carry two Registers Rd Rd + Rr + C Z,C,N,V,H 1 ADIW Rdl,K Add Immediate to Word Rdh:Rdl Rdh:Rdl + K Z,C,N,V,S 2 SUB Rd, Rr Subtract two Registers Rd Rd - Rr Z,C,N,V,H 1 SUBI Rd, K Subtract Constant from Register Rd Rd - K Z,C,N,V,H 1 SBC Rd, Rr Subtract with Carry two Registers Rd Rd - Rr - C Z,C,N,V,H 1 1 SBCI Rd, K Subtract with Carry Constant from Reg. Rd Rd - K - C Z,C,N,V,H 1 SBIW Rdl,K Subtract Immediate from Word Rdh:Rdl Rdh:Rdl - K Z,C,N,V,S 2 1 AND Rd, Rr Logical AND Registers Rd Rd Rr Z,N,V ANDI Rd, K Logical AND Register and Constant Rd Rd K Z,N,V 1 OR Rd, Rr Logical OR Registers Rd Rd v Rr Z,N,V 1 ORI Rd, K Logical OR Register and Constant Rd Rd v K Z,N,V 1 EOR Rd, Rr Exclusive OR Registers Rd Rd Rr Z,N,V 1 COM Rd One's Complement Rd 0xFF Rd Z,C,N,V 1 NEG Rd Two's Complement Rd 0x00 Rd Z,C,N,V,H 1 SBR Rd,K Set Bit(s) in Register Rd Rd v K Z,N,V 1 CBR Rd,K Clear Bit(s) in Register Rd Rd (0xFF - K) Z,N,V 1 INC Rd Increment Rd Rd + 1 Z,N,V 1 DEC Rd Decrement Rd Rd 1 Z,N,V 1 TST Rd Test for Zero or Minus Rd Rd Rd Z,N,V 1 CLR Rd Clear Register Rd Rd Rd Z,N,V 1 SER Rd Set Register Rd 0xFF None 1 MUL Rd, Rr Multiply Unsigned R1:R0 Rd x Rr Z,C 2 MULS Rd, Rr Multiply Signed R1:R0 Rd x Rr Z,C 2 MULSU Rd, Rr Multiply Signed with Unsigned R1:R0 Rd x Rr Z,C 2 FMUL Rd, Rr Fractional Multiply Unsigned R1:R0 (Rd x Rr) << Z,C 2 FMULS Rd, Rr Fractional Multiply Signed Z,C 2 FMULSU Rd, Rr Fractional Multiply Signed with Unsigned 1 R1:R0 (Rd x Rr) << 1 R1:R0 (Rd x Rr) << 1 Z,C 2 Relative Jump PC PC + k + 1 None 2 Indirect Jump to (Z) PC Z None 2 3 BRANCH INSTRUCTIONS RJMP k IJMP JMP k Direct Jump PC k None RCALL k Relative Subroutine Call PC PC + k + 1 None 3 Indirect Call to (Z) PC Z None 3 ICALL Direct Subroutine Call PC k None 4 RET Subroutine Return PC STACK None 4 RETI Interrupt Return PC STACK I if (Rd = Rr) PC PC + 2 or 3 None CALL k 4 CPSE Rd,Rr Compare, Skip if Equal 1/2/3 CP Rd,Rr Compare Rd Rr Z, N,V,C,H 1 CPC Rd,Rr Compare with Carry Rd Rr C Z, N,V,C,H 1 CPI Rd,K Compare Register with Immediate Rd K Z, N,V,C,H SBRC Rr, b Skip if Bit in Register Cleared if (Rr(b)=0) PC PC + 2 or 3 None 1 1/2/3 SBRS Rr, b Skip if Bit in Register is Set if (Rr(b)=1) PC PC + 2 or 3 None 1/2/3 SBIC P, b Skip if Bit in I/O Register Cleared if (P(b)=0) PC PC + 2 or 3 None 1/2/3 SBIS P, b Skip if Bit in I/O Register is Set if (P(b)=1) PC PC + 2 or 3 None 1/2/3 BRBS s, k Branch if Status Flag Set if (SREG(s) = 1) then PCPC+k + 1 None 1/2 BRBC s, k Branch if Status Flag Cleared if (SREG(s) = 0) then PCPC+k + 1 None 1/2 BREQ k Branch if Equal if (Z = 1) then PC PC + k + 1 None 1/2 BRNE k Branch if Not Equal if (Z = 0) then PC PC + k + 1 None 1/2 BRCS k Branch if Carry Set if (C = 1) then PC PC + k + 1 None 1/2 BRCC k Branch if Carry Cleared if (C = 0) then PC PC + k + 1 None 1/2 BRSH k Branch if Same or Higher if (C = 0) then PC PC + k + 1 None 1/2 BRLO k Branch if Lower if (C = 1) then PC PC + k + 1 None 1/2 BRMI k Branch if Minus if (N = 1) then PC PC + k + 1 None 1/2 BRPL k Branch if Plus if (N = 0) then PC PC + k + 1 None 1/2 BRGE k Branch if Greater or Equal, Signed if (N V= 0) then PC PC + k + 1 None 1/2 BRLT k Branch if Less Than Zero, Signed if (N V= 1) then PC PC + k + 1 None 1/2 BRHS k Branch if Half Carry Flag Set if (H = 1) then PC PC + k + 1 None 1/2 BRHC k Branch if Half Carry Flag Cleared if (H = 0) then PC PC + k + 1 None 1/2 BRTS k Branch if T Flag Set if (T = 1) then PC PC + k + 1 None 1/2 BRTC k Branch if T Flag Cleared if (T = 0) then PC PC + k + 1 None 1/2 BRVS k Branch if Overflow Flag is Set if (V = 1) then PC PC + k + 1 None 1/2 BRVC k Branch if Overflow Flag is Cleared if (V = 0) then PC PC + k + 1 None 1/2 240 ATmega406 2548F-AVR-03/2013 ATmega406 33. Instruction Set Summary (Continued) Mnemonics Operands Description Operation Flags #Clocks BRIE k Branch if Interrupt Enabled if ( I = 1) then PC PC + k + 1 None 1/2 BRID k Branch if Interrupt Disabled if ( I = 0) then PC PC + k + 1 None 1/2 BIT AND BIT-TEST INSTRUCTIONS SBI P,b Set Bit in I/O Register I/O(P,b) 1 None 2 CBI P,b Clear Bit in I/O Register I/O(P,b) 0 None 2 LSL Rd Logical Shift Left Rd(n+1) Rd(n), Rd(0) 0 Z,C,N,V 1 LSR Rd Logical Shift Right Rd(n) Rd(n+1), Rd(7) 0 Z,C,N,V 1 ROL Rd Rotate Left Through Carry Rd(0)C,Rd(n+1) Rd(n),CRd(7) Z,C,N,V 1 ROR Rd Rotate Right Through Carry Rd(7)C,Rd(n) Rd(n+1),CRd(0) Z,C,N,V 1 ASR Rd Arithmetic Shift Right Rd(n) Rd(n+1), n=0..6 Z,C,N,V 1 SWAP Rd Swap Nibbles Rd(3..0)Rd(7..4),Rd(7..4)Rd(3..0) None 1 BSET s Flag Set SREG(s) 1 SREG(s) 1 BCLR s Flag Clear SREG(s) 0 SREG(s) 1 BST Rr, b Bit Store from Register to T T Rr(b) T 1 BLD Rd, b Bit load from T to Register Rd(b) T None 1 1 SEC Set Carry C1 C CLC Clear Carry C0 C 1 SEN Set Negative Flag N1 N 1 CLN Clear Negative Flag N0 N 1 SEZ Set Zero Flag Z1 Z 1 CLZ Clear Zero Flag Z0 Z 1 SEI Global Interrupt Enable I1 I 1 CLI Global Interrupt Disable I 0 I 1 1 SES Set Signed Test Flag S1 S CLS Clear Signed Test Flag S0 S 1 SEV Set Twos Complement Overflow. V1 V 1 CLV Clear Twos Complement Overflow V0 V 1 SET Set T in SREG T1 T 1 CLT Clear T in SREG T0 T 1 SEH CLH Set Half Carry Flag in SREG Clear Half Carry Flag in SREG H1 H0 H H 1 1 None 1 None 1 DATA TRANSFER INSTRUCTIONS MOV Rd, Rr Move Between Registers MOVW Rd, Rr Copy Register Word Rd Rr Rd+1:Rd Rr+1:Rr LDI Rd, K Load Immediate Rd K None 1 LD Rd, X Load Indirect Rd (X) None 2 LD Rd, X+ Load Indirect and Post-Inc. Rd (X), X X + 1 None 2 LD Rd, - X Load Indirect and Pre-Dec. X X - 1, Rd (X) None 2 2 LD Rd, Y Load Indirect Rd (Y) None LD Rd, Y+ Load Indirect and Post-Inc. Rd (Y), Y Y + 1 None 2 LD Rd, - Y Load Indirect and Pre-Dec. Y Y - 1, Rd (Y) None 2 LDD Rd,Y+q Load Indirect with Displacement Rd (Y + q) None 2 LD Rd, Z Load Indirect Rd (Z) None 2 LD Rd, Z+ Load Indirect and Post-Inc. Rd (Z), Z Z+1 None 2 LD Rd, -Z Load Indirect and Pre-Dec. Z Z - 1, Rd (Z) None 2 LDD Rd, Z+q Load Indirect with Displacement Rd (Z + q) None 2 2 LDS Rd, k Load Direct from SRAM Rd (k) None ST X, Rr Store Indirect (X) Rr None 2 ST X+, Rr Store Indirect and Post-Inc. (X) Rr, X X + 1 None 2 ST - X, Rr Store Indirect and Pre-Dec. X X - 1, (X) Rr None 2 ST Y, Rr Store Indirect (Y) Rr None 2 ST Y+, Rr Store Indirect and Post-Inc. (Y) Rr, Y Y + 1 None 2 ST - Y, Rr Store Indirect and Pre-Dec. Y Y - 1, (Y) Rr None 2 STD Y+q,Rr Store Indirect with Displacement (Y + q) Rr None 2 ST Z, Rr Store Indirect (Z) Rr None 2 ST Z+, Rr Store Indirect and Post-Inc. (Z) Rr, Z Z + 1 None 2 ST -Z, Rr Store Indirect and Pre-Dec. Z Z - 1, (Z) Rr None 2 STD Z+q,Rr Store Indirect with Displacement (Z + q) Rr None 2 STS k, Rr Store Direct to SRAM (k) Rr None 2 Load Program Memory R0 (Z) None 3 LPM LPM Rd, Z Load Program Memory Rd (Z) None 3 LPM Rd, Z+ Load Program Memory and Post-Inc Rd (Z), Z Z+1 None 3 Store Program Memory (Z) R1:R0 None - In Port Rd P None 1 SPM IN Rd, P 241 2548F-AVR-03/2013 33. Instruction Set Summary (Continued) Mnemonics Operands Description Operation Flags #Clocks OUT P, Rr Out Port P Rr None 1 PUSH Rr Push Register on Stack STACK Rr None 2 POP Rd Pop Register from Stack Rd STACK None 2 MCU CONTROL INSTRUCTIONS NOP No Operation None 1 SLEEP Sleep (see specific descr. for Sleep function) None 1 WDR BREAK Watchdog Reset Break (see specific descr. for WDR/timer) For On-chip Debug Only None None 1 N/A 242 ATmega406 2548F-AVR-03/2013 ATmega406 34. Ordering Information Speed (MHz) Power Supply 1 4.0 - 25V Notes: Ordering Code Package(1) ATmega406-1AAU(2) 48AA Operation Range Industrial (-30C to 85C) 1. This device can also be supplied in wafer form. Please contact your local Atmel sales office for detailed ordering information and minimum quantities. 2. Pb-free packaging alternative, complies to the European Directive for Restriction of Hazardous Substances (RoHS directive). Also Halide free and fully Green. Package Type 48AA 48-lead, 7 x 7 x 1.44 mm body, 0.5 mm lead pitch, Low Profile Plastic Quad Flat Package (LQFP) 243 2548F-AVR-03/2013 35. Packaging Information 35.1 48AA PIN 1 B PIN 1 IDENTIFIER e E1 E D1 D C 0~7 A1 A2 A L COMMON DIMENSIONS (Unit of Measure = mm) Notes: 1. This package conforms to JEDEC reference MS-026, Variation BBC. 2. Dimensions D1 and E1 do not include mold protrusion. Allowable protrusion is 0.25 mm per side. Dimensions D1 and E1 are maximum plastic body size dimensions including mold mismatch. 3. Lead coplanarity is 0.08 mm maximum. SYMBOL MIN NOM MAX A - - 1.60 A1 0.05 - 0.15 A2 1.35 1.40 1.45 D 8.75 9.00 9.25 D1 6.90 7.00 7.10 E 8.75 9.00 9.25 E1 6.90 7.00 7.10 B 0.17 - 0.27 C 0.09 - 0.20 L 0.45 - 0.75 e NOTE Note 2 Note 2 0.50 TYP 2010-10-19 R 244 2325 Orchard Parkway San Jose, CA 95131 TITLE 48AA, 48-lead, 7 x 7 mm Body Size, 1.4 mm Body Thickness, 0.5 mm Lead Pitch, Low Profile Plastic Quad Flat Package (LQFP) DRAWING NO. 48AA REV. D ATmega406 2548F-AVR-03/2013 ATmega406 36. Errata 36.1 Rev. F * Voltage-ADC Common Mode Offset * Voltage Reference Spike 1. Voltage-ADC Common Mode Offset The cell conversion will have an Offset-error depending on the Common Mode (CM) level. This means that the error of a cell is depending on the voltage of the lower cells. The CM Offset is calibrated away in Atmel production when the cells are balanced. When the cells get un-balanced the CM depending offset will reappear: a. Cell 1 defines its own CM level, and will never be affected by the CM dependent offset. b. The CM level for Cell 2 will change if Cell 1 voltage deviates from Cell 2 voltage. c. The CM level for Cell 3 will change if Cell 1 and/or Cell 2 voltage deviates from the voltage at Cell 3. The worst-case error is when Cell 1 and 2 are balanced while Cell 3 voltage deviates from the voltage at Cell 1 and 2. d. The CM level for Cell 4 will change if Cell 1, Cell 2 and/or Cell 3 deviate from the voltage at Cell 4. The worst-case error is when Cell 1, Cell 2 and Cell 3 are balanced while Cell 4 voltage deviates from the voltage at Cell 1, 2 and 3. Figure 36-1 on page 246, shows the error of Cell2, Cell3 and Cell4 with 5% and 10% unbalanced cells. 245 2548F-AVR-03/2013 Figure 36-1. CM Offset with unbalanced cells. Problem Fix/Workaround Avoid getting unbalanced cells by using the internal cell balancing FETs. 2. Voltage Reference spike The Voltage Reference, VREF, will spike each time the internal temperature sensor is enabled. The temperature sensor is enabled when the VTEMP is selected in the VADMUX register and the V-ADC is enabled by the VADEN bit. The spike will be approximately 50mV and lasts for about 5ms, and it will affect any ongoing current accumulation in the CC-ADC, as well as V-ADC conversions in the period of the spike. Figure 36-2 on page 247 illustrates the Voltage Reference spike. 246 ATmega406 2548F-AVR-03/2013 ATmega406 Figure 36-2. Voltage Reference Spike Voltage V~50mV 1.1 V VREF t ~< 5ms time VADEN VADMUX3:0 XXX VTEMP Problem workaround: To get correct temperature measurement, the VADSC bit should not be written until the spike has settled (external decoupling capacitor of 1F). 247 2548F-AVR-03/2013 36.2 Rev. E * Voltage ADC not functional below 0C * Voltage-ADC Common Mode Offset * Voltage Reference Spike 1. Voltage-ADC Failing at Low Temperatures Voltage ADC not functional below 0C. The voltage ADC has a very large error below 0C, and can not be used Problem Fix/Workaround Do not use this revision below 0 celsius. 2. Voltage-ADC Common Mode Offset The cell conversion will have an Offset-error depending on the Common Mode (CM) level. This means that the error of a cell is depending on the voltage of the lower cells. The CM Offset is calibrated away in Atmel production when the cells are balanced. When the cells get un-balanced the CM depending offset will reappear: a. Cell 1 defines its own CM level, and will never be affected by the CM dependent offset. b. The CM level for Cell 2 will change if Cell 1 voltage deviates from Cell 2 voltage. c. The CM level for Cell 3 will change if Cell 1 and/or Cell 2 voltage deviates from the voltage at Cell 3. The worst-case error is when Cell 1 and 2 are balanced while Cell 3 voltage deviates from the voltage at Cell 1 and 2. d. The CM level for Cell 4 will change if Cell 1, Cell 2 and/or Cell 3 deviate from the voltage at Cell 4. The worst-case error is when Cell 1, Cell 2 and Cell 3 are balanced while Cell 4 voltage deviates from the voltage at Cell 1, 2 and 3. Figure 36-1 on page 246, shows the error of Cell2, Cell3 and Cell4 with 5% and 10% unbalanced cells. 248 ATmega406 2548F-AVR-03/2013 ATmega406 Figure 36-3. CM Offset with unbalanced cells. Problem Fix/Workaround Avoid getting unbalanced cells by using the internal cell balancing FETs. 3. Voltage Reference Spike The Voltage Reference, VREF, will spike each time a temperature measurement is started with the Voltage-ADC. Problem Fix/Workaround An accurate temperature measurement could be obtained by doing 10 temperature conversions immediately after each other. The first 9 results would be inaccurate, but the 10th conversion will be correct. Figure 36-4 on page 250 illustrates the spike on the Voltage Reference when doing 10 temperature conversions in a row (external decoupling capacitor of 1F). 249 2548F-AVR-03/2013 Figure 36-4. Voltage Reference Spike Voltage V~50mV 1.1 V VREF t ~< 5ms time VADSC VADMUX3:0 (10 VTEMP conversion in a row) XXX VTEMP If the CC-ADC is doing current accumulation while the V-ADC is doing temperature measurement, both the Instantaneous and the Accumulated conversion results will be affected. The spike on VREF will be visible on 1 Accumulated Current (CADAC3...0) and 2 Instantaneous Current (CADIC1...0) conversion results. 36.3 Rev. D * * * * * * Voltage ADC not functional below 0C Voltage-ADC Common Mode Offset Voltage Reference Spike Voltage Regulator Start-up sequence VREF influenced by MCU state EEPROM read from application code does not work in Lock Bit Mode 3 1. Voltage-ADC Failing at Low Temperatures Voltage ADC not functional below 0C. The voltage ADC has a very large error below 0C, and can not be used Problem Fix/Workaround 1. Voltage-ADC Common Mode Offset The cell conversion will have an Offset-error depending on the Common Mode (CM) level. This means that the error of a cell is depending on the voltage of the lower cells. The CM Offset is calibrated away in Atmel production when the cells are balanced. When the cells get un-balanced the CM depending offset will reappear: 250 ATmega406 2548F-AVR-03/2013 ATmega406 a. Cell 1 defines its own CM level, and will never be affected by the CM dependent offset. b. The CM level for Cell 2 will change if Cell 1 voltage deviates from Cell 2 voltage. c. The CM level for Cell 3 will change if Cell 1 and/or Cell 2 voltage deviates from the voltage at Cell 3. The worst-case error is when Cell 1 and 2 are balanced while Cell 3 voltage deviates from the voltage at Cell 1 and 2. d. The CM level for Cell 4 will change if Cell 1, Cell 2 and/or Cell 3 deviate from the voltage at Cell 4. The worst-case error is when Cell 1, Cell 2 and Cell 3 are balanced while Cell 4 voltage deviates from the voltage at Cell 1, 2 and 3. Figure 36-1 on page 246, shows the error of Cell2, Cell3 and Cell4 with 5% and 10% unbalanced cells. Figure 36-5. CM Offset with unbalanced cells. Problem Fix/Workaround Avoid getting unbalanced cells by using the internal cell balancing FETs. 251 2548F-AVR-03/2013 3. Voltage Reference Spike The Voltage Reference, VREF, will spike each time a temperature measurement is started with the Voltage-ADC. Problem Fix/Workaround An accurate temperature measurement could be obtained by doing 10 temperature conversions immediately after each other. The first 9 results would be inaccurate, but the 10th conversion will be correct. Figure 36-6 illustrates the spike on the Voltage Reference when doing 10 temperature conversions in a row (external decoupling capacitor of 1F). Figure 36-6. Voltage Reference Spike Voltage V~50mV 1.1 V VREF t ~< 5ms time VADSC VADMUX3:0 (10 VTEMP conversion in a row) XXX VTEMP If the CC-ADC is doing current accumulation while the V-ADC is doing temperature measurement, both the Instantaneous and the Accumulated conversion results will be affected. The spike on VREF will be visible on 1 Accumulated Current (CADAC3...0) and 2 Instantaneous Current (CADIC1...0) conversion results. 252 ATmega406 2548F-AVR-03/2013 ATmega406 4. Voltage Regulator Start-up sequence When powering up ATmega406 some precautions are necessary to ensure proper start-up of the Voltage Regulator. Problem Fix/Workaround The three steps below are needed to ensure proper start-up of the voltage regulator. a. Do NOT connect a capacitor larger than 100 nF on the VFET pin. This is to ensure fast rise time on the VFET pin when a supply voltage is connected. b. During assembly, always connect Cell1 first, then Cell2 and so on until the top cell is connected to PVT. If the cell voltages are about 2 volts or larger, the Voltage Regulator will normally start up properly in Power-off mode (VREG appr. 2.8 volts). c. After all cells have been assembled as described in step 2, a charger source must be connected at the BATT+ terminal to initialize the chip, see Section 8.3 "Power-on Reset and Charger Connect" on page 38 in the datasheet. If the Voltage Regulator started up in Power-off during assembly of the cells, the chip will initialize when the charger source makes the voltage at the BATT pin exceed 7 - 8 Volts. If the Voltage Regulator did not start up properly, the charger source has one additional requirement to ensure proper start up and initialization. In this case the charger source must ensure that the voltage at the VFET pin increases quickly at least 3 Volts above the voltage at the PVT pin, and that the voltage at the BATT pin exceeds 7 - 8 Volts. This will start up and initialize the chip directly. 5. VREF influenced by MCU state The reference voltage at the VREF pin depends on the following conditions of the device: a. Charger Over-current and/or Discharge Over-current Protection active but Short-circuit inactive. This will increase VREF voltage with typical 1 mV compared to a condition were all Current Protections are disabled. b. Short-circuit Protection active. Short-circuit measurements are activated when SCD in BPCR is zero (default) and DFE in FET Control and Status Register (FCSR) is set. This will increase VREF voltage with typical 8 mV compared to a condition with shortcircuit measurements inactive. c. V-ADC conversion of the internal VTEMP voltage. This will increase VREF voltage with typical 15 mV compared to a condition with short-circuit measurements inactive. Problem Fix/Work around To ensure the highest accuracy, set the Bandgap Calibration Register (BGCC) to get 1.100 V at VREF after the chip is configured with the actual Battery Protection settings and the Discharge FET is enabled. 6. EEPROM read from application code does not work in Lock Bit Mode 3 When the Memory Lock Bits LB2 and LB1 are programmed to mode 3, EEPROM read does not work from the application code. Problem Fix/Work around Do not set Lock Bit Protection Mode 3 when the application code needs to read from EEPROM. 253 2548F-AVR-03/2013 37. Datasheet Revision History 37.1 Rev 2548F - 03/13 1. 2. 3. 37.2 Rev 2548E - 07/06 1. 2. 3. 4. 5 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23 24. 25. 26 27. 28. 29. 30. 254 Updated heading titles of "PPI/NNI" and "PI/NI" on page 6. Updated Note 10 in Table 27-5 on page 189 Updated Section 30.2 on page 225. Updated "Pin Configurations" on page 2. Updated "ADC Noise Reduction Mode" on page 32. Updated "Power-save Mode" on page 32. Updated "Power-down Mode" on page 33. Updated "Power-off Mode" on page 33. Updated "Power Reduction Register" on page 36. Added "Voltage ADC" on page 37 and "Coloumb Counter" on page 38. Updated "Reset Sources" on page 39. Updated "Power-on Reset and Charger Connect" on page 40. Updated "External Reset" on page 41. VCC replaced by VREG in "Brown-out Detection" on page 42. Updated "Alternate Port Functions" on page 66. Updated "Internal Clock Source" on page 103. Updated "External Clock Source" on page 103. Updated Features in "Coulomb Counter - Dedicated Fuel Gauging Sigma-delta ADC" on page 106. Updated Operation in Section 18. "Coulomb Counter - Dedicated Fuel Gauging Sigma-delta ADC" on page 106. Updated Features in "Voltage Regulator" on page 114. Updated Operation in "Voltage Regulator" on page 114. Updated Bit description in "VADCL and VADCH - The V-ADC Data Register" on page 119. Updated "Writing to Bandgap Calibration Registers" on page 122. Updated Text in "Register Description for FET Control" on page 134. Added "MCUCR - MCU Control Register" on page 176. Updated "Operating Circuit" on page 223 Updated "Electrical Characteristics" on page 225. Added "Typical Characteristics - Preliminary" on page 232. Updated "Register Summary" on page 236. Updated "Errata" on page 245. Updated Table 9-2 on page 48, Table 27-5 on page 189. Updated Figure 8-1 on page 35, Figure 9-5 on page 42, Figure 17-2 on page 104, Figure 18-2 on page 107, Figure 18-3 on page 108, Figure 19-1 on page 114, Figure 29-1 on page 223. Updated Register Adresses. ATmega406 2548F-AVR-03/2013 ATmega406 37.3 Rev 2548D - 06/05 1. 37.4 Rev 2548C - 05/05 1. 37.5 Updated Section 36. "Errata" on page 245. Updated Section 36. "Errata" on page 245. Rev 2548B - 04/05 1. 2. 3. 4. 5. 6. 7. 8. 9. Typos updated, bit "PSRASY" removed, CS12:0 renamed CS1[2:0]. Removed "BGEN" bit in BGCCR register. The bandgap voltage reference is always enabled in ATmega406 revision E. Updated Figure 2-1 on page 3, Figure 6-1 on page 25, Figure 24-9 on page 137, Figure 21-1 on page 120. Updated Table 7-2 on page 33, Table 7-3 on page 34, Table 8-1 on page 38, Table 26-5 on page 181, Figure 27-1 on page 188. Updated Section 12.3.2 "Alternate Functions of Port A" on page 66 and Section 21. "Battery Protection" on page 118 description. Updated registers "External Interrupt Flag Register - EIFR" on page 55 and "Timer/Counter Control Register B - TCCR0B" on page 89. Updated Section 17.1 "Features" on page 103 and Section 17.2 "Operation" on page 103. Updated Section 19.1 "Features" on page 111. Updated Section 20.2 "Register Description for Voltage Reference and Temperature Sensor" on page 116. Updated Section 29. "Electrical Characteristics" on page 211. Updated Section 35. "Errata" on page 225. 255 2548F-AVR-03/2013 256 ATmega406 2548F-AVR-03/2013 ATmega406 Table of Contents Features ..................................................................................................... 1 1 Pin Configurations ................................................................................... 2 1.1 Disclaimer .................................................................................................................2 2 Overview ................................................................................................... 3 2.1 Block Diagram ..........................................................................................................3 2.2 Pin Descriptions .......................................................................................................5 3 Resources ................................................................................................. 7 4 About Code Examples ............................................................................. 7 5 AVR CPU Core .......................................................................................... 8 5.1 Introduction ...............................................................................................................8 5.2 Architectural Overview .............................................................................................8 5.3 ALU - Arithmetic Logic Unit .....................................................................................9 5.4 Status Register .......................................................................................................10 5.5 General Purpose Register File ...............................................................................11 5.6 Stack Pointer ..........................................................................................................12 5.7 Instruction Execution Timing ..................................................................................13 5.8 Reset and Interrupt Handling .................................................................................14 6 AVR Memories ........................................................................................ 16 6.1 In-System Reprogrammable Flash Program Memory ............................................16 6.2 SRAM Data Memory ..............................................................................................17 6.3 EEPROM Data Memory .........................................................................................18 6.4 I/O Memory .............................................................................................................24 7 System Clock and Clock Options ......................................................... 25 7.1 Clock Systems and their Distribution ......................................................................25 7.2 Clock Sources ........................................................................................................26 7.3 Calibrated Fast RC Oscillator .................................................................................26 7.4 32 kHz Crystal Oscillator ........................................................................................27 7.5 Slow RC Oscillator .................................................................................................27 7.6 Ultra Low Power RC Oscillator ...............................................................................27 7.7 CPU, I/O, Flash, and Voltage ADC Clock ...............................................................27 7.8 Coulomb Counter ADC and Wake-up Timer Clock ................................................28 7.9 Watchdog Timer and Battery Protection Clock .......................................................28 i 2548F-AVR-03/2013 7.10 Run-Time Clock Source Select ............................................................................28 7.11 Register Description .............................................................................................29 8 Power Management and Sleep Modes ................................................. 31 8.1 Idle Mode ................................................................................................................32 8.2 ADC Noise Reduction Mode ..................................................................................32 8.3 Power-save Mode ..................................................................................................32 8.4 Power-down Mode .................................................................................................33 8.5 Power-off Mode ......................................................................................................33 8.6 Power Reduction Register ......................................................................................36 8.7 Minimizing Power Consumption .............................................................................37 9 System Control and Reset .................................................................... 39 9.1 Resetting the AVR ..................................................................................................39 9.2 Reset Sources ........................................................................................................39 9.3 Watchdog Timer .....................................................................................................43 9.4 Register Description ...............................................................................................46 10 Wake-up Timer ....................................................................................... 49 10.1 Overview ..............................................................................................................49 10.2 Register Description .............................................................................................49 11 Interrupts ................................................................................................ 51 11.1 Interrupt Vectors in ATmega406 ..........................................................................51 11.2 Moving Interrupts Between Application and Boot Space .....................................54 11.3 Register Description .............................................................................................55 12 External Interrupts ................................................................................. 56 12.1 Overview ..............................................................................................................56 12.2 Register Description .............................................................................................56 13 Low Voltage I/O-Ports ............................................................................ 60 13.1 Introduction ...........................................................................................................60 13.2 Low Voltage Ports as General Digital I/O .............................................................61 13.3 Alternate Port Functions .......................................................................................66 13.4 Register Description .............................................................................................73 14 High Voltage I/O Ports ........................................................................... 75 14.1 High Voltage Ports as General Digital Outputs ....................................................75 14.2 Configuring the Pin ...............................................................................................76 14.3 Register Description for High Voltage Output Ports .............................................76 ii ATmega406 2548F-AVR-03/2013 ATmega406 15 8-bit Timer/Counter0 with PWM ............................................................ 77 15.1 Overview ..............................................................................................................77 15.2 Timer/Counter Clock Sources ..............................................................................78 15.3 Counter Unit .........................................................................................................78 15.4 Output Compare Unit ...........................................................................................79 15.5 Compare Match Output Unit .................................................................................81 15.6 Modes of Operation ..............................................................................................82 15.7 Timer/Counter Timing Diagrams ..........................................................................86 15.8 8-bit Timer/Counter Register Description .............................................................88 16 16-bit Timer/Counter1 ............................................................................ 95 16.1 Overview ..............................................................................................................95 16.2 Accessing 16-bit Registers ...................................................................................96 16.3 Timer/Counter Clock Sources ..............................................................................98 16.4 Counter Unit .........................................................................................................99 16.5 Output Compare Unit ...........................................................................................99 16.6 16-bit Timer/Counter Register Description .........................................................100 17 Timer/Counter0 and Timer/Counter1 Prescalers .............................. 103 17.1 Internal Clock Source .........................................................................................103 17.2 Prescaler Reset ..................................................................................................103 17.3 External Clock Source ........................................................................................103 17.4 Register Description ...........................................................................................105 18 Coulomb Counter - Dedicated Fuel Gauging Sigma-delta ADC ...... 106 18.1 Features .............................................................................................................106 18.2 Operation ............................................................................................................107 19 Voltage Regulator ................................................................................ 114 19.1 Features .............................................................................................................114 19.2 Operation ............................................................................................................114 20 Voltage ADC - 10-channel General Purpose 12-bit Sigma-Delta ADC .. 116 20.1 Features .............................................................................................................116 20.2 Operation ............................................................................................................117 20.3 Register Description ...........................................................................................118 21 Voltage Reference and Temperature Sensor .................................... 121 21.1 Features .............................................................................................................121 iii 2548F-AVR-03/2013 21.2 Writing to Bandgap Calibration Registers ...........................................................122 21.3 Register Description for Voltage Reference and Temperature Sensor ..............123 22 Battery Protection ................................................................................ 125 22.1 Features .............................................................................................................125 22.2 Deep Under-voltage Protection ..........................................................................126 22.3 Discharge Over-current Protection .....................................................................126 22.4 Charge Over-current Protection .........................................................................126 22.5 Short-circuit Protection .......................................................................................127 22.6 Battery Protection CPU Interface .......................................................................127 22.7 Register Description for Battery Protection ........................................................128 23 FET Control .......................................................................................... 133 23.1 FET Driver ..........................................................................................................134 23.2 Register Description for FET Control ..................................................................134 24 Cell Balancing ...................................................................................... 136 24.1 Register Description ...........................................................................................137 25 2-wire Serial Interface .......................................................................... 138 25.1 Features .............................................................................................................138 25.2 Two-wire Serial Interface Bus Definition .............................................................138 25.3 Data Transfer and Frame Format .......................................................................139 25.4 Multi-master Bus Systems, Arbitration and Synchronization ..............................142 25.5 Overview of the TWI Module ..............................................................................144 25.6 TWI Register Description ...................................................................................147 25.7 Using the TWI .....................................................................................................150 25.8 Transmission Modes ..........................................................................................153 25.9 Multi-master Systems and Arbitration .................................................................167 25.10 Bus Connect/Disconnect for Two-wire Serial Interface ....................................169 26 JTAG Interface and On-chip Debug System ..................................... 171 26.1 Features .............................................................................................................171 26.2 Overview ............................................................................................................171 26.3 Test Access Port - TAP .....................................................................................171 26.4 TAP Controller ....................................................................................................173 26.5 Using the On-chip Debug System ......................................................................174 26.6 On-chip Debug Specific JTAG Instructions ........................................................175 26.7 On-chip Debug Related Register .......................................................................176 iv ATmega406 2548F-AVR-03/2013 ATmega406 26.8 Using the JTAG Programming Capabilities ........................................................177 27 Boot Loader Support - Read-While-Write Self-Programming ......... 178 27.1 Boot Loader Features .........................................................................................178 27.2 Application and Boot Loader Flash Sections ......................................................178 27.3 Read-While-Write and No Read-While-Write Flash Sections .............................179 27.4 Boot Loader Lock Bits ........................................................................................181 27.5 Entering the Boot Loader Program .....................................................................183 27.6 Addressing the Flash During Self-Programming ................................................185 27.7 Self-Programming the Flash ...............................................................................186 28 Memory Programming ......................................................................... 195 28.1 Program And Data Memory Lock Bits ................................................................195 28.2 Fuse Bits ............................................................................................................196 28.3 Signature Bytes ..................................................................................................198 28.4 Calibration Bytes ................................................................................................198 28.5 Page Size ...........................................................................................................198 28.6 Parallel Programming .........................................................................................199 28.7 Programming via the JTAG Interface .................................................................211 29 Operating Circuit .................................................................................. 223 30 Electrical Characteristics .................................................................... 225 30.1 Absolute Maximum Ratings* ..............................................................................225 30.2 DC Characteristics .............................................................................................225 30.3 General I/O Lines characteristics .......................................................................228 30.4 2-wire Serial Interface Characteristics ................................................................229 30.5 Reset Characteristics .........................................................................................230 30.6 Supply Current of I/O Modules ...........................................................................231 31 Typical Characteristics - Preliminary ................................................ 232 31.1 Pin Pull-up ..........................................................................................................232 31.2 Pin Driver Strength .............................................................................................233 31.3 Internal Oscillator Speed ....................................................................................234 32 Register Summary ............................................................................... 236 33 Instruction Set Summary .................................................................... 240 34 Ordering Information ........................................................................... 243 35 Packaging Information ........................................................................ 244 v 2548F-AVR-03/2013 35.1 48AA ...................................................................................................................244 36 Errata ..................................................................................................... 245 36.1 Rev. F .................................................................................................................245 36.2 Rev. E .................................................................................................................248 36.3 Rev. D ................................................................................................................250 37 Datasheet Revision History ................................................................ 254 37.1 Rev 2548F - 03/13 ..............................................................................................254 37.2 Rev 2548E - 07/06 .............................................................................................254 37.3 Rev 2548D - 06/05 .............................................................................................255 37.4 Rev 2548C - 05/05 .............................................................................................255 37.5 Rev 2548B - 04/05 .............................................................................................255 Table of Contents....................................................................................... i vi ATmega406 2548F-AVR-03/2013 Atmel Corporation 1600 Technology Drive Atmel Asia Limited Unit 01-5 & 16, 19F Atmel Munich GmbH Business Campus Atmel Japan G.K. 16F Shin-Osaki Kangyo Bldg San Jose, CA 95110 BEA Tower, Millennium City 5 Parkring 4 1-6-4 Osaki, Shinagawa-ku USA 418 Kwun Tong Roa D-85748 Garching b. Munich Tokyo 141-0032 Tel: (+1) (408) 441-0311 Kwun Tong, Kowloon GERMANY JAPAN Fax: (+1) (408) 487-2600 HONG KONG Tel: (+49) 89-31970-0 Tel: (+81) (3) 6417-0300 www.atmel.com Tel: (+852) 2245-6100 Fax: (+49) 89-3194621 Fax: (+81) (3) 6417-0370 Fax: (+852) 2722-1369 (c) 2013 Atmel Corporation. All rights reserved. / Rev.: 2548F-AVR-03/2013 Atmel(R), Atmel logo and combinations thereof, Enabling Unlimited Possibilities(R), AVR(R), and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others. Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.