Features • High performance, low power Atmel® AVR® 8-bit microcontroller • Advanced RISC architecture • • • • • • • • • – 131 powerful instructions – most single clock cycle execution – 32 × 8 general purpose working registers – Fully static operation – Up to 20 MIPS throughput at 20MHz – On-chip 2-cycle multiplier High endurance non-volatile memory segments – 4/8/16 Kbytes of in-system self-programmable flash program memory – 256/512/512 bytes EEPROM – 512/1K/1Kbytes internal SRAM – Write/erase cyles
ATmega48/88/168 1. Pin configurations Figure 1-1. Pinout Atmel ATmega48/88/168.
ATmega48/88/168 1.1 1.1.1 Pin descriptions VCC Digital supply voltage. 1.1.2 GND Ground. 1.1.3 Port B (PB7:0) XTAL1/XTAL2/TOSC1/TOSC2 Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated.
ATmega48/88/168 resistors are activated. The Port D pins are tri-stated when a reset condition becomes active, even if the clock is not running. The various special features of Port D are elaborated in “Alternate functions of port D” on page 84. 1.1.7 AVCC AVCC is the supply voltage pin for the A/D Converter, PC3:0, and ADC7:6. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low-pass filter. Note that PC6..
ATmega48/88/168 2. Overview The Atmel ATmega48/88/168 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 ATmega48/88/168 achieves throughputs approaching 1 MIPS per MHz allowing the system designer to optimize power consumption versus processing speed. Block diagram Block diagram. GND Figure 2-1. VCC 2.
ATmega48/88/168 architecture is more code efficient while achieving throughputs up to ten times faster than conventional CISC microcontrollers.
ATmega48/88/168 ATmega88 and ATmega168 support a real Read-While-Write Self-Programming mechanism. There is a separate Boot Loader Section, and the SPM instruction can only execute from there. In ATmega48, there is no Read-While-Write support and no separate Boot Loader Section. The SPM instruction can execute from the entire Flash.
ATmega48/88/168 3. Resources A comprehensive set of development tools, application notes and datasheets are available for download on http://www.atmel.com/avr. 4. Data retention Reliability Qualification results show that the projected data retention failure rate is much less than 1 PPM over 20 years at 85°C or 100 years at 25°C. 5. About code examples This documentation contains simple code examples that briefly show how to use various parts of the device.
ATmega48/88/168 7. AVR CPU core 7.1 Overview 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. 7.2 Architectural overview Figure 7-1. Block diagram of the AVR architecture.
ATmega48/88/168 The fast-access Register File contains 32 × 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.
ATmega48/88/168 7.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 Instruction Set Reference. This will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code.
ATmega48/88/168 • Bit 0 – C: Carry flag The Carry Flag C indicates a carry in an arithmetic or logic operation. See the “Instruction Set Description” for detailed information. 7.5 General purpose register file The register file is optimized for the AVR enhanced RISC instruction set.
ATmega48/88/168 7.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 7-3. Figure 7-3. The X-, Y-, and Z-registers.
ATmega48/88/168 7.6.1 SPH and SPL – Stack pointer high and stack pointer low 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 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 RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND RAMEND Read/write Initial value 7.
ATmega48/88/168 7.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.
ATmega48/88/168 Assembly code example in r16, SREG cli ; store SREG value ; disable interrupts during timed sequence sbi EECR, EEMPE ; start EEPROM write sbi EECR, EEPE 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<
ATmega48/88/168 8. AVR memories 8.1 Overview This section describes the different memories in the Atmel ATmega48/88/168. The AVR architecture has two main memory spaces, the Data Memory and the Program Memory space. In addition, the ATmega48/88/168 features an EEPROM Memory for data storage. All three memory spaces are linear and regular. 8.2 In-system reprogrammable flash program memory The ATmega48/88/168 contains 4K/8K/16K bytes On-chip In-System Reprogrammable Flash memory for program storage.
ATmega48/88/168 Figure 8-1. Program memory map, Atmel ATmega48. Program memory 0x0000 Application flash section 0x7FF Figure 8-2. Program memory map, Atmel ATmega88 and Atmel ATmega168.
ATmega48/88/168 8.3 SRAM data memory Figure 8-3 shows how the Atmel ATmega48/88/168 SRAM Memory is organized. The ATmega48/88/168 is a complex microcontroller with more peripheral units than can be supported within the 64 locations reserved in the Opcode for the IN and OUT instructions. For the Extended I/O space from 0x60 - 0xFF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used.
ATmega48/88/168 Figure 8-4. On-chip data SRAM access cycles. T1 T2 T3 clkCPU Address Compute address Address valid Write Data WR Read Data RD Memory access instruction 8.4 Next instruction EEPROM data memory The Atmel ATmega48/88/168 contains 256/512/512 bytes of data EEPROM memory. It is organized as a separate data space, in which single bytes can be read and written. The EEPROM has an endurance of at least 100,000 write/erase cycles.
ATmega48/88/168 An EEPROM data corruption can be caused by two situations when the voltage is too low. First, a regular write sequence to the EEPROM requires a minimum voltage to operate correctly. Secondly, the CPU itself can execute instructions incorrectly, if the supply voltage is too low. EEPROM data corruption can easily be avoided by following this design recommendation: Keep the AVR RESET active (low) during periods of insufficient power supply voltage.
ATmega48/88/168 8.6 8.6.1 Register description EEARH and EEARL – The EEPROM address register Bit 15 14 13 12 11 10 9 8 0x22 (0x42) – – – – – – – EEAR8 EEARH 0x21 (0x41) EEAR7 EEAR6 EEAR5 EEAR4 EEAR3 EEAR2 EEAR1 EEAR0 EEARL 7 6 5 4 3 2 1 0 Read/write Initial value R R R R R R R 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 X X X X X X X X X • Bits 15..
ATmega48/88/168 While EEPE is set, any write to EEPMn will be ignored. During reset, the EEPMn bits will be reset to 0b00 unless the EEPROM is busy programming. Table 8-1. EEPROM mode bits. EEPM1 EEPM0 Programming time 0 0 3.4ms Erase and write in one operation (atomic operation) 0 1 1.8ms Erase only 1 0 1.
ATmega48/88/168 When the write access time has elapsed, the EEPE bit is cleared by hardware. The user software can poll this bit and wait for a zero before writing the next byte. When EEPE has been set, the CPU is halted for two cycles before the next instruction is executed. • Bit 0 – EERE: EEPROM read enable The EEPROM Read Enable Signal EERE is the read strobe to the EEPROM. When the correct address is set up in the EEAR Register, the EERE bit must be written to a logic one to trigger the EEPROM read.
ATmega48/88/168 Assembly code example EEPROM_write: ; Wait for completion of previous write sbic EECR,EEPE rjmp EEPROM_write ; Set up address (r18:r17) in address register out EEARH, r18 out EEARL, r17 ; Write data (r16) to Data Register out EEDR,r16 ; Write logical one to EEMPE sbi EECR,EEMPE ; Start eeprom write by setting EEPE sbi EECR,EEPE ret C code example void EEPROM_write(unsigned int uiAddress, unsigned char ucData) { /* Wait for completion of previous write */ while(EECR & (1<
ATmega48/88/168 The next code examples show assembly and C functions for reading the EEPROM. The examples assume that interrupts are controlled so that no interrupts will occur during execution of these functions.
ATmega48/88/168 9. System clock and clock options 9.1 Clock systems and their distribution Figure 9-1 presents the principal clock systems in the AVR and their distribution. All of the clocks need not be active at a given time. In order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes, as described in “Power management and sleep modes” on page 39. The clock systems are detailed below. Figure 9-1. Clock distribution.
ATmega48/88/168 9.1.4 Asynchronous timer clock – clkASY The Asynchronous Timer clock allows the Asynchronous Timer/Counter to be clocked directly from an external clock or an external 32kHz clock crystal. The dedicated clock domain allows using this Timer/Counter as a real-time counter even when the device is in sleep mode. 9.1.5 ADC clock – clkADC The ADC is provided with a dedicated clock domain. This allows halting the CPU and I/O clocks in order to reduce noise generated by digital circuitry.
ATmega48/88/168 selectable delays are shown in Table 9-2. The frequency of the Watchdog Oscillator is voltage dependent as shown in “Typical characteristics” on page 315. Table 9-2. Number of watchdog oscillator cycles. Typical time-out (VCC = 5.0V) Typical time-out (VCC = 3.0V) Number of cycles 0ms 0ms 0 4.1ms 4.3ms 4K (4,096) 65ms 69ms 8K (8,192) Main purpose of the delay is to keep the AVR in reset until it is supplied with minimum VCC.
ATmega48/88/168 Figure 9-2. Crystal oscillator connections. C2 XTAL2 C1 XTAL1 GND The Low Power Oscillator can operate in three different modes, each optimized for a specific frequency range. The operating mode is selected by the fuses CKSEL3..1 as shown in Table 9-3. Table 9-3. Low power crystal oscillator operating modes(3). Frequency range (MHz) Recommended range for capacitors C1 and C2 (pF) CKSEL3..1(1) 0.4 - 0.9 – 100(2) 0.9 - 3.0 12 - 22 101 3.0 - 8.0 12 - 22 110 8.0 - 16.
ATmega48/88/168 Table 9-4. Start-up times for the low power crystal oscillator clock selection. (Continued) Start-up time from power-down and power-save Additional delay from reset (VCC = 5.0V) CKSEL0 SUT1..0 Crystal Oscillator, BOD enabled 16KCK 14CK 1 01 Crystal Oscillator, fast rising power 16KCK 14CK + 4.1ms 1 10 Crystal Oscillator, slowly rising power 16KCK 14CK + 65ms 1 11 Oscillator source/ power conditions Notes: 9.4 1.
ATmega48/88/168 Figure 9-3. Crystal oscillator connections. C2 C1 XTAL2 XTAL1 GND Table 9-6. Start-up times for the full swing crystal oscillator clock selection. Start-up time from power-down and power-save Additional delay from reset (VCC = 5.0V) CKSEL0 SUT1..0 Ceramic resonator, fast rising power 258CK 14CK + 4.
ATmega48/88/168 9.5 Low frequency crystal oscillator The device can utilize a 32.768kHz watch crystal as clock source by a dedicated low frequency crystal oscillator. The crystal should be connected as shown in Figure 9-2 on page 30. When this oscillator is selected, start-up times are determined by the SUT fuses and CKSEL0 as shown in Table 9-7. Table 9-7. Start-up times for the low frequency crystal oscillator clock selection.
ATmega48/88/168 Internal calibrated RC oscillator operating modes(1)(2). Table 9-8. Notes: Frequency range (MHz) CKSEL3..0 7.3 - 8.1 0010 1. The device is shipped with this option selected. 2. If 8MHz frequency exceeds the specification of the device (depends on VCC), the CKDIV8 Fuse can be programmed in order to divide the internal frequency by 8. When this Oscillator is selected, start-up times are determined by the SUT Fuses as shown in Table 9-9. Table 9-9.
ATmega48/88/168 9.8 External clock To drive the device from an external clock source, XTAL1 should be driven as shown in Figure 9-4. To run the device on an external clock, the CKSEL fuses must be programmed to “0000” (see Table 9-12). Table 9-12. Figure 9-4. Crystal oscillator clock frequency. Frequency CKSEL3..0 0 - 20MHz 0000 External clock drive configuration.
ATmega48/88/168 oscillator, can be selected when the clock is output on CLKO. If the System Clock Prescaler is used, it is the divided system clock that is output. 9.10 Timer/counter oscillator The device can operate its Timer/Counter2 from an external 32.768kHz watch crystal or a external clock source. The Timer/Counter Oscillator Pins (TOSC1 and TOSC2) are shared with XTAL1 and XTAL2.
ATmega48/88/168 9.12 9.12.1 Register description OSCCAL – Oscillator calibration register Bit (0x66) Read/write 7 6 5 4 3 2 1 0 CAL7 CAL6 CAL5 CAL4 CAL3 CAL2 CAL1 CAL0 R/W R/W R/W R/W R/W R/W R/W R/W Initial value OSCCAL Device specific calibration value • Bits 7..0 – CAL7..0: Oscillator calibration value The oscillator calibration register is used to trim the calibrated internal RC oscillator to remove process variations from the oscillator frequency.
ATmega48/88/168 The CKDIV8 fuse determines the initial value of the CLKPS bits. If CKDIV8 is unprogrammed, the CLKPS bits will be reset to “0000”. If CKDIV8 is programmed, CLKPS bits are reset to “0011”, giving a division factor of eight at start up. This feature should be used if the selected clock source has a higher frequency than the maximum frequency of the device at the present operating conditions. Note that any value can be written to the CLKPS bits regardless of the CKDIV8 Fuse setting.
ATmega48/88/168 10. Power management and sleep modes Sleep modes enable the application to shut down unused modules in the MCU, thereby saving power. The AVR provides various sleep modes allowing the user to tailor the power consumption to the application’s requirements. 10.1 Sleep modes Figure 9-1 on page 27 presents the different clock systems in the Atmel ATmega48/88/168, and their distribution. The figure is helpful in selecting an appropriate sleep mode.
ATmega48/88/168 Idle mode enables the MCU to wake up from external triggered interrupts as well as internal ones like the timer overflow and USART transmit complete interrupts. If wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the ACD bit in the analog comparator control and status register – ACSR. This will reduce power consumption in Idle mode. If the ADC is enabled, a conversion starts automatically when this mode is entered. 10.
ATmega48/88/168 If Timer/Counter2 is not running, power-down mode is recommended instead of power-save mode. The Timer/Counter2 can be clocked both synchronously and asynchronously in power-save mode. If Timer/Counter2 is not using the asynchronous clock, the timer/counter oscillator is stopped during sleep. If Timer/Counter2 is not using the synchronous clock, the clock source is stopped during sleep.
ATmega48/88/168 10.8.3 Brown-out detector If the brown-out detector is not needed by the application, this module should be turned off. If the brown-out detector is enabled by the BODLEVEL Fuses, it will be enabled in all sleep modes, and hence, always consume power. In the deeper sleep modes, this will contribute significantly to the total current consumption. Refer to “Brown-out detection” on page 47 for details on how to configure the brown-out detector. 10.8.
ATmega48/88/168 10.9 10.9.1 Register description SMCR – Sleep mode control register The sleep mode control register contains control bits for power management. Bit 7 6 5 4 3 2 1 0 0x33 (0x53) – – – – SM2 SM1 SM0 SE Read/write R R R R R/W R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 SMCR • Bits 7..4 Res: Reserved bits These bits are unused bits in the Atmel ATmega48/88/168, and will always read as zero. • Bits 3..1 – SM2..
ATmega48/88/168 10.9.2 PRR – Power reduction register Bit 7 6 5 4 3 2 1 0 PRTWI PRTIM2 PRTIM0 – PRTIM1 PRSPI PRUSART0 PRADC Read/write R/W R/W R/W R R/W R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 (0x64) PRR • Bit 7 - PRTWI: Power reduction TWI Writing a logic one to this bit shuts down the TWI by stopping the clock to the module. When waking up the TWI again, the TWI should be re initialized to ensure proper operation.
ATmega48/88/168 11. System control and reset 11.1 Resetting the AVR During reset, all I/O registers are set to their initial values, and the program starts execution from the reset vector. For the Atmel ATmega168, the instruction placed at the reset vector must be a JMP – absolute jump – instruction to the reset handling routine. For the Atmel ATmega48 and Atmel ATmega88, the instruction placed at the reset vector must be an RJMP – relative jump – instruction to the reset handling routine.
ATmega48/88/168 Figure 11-1. Reset logic. DATA BUS PORF BORF EXTRF WDRF MCU status register (MCUSR) Power-on reset circuit Brown-out reset circuit BODLEVEL [2..0] Pull-up resistor Reset circuit SPIKE FILTER Watchdog timer RSTDISBL Watchdog oscillator Clock generator CK Delay counters TIMEOUT CKSEL[3:0] SUT[1:0] 11.3 Power-on reset A power-on reset (POR) pulse is generated by an On-chip detection circuit. The detection level is defined in “System and reset characteristics” on page 307.
ATmega48/88/168 Figure 11-3. MCU start-up, RESET extended externally. VCC VPOT RESET TIME-OUT VRST tTOUT INTERNAL RESET 11.4 External reset An external reset is generated by a low level on the RESET pin. Reset pulses longer than the minimum pulse width (see “System and reset characteristics” on page 307) will generate a reset, even if the clock is not running. Shorter pulses are not guaranteed to generate a reset.
ATmega48/88/168 Figure 11-5. Brown-out reset during operation. VCC VBOT- VBOT+ RESET tTOUT TIME-OUT INTERNAL RESET 11.6 Watchdog system reset When the watchdog times out, it will generate a short reset pulse of one CK cycle duration. On the falling edge of this pulse, the delay timer starts counting the Time-out period tTOUT. Refer to page 49 for details on operation of the watchdog timer. Figure 11-6. Watchdog system reset during operation. CC CK 11.
ATmega48/88/168 Thus, when the BOD is not enabled, after setting the ACBG bit or enabling the ADC, the user must always allow the reference to start up before the output from the analog comparator or ADC is used. To reduce power consumption in power-down mode, the user can avoid the three conditions above to ensure that the reference is turned off before entering power-down mode. 11.8 11.8.
ATmega48/88/168 The watchdog always on (WDTON) fuse, if programmed, will force the watchdog timer to system reset mode. With the fuse programmed the system reset mode bit (WDE) and Interrupt mode bit (WDIE) are locked to 1 and 0 respectively. To further ensure program security, alterations to the Watchdog setup must follow timed sequences. The sequence for clearing WDE and changing time-out configuration is as follows: 1.
ATmega48/88/168 Assembly code example(1) WDT_off: ; Turn off global interrupt cli ; Reset Watchdog Timer wdr ; Clear WDRF in MCUSR in r16, MCUSR andi r16, (0xff & (0<
ATmega48/88/168 The following code example shows one assembly and one C function for changing the time-out value of the watchdog timer. Assembly code example(1) WDT_Prescaler_Change: ; Turn off global interrupt cli ; Reset Watchdog Timer wdr ; Start timed sequence lds r16, WDTCSR r16, (1<
ATmega48/88/168 11.9 11.9.1 Register description MCUSR – MCU status register The MCU status register provides information on which reset source caused an MCU reset. Bit 7 6 5 4 3 2 1 0 0x35 (0x55) – – – – WDRF BORF EXTRF PORF Read/write R R R R R/W R/W R/W R/W Initial value 0 0 0 0 MCUSR See Bit Description • Bit 7..4: Res: Reserved bits These bits are unused bits in the Atmel ATmega48/88/168, and will always read as zero.
ATmega48/88/168 If WDE is set, the watchdog timer is in interrupt and system reset mode. The first time-out in the watchdog timer will set WDIF. Executing the corresponding interrupt vector will clear WDIE and WDIF automatically by hardware (the watchdog goes to system reset mode). This is useful for keeping the watchdog timer security while using the interrupt. To stay in interrupt and system reset mode, WDIE must be set after each interrupt.
ATmega48/88/168 • Bit 5, 2..0 - WDP3..0: Watchdog timer prescaler 3, 2, 1, and 0 The WDP3..0 bits determine the watchdog timer prescaling when the watchdog timer is running. The different prescaling values and their corresponding time-out periods are shown in Table 112. Table 11-2. Watchdog timer prescale select. WDP3 WDP2 WDP1 WDP0 Number of WDT oscillator cycles Typical time-out at VCC = 5.
ATmega48/88/168 12. Interrupts 12.1 Overview This section describes the specifics of the interrupt handling as performed in the Atmel ATmega48/88/168. For a general explanation of the AVR interrupt handling, refer to “Reset and interrupt handling” on page 15.
ATmega48/88/168 Table 12-1. Reset and interrupt vectors in ATmega48. (Continued) Vector no.
ATmega48/88/168 12.3 Interrupt vectors in Atmel ATmega88 Table 12-2. Reset and interrupt vectors in ATmega88. Vector no.
ATmega48/88/168 Table 12-3. Reset and interrupt vectors placement in Atmel ATmega88(1). BOOTRST IVSEL 1 Note: Reset address Interrupt vectors start address 0 0x000 0x001 1 1 0x000 Boot reset address + 0x001 0 0 Boot reset address 0x001 0 1 Boot reset address Boot reset address + 0x001 1. The boot reset address is shown in Table 27-6 on page 281. For the BOOTRST Fuse “1” means unprogrammed while “0” means programmed.
ATmega48/88/168 When the BOOTRST fuse is unprogrammed, the boot section size set to 2Kbytes 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 in Atmel ATmega88 is: Address Labels Code Comments 0x000 RESET: ldi 0x001 out SPH,r16 r16,high(RAMEND); Main program start 0x002 ldi r16,low(RAMEND) 0x003 0x004 out sei SPL,r16 0x005 ; Set Stack Pointer to top of RAM ; Enable
ATmega48/88/168 12.4 0xC1C ldi r16,low(RAMEND) 0xC1D 0xC1E out sei SPL,r16 0xC1F ; Enable interrupts xxx Interrupt vectors in Atmel ATmega168 Table 12-4. Reset and interrupt vectors in ATmega168. Vector no.
ATmega48/88/168 Table 12-5 shows reset and interrupt vectors placement for the various combinations of BOOTRST and IVSEL settings. If the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. This is also the case if the reset vector is in the application section while the interrupt vectors are in the boot section or vice versa. Table 12-5. Reset and interrupt vectors placement in Atmel ATmega168(1).
ATmega48/88/168 0x0034 out SPH,r16 0x0035 ldi r16, low(RAMEND) 0x0036 out SPL,r16 0x0037 sei 0x0038 ... ; Enable interrupts ... ... ; Set Stack Pointer to top of RAM xxx ...
ATmega48/88/168 Address Labels Code Comments ; .org 0x1C00 0x1C00 jmp RESET ; Reset handler 0x1C02 jmp EXT_INT0 ; IRQ0 Handler 0x1C04 jmp EXT_INT1 ; IRQ1 Handler ... ... ... ; 0x1C32 jmp SPM_RDY ; Store Program Memory Ready Handler ; 12.4.1 12.5 12.5.
ATmega48/88/168 • Bit 0 – IVCE: Interrupt vector change enable The IVCE bit must be written to logic one to enable change of the IVSEL bit. IVCE is cleared by hardware four cycles after it is written or when IVSEL is written. Setting the IVCE bit will disable interrupts, as explained in the IVSEL description above. See code example below.
ATmega48/88/168 13. External interrupts The external interrupts are triggered by the INT0 and INT1 pins or any of the PCINT23..0 pins. Observe that, if enabled, the interrupts will trigger even if the INT0 and INT1 or PCINT23..0 pins are configured as outputs. This feature provides a way of generating a software interrupt. The pin change interrupt PCI2 will trigger if any enabled PCINT23..16 pin toggles. The pin change interrupt PCI1 will trigger if any enabled PCINT14..8 pin toggles.
ATmega48/88/168 13.2 13.2.1 Register description EICRA – External interrupt control register A The external interrupt control register A contains control bits for interrupt sense control. Bit 7 6 5 4 3 2 1 0 (0x69) – – – – ISC11 ISC10 ISC01 ISC00 Read/write R R R R R/W R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 EICRA • Bit 7..4 – Res: Reserved bits These bits are unused bits in the Atmel ATmega48/88/168, and will always read as zero.
ATmega48/88/168 13.2.2 EIMSK – External interrupt mask register Bit 7 6 5 4 3 2 1 0 0x1D (0x3D) – – – – – – INT1 INT0 Read/write R R R R R R R/W R/W Initial value 0 0 0 0 0 0 0 0 EIMSK • Bit 7..2 – Res: Reserved bits These bits are unused bits in the Atmel ATmega48/88/168, and will always read as zero.
ATmega48/88/168 13.2.4 PCICR – Pin change interrupt control register Bit 7 6 5 4 3 2 1 0 (0x68) – – – – – PCIE2 PCIE1 PCIE0 Read/write R R R R R R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 PCICR • Bit 7..3 - Res: Reserved bits These bits are unused bits in the Atmel ATmega48/88/168, and will always read as zero.
ATmega48/88/168 • Bit 0 - PCIF0: Pin change interrupt flag 0 When a logic change on any PCINT7..0 pin triggers an interrupt request, PCIF0 becomes set (one). If the I-bit in SREG and the PCIE0 bit in PCICR are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it. 13.2.
ATmega48/88/168 14. I/O-ports 14.1 Overview All AVR ports have true Read-Modify-Write functionality when used as general digital I/O ports. This means that the direction of one port pin can be changed without unintentionally changing the direction of any other pin with the SBI and CBI instructions. The same applies when changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as input).
ATmega48/88/168 Note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital I/O. 14.2 Ports as general digital I/O The ports are bi-directional I/O ports with optional internal pull-ups. Figure 14-2 shows a functional description of one I/O-port pin, here generically called Pxn. Figure 14-2. General digital I/O(1).
ATmega48/88/168 If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero). 14.2.2 Toggling the pin Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port. 14.2.
ATmega48/88/168 Figure 14-3. Synchronization when reading an externally applied pin value. SYSTEM CLK INSTRUCTIONS XXX XXX in r17, PINx SYNC LATCH PINxn r17 0x00 0xFF t pd, max t pd, min Consider the clock period starting shortly after the first falling edge of the system clock. The latch is closed when the clock is low, and goes transparent when the clock is high, as indicated by the shaded region of the “SYNC LATCH” signal. The signal value is latched when the system clock goes low.
ATmega48/88/168 Assembly code example(1) ... ; Define pull-ups and set outputs high ; Define directions for port pins ldi r16,(1<
ATmega48/88/168 ing inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (Reset, Active mode and Idle mode). The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. In this case, the pull-up will be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or pull-down.
ATmega48/88/168 Table 14-2 summarizes the function of the overriding signals. The pin and port indexes from Figure 14-5 on page 76 are not shown in the succeeding tables. The overriding signals are generated internally in the modules having the alternate function. Table 14-2. Generic description of overriding signals for alternate functions. Signal name Full name Description PUOE Pull-up override enable If this signal is set, the pull-up enable is controlled by the PUOV signal.
ATmega48/88/168 14.3.1 Alternate functions of port B The port B pins with alternate functions are shown in Table 14-3. Table 14-3. Port pin Port B pins alternate functions.
ATmega48/88/168 TOSC1: Timer Oscillator pin 1. Used only if internal calibrated RC Oscillator is selected as chip clock source, and the asynchronous timer is enabled by the correct setting in ASSR. When the AS2 bit in ASSR is set (one) to enable asynchronous clocking of Timer/Counter2, pin PB6 is disconnected from the port, and becomes the input of the inverting Oscillator amplifier. In this mode, a crystal Oscillator is connected to this pin, and the pin can not be used as an I/O pin.
ATmega48/88/168 • OC1A/PCINT1 – Port B, bit 1 OC1A, Output Compare Match output: The PB1 pin can serve as an external output for the Timer/Counter1 Compare Match A. The PB1 pin has to be configured as an output (DDB1 set (one)) to serve this function. The OC1A pin is also the output pin for the PWM mode timer function. PCINT1: Pin Change Interrupt source 1. The PB1 pin can serve as an external interrupt source.
ATmega48/88/168 Notes: 1. INTRC means that one of the internal RC Oscillators are selected (by the CKSEL fuses), EXTCK means that external clock is selected (by the CKSEL fuses). Table 14-5. 14.3.2 Overriding signals for alternate functions in PB3..PB0.
ATmega48/88/168 • RESET/PCINT14 – Port C, bit 6 RESET, Reset pin: When the RSTDISBL Fuse is programmed, this pin functions as a normal I/O pin, and the part will have to rely on Power-on Reset and Brown-out Reset as its reset sources. When the RSTDISBL Fuse is unprogrammed, the reset circuitry is connected to the pin, and the pin can not be used as an I/O pin. If PC6 is used as a reset pin, DDC6, PORTC6 and PINC6 will all read 0. PCINT14: Pin Change Interrupt source 14.
ATmega48/88/168 PCINT9: Pin Change Interrupt source 9. The PC1 pin can serve as an external interrupt source. • ADC0/PCINT8 – Port C, bit 0 PC0 can also be used as ADC input Channel 0. Note that ADC input channel 0 uses analog power. PCINT8: Pin Change Interrupt source 8. The PC0 pin can serve as an external interrupt source. Table 14-7 and Table 14-8 relate the alternate functions of Port C to the overriding signals shown in Figure 14-5 on page 76. Table 14-7.
ATmega48/88/168 14.3.3 Alternate functions of port D The port D pins with alternate functions are shown in Table 14-9. Table 14-9. Port pin Port D pins alternate functions.
ATmega48/88/168 • T1/OC0B/PCINT21 – Port D, bit 5 T1, Timer/Counter1 counter source. OC0B, Output Compare Match output: The PD5 pin can serve as an external output for the Timer/Counter0 Compare Match B. The PD5 pin has to be configured as an output (DDD5 set (one)) to serve this function. The OC0B pin is also the output pin for the PWM mode timer function. PCINT21: Pin Change Interrupt source 21. The PD5 pin can serve as an external interrupt source.
ATmega48/88/168 Table 14-10. Overriding signals for alternate functions PD7..PD4.
ATmega48/88/168 14.4 14.4.1 Register description MCUCR – MCU control register Bit 7 6 5 4 3 2 1 0 0x35 (0x55) – – – PUD – – IVSEL IVCE Read/write R R R R/W R R R/W R/W Initial value 0 0 0 0 0 0 0 0 MCUCR • Bit 4 – PUD: Pull-up disable When this bit is written to one, the pull-ups in the I/O ports are disabled even if the DDxn and PORTxn Registers are configured to enable the pull-ups ({DDxn, PORTxn} = 0b01).
ATmega48/88/168 14.4.8 PORTD – The port D data register Bit 7 6 5 4 3 2 1 0 PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 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 0x0B (0x2B) 14.4.9 DDRD – The port D data direction register Bit 7 6 5 4 3 2 1 0 DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0 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 0x0A (0x2A) 14.4.
ATmega48/88/168 15. 8-bit Timer/Counter0 with PWM 15.1 Features • • • • • • • 15.2 Two independent output compare units Double buffered output compare registers Clear timer on compare match (auto reload) Glitch free, phase correct pulse width modulator (PWM) Variable PWM period Frequency generator Three independent interrupt sources (TOV0, OCF0A, and OCF0B) Overview Timer/Counter0 is a general purpose 8-bit Timer/Counter module, with two independent Output Compare Units, and with PWM support.
ATmega48/88/168 Figure 15-1. 8-bit timer/counter block diagram. Count Clear Direction TOVn (Int.req.) Control logic clkTn Clock select Edge detector TOP Tn BOTTOM (From prescaler) Timer/counter TCNTn = =0 OCnA (Int.req.) Waveform generation = OCnA DATA BUS OCRnA Fixed TOP value Waveform generation = OCnB OCRnB TCCRnA 15.2.1 OCnB (Int.req.) TCCRnB Definitions Many register and bit references in this section are written in general form.
ATmega48/88/168 The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on the T0 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source is selected. The output from the Clock Select logic is referred to as the timer clock (clkT0).
ATmega48/88/168 The counting sequence is determined by the setting of the WGM01 and WGM00 bits located in the Timer/Counter Control Register (TCCR0A) and the WGM02 bit located in the Timer/Counter Control Register B (TCCR0B). There are close connections between how the counter behaves (counts) and how waveforms are generated on the Output Compare outputs OC0A and OC0B. For more details about advanced counting sequences and waveform generation, see “Modes of operation” on page 94.
ATmega48/88/168 The OCR0x Register access may seem complex, but this is not case. When the double buffering is enabled, the CPU has access to the OCR0x Buffer Register, and if double buffering is disabled the CPU will access the OCR0x directly. 15.5.1 Force output compare In non-PWM waveform generation modes, the match output of the comparator can be forced by writing a one to the Force Output Compare (FOC0x) bit.
ATmega48/88/168 Figure 15-4. Compare match output unit, schematic. COMnx1 COMnx0 FOCn Waveform generator D Q 1 OCnx DATA BUS D 0 OCnx pin Q PORT D Q DDR clk I/O The general I/O port function is overridden by the Output Compare (OC0x) from the Waveform Generator if either of the COM0x1:0 bits are set. However, the OC0x pin direction (input or output) is still controlled by the Data Direction Register (DDR) for the port pin.
ATmega48/88/168 15.7.1 Normal mode The simplest mode of operation is the Normal mode (WGM02:0 = 0). In this mode the counting direction is always up (incrementing), and no counter clear is performed. The counter simply overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then restarts from the bottom (0x00). In normal operation the Timer/Counter Overflow Flag (TOV0) will be set in the same timer clock cycle as the TCNT0 becomes zero.
ATmega48/88/168 the pin is set to output. The waveform generated will have a maximum frequency of fOC0 = fclk_I/O/2 when OCR0A is set to zero (0x00). The waveform frequency is defined by the following equation: f clk_I/O f OCnx = ------------------------------------------------2 ⋅ N ⋅ ( 1 + OCRnx ) The N variable represents the prescale factor (1, 8, 64, 256, or 1024). As for the Normal mode of operation, the TOV0 Flag is set in the same timer clock cycle that the counter counts from MAX to 0x00. 15.7.
ATmega48/88/168 In fast PWM mode, the compare unit allows generation of PWM waveforms on the OC0x pins. Setting the COM0x1:0 bits to two will produce a non-inverted PWM and an inverted PWM output can be generated by setting the COM0x1:0 to three: Setting the COM0A1:0 bits to one allows the OC0A pin to toggle on Compare Matches if the WGM02 bit is set. This option is not available for the OC0B pin (see Table 15-6 on page 102).
ATmega48/88/168 Figure 15-7. Phase correct PWM mode, timing diagram. OCnx interrupt flag set OCRnx update TOVn interrupt flag set TCNTn (COMnx1:0 = 2) OCnx (COMnx1:0 = 3) OCnx Period 1 2 3 The Timer/Counter Overflow Flag (TOV0) is set each time the counter reaches BOTTOM. The Interrupt Flag can be used to generate an interrupt each time the counter reaches the BOTTOM value. In phase correct PWM mode, the compare unit allows generation of PWM waveforms on the OC0x pins.
ATmega48/88/168 symmetry around BOTTOM the OCnx value at MAX must correspond to the result of an upcounting Compare Match • The timer starts counting from a value higher than the one in OCRnx, and for that reason misses the Compare Match and hence the OCnx change that would have happened on the way up 15.8 Timer/counter timing diagrams The Timer/Counter is a synchronous design and the timer clock (clkT0) is therefore shown as a clock enable signal in the following figures.
ATmega48/88/168 Figure 15-10. Timer/counter timing diagram, setting of OCF0x, with prescaler (fclk_I/O/8). clkI/O clkTn (clkI/O /8) TCNTn OCRnx - 1 OCRnx OCRnx + 1 OCRnx + 2 OCRnx Value OCRnx OCFnx Figure 15-11 shows the setting of OCF0A and the clearing of TCNT0 in CTC mode and fast PWM mode where OCR0A is TOP. Figure 15-11. Timer/counter timing diagram, clear timer on compare match mode, with prescaler (fclk_I/O/8).
ATmega48/88/168 15.9 15.9.1 Register description TCCR0A – Timer/counter control register A Bit 7 6 5 4 3 2 1 0 0x24 (0x44) COM0A1 COM0A0 COM0B1 COM0B0 – – WGM01 WGM00 Read/write R/W R/W R/W R/W R R R/W R/W Initial value 0 0 0 0 0 0 0 0 TCCR0A • Bits 7:6 – COM0A1:0: Compare match output A mode These bits control the Output Compare pin (OC0A) behavior.
ATmega48/88/168 Table 15-4 shows the COM0A1:0 bit functionality when the WGM02:0 bits are set to phase correct PWM mode. Table 15-4. Compare output mode, phase correct PWM mode(1).
ATmega48/88/168 Note: 1. A special case occurs when OCR0B equals TOP and COM0B1 is set. In this case, the Compare Match is ignored, but the set or clear is done at TOP. See “Fast PWM mode” on page 96 for more details. Table 15-7 shows the COM0B1:0 bit functionality when the WGM02:0 bits are set to phase correct PWM mode. Table 15-7. Compare output mode, phase correct PWM mode(1).
ATmega48/88/168 15.9.2 TCCR0B – Timer/counter control register B Bit 7 6 5 4 3 2 1 0 0x25 (0x45) FOC0A FOC0B – – WGM02 CS02 CS01 CS00 Read/write W W R R R/W R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 TCCR0B • Bit 7 – FOC0A: Force output compare A The FOC0A bit is only active when the WGM bits specify a non-PWM mode. However, for ensuring compatibility with future devices, this bit must be set to zero when TCCR0B is written when operating in PWM mode.
ATmega48/88/168 Table 15-9. Clock select bit description. CS02 CS01 CS00 Description 0 0 0 No clock source (timer/counter stopped) 0 0 1 clkI/O/(no prescaling) 0 1 0 clkI/O/8 (from prescaler) 0 1 1 clkI/O/64 (from prescaler) 1 0 0 clkI/O/256 (from prescaler) 1 0 1 clkI/O/1024 (from prescaler) 1 1 0 External clock source on T0 pin. Clock on falling edge. 1 1 1 External clock source on T0 pin. Clock on rising edge.
ATmega48/88/168 15.9.6 TIMSK0 – Timer/counter interrupt mask register Bit 7 6 5 4 3 2 1 0 (0x6E) – – – – – OCIE0B OCIE0A TOIE0 Read/write R R R R R R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 TIMSK0 • Bits 7..3 – Res: Reserved bits These bits are reserved bits in the Atmel ATmega48/88/168 and will always read as zero.
ATmega48/88/168 • Bit 0 – TOV0: Timer/Counter0 overflow flag The bit TOV0 is set when an overflow occurs in Timer/Counter0. TOV0 is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, TOV0 is cleared by writing a logic one to the flag. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt Enable), and TOV0 are set, the Timer/Counter0 Overflow interrupt is executed. The setting of this flag is dependent of the WGM02:0 bit setting.
ATmega48/88/168 16. 16-bit Timer/Counter1 with PWM 16.1 Features • • • • • • • • • • • 16.
ATmega48/88/168 Figure 16-1. 16-bit timer/counter block diagram(1). Count Clear Direction TOVn (Int.req.) Control logic clkTn Clock select Edge detector TOP Tn BOTTOM (From prescaler) Timer/counter TCNTn = =0 OCnA (Int.req.) Waveform generation = OCnA DATA BUS OCRnA OCnB (Int.req.) Fixed TOP values Waveform generation = OCRnB OCnB (From analog comparator output) ICFn (Int.req.) Edge detector ICRn Noise canceler ICPn TCCRnA Note: 16.2.1 TCCRnB 1.
ATmega48/88/168 put compare units” on page 116.. The compare match event will also set the Compare Match Flag (OCF1A/B) which can be used to generate an Output Compare interrupt request. The Input Capture Register can capture the Timer/Counter value at a given external (edge triggered) event on either the Input Capture pin (ICP1) or on the Analog Comparator pins (See “Analog comparator” on page 241.
ATmega48/88/168 Assembly code examples(1) ... ; Set TCNT1 to 0x01FF ldi r17,0x01 ldi r16,0xFF out TCNT1H,r17 out TCNT1L,r16 ; Read TCNT1 into r17:r16 in r16,TCNT1L in r17,TCNT1H ... C code examples(1) unsigned int i; ... /* Set TCNT1 to 0x01FF */ TCNT1 = 0x1FF; /* Read TCNT1 into i */ i = TCNT1; ... Note: 1. See ”About code examples” on page 8.
ATmega48/88/168 Assembly code example(1) TIM16_ReadTCNT1: ; Save global interrupt flag in r18,SREG ; Disable interrupts cli ; Read TCNT1 into r17:r16 in r16,TCNT1L in r17,TCNT1H ; Restore global interrupt flag out SREG,r18 ret C code example(1) unsigned int TIM16_ReadTCNT1( void ) { unsigned char sreg; unsigned int i; /* Save global interrupt flag */ sreg = SREG; /* Disable interrupts */ _CLI(); /* Read TCNT1 into i */ i = TCNT1; /* Restore global interrupt flag */ SREG = sreg; return i; } Note: 1.
ATmega48/88/168 Assembly code example(1) TIM16_WriteTCNT1: ; Save global interrupt flag in r18,SREG ; Disable interrupts cli ; Set TCNT1 to r17:r16 out TCNT1H,r17 out TCNT1L,r16 ; Restore global interrupt flag out SREG,r18 ret C code example(1) void TIM16_WriteTCNT1( unsigned int i ) { unsigned char sreg; unsigned int i; /* Save global interrupt flag */ sreg = SREG; /* Disable interrupts */ _CLI(); /* Set TCNT1 to i */ TCNT1 = i; /* Restore global interrupt flag */ SREG = sreg; } Note: 1.
ATmega48/88/168 16.5 Counter unit The main part of the 16-bit Timer/Counter is the programmable 16-bit bi-directional counter unit. Figure 16-2 shows a block diagram of the counter and its surroundings. Figure 16-2. Counter unit block diagram. DATA BUS (8-bit) TOVn (Int.req.
ATmega48/88/168 16.6 Input capture unit The Timer/Counter incorporates an Input Capture unit that can capture external events and give them a time-stamp indicating time of occurrence. The external signal indicating an event, or multiple events, can be applied via the ICP1 pin or alternatively, via the analog-comparator unit. The time-stamps can then be used to calculate frequency, duty-cycle, and other features of the signal applied.
ATmega48/88/168 16.6.1 Input capture trigger source The main trigger source for the Input Capture unit is the Input Capture pin (ICP1). Timer/Counter1 can alternatively use the Analog Comparator output as trigger source for the Input Capture unit. The Analog Comparator is selected as trigger source by setting the Analog Comparator Input Capture (ACIC) bit in the Analog Comparator Control and Status Register (ACSR). Be aware that changing trigger source can trigger a capture.
ATmega48/88/168 pare Flag generates an Output Compare interrupt. The OCF1x Flag is automatically cleared when the interrupt is executed. Alternatively the OCF1x Flag can be cleared by software by writing a logical one to its I/O bit location. The Waveform Generator uses the match signal to generate an output according to operating mode set by the Waveform Generation mode (WGM13:0) bits and Compare Output mode (COM1x1:0) bits.
ATmega48/88/168 temporary register (TEMP). However, it is a good practice to read the low byte first as when accessing other 16-bit registers. Writing the OCR1x Registers must be done via the TEMP Register since the compare of all 16 bits is done continuously. The high byte (OCR1xH) has to be written first. When the high byte I/O location is written by the CPU, the TEMP Register will be updated by the value written.
ATmega48/88/168 Figure 16-5. Compare match output unit, schematic. COMnx1 COMnx0 FOCnx Waveform generator D Q 1 OCnx DATA BUS D 0 OCnx pin Q PORT D Q DDR clk I/O The general I/O port function is overridden by the Output Compare (OC1x) from the Waveform Generator if either of the COM1x1:0 bits are set. However, the OC1x pin direction (input or output) is still controlled by the Data Direction Register (DDR) for the port pin.
ATmega48/88/168 put mode (COM1x1:0) bits. The Compare Output mode bits do not affect the counting sequence, while the Waveform Generation mode bits do. The COM1x1:0 bits control whether the PWM output generated should be inverted or not (inverted or non-inverted PWM). For non-PWM modes the COM1x1:0 bits control whether the output should be set, cleared or toggle at a compare match (See “Compare match output unit” on page 118.
ATmega48/88/168 Figure 16-6. CTC mode, timing diagram. OCnA interrupt flag set or ICFn interrupt flag set (interrupt on TOP) TCNTn OCnA (toggle) Period (COMnA1:0 = 1) 1 2 3 4 An interrupt can be generated at each time the counter value reaches the TOP value by either using the OCF1A or ICF1 Flag according to the register used to define the TOP value. If the interrupt is enabled, the interrupt handler routine can be used for updating the TOP value.
ATmega48/88/168 The PWM resolution for fast PWM can be fixed to 8-bit, 9-bit, or 10-bit, or defined by either ICR1 or OCR1A. The minimum resolution allowed is 2-bit (ICR1 or OCR1A set to 0x0003), and the maximum resolution is 16-bit (ICR1 or OCR1A set to MAX).
ATmega48/88/168 to be written anytime. When the OCR1A I/O location is written the value written will be put into the OCR1A Buffer Register. The OCR1A Compare Register will then be updated with the value in the Buffer Register at the next timer clock cycle the TCNT1 matches TOP. The update is done at the same timer clock cycle as the TCNT1 is cleared and the TOV1 Flag is set. Using the ICR1 Register for defining TOP works well when using fixed TOP values.
ATmega48/88/168 to 0x0003), and the maximum resolution is 16-bit (ICR1 or OCR1A set to MAX). The PWM resolution in bits can be calculated by using the following equation: log ( TOP + 1 ) R PCPWM = ----------------------------------log ( 2 ) In phase correct PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGM13:0 = 1, 2, or 3), the value in ICR1 (WGM13:0 = 10), or the value in OCR1A (WGM13:0 = 11).
ATmega48/88/168 implies that the length of the falling slope is determined by the previous TOP value, while the length of the rising slope is determined by the new TOP value. When these two values differ the two slopes of the period will differ in length. The difference in length gives the unsymmetrical result on the output. It is recommended to use the phase and frequency correct mode instead of the phase correct mode when changing the TOP value while the Timer/Counter is running.
ATmega48/88/168 the maximum resolution is 16-bit (ICR1 or OCR1A set to MAX). The PWM resolution in bits can be calculated using the following equation: log ( TOP + 1 ) R PFCPWM = ----------------------------------log ( 2 ) In phase and frequency correct PWM mode the counter is incremented until the counter value matches either the value in ICR1 (WGM13:0 = 8), or the value in OCR1A (WGM13:0 = 9). The counter has then reached the TOP and changes the count direction.
ATmega48/88/168 Using the ICR1 Register for defining TOP works well when using fixed TOP values. By using ICR1, the OCR1A Register is free to be used for generating a PWM output on OC1A. However, if the base PWM frequency is actively changed by changing the TOP value, using the OCR1A as TOP is clearly a better choice due to its double buffer feature. In phase and frequency correct PWM mode, the compare units allow generation of PWM waveforms on the OC1x pins.
ATmega48/88/168 Figure 16-11. Timer/counter timing diagram, setting of OCF1x, with prescaler (fclk_I/O/8). clkI/O clkTn (clkI/O /8) TCNTn OCRnx - 1 OCRnx OCRnx + 1 OCRnx + 2 OCRnx value OCRnx OCFnx Figure 16-12 shows the count sequence close to TOP in various modes. When using phase and frequency correct PWM mode the OCR1x Register is updated at BOTTOM. The timing diagrams will be the same, but TOP should be replaced by BOTTOM, TOP-1 by BOTTOM+1 and so on.
ATmega48/88/168 Figure 16-13 shows the same timing data, but with the prescaler enabled. Figure 16-13. Timer/counter timing diagram, with prescaler (fclk_I/O/8).
ATmega48/88/168 16.11 Register description 16.11.1 TCCR1A – Timer/Counter1 control register A Bit 7 6 5 4 3 2 1 0 COM1A1 COM1A0 COM1B1 COM1B0 – – WGM11 WGM10 Read/write R/W R/W R/W R/W R R R/W R/W Initial value 0 0 0 0 0 0 0 0 (0x80) TCCR1A • Bit 7:6 – COM1A1:0: Compare output mode for channel A • Bit 5:4 – COM1B1:0: Compare output mode for channel B The COM1A1:0 and COM1B1:0 control the Output Compare pins (OC1A and OC1B respectively) behavior.
ATmega48/88/168 Note: 1. A special case occurs when OCR1A/OCR1B equals TOP and COM1A1/COM1B1 is set. In this case the compare match is ignored, but the set or clear is done at BOTTOM. See “Fast PWM mode” on page 121. for more details. Table 16-3 shows the COM1x1:0 bit functionality when the WGM13:0 bits are set to the phase correct or the phase and frequency correct, PWM mode. Table 16-3. Compare output mode, phase correct and phase and frequency correct PWM(1).
ATmega48/88/168 Waveform generation mode bit description(1). Table 16-4.
ATmega48/88/168 When the ICR1 is used as TOP value (see description of the WGM13:0 bits located in the TCCR1A and the TCCR1B Register), the ICP1 is disconnected and consequently the Input Capture function is disabled. • Bit 5 – Reserved bit This bit is reserved for future use. For ensuring compatibility with future devices, this bit must be written to zero when TCCR1B is written. • Bit 4:3 – WGM13:2: Waveform generation mode See TCCR1A Register description.
ATmega48/88/168 The FOC1A/FOC1B bits are always read as zero. 16.11.4 TCNT1H and TCNT1L – Timer/Counter1 Bit 7 6 5 4 3 (0x85) TCNT1[15:8] (0x84) TCNT1[7:0] 2 1 0 TCNT1H TCNT1L 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 two Timer/Counter I/O locations (TCNT1H and TCNT1L, combined TCNT1) give direct access, both for read and for write operations, to the Timer/Counter unit 16-bit counter.
ATmega48/88/168 16.11.7 ICR1H and ICR1L – Input capture register 1 Bit 7 6 5 4 3 (0x87) ICR1[15:8] (0x86) ICR1[7:0] 2 1 0 ICR1H ICR1L 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 Input Capture is updated with the counter (TCNT1) value each time an event occurs on the ICP1 pin (or optionally on the Analog Comparator output for Timer/Counter1). The Input Capture can be used for defining the counter TOP value.
ATmega48/88/168 16.11.9 TIFR1 – Timer/Counter1 interrupt flag register Bit 7 6 5 4 3 2 1 0 0x16 (0x36) – – ICF1 – – OCF1B OCF1A TOV1 Read/write R R R/W R R R/W R/W R/W Initial value 0 0 0 0 0 0 0 0 TIFR1 • Bit 7, 6 – Res: Reserved bits These bits are unused bits in the Atmel ATmega48/88/168, and will always read as zero. • Bit 5 – ICF1: Timer/Counter1, input capture flag This flag is set when a capture event occurs on the ICP1 pin.
ATmega48/88/168 17. Timer/Counter0 and Timer/Counter1 prescalers “8-bit Timer/Counter0 with PWM” on page 89 and “16-bit Timer/Counter1 with PWM” on page 108 share the same prescaler module, but the Timer/Counters can have different prescaler settings. The description below applies to both Timer/Counter1 and Timer/Counter0. 17.0.1 Internal clock source The Timer/Counter can be clocked directly by the system clock (by setting the CSn2:0 = 1).
ATmega48/88/168 Enabling and disabling of the clock input must be done when T1/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.
ATmega48/88/168 17.1 17.1.1 Register description GTCCR – General timer/counter control register Bit 7 6 5 4 3 2 1 0 0x23 (0x43) TSM – – – – – PSRASY PSRSYNC Read/write R/W R R R R R R/W 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.
ATmega48/88/168 18. 8-bit Timer/Counter2 with PWM and asynchronous operation 18.1 Features • • • • • • • 18.
ATmega48/88/168 18.2.1 Registers The Timer/Counter (TCNT2) and Output Compare Register (OCR2A and OCR2B) are 8-bit registers. Interrupt request (shorten as Int.Req.) signals are all visible in the Timer Interrupt Flag Register (TIFR2). All interrupts are individually masked with the Timer Interrupt Mask Register (TIMSK2). TIFR2 and TIMSK2 are not shown in the figure.
ATmega48/88/168 Figure 18-2. Counter unit block diagram. TOVn (Int.req.) DATA BUS TOSC1 count TCNTn clear clk Tn Control logic Prescaler T/C oscillator direction bottom TOSC2 top clkI/O Signal description (internal signals): count Increment or decrement TCNT2 by 1. direction Selects between increment and decrement. clear Clear TCNT2 (set all bits to zero). clkTn Timer/Counter clock, referred to as clkT2 in the following. top Signalizes that TCNT2 has reached maximum value.
ATmega48/88/168 Figure 18-3. Output compare unit, block diagram. DATA BUS OCRnx TCNTn = (8-bit comparator) OCFnx (int.req.) top bottom Waveform generator OCnx FOCn WGMn1:0 COMnX1:0 The OCR2x Register is double buffered when using any of the Pulse Width Modulation (PWM) modes. For the Normal and Clear Timer on Compare (CTC) modes of operation, the double buffering is disabled.
ATmega48/88/168 The setup of the OC2x should be performed before setting the Data Direction Register for the port pin to output. The easiest way of setting the OC2x value is to use the Force Output Compare (FOC2x) strobe bit in Normal mode. The OC2x Register keeps its value even when changing between Waveform Generation modes. Be aware that the COM2x1:0 bits are not double buffered together with the compare value. Changing the COM2x1:0 bits will take effect immediately. 18.
ATmega48/88/168 18.6.1 Compare output mode and waveform generation The Waveform Generator uses the COM2x1:0 bits differently in normal, CTC, and PWM modes. For all modes, setting the COM2x1:0 = 0 tells the Waveform Generator that no action on the OC2x Register is to be performed on the next compare match. For compare output actions in the non-PWM modes refer to Table 18-5 on page 154. For fast PWM mode, refer to Table 18-6 on page 155, and for phase correct PWM refer to Table 18-7 on page 155.
ATmega48/88/168 Figure 18-5. CTC mode, timing diagram. OCnx interrupt flag set TCNTn OCnx (toggle) Period (COMnx1:0 = 1) 1 2 3 4 An interrupt can be generated each time the counter value reaches the TOP value by using the OCF2A Flag. If the interrupt is enabled, the interrupt handler routine can be used for updating the TOP value.
ATmega48/88/168 In fast PWM mode, the counter is incremented until the counter value matches the TOP value. The counter is then cleared at the following timer clock cycle. The timing diagram for the fast PWM mode is shown in Figure 18-6. The TCNT2 value is in the timing diagram shown as a histogram for illustrating the single-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal line marks on the TCNT2 slopes represent compare matches between OCR2x and TCNT2.
ATmega48/88/168 generated will have a maximum frequency of foc2 = fclk_I/O/2 when OCR2A is set to zero. This feature is similar to the OC2A toggle in CTC mode, except the double buffer feature of the Output Compare unit is enabled in the fast PWM mode. 18.7.4 Phase correct PWM mode The phase correct PWM mode (WGM22:0 = 1 or 5) provides a high resolution phase correct PWM waveform generation option. The phase correct PWM mode is based on a dual-slope operation.
ATmega48/88/168 output can be generated by setting the COM2x1:0 to three. TOP is defined as 0xFF when WGM2:0 = 3, and OCR2A when MGM2:0 = 7 (See Table 18-4 on page 154). The actual OC2x value will only be visible on the port pin if the data direction for the port pin is set as output.
ATmega48/88/168 Figure 18-9. Timer/counter timing diagram, with prescaler (fclk_I/O/8). clkI/O clkTn (clkI/O /8) TCNTn MAX - 1 MAX BOTTOM BOTTOM + 1 TOVn Figure 18-10 shows the setting of OCF2A in all modes except CTC mode. Figure 18-10. Timer/counter timing diagram, setting of OCF2A, with prescaler (fclk_I/O/8). clkI/O clkTn (clkI/O /8) TCNTn OCRnx - 1 OCRnx OCRnx + 1 OCRnx + 2 OCRnx value OCRnx OCFnx Figure 18-11 shows the setting of OCF2A and the clearing of TCNT2 in CTC mode.
ATmega48/88/168 18.9 Asynchronous operation of Timer/Counter2 When Timer/Counter2 operates asynchronously, some considerations must be taken. • Warning: When switching between asynchronous and synchronous clocking of Timer/Counter2, the Timer Registers TCNT2, OCR2x, and TCCR2x might be corrupted. A safe procedure for switching clock source is: a. Disable the Timer/Counter2 interrupts by clearing OCIE2x and TOIE2. b. Select clock source by setting AS2 as appropriate. c.
ATmega48/88/168 • Description of wake up from Power-save or ADC Noise Reduction mode when the timer is clocked asynchronously: When the interrupt condition is met, the wake up process is started on the following cycle of the timer clock, that is, the timer is always advanced by at least one before the processor can read the counter value.
ATmega48/88/168 The clock source for Timer/Counter2 is named clkT2S. clkT2S is by default connected to the main system I/O clock clk IO. By setting the AS2 bit in ASSR, Timer/Counter2 is asynchronously clocked from the TOSC1 pin. This enables use of Timer/Counter2 as a Real Time Counter (RTC). When AS2 is set, pins TOSC1 and TOSC2 are disconnected from Port C. A crystal can then be connected between the TOSC1 and TOSC2 pins to serve as an independent clock source for Timer/Counter2.
ATmega48/88/168 Table 18-3. Compare output mode, fast PWM mode(1). COM2A1 COM2A0 0 0 Normal port operation, OC2A disconnected 0 1 WGM22 = 0: Normal port operation, OC0A disconnected WGM22 = 1: Toggle OC2A on compare match 1 0 Clear OC2A on compare match, set OC2A at BOTTOM, (non-inverting mode) 1 1 Set OC2A on compare match, clear OC2A at BOTTOM, (inverting mode) Note: Description 1. A special case occurs when OCR2A equals TOP and COM2A1 is set.
ATmega48/88/168 Table 18-6 shows the COM2B1:0 bit functionality when the WGM22:0 bits are set to fast PWM mode. Table 18-6. Compare output mode, fast PWM mode(1). COM2B1 COM2B0 0 0 Normal port operation, OC2B disconnected 0 1 Reserved 1 0 Clear OC2B on compare match, set OC2B at BOTTOM, (non-inverting mode) 1 1 Set OC2B on compare match, clear OC2B at BOTTOM, (invertiing mode) Note: Description 1. A special case occurs when OCR2B equals TOP and COM2B1 is set.
ATmega48/88/168 Table 18-8. Mode WGM2 WGM1 WGM0 Timer/counter mode of operation 0 0 0 0 Normal 0xFF Immediate MAX 1 0 0 1 PWM, phase correct 0xFF TOP BOTTOM 2 0 1 0 CTC OCRA Immediate MAX 3 0 1 1 Fast PWM 0xFF BOTTOM MAX 4 1 0 0 Reserved – – – 5 1 0 1 PWM, phase correct OCRA TOP BOTTOM 6 1 1 0 Reserved – – – 7 1 1 1 Fast PWM OCRA BOTTOM TOP Notes: 18.11.2 Waveform generation mode bit description.
ATmega48/88/168 A FOC2B strobe will not generate any interrupt, nor will it clear the timer in CTC mode using OCR2B as TOP. The FOC2B bit is always read as zero. • Bits 5:4 – Res: Reserved bits These bits are reserved bits in the Atmel ATmega48/88/168 and will always read as zero. • Bit 3 – WGM22: Waveform generation mode See the description in the “TCCR2A – Timer/counter control register A” on page 153.
ATmega48/88/168 The Output Compare Register A contains an 8-bit value that is continuously compared with the counter value (TCNT2). A match can be used to generate an Output Compare interrupt, or to generate a waveform output on the OC2A pin. 18.11.
ATmega48/88/168 • Bit 1 – OCF2A: Output compare flag 2 A The OCF2A bit is set (one) when a compare match occurs between the Timer/Counter2 and the data in OCR2A – Output Compare Register2. OCF2A is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, OCF2A is cleared by writing a logic one to the flag. When the I-bit in SREG, OCIE2A (Timer/Counter2 Compare match Interrupt Enable), and OCF2A are set (one), the Timer/Counter2 Compare match Interrupt is executed.
ATmega48/88/168 • Bit 1 – TCR2AUB: Timer/counter control Register2 update busy When Timer/Counter2 operates asynchronously and TCCR2A is written, this bit becomes set. When TCCR2A has been updated from the temporary storage register, this bit is cleared by hardware. A logical zero in this bit indicates that TCCR2A is ready to be updated with a new value. • Bit 0 – TCR2BUB: Timer/counter control Register2 update busy When Timer/Counter2 operates asynchronously and TCCR2B is written, this bit becomes set.
ATmega48/88/168 19. SPI – Serial peripheral interface 19.1 Features • • • • • • • • 19.
ATmega48/88/168 Figure 19-1. SPI block diagram(1). SPI2X SPI2X DIVIDER /2/4/8/16/32/64/128 Note: 1. Refer to Figure 1-1 on page 2, and Table 14-3 on page 78 for SPI pin placement. The interconnection between Master and Slave CPUs with SPI is shown in Figure 19-2 on page 163. The system consists of two shift Registers, and a Master clock generator. The SPI Master initiates the communication cycle when pulling low the Slave Select SS pin of the desired Slave.
ATmega48/88/168 Flag, SPIF is set. If the SPI Interrupt Enable bit, SPIE, in the SPCR Register is set, an interrupt is requested. The Slave may continue to place new data to be sent into SPDR before reading the incoming data. The last incoming byte will be kept in the Buffer Register for later use. Figure 19-2. SPI master-slave interconnection. SHIFT ENABLE The system is single buffered in the transmit direction and double buffered in the receive direction.
ATmega48/88/168 Assembly code example(1) SPI_MasterInit: ; Set MOSI and SCK output, all others input ldi r17,(1<
ATmega48/88/168 The following code examples show how to initialize the SPI as a Slave and how to perform a simple reception.
ATmega48/88/168 19.3 19.3.1 SS pin functionality Slave mode When the SPI is configured as a Slave, the Slave Select (SS) pin is always input. When SS is held low, the SPI is activated, and MISO becomes an output if configured so by the user. All other pins are inputs. When SS is driven high, all pins are inputs, and the SPI is passive, which means that it will not receive incoming data. Note that the SPI logic will be reset once the SS pin is driven high.
ATmega48/88/168 Table 19-2. CPOL functionality. Leading edge Trailing edge SPI mode CPOL=0, CPHA=0 Sample (rising) Setup (falling) 0 CPOL=0, CPHA=1 Setup (rising) Sample (falling) 1 CPOL=1, CPHA=0 Sample (falling) Setup (rising) 2 CPOL=1, CPHA=1 Setup (falling) Sample (rising) 3 Figure 19-3. SPI transfer format with CPHA = 0.
ATmega48/88/168 19.5 19.5.1 Register description SPCR – SPI control register Bit 7 6 5 4 3 2 1 0 0x2C (0x4C) SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0 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 SPCR • Bit 7 – SPIE: SPI interrupt enable This bit causes the SPI interrupt to be executed if SPIF bit in the SPSR Register is set and the if the Global Interrupt Enable bit in SREG is set.
ATmega48/88/168 • Bits 1, 0 – SPR1, SPR0: SPI clock rate select 1 and 0 These two bits control the SCK rate of the device configured as a Master. SPR1 and SPR0 have no effect on the Slave. The relationship between SCK and the Oscillator Clock frequency fosc is shown in Table 19-5: Table 19-5. 19.5.2 Relationship between SCK and the oscillator frequency.
ATmega48/88/168 19.5.3 SPDR – SPI data register Bit 7 6 5 4 3 2 1 0 0x2E (0x4E) MSB LSB Read/write R/W R/W R/W R/W R/W R/W R/W R/W Initial value X X X X X X X X SPDR Undefined The SPI Data Register is a read/write register used for data transfer between the Register File and the SPI Shift Register. Writing to the register initiates data transmission. Reading the register causes the Shift Register Receive buffer to be read.
ATmega48/88/168 20. USART0 20.
ATmega48/88/168 Figure 20-1. USART block diagram(1). Clock generator UBRRn [H:L] OSC BAUD RATE GENERATOR SYNC LOGIC PIN CONTROL XCKn Transmitter TX CONTROL DATA BUS UDRn (transmit) PARITY GENERATOR 20.3 TxDn Receiver UCSRnA Note: PIN CONTROL TRANSMIT SHIFT REGISTER CLOCK RECOVERY RX CONTROL RECEIVE SHIFT REGISTER DATA RECOVERY PIN CONTROL UDRn (receive) PARITY CHECKER UCSRnB RxDn UCSRnC 1. Refer to Figure 1-1 on page 2 and Table 14-9 on page 84 for USART0 pin placement.
ATmega48/88/168 Figure 20-2. Clock generation logic, block diagram. UBRRn U2Xn foscn Prescaling down-counter UBRRn+1 /2 /4 /2 0 1 0 OSC DDR_XCKn xcki XCKn pin Sync register Edge detector xcko DDR_XCKn 1 0 UMSELn 1 UCPOLn txclk 1 0 rxclk Signal description: 20.3.1 txclk Transmitter clock (internal signal). rxclk Receiver base clock (internal signal). xcki Input from XCK pin (internal signal). Used for synchronous slave operation. xcko Clock output to XCK pin (internal signal).
ATmega48/88/168 Table 20-1 contains equations for calculating the baud rate (in bits per second) and for calculating the UBRRn value for each mode of operation using an internally generated clock source. Table 20-1. Equations for calculating baud rate register setting.
ATmega48/88/168 20.3.3 External clock External clocking is used by the synchronous slave modes of operation. The description in this section refers to Figure 20-2 on page 173 for details. External clock input from the XCKn pin is sampled by a synchronization register to minimize the chance of meta-stability. The output from the synchronization register must then pass through an edge detector before it can be used by the Transmitter and Receiver.
ATmega48/88/168 A frame starts with the start bit followed by the least significant data bit. Then the next data bits, up to a total of nine, are succeeding, ending with the most significant bit. If enabled, the parity bit is inserted after the data bits, before the stop bits. When a complete frame is transmitted, it can be directly followed by a new frame, or the communication line can be set to an idle (high) state. Figure 20-4 illustrates the possible combinations of the frame formats.
ATmega48/88/168 Transmitter or the Receiver depending on the usage. For interrupt driven USART operation, the Global Interrupt Flag should be cleared (and interrupts globally disabled) when doing the initialization. Before doing a re-initialization with changed baud rate or frame format, be sure that there are no ongoing transmissions during the period the registers are changed.
ATmega48/88/168 For the assembly code, the baud rate parameter is assumed to be stored in the r17:r16 Registers. Assembly code example(1) USART_Init: ; Set baud rate out UBRRnH, r17 out UBRRnL, r16 ; Enable receiver and transmitter ldi r16, (1<
ATmega48/88/168 20.6 Data transmission – The USART transmitter The USART Transmitter is enabled by setting the Transmit Enable (TXEN) bit in the UCSRnB Register. When the Transmitter is enabled, the normal port operation of the TxDn pin is overridden by the USART and given the function as the Transmitter’s serial output. The baud rate, mode of operation and frame format must be set up once before doing any transmissions.
ATmega48/88/168 The function simply waits for the transmit buffer to be empty by checking the UDREn Flag, before loading it with new data to be transmitted. If the Data Register Empty interrupt is utilized, the interrupt routine writes the data into the buffer. 20.6.2 Sending frames with 9 data bits If 9-bit characters are used (UCSZn = 7), the ninth bit must be written to the TXB8 bit in UCSRnB before the low byte of the character is written to UDRn.
ATmega48/88/168 contains data to be transmitted that has not yet been moved into the Shift Register. For compatibility with future devices, always write this bit to zero when writing the UCSRnA Register. When the Data Register Empty Interrupt Enable (UDRIEn) bit in UCSRnB is written to one, the USART Data Register Empty Interrupt will be executed as long as UDREn is set (provided that global interrupts are enabled). UDREn is cleared by writing UDRn.
ATmega48/88/168 The following code example shows a simple USART receive function based on polling of the Receive Complete (RXCn) Flag. When using frames with less than eight bits the most significant bits of the data read from the UDRn will be masked to zero. The USART has to be initialized before the function can be used.
ATmega48/88/168 Assembly code example(1) USART_Receive: ; Wait for data to be received sbis UCSRnA, RXCn rjmp USART_Receive ; Get status and 9th bit, then data from buffer in r18, UCSRnA in r17, UCSRnB in r16, UDRn ; If error, return -1 andi r18,(1<
ATmega48/88/168 20.7.3 Receive complete flag and interrupt The USART Receiver has one flag that indicates the Receiver state. The Receive Complete (RXCn) Flag indicates if there are unread data present in the receive buffer. This flag is one when unread data exist in the receive buffer, and zero when the receive buffer is empty (that is, does not contain any unread data). If the Receiver is disabled (RXENn = 0), the receive buffer will be flushed and consequently the RXCn bit will become zero.
ATmega48/88/168 The UPEn bit is set if the next character that can be read from the receive buffer had a Parity Error when received and the Parity Checking was enabled at that point (UPMn1 = 1). This bit is valid until the receive buffer (UDRn) is read. 20.7.6 Disabling the receiver In contrast to the Transmitter, disabling of the Receiver will be immediate. Data from ongoing receptions will therefore be lost.
ATmega48/88/168 Figure 20-5. Start bit sampling. RxD IDLE START BIT 0 Sample (U2X = 0) 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 Sample (U2X = 1) 0 1 2 3 4 5 6 7 8 1 2 When the clock recovery logic detects a high (idle) to low (start) transition on the RxDn line, the start bit detection sequence is initiated. Let sample 1 denote the first zero-sample as shown in the figure.
ATmega48/88/168 Figure 20-7. Stop bit sampling and next start bit sampling. RxD STOP 1 (A) (B) (C) Sample (U2X = 0) 1 2 3 4 5 6 7 8 9 10 0/1 0/1 0/1 Sample (U2X = 1) 1 2 3 4 5 6 0/1 The same majority voting is done to the stop bit as done for the other bits in the frame. If the stop bit is registered to have a logic 0 value, the Frame Error (FEn) Flag will be set.
ATmega48/88/168 Table 20-2. Recommended maximum receiver baud rate error for normal speed mode (U2Xn = 0). D # (Data+parity bit) Rslow (%) Rfast (%) Max. total error (%) Recommended max. receiver error (%) 5 93.20 106.67 +6.67/-6.8 ±3.0 6 94.12 105.79 +5.79/-5.88 ±2.5 7 94.81 105.11 +5.11/-5.19 ±2.0 8 95.36 104.58 +4.58/-4.54 ±2.0 9 95.81 104.14 +4.14/-4.19 ±1.5 10 96.17 103.78 +3.78/-3.83 ±1.5 Table 20-3.
ATmega48/88/168 nine data bits, then the ninth bit (RXB8n) is used for identifying address and data frames. When the frame type bit (the first stop or the ninth bit) is one, the frame contains an address. When the frame type bit is zero the frame is a data frame. The Multi-processor Communication mode enables several slave MCUs to receive data from a master MCU. This is done by first decoding an address frame to find out which MCU has been addressed.
ATmega48/88/168 20.10 Register description 20.10.1 UDRn – USART I/O data register n Bit 7 6 5 4 3 2 1 0 RXB[7:0] UDRn (Read) TXB[7:0] UDRn (Write) 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 USART Transmit Data Buffer Register and USART Receive Data Buffer Registers share the same I/O address referred to as USART Data Register or UDRn.
ATmega48/88/168 UDREn is set after a reset to indicate that the Transmitter is ready. • Bit 4 – FEn: Frame error This bit is set if the next character in the receive buffer had a Frame Error when received, that is, when the first stop bit of the next character in the receive buffer is zero. This bit is valid until the receive buffer (UDRn) is read. The FEn bit is zero when the stop bit of received data is one. Always set this bit to zero when writing to UCSRnA.
ATmega48/88/168 • Bit 5 – UDRIEn: USART data register empty interrupt enable n Writing this bit to one enables interrupt on the UDREn Flag. A Data Register Empty interrupt will be generated only if the UDRIEn bit is written to one, the Global Interrupt Flag in SREG is written to one and the UDREn bit in UCSRnA is set. • Bit 4 – RXENn: Receiver enable n Writing this bit to one enables the USART Receiver. The Receiver will override normal port operation for the RxDn pin when enabled.
ATmega48/88/168 • Bits 5:4 – UPMn1:0: Parity mode These bits enable and set type of parity generation and check. If enabled, the Transmitter will automatically generate and send the parity of the transmitted data bits within each frame. The Receiver will generate a parity value for the incoming data and compare it to the UPMn setting. If a mismatch is detected, the UPEn Flag in UCSRnA will be set. Table 20-5. UPMn bits settings.
ATmega48/88/168 Table 20-8. Transmitted data changed (output of TxDn pin) Received data sampled (input on RxDn pin) 0 Rising XCKn edge Falling XCKn edge 1 Falling XCKn edge Rising XCKn edge UCPOLn 20.10.5 UCPOLn bit settings.
ATmega48/88/168 Table 20-9. Examples of UBRRn settings for commonly used oscillator frequencies. fosc = 1.0000MHz fosc = 1.8432MHz Baud rate (bps) UBRRn 2400 25 0.2% 51 0.2% 47 4800 12 0.2% 25 0.2% 9600 6 -7.0% 12 14.4k 3 8.5% 19.2k 2 28.8k U2Xn = 0 U2Xn = 1 UBRRn Error 0.0% 95 0.0% 51 0.2% 103 0.2% 23 0.0% 47 0.0% 25 0.2% 51 0.2% 0.2% 11 0.0% 23 0.0% 12 0.2% 25 0.2% 8 -3.5% 7 0.0% 15 0.0% 8 -3.5% 16 2.1% 8.5% 6 -7.0% 5 0.0% 11 0.
ATmega48/88/168 Table 20-10. Examples of UBRRn settings for commonly used oscillator frequencies. fosc = 3.6864MHz fosc = 4.0000MHz fosc = 7.3728MHz Baud rate (bps) UBRRn Error UBRRn Error UBRRn Error UBRRn Error UBRRn Error UBRRn Error 2400 95 0.0% 191 0.0% 103 0.2% 207 0.2% 191 0.0% 383 0.0% 4800 47 0.0% 95 0.0% 51 0.2% 103 0.2% 95 0.0% 191 0.0% 9600 23 0.0% 47 0.0% 25 0.2% 51 0.2% 47 0.0% 95 0.0% 14.4k 15 0.0% 31 0.0% 16 2.1% 34 -0.8% 31 0.
ATmega48/88/168 Table 20-11. Examples of UBRRn settings for commonly used oscillator frequencies. fosc = 8.0000MHz fosc = 11.0592MHz fosc = 14.7456MHz Baud rate (bps) UBRRn Error UBRRn Error UBRRn Error UBRRn Error UBRRn Error UBRRn Error 2400 207 0.2% 416 -0.1% 287 0.0% 575 0.0% 383 0.0% 767 0.0% 4800 103 0.2% 207 0.2% 143 0.0% 287 0.0% 191 0.0% 383 0.0% 9600 51 0.2% 103 0.2% 71 0.0% 143 0.0% 95 0.0% 191 0.0% 14.4k 34 -0.8% 68 0.6% 47 0.
ATmega48/88/168 Table 20-12. Examples of UBRRn settings for commonly used oscillator frequencies. fosc = 16.0000MHz fosc = 18.4320MHz fosc = 20.0000MHz Baud rate (bps) UBRRn Error UBRRn Error UBRRn Error UBRRn Error UBRRn Error UBRRn Error 2400 416 -0.1% 832 0.0% 479 0.0% 959 0.0% 520 0.0% 1041 0.0% 4800 207 0.2% 416 -0.1% 239 0.0% 479 0.0% 259 0.2% 520 0.0% 9600 103 0.2% 207 0.2% 119 0.0% 239 0.0% 129 0.2% 259 0.2% 14.4k 68 0.6% 138 -0.1% 79 0.
ATmega48/88/168 21. USART in SPI mode 21.1 Features • • • • • • • • 21.
ATmega48/88/168 Table 21-1. Equations for calculating baud rate register setting. Operating mode Synchronous Master mode Note: 21.4 Equation for calculating baud rate(1) Equation for calculating UBRRn value f OSC BAUD = -------------------------------------2 ( UBRRn + 1 ) f OSC UBRRn = -------------------–1 2BAUD 1. The baud rate is defined to be the transfer rate in bit per second (bps).
ATmega48/88/168 Figure 21-1. UCPHAn and UCPOLn data transfer timing diagrams. UCPHA=0 UCPHA=1 UCPOL=0 21.5 UCPOL=1 XCK XCK Data setup (TXD) Data setup (TXD) Data sample (RXD) Data sample (RXD) XCK XCK Data setup (TXD) Data setup (TXD) Data sample (RXD) Data sample (RXD) Frame formats A serial frame for the MSPIM is defined to be one character of 8 data bits.
ATmega48/88/168 The following simple USART initialization code examples show one assembly and one C function that are equal in functionality. The examples assume polling (no interrupts enabled). The baud rate is given as a function parameter. For the assembly code, the baud rate parameter is assumed to be stored in the r17:r16 registers. Assembly code example(1) USART_Init: clr r18 out UBRRnH,r18 out UBRRnL,r18 ; Setting the XCKn port pin as output, enables master mode.
ATmega48/88/168 21.6 Data transfer Using the USART in MSPI mode requires the Transmitter to be enabled, that is, the TXENn bit in the UCSRnB register is set to one. When the Transmitter is enabled, the normal port operation of the TxDn pin is overridden and given the function as the Transmitter's serial output. Enabling the receiver is optional and is done by setting the RXENn bit in the UCSRnB register to one.
ATmega48/88/168 Assembly code example(1) USART_MSPIM_Transfer: ; Wait for empty transmit buffer sbis UCSRnA, UDREn rjmp USART_MSPIM_Transfer ; Put data (r16) into buffer, sends the data out UDRn,r16 ; Wait for data to be received USART_MSPIM_Wait_RXCn: sbis UCSRnA, RXCn rjmp USART_MSPIM_Wait_RXCn ; Get and return received data from buffer in r16, UDRn ret C code example(1) unsigned char USART_Receive( void ) { /* Wait for empty transmit buffer */ while ( !( UCSRnA & (1<
ATmega48/88/168 21.7 AVR USART MSPIM vs. AVR SPI The USART in MSPIM mode is fully compatible with the AVR SPI regarding: • Master mode timing diagram • The UCPOLn bit functionality is identical to the SPI CPOL bit • The UCPHAn bit functionality is identical to the SPI CPHA bit • The UDORDn bit functionality is identical to the SPI DORD bit However, since the USART in MSPIM mode reuses the USART resources, the use of the USART in MSPIM mode is somewhat different compared to the SPI.
ATmega48/88/168 21.8 Register description The following section describes the registers used for SPI operation using the USART. 21.8.1 UDRn – USART MSPIM I/O data register The function and bit description of the USART data register (UDRn) in MSPI mode is identical to normal USART operation. See “UDRn – USART I/O data register n” on page 190. 21.8.
ATmega48/88/168 • Bit 6 - TXCIEn: TX complete interrupt enable Writing this bit to one enables interrupt on the TXCn Flag. A USART Transmit Complete interrupt will be generated only if the TXCIEn bit is written to one, the Global Interrupt Flag in SREG is written to one and the TXCn bit in UCSRnA is set. • Bit 5 - UDRIE: USART data register empty interrupt enable Writing this bit to one enables interrupt on the UDREn Flag.
ATmega48/88/168 • Bit 5:3 - Reserved bits in MSPI mode When in MSPI mode, these bits are reserved for future use. For compatibility with future devices, these bits must be written to zero when UCSRnC is written. • Bit 2 - UDORDn: Data order When set to one the LSB of the data word is transmitted first. When set to zero the MSB of the data word is transmitted first. Refer to section “Frame formats” on page 175 for details.
ATmega48/88/168 22. 2-wire serial interface 22.1 Features • • • • • • • • • • • 22.
ATmega48/88/168 Table 22-1. TWI terminology. Term Description Slave The device addressed by a master. Transmitter The device placing data on the bus. Receiver The device reading data from the bus. The PRTWI bit in “Minimizing power consumption” on page 41 must be written to zero to enable the 2-wire serial interface. 22.2.2 Electrical interconnection As depicted in Figure 22-1 on page 209, both bus lines are connected to the positive supply voltage through pull-up resistors.
ATmega48/88/168 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.
ATmega48/88/168 Figure 22-4. Address packet format. Addr MSB Addr LSB R/W ACK 7 8 9 SDA SCL 1 2 START 22.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.
ATmega48/88/168 Figure 22-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 22.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.
ATmega48/88/168 Figure 22-7. SCL synchronization between multiple masters. TA low TA high SCL from Master A SCL from Master B SCL bus line TBlow Masters start counting low period TBhigh 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.
ATmega48/88/168 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 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.
ATmega48/88/168 22.5 Overview of the TWI module The TWI module is comprised of several submodules, as shown in Figure 22-9. All registers drawn in a thick line are accessible through the AVR data bus. Figure 22-9. Overview of the TWI module.
ATmega48/88/168 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: CPU Clock frequency SCL frequency = ----------------------------------------------------------------------------------------16 + 2(TWBR) ⋅ ( PrescalerValue ) • TWBR = Value of the TWI Bit Rate Register • PrescalerValue = Value of the prescaler, see Table 22-7 on page 238 Note: 22.5.
ATmega48/88/168 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
ATmega48/88/168 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.
ATmega48/88/168 • 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.
ATmega48/88/168 Assembly code example ldi r16, (1<
ATmega48/88/168 22.7 Transmission modes The TWI can operate in one of four major modes. These are named Master Transmitter (MT), Master Receiver (MR), Slave Transmitter (ST) and Slave Receiver (SR). Several of these modes can be used in the same application. As an example, the TWI can use MT mode to write data into a TWI EEPROM, MR mode to read the data back from the EEPROM. If other masters are present in the system, some of these might transmit data to the TWI, and then SR mode would be used.
ATmega48/88/168 Figure 22-11. Data transfer in master transmitter mode. VCC Device 1 Device 2 MASTER TRANSMITTER SLAVE RECEIVER Device 3 ........ Device n R1 R2 SDA SCL A START condition is sent by writing the following value to TWCR: TWCR TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE Value 1 X 1 0 X 1 0 X TWEN must be set to enable the 2-wire Serial Interface, TWSTA must be written to one to transmit a START condition and TWINT must be written to one to clear the TWINT Flag.
ATmega48/88/168 After a repeated START condition (state 0x10) the 2-wire Serial Interface can access the same Slave again, or a new Slave without transmitting a STOP condition. Repeated START enables the Master to switch between Slaves, Master Transmitter mode and Master Receiver mode without losing control of the bus. Table 22-2. Status code (TWSR) prescaler bits are 0 Status codes for master transmitter mode.
ATmega48/88/168 Figure 22-12. Formats and states in the master transmitter mode.
ATmega48/88/168 Figure 22-13. Data transfer in master receiver mode. VCC Device 1 Device 2 MASTER RECEIVER SLAVE TRANSMITTER Device 3 ........ Device n R1 R2 SDA SCL A START condition is sent by writing the following value to TWCR: TWCR TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE Value 1 X 1 0 X 1 0 X TWEN must be written to one to enable the 2-wire Serial Interface, TWSTA must be written to one to transmit a START condition and TWINT must be set to clear the TWINT Flag.
ATmega48/88/168 the Master to switch between Slaves, Master Transmitter mode and Master Receiver mode without losing control over the bus. Table 22-3. Status code (TWSR) prescaler bits are 0 Status codes for master receiver mode.
ATmega48/88/168 Figure 22-14. Formats and states in the master receiver mode.
ATmega48/88/168 The upper seven bits are the address to which the 2-wire Serial Interface will respond when addressed by a Master. If the LSB is set, the TWI will respond to the general call address (0x00), otherwise it will ignore the general call address. TWCR TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE Value 0 1 0 0 0 1 0 X TWEN must be written to one to enable the TWI.
ATmega48/88/168 Table 22-4. Status code (TWSR) prescaler bits are 0 Status codes for slave receiver mode.
ATmega48/88/168 Figure 22-16. Formats and states in the slave receiver mode. Reception of the own slave address and one or more data bytes.
ATmega48/88/168 To initiate the Slave Transmitter mode, TWAR and TWCR must be initialized as follows: TWAR TWA6 TWA5 Value TWA4 TWA3 TWA2 TWA1 TWA0 TWGCE Device’s own slave address The upper seven bits are the address to which the 2-wire Serial Interface will respond when addressed by a Master. If the LSB is set, the TWI will respond to the general call address (0x00), otherwise it will ignore the general call address.
ATmega48/88/168 Table 22-5. Status code (TWSR) prescaler bits are 0 0xA8 0xB0 0xB8 0xC0 0xC8 Status codes for slave transmitter mode.
ATmega48/88/168 Figure 22-18. Formats and states in the slave transmitter mode. Reception of the own slave address and one or more data bytes S SLA R A DATA $A8 Arbitration lost as master and addressed as slave A DATA $B8 A P or S $C0 A $B0 Last data byte transmitted. Switched to not addressed slave (TWEA = '0') A All 1's P or S $C8 DATA From master to slave From slave to master 22.7.
ATmega48/88/168 Note that data is transmitted both from Master to Slave and vice versa. The Master must instruct the Slave what location it wants to read, requiring the use of the MT mode. Subsequently, data must be read from the Slave, implying the use of the MR mode. Thus, the transfer direction must be changed. The Master must keep control of the bus during all these steps, and the steps should be carried out as an atomical operation.
ATmega48/88/168 • Two or more masters are accessing different slaves. In this case, arbitration will occur in the SLA bits. Masters trying to output a one on SDA while another Master outputs a zero will lose the arbitration. Masters losing arbitration in SLA will switch to Slave mode to check if they are being addressed by the winning Master. If addressed, they will switch to SR or ST mode, depending on the value of the READ/WRITE bit.
ATmega48/88/168 • 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.
ATmega48/88/168 • 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. 22.9.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 5 bits reflect the status of the TWI logic and the 2-wire Serial Bus.
ATmega48/88/168 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 2-wire Serial Bus. 22.9.
ATmega48/88/168 Figure 22-22. 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 Atmel ATmega48/88/168, and will always read as zero.
ATmega48/88/168 23. Analog comparator 23.1 Overview The Analog Comparator compares the input values on the positive pin AIN0 and negative pin AIN1. When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin AIN1, the Analog Comparator output, ACO, is set. The comparator’s output can be set to trigger the Timer/Counter1 Input Capture function. In addition, the comparator can trigger a separate interrupt, exclusive to the Analog Comparator.
ATmega48/88/168 Table 23-1. 23.3 23.3.1 Analog comparator multiplexed input. (Continued) ACME ADEN MUX2..
ATmega48/88/168 • Bit 4 – ACI: Analog comparator interrupt flag This bit is set by hardware when a comparator output event triggers the interrupt mode defined by ACIS1 and ACIS0. The Analog Comparator interrupt routine is executed if the ACIE bit is set and the I-bit in SREG is set. ACI is cleared by hardware when executing the corresponding interrupt handling vector. Alternatively, ACI is cleared by writing a logic one to the flag.
ATmega48/88/168 24. Analog-to-digital converter 24.1 Features • • • • • • • • • • • • • 24.2 10-bit resolution 0.5LSB integral non-linearity ±2LSB absolute accuracy 13µs - 260µs conversion time Up to 76.9kSPS (Up to 15kSPS at maximum resolution) Six multiplexed single ended input channels Two additional multiplexed single ended input channels (TQFP and QFN/MLF package only) Optional left adjustment for ADC result readout 0 - VCC ADC input voltage range Selectable 1.
ATmega48/88/168 Figure 24-1. Analog to digital converter block schematic operation. ADC CONVERSION COMPLETE IRQ ADC[9:0] ADPS1 0 ADC DATA REGISTER (ADCH/ADCL) ADPS0 ADPS2 ADIF ADFR ADEN ADSC MUX1 15 ADC CTRL. & STATUS REGISTER (ADCSRA) MUX0 MUX3 MUX2 ADLAR REFS0 REFS1 ADC MULTIPLEXER SELECT (ADMUX) ADIE ADIF 8-BIT DATA BUS MUX DECODER CHANNEL SELECTION PRESCALER AVCC CONVERSION LOGIC INTERNAL 1.
ATmega48/88/168 read, neither register is updated and the result from the conversion is lost. When ADCH is read, ADC access to the ADCH and ADCL Registers is re-enabled. The ADC has its own interrupt which can be triggered when a conversion completes. When ADC access to the Data Registers is prohibited between reading of ADCH and ADCL, the interrupt will trigger even if the result is lost. 24.
ATmega48/88/168 If Auto Triggering is enabled, single conversions can be started by writing ADSC in ADCSRA to one. ADSC can also be used to determine if a conversion is in progress. The ADSC bit will be read as one during a conversion, independently of how the conversion was started. 24.4 Prescaling and conversion timing Figure 24-3. ADC prescaler.
ATmega48/88/168 In Free Running mode, a new conversion will be started immediately after the conversion completes, while ADSC remains high. For a summary of conversion times, see Table 24-1 on page 249. Figure 24-4. ADC timing diagram, first conversion (single conversion mode).
ATmega48/88/168 Figure 24-7. ADC timing diagram, free running conversion. One conversion Cycle number 11 12 Next conversion 13 1 2 3 4 ADC clock ADSC ADIF ADCH Sign and MSB of result ADCL LSB of result Sample & hold Conversion complete Table 24-1. ADC conversion time. Sample & hold (cycles from start of conversion) Conversion time (cycles) First conversion 13.5 25 Normal conversions, single ended 1.5 13 2 13.5 Condition Auto triggered conversions 24.
ATmega48/88/168 24.5.1 ADC input channels When changing channel selections, the user should observe the following guidelines to ensure that the correct channel is selected: In Single Conversion mode, always select the channel before starting the conversion. The channel selection may be changed one ADC clock cycle after writing one to ADSC. However, the simplest method is to wait for the conversion to complete before changing the channel selection.
ATmega48/88/168 24.6.1 Analog input circuitry The analog input circuitry for single ended channels is illustrated in Figure 24-8. An analog source applied to ADCn is subjected to the pin capacitance and input leakage of that pin, regardless of whether that channel is selected as input for the ADC. When the channel is selected, the source must drive the S/H capacitor through the series resistance (combined resistance in the input path).
ATmega48/88/168 and ADC5) will only affect the conversion on ADC4 and ADC5 and not the other ADC channels. Analog ground plane PC2 (ADC2) PC3 (ADC3) PC4 (ADC4/SDA) PC5 (ADC5/SCL) VCC GND Figure 24-9. ADC power connections. PC1 (ADC1) PC0 (ADC0) ADC7 ADC6 AVCC 100nF AREF 10µH GND PB5 24.6.3 ADC accuracy definitions An n-bit single-ended ADC converts a voltage linearly between GND and V REF in 2 n steps (LSBs). The lowest code is read as 0, and the highest code is read as 2n-1.
ATmega48/88/168 Figure 24-10. Offset error. Output code Ideal ADC Actual ADC Offset error VREF Input voltage • Gain error: After adjusting for offset, the gain error is found as the deviation of the last transition (0x3FE to 0x3FF) compared to the ideal transition (at 1.5LSB below maximum). Ideal value: 0LSB Figure 24-11. Gain error.
ATmega48/88/168 Figure 24-12. Integral non-linearity (INL). Output code INL Ideal ADC Actual ADC VREF Input voltage • Differential Non-linearity (DNL): The maximum deviation of the actual code width (the interval between two adjacent transitions) from the ideal code width (1LSB). Ideal value: 0LSB Figure 24-13. Differential non-linearity (DNL).
ATmega48/88/168 24.7 ADC conversion result After the conversion is complete (ADIF is high), the conversion result can be found in the ADC Result Registers (ADCL, ADCH). For single ended conversion, the result is V IN ⋅ 1024 ADC = -------------------------V REF where VIN is the voltage on the selected input pin and VREF the selected voltage reference (see Table 24-2 and Table 24-3 on page 256). 0x000 represents analog ground, and 0x3FF represents the selected reference voltage minus one LSB. 24.8 24.8.
ATmega48/88/168 • Bits 3:0 – MUX3:0: Analog channel selection bits The value of these bits selects which analog inputs are connected to the ADC. See Table 24-3 for details. If these bits are changed during a conversion, the change will not go in effect until this conversion is complete (ADIF in ADCSRA is set). Table 24-3. Input channel selections. MUX3..0 24.8.
ATmega48/88/168 • Bit 5 – ADATE: ADC auto trigger enable When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start a conversion on a positive edge of the selected trigger signal. The trigger source is selected by setting the ADC Trigger Select bits, ADTS in ADCSRB. • Bit 4 – ADIF: ADC interrupt flag This bit is set when an ADC conversion completes and the Data Registers are updated.
ATmega48/88/168 24.8.3 24.8.3.1 ADCL and ADCH – The ADC data register ADLAR = 0 Bit 15 14 13 12 11 10 9 8 (0x79) – – – – – – ADC9 ADC8 ADCH (0x78) ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0 ADCL 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 Read/write Initial value 24.8.3.
ATmega48/88/168 trigger signal. If ADEN in ADCSRA is set, this will start a conversion. Switching to Free Running mode (ADTS[2:0]=0) will not cause a trigger event, even if the ADC Interrupt Flag is set. Table 24-5. 24.8.5 ADC auto trigger source selections.
ATmega48/88/168 25. debugWIRE on-chip debug system 25.1 Features • • • • • • • • • • 25.
ATmega48/88/168 When designing a system where debugWIRE will be used, the following observations must be made for correct operation: • Pull-up resistors on the dW/(RESET) line must not be smaller than 10kΩ. The pull-up resistor is not required for debugWIRE functionality • Connecting the RESET pin directly to VCC will not work • Capacitors connected to the RESET pin must be disconnected when using debugWire • All external reset sources must be disconnected 25.
ATmega48/88/168 26. Self-programming the flash, Atmel ATmega48 26.1 Overview In ATmega48, there is no Read-While-Write support, and no separate Boot Loader Section. The SPM instruction can be executed from the entire Flash. The device provides a Self-Programming mechanism for downloading and uploading program code by the MCU itself. The Self-Programming can use any available data interface and associated protocol to read code and write (program) that code into the Program memory.
ATmega48/88/168 If the EEPROM is written in the middle of an SPM Page Load operation, all data loaded will be lost. 26.1.3 Performing a page write To execute Page Write, set up the address in the Z-pointer, write “00000101” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR. The data in R1 and R0 is ignored. The page address must be written to PCPAGE. Other bits in the Z-pointer must be written to zero during this operation. • The CPU is halted during the Page Write operation 26.
ATmega48/88/168 26.2.1 EEPROM write prevents writing to SPMCSR Note that an EEPROM write operation will block all software programming to Flash. Reading the Fuses and Lock bits from software will also be prevented during the EEPROM write operation. It is recommended that the user checks the status bit (EEPE) in the EECR Register and verifies that the bit is cleared before writing to the SPMCSR Register. 26.2.
ATmega48/88/168 A Flash program corruption can be caused by two situations when the voltage is too low. First, a regular write sequence to the Flash requires a minimum voltage to operate correctly. Secondly, the CPU itself can execute instructions incorrectly, if the supply voltage for executing instructions is too low. Flash corruption can easily be avoided by following these design recommendations (one is sufficient): 1. Keep the AVR RESET active (low) during periods of insufficient power supply voltage.
ATmega48/88/168 rcallDo_spm ; transfer data from RAM to Flash page buffer ldi looplo, low(PAGESIZEB) ;init loop variable ldi loophi, high(PAGESIZEB) ;not required for PAGESIZEB<=256 Wrloop: ld r0, Y+ ld r1, Y+ ldi spmcrval, (1<
ATmega48/88/168 Wait_ee: sbic EECR, EEPE rjmp Wait_ee ; SPM timed sequence out SPMCSR, spmcrval spm ; restore SREG (to enable interrupts if originally enabled) out SREG, temp2 ret 26.3 26.3.1 Register description SPMCSR – Store program memory control and status register The Store Program Memory Control and Status Register contains the control bits needed to control the Program memory operations.
ATmega48/88/168 • Bit 1 – PGERS: Page erase If this bit is written to one at the same time as SELFPRGEN, the next SPM instruction within four clock cycles executes Page Erase. The page address is taken from the high part of the Zpointer. The data in R1 and R0 are ignored. The PGERS bit will auto-clear upon completion of a Page Erase, or if no SPM instruction is executed within four clock cycles. The CPU is halted during the entire Page Write operation.
ATmega48/88/168 27. Boot loader support – Read-while-write self-programming, Atmel ATmega88 and Atmel ATmega168 27.1 Features • • • • • • • Read-while-write self-programming Flexible boot memory size High security (separate boot lock bits for a flexible protection) Separate fuse to select reset vector Optimized page(1) size Code efficient algorithm Efficient read-modify-write support Note: 27.2 1.
ATmega48/88/168 27.4 Read-while-write and no read-while-write flash sections Whether the CPU supports Read-While-Write or if the CPU is halted during a Boot Loader software update is dependent on which address that is being programmed. In addition to the two sections that are configurable by the BOOTSZ Fuses as described above, the Flash is also divided into two fixed sections, the Read-While-Write (RWW) section and the No Read-WhileWrite (NRWW) section.
ATmega48/88/168 Figure 27-1. Read-while-write vs. no read-while-write. Read-while-write (RWW) section Z-pointer addresses RWW section Z-pointer addresses NRWW section No read-while-write (NRWW) section CPU is halted during the operation Code located in NRWW section.
ATmega48/88/168 Figure 27-2. Memory sections.
ATmega48/88/168 Table 27-2. BLB0 mode BLB02 BLB01 1 1 1 No restrictions for SPM or LPM accessing the Application section. 2 1 0 SPM is not allowed to write to the Application section. 0 SPM is not allowed to write to the Application section, and LPM executing from the Boot Loader section is not allowed to read from the Application section. If Interrupt Vectors are placed in the Boot Loader section, interrupts are disabled while executing from the Application section.
ATmega48/88/168 Table 27-4. BOOTRST Note: 27.7 Boot reset fuse(1). Reset address 1 Reset vector = Application reset (address 0x0000) 0 Reset vector = Boot loader reset (see Table 27-6 on page 281) 1. “1” means unprogrammed, “0” means programmed. Addressing the flash during self-programming The Z-pointer is used to address the SPM commands.
ATmega48/88/168 Figure 27-3. Addressing the flash during SPM(1). BIT 15 ZPCMSB ZPAGEMSB Z - REGISTER 1 0 0 PCMSB PROGRAM COUNTER PAGEMSB PCPAGE PCWORD PAGE ADDRESS WITHIN THE FLASH PROGRAM MEMORY PAGE WORD ADDRESS WITHIN A PAGE PAGE INSTRUCTION WORD PCWORD[PAGEMSB:0]: 00 01 02 PAGEEND Note: 27.8 1. The different variables used in Figure 27-3 are listed in Table 27-8 on page 281. Self-programming the flash The program memory is updated in a page-by-page fashion.
ATmega48/88/168 27.8.1 Performing page erase by SPM To execute Page Erase, set up the address in the Z-pointer, write “X0000011” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR. The data in R1 and R0 is ignored. The page address must be written to PCPAGE in the Z-register. Other bits in the Z-pointer will be ignored during this operation.
ATmega48/88/168 RWWSB by writing the RWWSRE. See “Simple assembly code example for a boot loader” on page 279 for an example. 27.8.7 Setting the boot loader lock bits by SPM To set the Boot Loader Lock bits and general lock bits, write the desired data to R0, write “X0001001” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR.
ATmega48/88/168 shown below. Refer to Table 28-6 on page 287 for detailed description and mapping of the Fuse High byte. Bit 7 6 5 4 3 2 1 0 Rd FHB7 FHB6 FHB5 FHB4 FHB3 FHB2 FHB1 FHB0 When reading the Extended Fuse byte, load 0x0002 in the Z-pointer. When an LPM instruction is executed within three cycles after the BLBSET and SELFPRGEN bits are set in the SPMCSR, the value of the Extended Fuse byte (EFB) will be loaded in the destination register as shown below.
ATmega48/88/168 27.8.11 Programming time for flash when using SPM The calibrated RC Oscillator is used to time Flash accesses. Table 27-5 shows the typical programming time for Flash accesses from the CPU. Table 27-5. Symbol Min. programming time Max. programming time Flash write (page erase, page write, and write lock bits by SPM) 3.7ms 4.5ms Note: 27.8.12 SPM programming time(1). 1. Minimum and maximum programming time is per individual operation.
ATmega48/88/168 ldi spmcrval, (1<
ATmega48/88/168 27.8.13 Atmel ATmega88 boot loader parameters In Table 27-6 through Table 27-8, the parameters used in the description of the self programming are given. Table 27-6. BOOTSZ1 Note: Boot size configuration, ATmega88.
ATmega48/88/168 See “Addressing the flash during self-programming” on page 274 for details about the use of Z-pointer during self-programming. 27.8.14 Atmel ATmega168 boot loader parameters In Table 27-9 through Table 27-11 on page 283, the parameters used in the description of the self programming are given. Table 27-9. Boot size configuration, ATmega168.
ATmega48/88/168 Table 27-11. Explanation of different variables used in Figure 27-3 on page 275 and the mapping to the Z-pointer, Atmel ATmega168. Corresponding Z-value(1) Variable PCMSB 12 Most significant bit in the Program Counter. (The program counter is 12 bits PC[11:0]) PAGEMSB 5 Most significant bit which is used to address the words within one page (64 words in a page requires 6 bits PC [5:0]) ZPCMSB Z13 Bit in Z-register that is mapped to PCMSB.
ATmega48/88/168 • Bit 5 – Res: Reserved bit This bit is a reserved bit in the Atmel ATmega48/88/168 and always read as zero. • Bit 4 – RWWSRE: Read-while-write section read enable When programming (Page Erase or Page Write) to the RWW section, the RWW section is blocked for reading (the RWWSB will be set by hardware). To re-enable the RWW section, the user software must wait until the programming is completed (SELFPRGEN will be cleared).
ATmega48/88/168 28. Memory programming 28.1 Program and data memory lock bits The Atmel ATmega88/168 provides six Lock bits which can be left unprogrammed (“1”) or can be programmed (“0”) to obtain the additional features listed in Table 28-2. The Lock bits can only be erased to “1” with the Chip Erase command.The Atmel ATmega48 has no separate Boot Loader section. The SPM instruction is enabled for the whole Flash if the SELFPRGEN fuse is programmed (“0”), otherwise it is disabled. Table 28-1.
ATmega48/88/168 Table 28-3. BLB0 mode BLB02 BLB01 1 1 1 No restrictions for SPM or LPM accessing the application section. 2 1 0 SPM is not allowed to write to the application section. 0 SPM is not allowed to write to the application section, and LPM executing from the boot loader section is not allowed to read from the application section. If interrupt vectors are placed in the boot loader section, interrupts are disabled while executing from the application section.
ATmega48/88/168 Table 28-5. Extended fuse byte for mega88/168. Extended fuse byte Bit no. Description Default value – 7 – 1 – 6 – 1 – 5 – 1 – 4 – 1 – 3 – 1 2 Select boot size (see Table 27-6 on page 281 and Table 27-9 on page 282 for details) 0 (programmed)(1) BOOTSZ0 1 Select boot size (see Table 27-6 on page 281 and Table 27-9 on page 282 for details) 0 (programmed)(1) BOOTRST 0 Select reset vector 1 (unprogrammed) BOOTSZ1 Note: 1. The default value of BOOTSZ1..
ATmega48/88/168 Table 28-7. Fuse low byte. Low fuse byte Description Default value 7 Divide clock by 8 0 (programmed) CKOUT(3) 6 Clock output 1 (unprogrammed) SUT1 5 Select start-up time 1 (unprogrammed)(1) SUT0 4 Select start-up time 0 (programmed)(1) CKSEL3 3 Select clock source 0 (programmed)(2) CKSEL2 2 Select clock source 0 (programmed)(2) CKSEL1 1 Select clock source 1 (unprogrammed)(2) CKSEL0 0 Select clock source 0 (programmed)(2) (4) CKDIV8 Note: Bit no. 1.
ATmega48/88/168 28.5 Page size Table 28-9. No. of words in a page and no. of pages in the flash. Device Flash size Page size PCWORD No. of pages PCPAGE PCMSB Atmel ATmega48 2K words (4Kbytes) 32 words PC[4:0] 64 PC[10:5] 10 Atmel ATmega88 4K words (8Kbytes) 32 words PC[4:0] 128 PC[11:5] 11 Atmel ATmega168 (16Kbytes) 64 words PC[5:0] 128 PC[12:6] 12 8K words Table 28-10. No. of words in a page and no. of pages in the EEPROM. 28.6 Device EEPROM size Page size PCWORD No.
ATmega48/88/168 Figure 28-1. Parallel programming. +4.5V - 5.5V RDY/BSY PD1 OE PD2 WR PD3 BS1 PD4 XA0 PD5 XA1 PD6 PAGEL PD7 +12V VCC +4.5V - 5.5V AVCC PC[1:0]:PB[5:0] DATA RESET BS2 PC2 XTAL1 GND Note: VCC - 0.3V < AVCC < VCC + 0.3V, however, AVCC should always be within 4.5V - 5.5V. Table 28-11. Pin name mapping.
ATmega48/88/168 Table 28-13. XA1 and XA0 coding. XA1 XA0 Action when XTAL1 is pulsed 0 0 Load flash or EEPROM address (high or low address byte determined by BS1) 0 1 Load data (high or low data byte for flash determined by BS1) 1 0 Load command 1 1 No action, idle Table 28-14. Command byte bit coding. Command byte 28.7 28.7.
ATmega48/88/168 4. Keep the Prog_enable pins unchanged for at least 10µs after the High-voltage has been applied to ensure the Prog_enable Signature has been latched. 5. Wait until VCC actually reaches 4.5V - 5.5V before giving any parallel programming commands. 6. Exit Programming mode by power the device down or by bringing RESET pin to 0V. 28.7.2 Considerations for efficient programming The loaded command and address are retained in the device during programming.
ATmega48/88/168 4. Give XTAL1 a positive pulse. This loads the address low byte. C. Load Data Low Byte 1. Set XA1, XA0 to “01”. This enables data loading. 2. Set DATA = Data low byte (0x00 - 0xFF). 3. Give XTAL1 a positive pulse. This loads the data byte. D. Load Data High Byte 1. Set BS1 to “1”. This selects high data byte. 2. Set XA1, XA0 to “01”. This enables data loading. 3. Set DATA = Data high byte (0x00 - 0xFF). 4. Give XTAL1 a positive pulse. This loads the data byte. E. Latch Data 1.
ATmega48/88/168 Figure 28-2. Addressing the flash which is organized in pages(1). PCMSB PROGRAM COUNTER PAGEMSB PCPAGE PCWORD PAGE ADDRESS WITHIN THE FLASH WORD ADDRESS WITHIN A PAGE PROGRAM MEMORY PAGE PAGE PCWORD[PAGEMSB:0]: 00 INSTRUCTION WORD 01 02 PAGEEND Note: 1. PCPAGE and PCWORD are listed in Table 28-9 on page 289. Figure 28-3. Programming the flash waveforms(1). F DATA A B 0x10 ADDR. LOW C DATA LOW D E DATA HIGH XX B ADDR. LOW C D DATA LOW DATA HIGH E XX G ADDR.
ATmega48/88/168 K: Repeat 3 through 5 until the entire buffer is filled. L: Program EEPROM page 1. Set BS1 to “0”. 2. Give WR a negative pulse. This starts programming of the EEPROM page. RDY/BSY goes low. 3. Wait until to RDY/BSY goes high before programming the next page (see Figure 28-4 for signal waveforms). Figure 28-4. Programming the EEPROM waveforms. K DATA A G 0x11 ADDR. HIGH B ADDR. LOW C DATA E XX B ADDR.
ATmega48/88/168 28.7.8 Programming the fuse low bits The algorithm for programming the Fuse Low bits is as follows (refer to “Programming the flash” on page 292 for details on Command and Data loading): 1. A: Load Command “0100 0000”. 2. C: Load Data Low Byte. Bit n = “0” programs and bit n = “1” erases the Fuse bit. 3. Give WR a negative pulse and wait for RDY/BSY to go high. 28.7.
ATmega48/88/168 28.7.11 Programming the lock bits The algorithm for programming the Lock bits is as follows (refer to “Programming the flash” on page 292 for details on Command and Data loading): 1. A: Load Command “0010 0000”. 2. C: Load Data Low Byte. Bit n = “0” programs the Lock bit. If LB mode 3 is programmed (LB1 and LB2 is programmed), it is not possible to program the Boot Lock bits by any External Programming mode. 3. Give WR a negative pulse and wait for RDY/BSY to go high.
ATmega48/88/168 28.7.14 Reading the calibration byte The algorithm for reading the Calibration byte is as follows (refer to “Programming the flash” on page 292 for details on Command and Address loading): 1. A: Load Command “0000 1000”. 2. B: Load Address Low Byte, 0x00. 3. Set OE to “0”, and BS1 to “1”. The Calibration byte can now be read at DATA. 4. Set OE to “1”. 28.7.15 28.
ATmega48/88/168 28.8.1 Serial programming pin mapping Table 28-15. Pin mapping serial programming. 28.8.2 Symbol Pins I/O Description MOSI PB3 I Serial Data in MISO PB4 O Serial Data out SCK PB5 I Serial Clock Serial programming algorithm When writing serial data to the Atmel ATmega48/88/168, data is clocked on the rising edge of SCK. When reading data from the ATmega48/88/168, data is clocked on the falling edge of SCK. See Figure 28-9 on page 302 for timing details.
ATmega48/88/168 28-16 on page 300). In a chip erased device, no 0xFF in the data file(s) need to be programmed. 6. Any memory location can be verified by using the Read instruction which returns the content at the selected address at serial output MISO. 7. At the end of the programming session, RESET can be set high to commence normal operation. 8. Power-off sequence (if needed): Set RESET to “1”. Turn VCC power off. Table 28-16. Typical wait delay before writing the next flash or EEPROM location. 28.8.
ATmega48/88/168 Table 28-17. Serial programming instruction set (hexadecimal values).
ATmega48/88/168 Figure 28-8. Serial programming instruction example. Serial programming instruction Load program memory page (high/low byte)/ Load EEPROM memory page (page access) Byte 1 Byte 2 Adr M A MSB SB Bit 15 B Byte 3 Write program memory page/ Write EEPROM memory page Byte 1 Byte 4 Byte 2 Adr MSB Adr LSB Bit 15 B 0 Byte 3 Byte 4 A Adr drr L LSB SB B 0 Page buffer Page offset Page 0 Page 1 Page 2 Page number Page N-1 Program memory/ EEPROM memory 28.8.
ATmega48/88/168 29. Electrical characteristics 29.1 Absolute maximum ratings* Operating temperature................................... -55°C to +125°C *NOTICE: Storage temperature...................................... -65°C to +150°C Voltage on any pin except RESET with respect to ground .................................-0.5V to VCC+0.5V Voltage on RESET with respect to ground ......-0.5V to +13.0V Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage to the device.
ATmega48/88/168 TA = -40°C to 85°C, VCC = 1.8V to 5.5V (unless otherwise noted). (Continued) Symbol Parameter Power supply current Condition (5) Power-down mode Typical Maximum Active 1MHz, VCC = 2V (Atmel ATmega48/88/168V) 0.55 Active 4MHz, VCC = 3V (Atmel ATmega48/88/168L) 3.5 Active 8MHz, VCC = 5V (Atmel ATmega48/88/168) 12 Units mA Idle 1MHz, VCC = 2V (ATmega48/88/168V) ICC 0.25 0.5 Idle 4MHz, VCC = 3V (ATmega48/88/168L) 1.5 Idle 8MHz, VCC = 5V (ATmega48/88/168) 5.
ATmega48/88/168 29.3 Speed grades Maximum frequency is dependent on VCC. As shown in Figure 29-1 and Figure 29-2, the Maximum Frequency vs. VCC curve is linear between 1.8V < VCC < 2.7V and between 2.7V < VCC < 4.5V. Figure 29-1. Maximum frequency vs. VCC, Atmel ATmega48V/88V/168V. 10MHz Safe operating area 4MHz 1.8V 2.7V 5.5V Figure 29-2. Maximum frequency vs. VCC, ATmega48/88/168. 20MHz 10MHz Safe operating area 2.7V 4.5V 5.
ATmega48/88/168 29.4 29.4.1 Clock characteristics Calibrated internal RC oscillator accuracy Table 29-1. Calibration accuracy of internal RC oscillator. Frequency VCC Temperature Calibration accuracy 8.0MHz 3V 25°C ±10% 7.3MHz - 8.1MHz 1.8V - 5.5V(1) 2.7V - 5.5V(2) -40°C - 85°C ±1% Factory calibration User calibration Notes: 29.4.2 1. Voltage range for Atmel ATmega48V/88V/168V. 2. Voltage range for Atmel ATmega48/88/168. External clock drive waveforms Figure 29-3.
ATmega48/88/168 29.5 System and reset characteristics Table 29-3. Reset, brown-out and internal voltage characteristics. Symbol Parameter VPOT VPONSR Min. Typ. Max. Power-on reset threshold voltage (rising) 0.7 1.0 1.4 Power-on reset threshold voltage (falling)(1) 0.05 0.9 1.3 Power-on slope rate 0.01 4.5 V/ms 0.2VCC 0.9VCC V 2.
ATmega48/88/168 29.6 2-wire serial interface characteristics Table 29-5 describes the requirements for devices connected to the 2-wire Serial Bus. The Atmel ATmega48/88/168 2-wire Serial Interface meets or exceeds these requirements under the noted conditions. Timing symbols refer to Figure 29-4 on page 309. Table 29-5. 2-wire serial bus requirements. Symbol Parameter VIL VIH Vhys (1) VOL(1) (1) tr (1) tof Min. Max. Input low-voltage -0.5 0.3VCC Input high-voltage 0.
ATmega48/88/168 3. Cb = capacitance of one bus line in pF. 4. fCK = CPU clock frequency. 5. This requirement applies to all Atmel ATmega48/88/168 2-wire Serial Interface operation. Other devices connected to the 2wire Serial Bus need only obey the general fSCL requirement. Figure 29-4. 2-wire serial bus timing. tHIGH tof tr tLOW tLOW SCL tSU;STA tHD;STA tHD;DAT tSU;DAT SDA tSU;STO tBUF 29.7 SPI timing characteristics See Figure 29-5 on page 310 and Figure 29-6 on page 310 for details.
ATmega48/88/168 Figure 29-5. SPI interface timing requirements (master mode). SS 6 1 SCK (CPOL = 0) 2 2 SCK (CPOL = 1) 4 MISO (Data input) 5 3 MSB ... LSB 8 7 MOSI (Data output) MSB ... LSB Figure 29-6. SPI interface timing requirements (slave mode). SS 10 9 16 SCK (CPOL = 0) 11 11 SCK (CPOL = 1) 13 MOSI (Data input) 14 12 MSB ... LSB 17 15 MISO (Data output) MSB ...
ATmega48/88/168 29.8 ADC characteristics Table 29-7. Symbol ADC characteristics. Parameter Condition Minimum Resolution Absolute accuracy (Including INL, DNL, quantization error, gain and offset error) VREF VIN Maximum 10 VREF = 4V, VCC = 4V, ADC clock = 200kHz 2 VREF = 4V, VCC = 4V, ADC clock = 1MHz 4.5 VREF = 4V, VCC = 4V, ADC clock = 200kHz Noise reduction mode 2 VREF = 4V, VCC = 4V, ADC clock = 1MHz Noise reduction mode 4.
ATmega48/88/168 29.9 Parallel programming characteristics Figure 29-7. Parallel programming timing, including some general timing requirements. tXLWL tXHXL XTAL1 tDVXH tXLDX tBVPH tPLBX t BVWL Data & contol (DATA, XA0/1, BS1, BS2) PAGEL tWLBX tPHPL tWLWH WR tPLWL WLRL RDY/BSY tWLRH Figure 29-8. Parallel programming timing, loading sequence with timing requirements(1).
ATmega48/88/168 Figure 29-9. Parallel programming timing, reading sequence (within the same page) with timing requirements(1). LOAD ADDRESS (LOW BYTE) READ DATA (LOW BYTE) READ DATA (HIGH BYTE) LOAD ADDRESS (LOW BYTE) tXLOL XTAL1 tBVDV BS1 tOLDV OE DATA tOHDZ ADDR0 (low byte) DATA (low byte) DATA (high byte) ADDR1 (low byte) XA0 XA1 Note: 1. The timing requirements shown in Figure 29-7 on page 312 (that is, tDVXH, tXHXL, and tXLDX) also apply to reading operation. Table 29-8.
ATmega48/88/168 Table 29-8. Parallel programming characteristics, VCC = 5V ±10%. (Continued) Symbol Parameter tXLOL XTAL1 low to OE low 0 tBVDV BS1 valid to DATA valid 0 tOLDV OE low to DATA valid 250 tOHDZ OE high to DATA tri-stated 250 Notes: Min. Typ. Max. Units 250 ns 1. 2. tWLRH is valid for the write flash, write EEPROM, write fuse bits and write lock bits commands. tWLRH_CE is valid for the chip erase command.
ATmega48/88/168 30. Typical characteristics The following charts show typical behavior. These figures are not tested during manufacturing. All current consumption measurements are performed with all I/O pins configured as inputs and with internal pull-ups enabled. A square wave generator with rail-to-rail output is used as clock source. All Active- and Idle current consumption measurements are done with all bits in the PRR register set and thus, the corresponding I/O modules are turned off.
ATmega48/88/168 Figure 30-2. Active supply current vs. frequency (1MHz - 24MHz). 18 16 5.5V 14 5.0V ICC (mA) 12 4.5V 10 8 4.0V 6 3.3V 4 2.7V 2 1.8V 0 0 4 8 12 16 20 24 Frequency (MHz) Figure 30-3. Active supply current vs. VCC (internal, RC oscillator, 128kHz). 0.14 -40°C 25°C 85°C 0.12 ICC (mA) 0.1 0.08 0.06 0.04 0.02 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-4. Active supply current vs. VCC (internal, RC oscillator, 1MHz). 1.4 25°C -40°C 85°C 1.2 ICC (mA) 1 0.8 0.6 0.4 0.2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-5. Active supply current vs. VCC (internal RC , oscillator, 8MHz). 7 25°C -40°C 85°C 6 ICC (mA) 5 4 3 2 1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-6. Active supply current vs. VCC (32kHz external oscillator). 60 25°C 50 ICC (µA) 40 30 20 10 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.2 Idle supply current Figure 30-7. Idle supply current vs. frequency (0.1MHz - 1.0MHz). 0.18 5.5V 0.16 5.0V 0.14 4.5V ICC (mA) 0.12 4.0V 0.1 0.08 3.3V 0.06 2.7V 0.04 1.8V 0.02 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.
ATmega48/88/168 Figure 30-8. Idle supply current vs. frequency (1MHz - 24MHz). ICC (mA) 4.5 4 5.5V 3.5 5.0V 3 4.5V 2.5 4.0V 2 1.5 3.3V 1 2.7V 0.5 1.8V 0 0 4 8 12 16 20 24 Frequency (MHz) Figure 30-9. Idle supply current vs. VCC (internal RC oscillator, 128kHz). 0.03 -40°C 85°C 25°C 0.025 ICC (mA) 0.02 0.015 0.01 0.005 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-10. Idle supply current vs. VCC (internal RC , oscillator, 1MHz). 0.35 85°C 25°C -40°C 0.3 ICC (mA) 0.25 0.2 0.15 0.1 0.05 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-11. Idle supply current vs. VCC (internal RC, oscillator, 8MHz). 1.6 85°C 25°C -40°C 1.4 1.2 ICC (mA) 1 0.8 0.6 0.4 0.2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-12. Idle supply current vs. VCC (32kHz external oscillator). 30 25 25°C ICC (µA) 20 15 10 5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.3 Supply current of I/O modules 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. The enabling or disabling of the I/O modules are controlled by the Power Reduction Register. See “Power reduction register” on page 41 for details.
ATmega48/88/168 Table 30-2. Additional current consumption (percentage) in active and idle mode. (Continued) PRR bit Additional current consumption compared to active with external clock (see Figure 30-1 on page 315 and Figure 30-2 on page 316) Additional current consumption compared to Idle with external clock (see Figure 30-7 on page 318 and Figure 30-8 on page 319) PRTIM1 2.0% 11% PRTIM0 1.6% 8.5% PRSPI 6.1% 33% PRADC 4.
ATmega48/88/168 30.4 Power-down supply current Figure 30-13. Power-down supply current vs. VCC (watchdog timer disabled). 2.5 85°C ICC (µA) 2 1.5 1 25°C -40°C 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-14. Power-down supply current vs. VCC (watchdog timer enabled). 12 10 85°C -40°C 25°C ICC (µA) 8 6 4 2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 30.5 Power-save supply current Figure 30-15. Power-save supply current vs. VCC (watchdog timer disabled). 12 10 25°C ICC (µA) 8 6 4 2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.6 Standby supply current Figure 30-16. Standby supply current vs. VCC (low power crystal oscillator). 180 6MHz Xtal 6MHz Res. 160 140 4MHz Res. 4MHz Xtal ICC (µA) 120 100 80 2MHz Xtal 2MHz Res. 60 455kHz Res. 1MHz Res. 40 20 32kHz Xtal 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-17. Standby supply current vs. VCC (full swing crystal oscillator). 500 16MHz Xtal 450 400 12MHz Xtal ICC (µA) 350 300 250 6MHz Xtal (ckopt) 200 4MHz Xtal (ckopt) 2MHz Xtal (ckopt) 150 100 50 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.7 Pin pull-up Figure 30-18. I/O pin pull-up resistor current vs. input voltage (VCC = 5V).
ATmega48/88/168 Figure 30-19. I/O pin pull-up resistor current vs. input voltage (VCC = 2.7V). 90 80 25°C 85°C 70 -40°C IOP (µA) 60 50 40 30 20 10 0 0 0.5 1 1.5 2 2.5 3 VOP (V) Figure 30-20. Reset pull-up resistor current vs. reset pin voltage (VCC = 5V).
ATmega48/88/168 Figure 30-21. Reset pull-up resistor current vs. reset pin voltage (VCC = 2.7V). 70 60 25°C -40°C 50 IRESET (µA) 85°C 40 30 20 10 0 0 0.5 1 1.5 2 2.5 3 VRESET (V) 30.8 Pin driver strength Figure 30-22. I/O pin source current vs. output voltage (VCC = 5V).
ATmega48/88/168 Figure 30-23. I/O pin source current vs. output voltage (VCC = 2.7V). 35 30 -40°C 25°C 85°C IOH (mA) 25 20 15 10 5 0 0 0.5 1 1.5 2 2.5 3 VOH (V) Figure 30-24. I/O pin source current vs. output voltage (VCC = 1.8V). 9 -40°C 25°C 8 85°C 7 IOH (mA) 6 5 4 3 2 1 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.
ATmega48/88/168 Figure 30-25. I/O pin sink current vs. output voltage (VCC = 5V). 80 25°C 70 85°C 60 IOL (mA) 50 40 30 20 10 0 0 0.5 1 1.5 2 2.5 VOL (V) Figure 30-26. I/O pin sink current vs. output voltage (VCC = 2.7V). 40 35 -40°C 30 25°C IOL (mA) 25 85°C 20 15 10 5 0 0 0.5 1 1.5 2 2.
ATmega48/88/168 Figure 30-27. I/O pin sink current vs. output voltage (VCC = 1.8V). 14 12 -40°C 25°C 10 IOL (mA) 85°C 8 6 4 2 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 VOL (V) 30.9 Pin thresholds and hysteresis Figure 30-28. I/O pin input threshold voltage vs. VCC (VIH, I/O pin read as '1'). 3 85°C 25°C -40°C 2.5 Threshold (V) 2 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-29. I/O pin input threshold voltage vs. VCC (VIL, I/O pin read as '0'). 3 85°C 2.5 Threshold (V) 25°C -40°C 2 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 V CC (V) Figure 30-30. I/O pin input hystreresis vs. Vcc. 0.6 -40°C 25°C 85°C Input hysteresis (V) 0.5 0.4 0.3 0.2 0.1 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-31. Reset input threshold voltage vs. VCC (VIH, reset pin read as '1'). 3 25°C 85°C -40°C 2.5 Threshold (V) 2 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-32. Reset input threshold voltage vs. VCC (VIL, reset pin read as '0'). 3 -40°C 85°C 25°C 2.5 Threshold (V) 2 1.5 1 0.5 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-33. Reset input pin hysteresis vs. VCC. 600 Input hysteresis (mV) 500 400 VIL 300 200 100 0 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) 30.10 BOD thresholds and analog comparator offset Figure 30-34. BOD thresholds vs. temperature (BODLEVEL is 4.3V). 4.5 4.45 Rising Vcc Threshold (V) 4.4 4.35 4.3 Falling Vcc 4.25 4.
ATmega48/88/168 Figure 30-35. BOD thresholds vs. temperature (BODLEVEL is 2.7V). 2.9 2.85 Rising Vcc Threshold (V) 2.8 2.75 2.7 Falling Vcc 2.65 2.6 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 90 100 Temperature (°C) Figure 30-36. BOD thresholds vs. temperature (BODLEVEL is 1.8V). 1.86 1.84 Threshold (V) Rising Vcc 1.82 1.8 Falling Vcc 1.78 1.
ATmega48/88/168 Figure 30-37. Bandgap voltage vs. VCC. Bandgap voltage (V ) 1.1 1.095 -40°C 1.09 85°C 1.085 1.08 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 VCC (V) Figure 30-38. Analog comparator offset voltage vs. common mode voltage (VCC = 5V). Analog comparator offset voltage (V) 0.009 0.008 85°C 0.007 -40°C 0.006 0.005 0.004 0.003 0.002 0.001 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.
ATmega48/88/168 Figure 30-39. Analog comparator offset voltage vs. common mode voltage (VCC = 2.7V). 4 85°C -40°C 3 2.5 (mV) Analog comparator offset voltage 3.5 2 1.5 1 0.5 0 0 0.5 1 1.5 2 2.5 Common Mode Voltage (V) 30.11 Internal oscillator speed Figure 30-40. Watchdog oscillator frequency vs. VCC. 120 115 FRC (kHz) -40°C 110 25°C 105 85°C 100 95 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-41. Calibrated 8MHz RC oscillator frequency vs. temperature. 8.4 8.3 5.0V 2.7V 1.8V 8.2 FRC (MHz) 8.1 8 7.9 7.8 7.7 7.6 7.5 7.4 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 Temperature (°C) Figure 30-42. Calibrated 8MHz RC oscillator frequency vs. VCC. 8.6 8.4 85°C FRC (MHz) 8.2 25°C 8 -40°C 7.8 7.6 7.4 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-43. Calibrated 8MHz RC oscillator frequency vs. osccal value. 85°C 25°C -40°C 13.5 FRC (MHz) 11.5 9.5 7.5 5.5 3.5 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 OSCCAL VALUE 30.12 Current consumption of peripheral units Figure 30-44. Brownout detector current vs. VCC. 32 -40°C 30 ICC (µA) 28 25°C 26 85°C 24 22 20 18 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-45. ADC current vs. VCC (AREF = AVCC). 500 450 -40°C 400 25°C ICC (µA) 85°C 350 300 250 200 150 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-46. AREF external reference current vs. VCC. 180 85°C 25°C -40°C 160 140 ICC (µA) 120 100 80 60 40 20 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 Figure 30-47. Analog comparator current vs. VCC. 140 -40°C 120 25°C ICC (µA) 100 85°C 80 60 40 20 0 1.5 2 2.5 3 3.5 4 4.5 5 5.5 VCC (V) Figure 30-48. Programming current vs. VCC. 14 -40°C 12 IICC (mA) CC (mA) 10 25°C 8 85°C 6 4 2 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 30.13 Current consumption in reset and reset pulse width Figure 30-49. Reset supply current vs. VCC (0.1MHz - 1.0MHz, excluding current through the reset pull-up). 0.18 5.5V 0.16 5.0V 0.14 4.5V ICC (mA) 0.12 4.0V 0.1 3.3V 0.08 2.7V 0.06 1.8V 0.04 0.02 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Frequency (MHz) Figure 30-50. Reset supply current vs. VCC (1MHz - 24MHz, excluding current through the reset pull-up). , ICC (mA) 4.5 4 5.5V 3.5 5.0V 3 4.5V 2.5 2 4.
ATmega48/88/168 Figure 30-51. Reset pulse width vs. VCC. 2500 Pulsewidth (ns) 2000 1500 1000 85°C -40°C 25°C 500 0 1.5 2 2.5 3 3.5 4 4.5 5 5.
ATmega48/88/168 31.
ATmega48/88/168 Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0xBF) Reserved – – – – – – – – – Page (0xBE) Reserved – – – – – – – (0xBD) TWAMR TWAM6 TWAM5 TWAM4 TWAM3 TWAM2 TWAM1 TWAM0 – 239 (0xBC) TWCR TWINT TWEA TWSTA TWSTO TWWC TWEN – TWIE 236 (0xBB) TWDR (0xBA) TWAR TWA6 TWA5 TWA4 TWA3 TWA2 TWA1 TWA0 TWGCE 239 (0xB9) TWSR TWS7 TWS6 TWS5 TWS4 TWS3 – TWPS1 TWPS0 238 (0xB8) TWBR (0xB7) Reserved – (0xB6) ASSR
ATmega48/88/168 Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 (0x7D) Reserved – – – – – – – – Page (0x7C) ADMUX REFS1 REFS0 ADLAR – MUX3 MUX2 MUX1 MUX0 255 (0x7B) ADCSRB – ACME – – – ADTS2 ADTS1 ADTS0 258 (0x7A) ADCSRA ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0 256 (0x79) ADCH ADC data register high byte (0x78) ADCL ADC data register low byte (0x77) Reserved – – – – – – – – (0x76) Reserved – – – – – – – – (0x75)
ATmega48/88/168 Address Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0x1B (0x3B) PCIFR – – – – – PCIF2 PCIF1 PCIF0 0x1A (0x3A) Reserved – – – – – – – – 0x19 (0x39) Reserved – – – – – – – – 0x18 (0x38) Reserved – – – – – – – – 0x17 (0x37) TIFR2 – – – – – OCF2B OCF2A TOV2 158 0x16 (0x36) TIFR1 – – ICF1 – – OCF1B OCF1A TOV1 136 0x15 (0x35) TIFR0 – – – – – OCF0B OCF0A TOV0 0x14 (0x34) Reserved – – – – – – – – 0x1
ATmega48/88/168 32.
ATmega48/88/168 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), R
ATmega48/88/168 Mnemonics POP Operands Rd Description Pop register from stack Operation Rd ← STACK Flags #Clocks None 2 None 1 MCU CONTROL INSTRUCTIONS NOP No operation 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 Note: 1. These instructions are only available in Atmel ATmega168.
ATmega48/88/168 33. Ordering information 33.1 Atmel ATmega48 Speed (MHz) Power supply Ordering code(2) Package(1) (5) 10(3) 20(3) Note: Operational range 1.8V - 5.5V ATmega48V-10AUR ATmega48V-10MUR(5) ATmega48V-10AU ATmega48V-10MMU ATmega48V-10MMUR(5) ATmega48V-10MMH(4) ATmega48V-10MMHR(4)(5) ATmega48V-10MU ATmega48V-10PU 32A 32M1-A 32A 28M1 28M1 28M1 28M1 32M1-A 28P3 Industrial (-40°C to 85°C) 2.7V - 5.
ATmega48/88/168 33.2 Atmel ATmega88 Speed (MHz) Power supply Ordering code(2) Package(1) (4) 10(3) 20(3) Note: Operational range 1.8V - 5.5V ATmega88V-10AUR ATmega88V-10MUR(4) ATmega88V-10AU ATmega88V-10MU ATmega88V-10PU 32A 32M1-A 32A 32M1-A 28P3 Industrial (-40°C to 85°C) 2.7V - 5.5V ATmega88-20AUR(4) ATmega88-20MUR(4) ATmega88-20AU ATmega88-20MU ATmega88-20PU 32A 32M1-A 32A 32M1-A 28P3 Industrial (-40°C to 85°C) 1. This device can also be supplied in wafer form.
ATmega48/88/168 33.3 Atmel ATmega168 Speed (MHz)(3) Power supply Ordering code(2) Package(1) (4) 10 20 Note: Operational range 1.8V - 5.5V ATmega168V-10AUR ATmega168V-10MUR(4) ATmega168V-10AU ATmega168V-10MU ATmega168V-10PU 32A 32M1-A 32A 32M1-A 28P3 Industrial (-40°C to 85°C) 2.7V - 5.5V ATmega168-20AUR(4) ATmega168-20MUR(4) ATmega168-20AU ATmega168-20MU ATmega168-20PU 32A 32M1-A 32A 32M1-A 28P3 Industrial (-40°C to 85°C) 1. This device can also be supplied in wafer form.
ATmega48/88/168 34. Packaging information 34.1 32A PIN 1 IDENTIFIER PIN 1 e B E1 E D1 D C 0°~7° A1 A2 A L COMMON DIMENSIONS (Unit of Measure = mm) MIN NOM MAX A – – 1.20 A1 0.05 – 0.15 SYMBOL Notes: 1. This package conforms to JEDEC reference MS-026, Variation ABA. 2. Dimensions D1 and E1 do not include mold protrusion. Allowable protrusion is 0.25mm per side. Dimensions D1 and E1 are maximum plastic body size dimensions including mold mismatch. 3. Lead coplanarity is 0.10mm maximum.
ATmega48/88/168 34.2 28M1 D C 1 2 Pin 1 ID 3 E SIDE VIEW A1 TOP VIEW A y D2 K 1 0.45 2 R 0.20 3 E2 b COMMON DIMENSIONS (Unit of Measure = mm) SYMBOL MIN NOM MAX A 0.80 0.90 1.00 A1 0.00 0.02 0.05 b 0.17 0.22 0.27 C L e 0.4 Ref (4x) Note: 0.20 REF D 3.95 4.00 4.05 D2 2.35 2.40 2.45 E 3.95 4.00 4.05 E2 2.35 2.40 2.45 e BOTTOM VIEW The terminal #1 ID is a Laser-marked Feature. NOTE 0.45 L 0.35 0.40 0.45 y 0.00 – 0.08 K 0.
ATmega48/88/168 34.3 32M1-A D D1 1 2 3 0 Pin 1 ID E1 SIDE VIEW E TOP VIEW A3 A2 A1 A K 0.08 C P D2 1 2 3 P Pin #1 Notch (0.20 R) K e SYMBOL MIN NOM MAX A 0.80 0.90 1.00 A1 – 0.02 0.05 A2 – 0.65 1.00 A3 E2 b COMMON DIMENSIONS (Unit of Measure = mm) L BOTTOM VIEW 0.20 REF b 0.18 0.23 0.30 D 4.90 5.00 5.10 D1 4.70 4.75 4.80 D2 2.95 3.10 3.25 E 4.90 5.00 5.10 E1 4.70 4.75 4.80 E2 2.95 3.10 3.25 e Note: JEDEC Standard MO-220, Fig.
ATmega48/88/168 34.4 28P3 D PIN 1 E1 A SEATING PLANE L B2 B1 B A1 (4 PLACES) e E 0º ~ 15º C COMMON DIMENSIONS (Unit of Measure = mm) REF SYMBOL eB Note: 1. Dimensions D and E1 do not include mold Flash or Protrusion. Mold Flash or Protrusion shall not exceed 0.25mm (0.010"). MIN NOM MAX A – – 4.5724 A1 0.508 – – D 34.544 – 34.798 E 7.620 – 8.255 E1 7.112 – 7.493 B 0.381 – 0.533 B1 1.143 – 1.397 B2 0.762 – 1.143 L 3.175 – 3.429 C 0.203 – 0.
ATmega48/88/168 35. Errata 35.1 Errata Atmel ATmega48 The revision letter in this section refers to the revision of the ATmega48 device. 35.1.1 Rev. D • Interrupts may be lost when writing the timer registers in the asynchronous timer 1. Interrupts may be lost when writing the timer registers in the asynchronous timer The interrupt will be lost if a timer register that is synchronous timer clock is written when the asynchronous Timer/Counter register (TCNTx) is 0x00.
ATmega48/88/168 35.1.4 Rev A • • • • • • • Part may hang in reset Wrong values read after erase only operation Watchdog timer interrupt disabled Start-up time with crystal oscillator is higher than expected High power consumption in power-down with external clock Asynchronous oscillator does not stop in power-down Interrupts may be lost when writing the timer registers in the asynchronous timer 1.
ATmega48/88/168 3. Watchdog timer interrupt disabled If the watchdog timer interrupt flag is not cleared before a new timeout occurs, the watchdog will be disabled, and the interrupt flag will automatically be cleared. This is only applicable in interrupt only mode. If the Watchdog is configured to reset the device in the watchdog timeout following an interrupt, the device works correctly.
ATmega48/88/168 35.2 Errata Atmel ATmega88 The revision letter in this section refers to the revision of the ATmega88 device. 35.2.1 Rev. D • Interrupts may be lost when writing the timer registers in the asynchronous timer 1. Interrupts may be lost when writing the timer registers in the asynchronous timer The interrupt will be lost if a timer register that is synchronous timer clock is written when the asynchronous Timer/Counter register (TCNTx) is 0x00.
ATmega48/88/168 Problem fix/workaround The first case can be avoided during run-mode by ensuring that only one reset source is active. If an external reset push button is used, the reset start-up time should be selected such that the reset line is fully debounced during the start-up time. The second case can be avoided by not using the system clock prescaler. The third case occurs during In-System programming only. It is most frequently seen when using the internal RC at maximum frequency.
ATmega48/88/168 - A reset is applied in a 10ns window while the system clock prescaler value is updated by software. - Leaving SPI-programming mode generates an internal reset signal that can trigger this case. The two first cases can occur during normal operating mode, while the last case occurs only during programming of the device. Problem fix/workaround The first case can be avoided during run-mode by ensuring that only one reset source is active.
ATmega48/88/168 - Two succeeding resets are applied where the second reset occurs in the 10ns window before the device is out of the reset-state caused by the first reset. - A reset is applied in a 10ns window while the system clock prescaler value is updated by software. - Leaving SPI-programming mode generates an internal reset signal that can trigger this case. The two first cases can occur during normal operating mode, while the last case occurs only during programming of the device.
ATmega48/88/168 36. Datasheet revision history Please note that the referring page numbers in this section are referred to this document. The referring revision in this section are referring to the document revision. 36.1 Rev. 2545T-04/11 1. 2. 3. 4. 36.2 Rev. 2545S-07/10 1. 2. 36.3 Updated “Errata” on page 357. Updated the last page with the Atmel new addresses. Rev. 2545Q-06/09 1. 2. 36.5 Note 6 and Note 7 in Table 29-5, “2-wire serial bus requirements.,” on page 308 have been removed.
ATmega48/88/168 36.6 Rev. 2545O-02/09 1. 2. 36.7 Rev. 2545N-01/09 1. 2. 3. 4. 5. 6. 7. 36.8 Updated “Features” on page 1 and added the note “Not recommended for new designs”. Merged the sections Resources, Data Retention and About Code Examples under one common section, “Resources” on page 8. Updated Figure 9-4 on page 35. Updated “System clock prescaler” on page 36. Updated “Alternate functions of port B” on page 78. Added section “” on page 307. Updated “Pin thresholds and hysteresis” on page 330.
ATmega48/88/168 2. 3. 4. Updated Table 1-1 on page 2. Updated “Ordering information” on page 350. Updated “Packaging information” on page 353. 36.12 Rev. 2545I-11/06 1. 2. 3. Updated “Features” on page 1. Updated Features in “2-wire serial interface” on page 209. Fixed typos in Table 29-3 on page 307. 36.13 Rev. 2545H-10/06 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Updated typos. Updated “Features” on page 1. Updated “Calibrated internal RC oscillator” on page 33.
ATmega48/88/168 11. 12. 13. 14. 15. 16. 17 18. 19. 20. Updated “UCSRnA – USART MSPIM control and status register n A” on page 206. Updated note in “Bit rate generator unit” on page 216. Updated “Bit 6 – ACBG: Analog comparator bandgap select” on page 242. Updated Features in “Analog-to-digital converter” on page 244. Updated “Prescaling and conversion timing” on page 247. Updated “Limitations of debugWIRE” on page 261. Added Table 29-1 on page 306. Updated Figure 16-7 on page 122, Figure 30-45 on page 339.
ATmega48/88/168 4. 5. 6. 7. 8. 9. 10. 11. Moved “TIMSK2 – Timer/Counter2 interrupt mask register” on page 158 and “TIFR2 – Timer/Counter2 interrupt flag register” on page 158 to “Register description” on page 153. Updated cross-reference in “Electrical interconnection” on page 210. Updated equation in “Bit rate generator unit” on page 216. Added “Page size” on page 289. Updated “Serial programming algorithm” on page 299. Updated Ordering Information for “Atmel ATmega168” on page 352.
ATmega48/88/168 Table of contents Features ..................................................................................................... 1 1 Pin configurations ................................................................................... 2 1.1Pin descriptions .........................................................................................................3 2 Overview ................................................................................................... 5 2.
ATmega48/88/168 9.5Low frequency crystal oscillator ..............................................................................33 9.6Calibrated internal RC oscillator ..............................................................................33 9.7128kHz internal oscillator ........................................................................................34 9.8External clock ..........................................................................................................35 9.
ATmega48/88/168 13.1Pin change interrupt timing ....................................................................................66 13.2Register description ...............................................................................................67 14 I/O-ports .................................................................................................. 71 14.1Overview ...............................................................................................................71 14.
ATmega48/88/168 18.3Timer/counter clock sources ...............................................................................141 18.4Counter unit .........................................................................................................141 18.5Output compare unit ............................................................................................142 18.6Compare match output unit .................................................................................144 18.
ATmega48/88/168 21.8Register description .............................................................................................206 22 2-wire serial interface .......................................................................... 209 22.1Features ..............................................................................................................209 22.22-wire serial interface bus definition ....................................................................209 22.
ATmega48/88/168 26.3Register description .............................................................................................267 27 Boot loader support – Read-while-write self-programming, Atmel ATmega88 and Atmel ATmega168 269 27.1Features ..............................................................................................................269 27.2Overview .............................................................................................................269 27.
ATmega48/88/168 30.4Power-down supply current .................................................................................323 30.5Power-save supply current ..................................................................................324 30.6Standby supply current ........................................................................................324 30.7Pin pull-up ...........................................................................................................325 30.
ATmega48/88/168 36.9Rev. 2545L-08/07 ................................................................................................365 36.10Rev. 2545K-04/07 .............................................................................................365 36.11Rev. 2545J-12/06 ..............................................................................................365 36.12Rev. 2545I-11/06 ...............................................................................................366 36.13Rev.
Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131 USA Tel: (+1)(408) 441-0311 Fax: (+1)(408) 487-2600 www.atmel.com Atmel Asia Limited Unit 1-5 & 16, 19/F BEA Tower, Millennium City 5 418 Kwun Tong Road Kwun Tong, Kowloon HONG KONG Tel: (+852) 2245-6100 Fax: (+852) 2722-1369 Atmel Munich GmbH Business Campus Parkring 4 D-85748 Garching b. Munich GERMANY Tel: (+49) 89-31970-0 Fax: (+49) 89-3194621 Atmel Japan 9F, Tonetsu Shinkawa Bldg.