ATMEGA325A-MU Atmel, ATMEGA325A-MU Datasheet
![IC MCU AVR 32K FLASH 64VQFN](/photos/12/13/121326/313-64-qfn_sml.jpg)
ATMEGA325A-MU
Specifications of ATMEGA325A-MU
Related parts for ATMEGA325A-MU
ATMEGA325A-MU Summary of contents
Page 1
... Programmable I/O Lines – 64/100-lead TQFP, 64-pad QFN/MLF and 64-pad DRQFN • Speed Grade: – ATmega 165A/165PA/645A/645P 16MHz @ 1.8 - 5.5V – ATmega325A/325PA/3250A/3250PA/6450A/6450P 20MHz @ 1.8 - 5.5V • Temperature range: – -40°C to 85°C Industrial • Ultra-Low Power Consumption (picoPower devices) – ...
Page 2
... Pin Configurations 1.1 Pinout - TQFP and QFN/MLF Figure 1-1. 64A (TQFP)and 64M1 (QFN/MLF) Pinout ATmega165A/ATmega165PA/ATmega325A/ATmega325PA/ATmega645A/ATmega645P DNC 1 (RXD/PCINT0) PE0 2 (TXD/PCINT1) PE1 3 (XCK/AIN0/PCINT2) PE2 4 (AIN1/PCINT3) PE3 5 (USCK/SCL/PCINT4) PE4 6 (DI/SDA/PCINT5) PE5 7 (DO/PCINT6) PE6 8 (CLKO/PCINT7) PE7 9 (SS/PCINT8) PB0 10 (SCK/PCINT9) PB1 11 (MOSI/PCINT10) PB2 12 (MISO/PCINT11) PB3 13 (OC0A/PCINT12) PB4 ...
Page 3
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 1.2 Pinout - 100A (TQFP) Figure 1-2. Pinout ATmega3250A/ATmega3250PA/ATmega6450A/ATmega6450P 1 DNC 2 (RXD/PCINT0) PE0 3 (TXD/PCINT1) PE1 4 (XCK/AIN0/PCINT2) PE2 (AIN1/PCINT3) PE3 5 6 (USCK/SCL/PCINT4) PE4 7 (DI/SDA/PCINT5) PE5 8 (DO/PCINT6) PE6 9 (CLKO/PCINT7) PE7 VCC 10 11 GND ...
Page 4
Overview The ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P is a low-power CMOS 8-bit microcon- troller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, this microcontroller achieves throughputs approaching 1 MIPS per MHz allowing the system designer ...
Page 5
... Application Flash section is updated, providing true Read-While-Write operation. By combining an 8-bit RISC CPU with In-System Self-Programmable Flash on a monolithic chip, the Atmel devise is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. ...
Page 6
... Comparison Between ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P Table 2-1. Differences between: ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P Device ATmega165A ATmega165PA ATmega325A ATmega325PA ATmega3250A ATmega3250PA ATmega645A ATmega645P ATmega6450A ATmega6450P 2.3 Pin Descriptions 2.3.1 VCC Digital supply voltage. 2.3.2 GND Ground. 2.3.3 Port A (PA7:PA0) Port 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability ...
Page 7
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 2.3.5 Port C (PC7:PC0) Port 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port C output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port ...
Page 8
Port G also serves the functions of various special features of the ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P as listed on 84. 2.3.10 Port H (PH7:PH0) Port 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port H ...
Page 9
... ATmega165A/165PA/325A/325PA/3250A/3250PA/6 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. ...
Page 10
AVR CPU Core 6.1 Introduction This section discusses the AVR core architecture in general. The main function of the CPU core is to ensure correct program execution. The CPU must therefore be able to access memories, perform calculations, control ...
Page 11
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 ical ALU operation, two operands are output from the Register File, the operation is executed, and the result is stored back in the Register File – in one clock cycle. Six of the 32 registers can be used as ...
Page 12
The Status Register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software. 6.4.1 SREG – AVR Status Register The SREG is defined as: Bit 0x3F (0x5F) Read/Write ...
Page 13
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 6.5 General Purpose Register File The Register File is optimized for the AVR Enhanced RISC instruction set. In order to achieve the required performance and flexibility, the following input/output schemes are supported by the Register File: • One 8-bit ...
Page 14
Figure 6-3. X-register Y-register Z-register In the different addressing modes these address registers have functions as fixed displacement, automatic increment, and automatic decrement (see the instruction set reference for details). 6.6 Stack Pointer The Stack is mainly used for storing ...
Page 15
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 6.6.1 SPH and SPL – Stack Pointer Bit 0x3E (0x5E) 0x3D (0x5D) Read/Write Initial Value 6.7 Instruction Execution Timing This section describes the general access timing concepts for instruction execution. The AVR CPU is driven by the CPU clock ...
Page 16
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 ...
Page 17
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Assembly Code Example in r16, SREG cli sbi EECR, EEMWE sbi EECR, EEWE out SREG, r16 C Code Example char cSREG; cSREG = SREG; /* store SREG value */ /* disable interrupts during timed sequence */ __disable_interrupt(); EECR |= ...
Page 18
AVR Memories This section describes the different memories in the ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P. The AVR archi- tecture has two main memory spaces, the Data Memory and the Program Memory space. In addition, the ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P fea- tures an EEPROM Memory for data ...
Page 19
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 7-1. 7.2 SRAM Data Memory Figure 7-2 on page 20 The ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P 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. ...
Page 20
Figure 7-2. 7.2.1 Data Memory Access Times This section describes the general access timing concepts for internal memory access. The internal data SRAM access is performed in two clk 20. Figure 7-3. ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/645 20 Data Memory Data Memory X 8 ...
Page 21
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 7.3 EEPROM Data Memory The ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P contains 512 bytes of data EEPROM memory 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 ...
Page 22
When the write access time has elapsed, the EEWE bit is cleared by hardware. The user soft- ware can poll this bit and wait for a zero before writing the next byte. When EEWE has been set, the CPU is ...
Page 23
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Assembly Code Example EEPROM_write: C Code Example void EEPROM_write(unsigned int uiAddress, unsigned char ucData The next code examples show assembly and C functions for reading the EEPROM. The exam- ples assume that interrupts are controlled so that ...
Page 24
Assembly Code Example EEPROM_read: ; Wait for completion of previous write sbic EECR,EEWE rjmp EEPROM_read ; Set up address (r18:r17) in address register out EEARH, r18 out EEARL, r17 ; Start eeprom read by writing EERE sbi EECR,EERE ; Read ...
Page 25
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 be used reset occurs while a write operation is in progress, the write operation will be com- pleted provided that the power supply voltage is sufficient. 7.4 I/O Memory The I/O space definition is shown in All ...
Page 26
... Initial Value • Bits 15:11 – Reserved These bits are reserved bits in the ATmega325A/325PA/3250A/3250PA/645A/645P/6450A/6450P and will always read as zero. • Bits 10:0 – EEAR10:0: EEPROM Address The EEPROM Address Registers – EEARH and EEARL specify the EEPROM address in the 1/2K bytes EEPROM space. The EEPROM data bytes are addressed linearly between 0 and 1023/2047 ...
Page 27
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 • Bits 7:0 – EEDR7:0: EEPROM Data For the EEPROM write operation, the EEDR Register contains the data to be written to the EEPROM in the address given by the EEAR Register. For the EEPROM read operation, the EEDR ...
Page 28
GPIOR0 – General Purpose I/O Register 0 Bit 0x1E (0x3E) Read/Write Initial Value ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/645 MSB R/W R/W R/W R LSB GPIOR0 R/W R/W R/W R/W 0 ...
Page 29
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 8. System Clock and Clock Options 8.1 Clock Systems and their Distribution Figure 8-1 on page 29 of the clocks need not be active at a given time. In order to reduce power consumption, the clocks to modules not ...
Page 30
Asynchronous Timer Clock – clk 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 ...
Page 31
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 8.3 Default Clock Source The device is shipped with CKSEL = “0010”, SUT = “10”, and CKDIV8 programmed. The default clock source setting is the Internal RC Oscillator with longest start-up time and an initial system clock prescaling of ...
Page 32
Crystal Oscillator XTAL1 and XTAL2 are input and output, respectively inverting amplifier which can be con- figured for use as an On-chip Oscillator, as shown in ceramic resonator may be used. C1 and C2 should always be ...
Page 33
... Table 8-7. Note: The Low-frequency Crystal Oscillator provides an internal load capacitance, see page 34 ATmega325A/325PA/3250A/3250PA/645A/645P/6450A/6450P oscillator is optimized for very low power consumption, and thus when selecting crystals, see mum ESR recommendations on 6.5 pF, 9.0 pF and 12.5 pF crystals. 8285B–AVR–03/11 Start-up Times for the Crystal Oscillator Clock Selection (Continued) ...
Page 34
... Table 8-11. CKSEL3... 0 (1) 0110 0111 Note: 1. This option should only be used if frequency stability at start-up is not important for the ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/645 34 Maximum ESR Recommendation for 32.768kHz Watch Crystal ATmega325A/325PA/3250A/3250PA/645A/645P/6450A/6450P Crystal CL (pF) 6.5 9.0 12.5 32kHz Osc. Type Cap (Xtal1/Tosc1) System Osc. Timer Osc ...
Page 35
... To run the device on an external clock, the CKSEL Fuses must be programmed to “0000”. Figure 8-3. When this clock source is selected, start-up times are determined by the SUT Fuses as shown in Table 8-13. Table 8-12. ATmega 165A/165PA/645A/645P ATmega325A/325PA/3250A/3250PA/6450A/6450P Table 8-13. SUT1.. When applying an external clock required to avoid sudden changes in the applied clock fre- quency to ensure stable operation of the MCU ...
Page 36
Timer/Counter Oscillator ATmega169P uses the same crystal oscillator for Low-frequency Oscillator and Timer/Counter Oscillator. See crystal requirements. ATmega169P share the Timer/Counter Oscillator Pins (TOSC1 and TOSC2) with XTAL1 and XTAL2. When using the Timer/Counter Oscillator, the system clock needs ...
Page 37
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 8.11 Register Description 8.11.1 OSCCAL – Oscillator Calibration Register Bit (0x66) Read/Write Initial 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 ...
Page 38
To avoid unintentional changes of clock frequency, a special write procedure must be followed to change the CLKPS bits: 1. Write the Clock Prescaler Change Enable (CLKPCE) bit to one and all other bits in CLKPR to zero. 2. Within ...
Page 39
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 9. Power Management and Sleep Modes 9.1 Overview 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 ...
Page 40
Upon wake-up from sleep, BOD is automatically enabled again. This ensures safe operation in case the VCC level has dropped during the sleep period. When the BOD has been disabled, ...
Page 41
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 When waking up from Power-down mode, there is a delay from the wake-up condition occurs until the wake-up becomes effective. This allows the clock to restart and become stable after having been stopped. The wake-up period is defined by ...
Page 42
Refer to 214 for details on ADC operation. 9.10.2 Analog Comparator When entering Idle mode, the Analog Comparator should be disabled if not used. When entering ADC Noise Reduction mode, the Analog Comparator should ...
Page 43
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 9.10.7 JTAG Interface and On-chip Debug System If the On-chip debug system is enabled by the OCDEN Fuse and the chip enter Power down or Power save sleep mode, the main clock source remains enabled. In these sleep modes, ...
Page 44
Register Description 9.11.1 SMCR – Sleep Mode Control Register The Sleep Mode Control Register contains control bits for power management. Bit 0x33 (0x53) Read/Write Initial Value • Bits – SM2:0: Sleep Mode Select Bits 2, 1, ...
Page 45
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The BODS bit is active three clock cycles after it is set. A sleep instruction must be executed while BODS is active in order to turn off the BOD for the actual sleep mode. The BODS bit is automatically ...
Page 46
System Control and Reset 10.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. The instruction placed at the Reset Vector must be a JMP ...
Page 47
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 10-1. Reset Logic BODLEVEL [2..0] 10.2.1 Power-on Reset A Power-on Reset (POR) pulse is generated by an On-chip detection circuit. The detection level is defined below the detection level. The POR circuit can be used ...
Page 48
Figure 10-3. MCU Start-up, RESET Extended Externally RESET TIME-OUT INTERNAL RESET 10.2.2 External Reset An External Reset is generated by a low level on the RESET pin. Reset pulses longer than the minimum pulse width (see reset, even if the ...
Page 49
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 10.2.3 Brown-out Detection ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P has an On-chip Brown-out Detection (BOD) circuit for monitoring the fixed trigger level. The trigger level for the BOD can be selected by the BODLEVEL Fuses. The trigger level has a hysteresis ...
Page 50
Internal Voltage Reference ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/6450A/6450P features an inter- nal bandgap reference. This reference is used for Brown-out Detection, and it can be used as an input to the Analog Comparator or the ADC. 10.3.1 Voltage Reference Enable Signals and Start-up ...
Page 51
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 10-7. Watchdog Timer 10.4.1 Timed Sequences for Changing the Configuration of the Watchdog Timer The sequence for changing configuration differs slightly between the two safety levels. Separate procedures are described for each level. 10.4.1.1 Safety Level 1 In ...
Page 52
Assembly Code Example WDT_off: ; Reset WDT wdr ; Write logical one to WDCE and WDE in ori r16, (1<<WDCE)|(1<<WDE) out WDTCR, r16 ; Turn off WDT ldi r16, (0<<WDE) out WDTCR, r16 ret C Code Example void WDT_off(void) { ...
Page 53
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 10.5 Register Description 10.5.1 MCUSR – MCU Status Register The MCU Status Register provides information on which reset source caused an MCU reset. Bit 0x35 (0x55) Read/Write Initial Value • Bit 4 – JTRF: JTAG Reset Flag This bit ...
Page 54
Bit 3 – WDE: Watchdog Enable When the WDE is written to logic one, the Watchdog Timer is enabled, and if the WDE is written to logic zero, the Watchdog Timer function is disabled. WDE can only be cleared ...
Page 55
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 11. Interrupts ...
Page 56
When the IVSEL bit in MCUCR is set, Interrupt Vectors will be moved to the start of the Boot Table 11-2 on page 56 tions of BOOTRST and IVSEL settings. If the program never enables an interrupt source, the ...
Page 57
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 0x0031 0x0032 0x0033 ... When the BOOTRST Fuse is unprogrammed, the Boot section size set to 2K bytes and the IVSEL bit in the MCUCR Register is set before any interrupts are enabled, the most typical and general program ...
Page 58
... The most typical and general program setup for the Reset and Interrupt Vector Addresses in ATmega325A/325PA/3250A/3250PA/625A/645P/6450A/6450P is: Address 0x0000 0x0002 0x0004 0x0006 0x0008 0x000A 0x000C 0x000E 0x0010 0x0012 0x0014 0x0016 0X0018 0x001A 0x001C 0x001E 0x0020 0x0022 0x0024 ...
Page 59
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Address Labels Code 0x0000 0x0001 0x0002 0x0003 0x0004 0x0005 ; .org 0x3802/0x7802 0x3804/0x7804 0x3806/0x7806 ... 0x1C2C When the BOOTRST Fuse is programmed and the Boot section size set to 4K bytes, the most typical and general program setup for ...
Page 60
Moving Interrupts Between Application and Boot Space The General Interrupt Control Register controls the placement of the Interrupt Vector table, see ”MCUCR – MCU Control Register” on page To avoid unintentional changes of Interrupt Vector tables, a special write ...
Page 61
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 11.3 Register Description 11.3.1 MCUCR – MCU Control Register Bit 0x35 (0x55) Read/Write Initial Value Note: • Bit 1 – IVSEL: Interrupt Vector Select When the IVSEL bit is cleared (zero), the Interrupt Vectors are placed at the start ...
Page 62
... PCINT30:16 are only present in ATmega3250A/3250PA/6450A/6450P. Only PCINT15:0 are 12.1 Pin Change Interrupt Timing An example of timing of a pin change interrupt is shown in ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/645 62 ”EICRA – External Interrupt Control Register A” on page 29. present in ATmega165A/165PA; ATmega325A/325PA and ATmega645A/645P. See figurations” on page 2 and ”Register Description” on page 64 (2) (1) (1) , PCMSK2 , PCMSK1, and PCMSK0 Reg- ” ...
Page 63
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 12-1. Pin Change Interrupt 8285B–AVR–03/11 pin_lat PCINT( pin_sync LE clk PCINT(0) in PCMSK(x) clk PCINT(n) pin_lat pin_sync pcint_in_(n) pcint_syn pcint_setflag PCIF pcint_in_(0) 0 pcint_syn pcint_setflag x clk PCIF 63 ...
Page 64
... The falling edge of INT0 generates an interrupt request. 1 The rising edge of INT0 generates an interrupt request (1) (1) PCIE3 PCIE2 PCIE1 PCIE0 R R R/W R ATmega165A/ATmega165PA/ATmega325A/ATmega325PA/ATmega645A/ATmega645P and should always be written to zero. ATmega165A/ATmega165PA/ATmega325A/ATmega325PA/ATmega645A/ATmega645P and should always be written to zero – – ISC01 ISC00 EICRA R R R/W R – ...
Page 65
... PCIF3 PCIF2 PCIF1 R/W R/W R This bit is a reserved bit in ATmega165A/ATmega165PA/ATmega325A/ATmega325PA/ATmega645A/ATmega645P and should always be written to zero. 1. This bit is a reserved bit in ATmega165A/ATmega165PA/ATmega325A/ATmega325PA/ATmega645A/ATmega645P and should always be written to zero PCIF0 – – – R INTF0 ...
Page 66
Bit 4 – 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 EIMSK are set (one), ...
Page 67
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 12.2.7 PCMSK0 – Pin Change Mask Register 0 Bit (0x6B) Read/Write Initial Value • Bit 7:0 – PCINT7:0: Pin Change Enable Mask 7:0 Each PCINT7:0 bit selects whether pin change interrupt is enabled on the corresponding I/O pin. If ...
Page 68
I/O-Ports 13.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 ...
Page 69
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 13.2 Ports as General Digital I/O The ports are bi-directional I/O ports with optional internal pull-ups. tional description of one I/O-port pin, here generically called Pxn. Figure 13-2. General Digital I/O Note: 13.2.1 Configuring the Pin Each port pin ...
Page 70
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. 13.2.3 Switching Between ...
Page 71
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 13-3. Synchronization when Reading an Externally Applied Pin value INSTRUCTIONS 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 ...
Page 72
Assembly Code Example ... ; Define pull-ups and set outputs high ...
Page 73
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 13.2.6 Unconnected Pins If some pins are unused recommended to ensure that these pins have a defined level. Even though most of the digital inputs are disabled in the deep sleep modes as described above, float- ing ...
Page 74
Alternate Port Functions Most port pins have alternate functions in addition to being general digital I/Os. shows how the port pin control signals from the simplified alternate functions. The overriding signals may not be present in all port pins, ...
Page 75
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 13-2. Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO The following subsections shortly describe the alternate functions for each port, and relate the overriding signals to the alternate function. Refer to the alternate ...
Page 76
Alternate Functions of Port B The Port B pins with alternate functions are shown in Table 13-3. Port Pin PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 The alternate pin configuration is as follows: • OC2A/PCINT15, Bit 7 OC2, ...
Page 77
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 • MISO/PCINT11 – Port B, Bit 3 MISO: Master Data input, Slave Data output pin for SPI. When the SPI is enabled as a Master, this pin is configured as an input regardless of the setting of DDB3. When ...
Page 78
Table 13-4. Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO Table 13-5. Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO ATmega165A/165PA/325A/325PA/3250A/3250PA/645A/645P/645 78 Overriding Signals for Alternate Functions in PB7:PB4 PB7/OC2A/ ...
Page 79
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 13.3.2 Alternate Functions of Port D The Port D pins with alternate functions are shown in Table 13-6. Port Pin PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 The alternate pin configuration is as follows: • INT0 – Port ...
Page 80
Alternate Functions of Port E The Port E pins with alternate functions are shown in Table 13-8. Port Pin PE7 PE6 PE5 PE4 PE3 PE2 PE1 PE0 • PCINT7 – Port E, Bit 7 PCINT7, Pin Change Interrupt Source ...
Page 81
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 • XCK/AIN0/PCINT2 – Port E, Bit 2 XCK, USART External Clock. The Data Direction Register (DDE2) controls whether the clock is output (DDE2 set) or input (DDE2 cleared). The XCK pin is active only when the USART oper- ates ...
Page 82
Table 13-10. Overriding Signals for Alternate Functions in PE3:PE0 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO Note: 1. AIN0D and AIN1D is described in 13.3.4 Alternate Functions of Port F The Port F has ...
Page 83
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 • TDO, ADC6 – Port F, Bit 6 ADC6, Analog to Digital Converter, Channel 6 TDO, JTAG Test Data Out: Serial output data from Instruction Register or Data Register. When the JTAG interface is enabled, this pin can not ...
Page 84
Table 13-13. Overriding Signals for Alternate Functions in PF3:PF0 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO 13.3.5 Alternate Functions of Port G The alternate pin configuration is as follows: Table 13-14. Port G Pins ...
Page 85
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 13-14 shown in Table 13-15. Overriding Signals for Alternate Functions in PG4:PG3 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO 13.3.6 Alternate Functions of Port H Port H is only present in ATmega3250A/3250PA/6450A/6450P. ...
Page 86
PCINT21 – Port H, Bit 5 PCINT21, Pin Change Interrupt Source 21: The PH5 pin can serve as an external interrupt source. • PCINT20 – Port H, Bit 4 PCINT20, Pin Change Interrupt Source 20: The PH4 pin can ...
Page 87
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 13-18. Overriding Signals for Alternate Functions in PH3:PH0 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO 13.3.7 Alternate Functions of Port J Port J is only present in ATmega3250A/3250PA/6450A/6450P. The alternate pin configuration ...
Page 88
PCINT27 – Port J, Bit 3 PCINT27, Pin Change Interrupt Source 27: The PE27 pin can serve as an external interrupt source. • PCINT26 – Port J, Bit 2 PCINT26, Pin Change Interrupt Source 26: The PE26 pin can ...
Page 89
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 13-21. Overriding Signals for Alternate Functions in PJ3:PJ0 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV PTOE DIEOE DIEOV DI AIO 8285B–AVR–03/11 PJ3/PCINT27 PJ2/PCINT26 – – ...
Page 90
Register Description 13.4.1 MCUCR – MCU Control Register Bit 0x35 (0x55) Read/Write Initial Value • 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 ...
Page 91
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 13.4.8 PORTC – Port C Data Register Bit 0x08 (0x28) Read/Write Initial Value 13.4.9 DDRC – Port C Data Direction Register Bit 0x07 (0x27) Read/Write Initial Value 13.4.10 PINC – Port C Input Pins Address Bit 0x06 (0x26) Read/Write ...
Page 92
PINE – Port E Input Pins Address Bit 0x0C (0x2C) Read/Write Initial Value 13.4.17 PORTF – Port F Data Register Bit 0x11 (0x31) Read/Write Initial Value 13.4.18 DDRF – Port F Data Direction Register Bit 0x10 (0x30) Read/Write Initial ...
Page 93
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 13.4.24 DDRH – Port H Data Direction Register Bit (0xD9) Read/Write Initial Value 13.4.25 PINH – Port H Input Pins Address Bit (0xD8) Read/Write Initial Value 13.4.26 PORTJ – Port J Data Register Bit (0xDD) Read/Write Initial Value 13.4.27 ...
Page 94
Timer/Counter0 with PWM 14.1 Features • Single Compare Unit Counter • Clear Timer on Compare Match (Auto Reload) • Glitch-free, Phase Correct Pulse Width Modulator (PWM) • Frequency Generator • External Event Counter • 10-bit Clock Prescaler • ...
Page 95
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The definitions in Table 14-1. BOTTOM MAX TOP 14.2.2 Registers The Timer/Counter (TCNT0) and Output Compare Register (OCR0A) are 8-bit registers. Inter- rupt request (abbreviated to Int.Req. in the figure) signals are all visible in the Timer Interrupt Flag ...
Page 96
Signal description (internal signals): count direction clear clk top bottom Depending of the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk selected by the Clock Select bits (CS02:0). When no clock source ...
Page 97
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 14-3. Output Compare Unit, Block Diagram The OCR0A 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 buff- ering ...
Page 98
The setup of the OC0A should be performed before setting the Data Direction Register for the port pin to output. The easiest way of setting the OC0A value is to use the Force Output Com- pare (FOC0A) strobe bits in ...
Page 99
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 A change of the COM0A1:0 bits state will have effect at the first compare match after the bits are written. For non-PWM modes, the action can be forced to have immediate effect by using the FOC0A strobe bits. 14.7 ...
Page 100
Figure 14-5. CTC Mode, Timing Diagram TCNTn OCn (Toggle) Period An interrupt can be generated each time the counter value reaches the TOP value by using the OCF0A Flag. If the interrupt is enabled, the interrupt handler routine can be ...
Page 101
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 14-6. Fast PWM Mode, Timing Diagram TCNTn OCn OCn Period The Timer/Counter Overflow Flag (TOV0) is set each time the counter reaches MAX. If the inter- rupt is enabled, the interrupt handler routine can be used for updating ...
Page 102
However, due to the sym- metric feature of the dual-slope PWM modes, these modes are preferred for motor control applications. The PWM resolution for the phase correct PWM mode is fixed ...
Page 103
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 output will be continuously low and if set equal to MAX the output will be continuously high for non-inverted PWM mode. For inverted PWM the output will have the opposite logic values. At the very start of period 2 ...
Page 104
Figure 14-10. Timer/Counter Timing Diagram, Setting of OCF0A, with Prescaler (f clk I/O clk Tn (clk /8) I/O TCNTn OCRnx OCFnx Figure 14-11 Figure 14-11. Timer/Counter Timing Diagram, Clear Timer on Compare Match mode, with Pres- clk I/O clk Tn ...
Page 105
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 14.9 Register Description 14.9.1 TCCR0A – Timer/Counter Control Register A Bit 0x24 (0x44) Read/Write Initial Value • Bit 7 – FOC0A: Force Output Compare A The FOC0A bit is only active when the WGM00 bit specifies a non-PWM mode. ...
Page 106
When OC0A is connected to the pin, the function of the COM0A1:0 bits depends on the WGM01:0 bit setting. are set to a normal or CTC mode (non-PWM). Table 14-3. COM0A1 Table 14-4 mode. Table 14-4. ...
Page 107
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 14-6. CS02 external pin modes are used for the Timer/Counter0, transitions on the T0 pin will clock the counter even if the pin is configured as an output. This ...
Page 108
Timer/Counter0 occurs, i.e., when the OCF0A bit is set in the Timer/Coun- ter 0 Interrupt Flag Register – TIFR0. • Bit 0 – TOIE0: Timer/Counter0 Overflow Interrupt Enable When the TOIE0 bit is written to ...
Page 109
... TCNT1 for accessing Timer/Counter1 counter value and so on. A simplified block diagram of the 16-bit Timer/Counter is shown ATmega165A/ATmega165PA/ATmega325A/ATmega325PA/ATmega645A/ATmega645P” on page 2. CPU accessible I/O Registers, including I/O bits and I/O pins, are shown in bold. The device-specific I/O Register and bit locations are listed in the 130 ...
Page 110
Figure 15-1. 16-bit Timer/Counter Block Diagram Note: 1. Refer to 15.2.1 Registers The Timer/Counter (TCNT1), Output Compare Registers (OCR1A/B), and Input Capture Regis- ter (ICR1) are all 16-bit registers. Special procedures must be followed when accessing the 16- bit registers. ...
Page 111
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The Input Capture Register can capture the Timer/Counter value at a given external (edge trig- gered) event on either the Input Capture pin (ICP1 the Analog Comparator pins - Analog Comparator” on page Canceler) for reducing the ...
Page 112
CPU, the high byte stored in the temporary register, and the low byte written are both copied into the 16-bit register in the same clock cycle. When the low byte of a 16-bit register ...
Page 113
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Assembly Code Example TIM16_ReadTCNT1: C Code Example unsigned int TIM16_ReadTCNT1( void ) { } Note: The assembly code example returns the TCNT1 value in the r17:r16 register pair. The following code examples show how atomic write ...
Page 114
Assembly Code Example 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 void TIM16_WriteTCNT1( unsigned int i ...
Page 115
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 15.5 Counter Unit The main part of the 16-bit Timer/Counter is the programmable 16-bit bi-directional counter unit. Figure 15-2 Figure 15-2. Counter Unit Block Diagram Signal description (internal signals): Count Direction Clear clk TOP BOTTOM The 16-bit counter is ...
Page 116
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 mul- tiple events, can be applied via the ...
Page 117
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 For more information on how to access the 16-bit registers refer to on page 15.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 ...
Page 118
Output Compare Units The 16-bit comparator continuously compares TCNT1 with the Output Compare Register (OCR1x). If TCNT equals OCR1x the comparator signals a match. A match will set the Output Compare Flag (OCF1x) at the next timer clock cycle. ...
Page 119
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 abled the CPU will access the OCR1x directly. The content of the OCR1x (Buffer or Compare) Register is only changed by a write operation (the Timer/Counter does not update this register automatically as the TCNT1 and ICR1 Register). Therefore ...
Page 120
Figure 15-5. Compare Match Output Unit, Schematic COMnx1 COMnx0 FOCnx 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 ...
Page 121
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 15.9 Modes of Operation The mode of operation, i.e., the behavior of the Timer/Counter and the Output Compare pins, is defined by the combination of the Waveform Generation mode (WGM13:0) and Compare Output mode (COM1x1:0) bits. The Compare Output ...
Page 122
Figure 15-6. CTC Mode, Timing Diagram TCNTn OCnA (Toggle) Period 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 ...
Page 123
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 imum resolution is 16-bit (ICR1 or OCR1A set to MAX). The PWM resolution in bits can be calculated by using the following equation: In fast PWM mode the counter is incremented until the counter value matches either one of ...
Page 124
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 ...
Page 125
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 TOP and changes the count direction. The TCNT1 value will be equal to TOP for one timer clock cycle. The timing diagram for the phase correct PWM mode is shown on shows phase correct PWM mode when OCR1A or ...
Page 126
The actual OC1x value will only be visible on the port pin if the data direction for the port pin is set as output (DDR_OC1x). The PWM waveform is generated by setting (or clearing) the OC1x Register at the compare ...
Page 127
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 15-9. Phase and Frequency Correct PWM Mode, Timing Diagram TCNTn OCnx OCnx Period The Timer/Counter Overflow Flag (TOV1) is set at the same timer clock cycle as the OCR1x Registers are updated with the double buffer value (at ...
Page 128
The extreme values for the OCR1x Register represents special cases when generating a PWM waveform output in the phase and frequency correct PWM mode. If the OCR1x is set equal to BOTTOM the output will be continuously low and if ...
Page 129
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 15-12. Timer/Counter Timing Diagram, no Prescaling (PC and PFC PWM) Figure 15-13 Figure 15-13. Timer/Counter Timing Diagram, with Prescaler (f and ICF n 8285B–AVR–03/11 clk I/O clk Tn (clk /1) I/O TCNTn TOP - 1 (CTC and FPWM) ...
Page 130
Register Description 15.11.1 TCCR1A – Timer/Counter1 Control Register A Bit (0x80) Read/Write Initial Value • Bit 7:6 – COM1A[1:0]: Compare Output Mode for Unit A • Bit 5:4 – COM1B[1:0]: Compare Output Mode for Unit B The COM1A[1:0] and ...
Page 131
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 15-4 correct or the phase and frequency correct, PWM mode. Table 15-4. COM1A1/COM1B1 Note: • Bit 1:0 – WGM1[1:0]: Waveform Generation Mode Combined with the WGM1[3:2] bits found in the TCCR1B Register, these bits control the count- ing ...
Page 132
Table 15-5. Waveform Generation Mode Bit Description WGM12 WGM11 Mode WGM13 (CTC1) (PWM11 ...
Page 133
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 When the ICR1 is used as TOP value (see description of the WGM1[3:0] bits located in the TCCR1A and the TCCR1B Register), the ICP1 is disconnected and consequently the Input Cap- ture function is disabled. • Bit 5 – ...
Page 134
A FOC1A/FOC1B strobe will not generate any interrupt nor will it clear the timer in Clear Timer on Compare match (CTC) mode using OCR1A as TOP. The FOC1A/FOC1B bits are always read as zero. 15.11.4 TCNT1H and TCNT1L – Timer/Counter1 ...
Page 135
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 15.11.7 ICR1H and ICR1L – Input Capture Register 1 Bit (0x87) (0x86) Read/Write Initial Value The Input Capture is updated with the counter (TCNT1) value each time an event occurs on the ICP1 pin (or optionally on the Analog ...
Page 136
TIFR1 – Timer/Counter1 Interrupt Flag Register Bit 0x16 (0x36) Read/Write Initial Value • Bit 5 – ICF1: Timer/Counter1, Input Capture Flag This flag is set when a capture event occurs on the ICP1 pin. When the Input Capture Register ...
Page 137
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 16. Timer/Counter0 and Timer/Counter1 Prescalers Timer/Counter1 and Timer/Counter0 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. 16.1 Prescaler Reset The prescaler is free running, i.e., ...
Page 138
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 sys- tem clock frequency (f sampling, the maximum ...
Page 139
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 16.4 Register Description 16.4.1 GTCCR – General Timer/Counter Control Register Bit 0x23 (0x43) Read/Write Initial Value • Bit 7 – TSM: Timer/Counter Synchronization Mode Writing the TSM bit to one activates the Timer/Counter Synchronization mode. In this mode, the ...
Page 140
Timer/Counter2 with PWM and Asynchronous Operation 17.1 Features • Single Compare Unit Counter • Clear Timer on Compare Match (Auto Reload) • Glitch-free, Phase Correct Pulse Width Modulator (PWM) • Frequency Generator • 10-bit Clock Prescaler • Overflow ...
Page 141
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The Timer/Counter can be clocked internally, via the prescaler, or asynchronously clocked from the TOSC1/2 pins, as detailed later in this section. The asynchronous operation is controlled by the Asynchronous Status Register (ASSR). The Clock Select logic block controls ...
Page 142
Signal description (internal signals): count direction clear clk top bottom Depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clk selected by the Clock Select bits (CS2[2:0]). When no clock source ...
Page 143
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Figure 17-3. Output Compare Unit, Block Diagram The OCR2A 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 ...
Page 144
The setup of the OC2A should be performed before setting the Data Direction Register for the port pin to output. The easiest way of setting the OC2A value is to use the Force Output Com- pare (FOC2A) strobe bit in ...
Page 145
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 on the OC2A Register performed on the next compare match. For compare output actions in the non-PWM modes refer to Table 17-4 on page A change of the COM2A[1:0] bits state will have effect at the ...
Page 146
Figure 17-5. CTC Mode, Timing Diagram TCNTn OCnx (Toggle) Period 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 ...
Page 147
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 inverted PWM outputs. The small horizontal line marks on the TCNT2 slopes represent compare matches between OCR2A and TCNT2. Figure 17-6. Fast PWM Mode, Timing Diagram TCNTn OCnx OCnx Period The Timer/Counter Overflow Flag (TOV2) is set each time ...
Page 148
The counter counts repeatedly from BOTTOM to MAX and then from MAX to BOTTOM. In non- inverting Compare Output mode, the Output Compare (OC2A) is cleared on the compare match between TCNT2 and OCR2A while upcounting, and set on the ...
Page 149
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The N variable represents the prescale factor (1, 8, 32, 64, 128, 256, or 1024). The extreme values for the OCR2A Register represent special cases when generating a PWM waveform output in the phase correct PWM mode. If the ...
Page 150
Figure 17-9. Timer/Counter Timing Diagram, with Prescaler (f clk clk (clk TCNTn TOVn Figure 17-10 Figure 17-10. Timer/Counter Timing Diagram, Setting of OCF2A, with Prescaler (f clk clk (clk TCNTn OCRnx OCFnx Figure 17-11 Figure 17-11. Timer/Counter Timing Diagram, Clear ...
Page 151
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 17.9 Asynchronous operation of the Timer/Counter 17.9.1 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, OCR2A, and TCCR2A might ...
Page 152
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, ...
Page 153
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 17.10 Timer/Counter Prescaler Figure 17-12. Prescaler for Timer/Counter2 TOSC1 The clock source for Timer/Counter2 is named clk system I/O clock clk clocked from the TOSC1 pin. This enables use of Timer/Counter2 as a Real Time Counter (RTC). When AS2 ...
Page 154
Register Description 17.11.1 TCCR2A – Timer/Counter Control Register A Bit (0xB0) Read/Write Initial Value • Bit 7 – FOC2A: Force Output Compare A The FOC2A bit is only active when the WGM bits specify a non-PWM mode. However, for ...
Page 155
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 17-3. COM2A1 Table 17-4 PWM mode. Table 17-4. COM2A1 Note: Table 17-5 correct PWM mode. Table 17-5. COM2A1 Note: 8285B–AVR–03/11 Compare Output Mode, non-PWM Mode COM2A0 Description 0 0 Normal ...
Page 156
Bit 2:0 – CS2[2:0]: Clock Select The three Clock Select bits select the clock source to be used by the Timer/Counter, see 17-6. Table 17-6. CS22 17.11.2 TCNT2 – Timer/Counter Register ...
Page 157
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 17.11.4 TIMSK2 – Timer/Counter2 Interrupt Mask Register Bit (0x70) Read/Write Initial Value • Bit 1 – OCIE2A: Timer/Counter2 Output Compare Match A Interrupt Enable When the OCIE2A bit is written to one and the I-bit in the Status Register ...
Page 158
Bit 3 – AS2: Asynchronous Timer/Counter2 When AS2 is written to zero, Timer/Counter2 is clocked from the I/O clock, clk written to one, Timer/Counter2 is clocked from a crystal Oscillator connected to the Timer Oscil- lator 1 (TOSC1) pin. ...
Page 159
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 18. SPI – Serial Peripheral Interface 18.1 Features • Full-duplex, Three-wire Synchronous Data Transfer • Master or Slave Operation • LSB First or MSB First Data Transfer • Seven Programmable Bit Rates • End of Transmission Interrupt Flag • ...
Page 160
Slave Select SS pin of the desired Slave. Master and Slave prepare the data to be sent in their respective shift Registers, and the Master generates the required clock pulses on the SCK line ...
Page 161
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 18-1. Pin MOSI MISO SCK SS Note: The following code examples show how to initialize the SPI as a Master and how to perform a simple transmission. DDR_SPI in the examples must be replaced by the actual Data ...
Page 162
Assembly Code Example SPI_MasterInit: ; Set MOSI and SCK output, all others input ldi out ; Enable SPI, Master, set clock rate fck/16 ldi out ret SPI_MasterTransmit: ; Start transmission of data (r16) out Wait_Transmit: ; Wait for transmission complete ...
Page 163
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The following code examples show how to initialize the SPI as a Slave and how to perform a simple reception. Assembly Code Example SPI_SlaveInit: SPI_SlaveReceive: C Code Example void SPI_SlaveInit(void char SPI_SlaveReceive(void Note: 8285B–AVR–03/11 (1) ...
Page 164
SS Pin Functionality 18.3.1 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 ...
Page 165
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 18.4 Data Modes There are four combinations of SCK phase and polarity with respect to serial data, which are determined by control bits CPHA and CPOL. The SPI data transfer formats are shown in 18-3 and nal, ensuring sufficient ...
Page 166
Register Description 18.5.1 SPCR – SPI Control Register Bit 0x2C (0x4C) Read/Write Initial Value • Bit 7 – SPIE: SPI Interrupt Enable This bit causes the SPI interrupt to be executed if SPIF bit in the SPSR Register is ...
Page 167
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The relationship between SCK and the Oscillator Clock frequency f table: Table 18-5. SPI2X 18.5.2 SPSR – SPI Status Register Bit 0x2D (0x4D) Read/Write Initial Value • Bit 7 – SPIF: SPI Interrupt Flag When a serial transfer is ...
Page 168
SPDR – SPI Data Register Bit 0x2E (0x4E) Read/Write Initial Value 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. ...
Page 169
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19. USART 19.1 Features • Full Duplex Operation (Independent Serial Receive and Transmit Registers) • Asynchronous or Synchronous Operation • Master or Slave Clocked Synchronous Operation • High Resolution Baud Rate Generator • Supports Serial Frames with 5, 6, ...
Page 170
Figure 19-1. USART Block Diagram Note: 1. Refer to The dashed boxes in the block diagram separate the three main parts of the USART (listed from the top): Clock Generator, Transmitter and Receiver. Control Registers are shared by all units. ...
Page 171
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.2.1 AVR USART vs. AVR UART – Compatibility The USART is fully compatible with the AVR UART regarding: • Bit locations inside all USART Registers. • Baud Rate Generation. • Transmitter Operation. • Transmit Buffer Functionality. • Receiver Operation. ...
Page 172
Signal description: txclk rxclk xcki xcko fosc 19.3.1 Internal Clock Generation – The Baud Rate Generator Internal clock generation is used for the asynchronous and the synchronous master modes of operation. The description in this section refers to The USART ...
Page 173
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Setting this bit will reduce the divisor of the baud rate divider from effectively doubling the transfer rate for asynchronous communication. Note however that the Receiver will in this case only use half the number of ...
Page 174
Frame Formats A serial frame is defined to be one character of data bits with synchronization bits (start and stop bits), and optionally a parity bit for error checking. The USART accepts all 30 combinations of the following as ...
Page 175
ATmega165A/165PA/325A/325PA/3250A/3250PA used, the parity bit is located between the last data bit and first stop bit of a serial frame. 19.5 USART Initialization The USART has to be initialized before any communication can take place. The ...
Page 176
Assembly Code Example USART_Init: ; Set baud rate sts sts ; Enable receiver and transmitter ldi sts ; Set frame format: 8data, 2stop bit ldi sts ret C Code Example #define FOSC 1843200// Clock Speed #define BAUD 9600 #define MYUBRR ...
Page 177
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.6 Data Transmission – The USART Transmitter The USART Transmitter is enabled by setting the Transmit Enable (TXENn) bit in the UCSRnB Register. When the Transmitter is enabled, the normal port operation of the TxD pin is overrid- den ...
Page 178
For the assembly code, the data to be sent is assumed to be stored in registers R17:R16. Assembly Code Example USART_Transmit: ; Wait for empty transmit buffer sbis UCSR0A,UDRE0 rjmp USART_Transmit ; ...
Page 179
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.6.3 Transmitter Flags and Interrupts The USART Transmitter has two flags that indicate its state: USART Data Register Empty (UDREn) and Transmit Complete (TXCn). Both flags can be used for generating interrupts. The Data Register Empty (UDREn) Flag indicates ...
Page 180
Data Reception – The USART Receiver The USART Receiver is enabled by writing the Receive Enable (RXENn) bit in the UCSRnB Register to one. When the Receiver is enabled, the normal pin operation of the RxD pin is over- ...
Page 181
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.7.2 Receiving Frames with 9 Data Bits If 9-bit characters are used (UCSZ=7) the ninth bit must be read from the RXB8n bit in UCSRnB before reading the low bits from the UDRn. This rule applies to the FEn, ...
Page 182
Assembly Code Example USART_Receive: ; Wait for data to be received sbis UCSR0A, RXC0 rjmp USART_Receive ; Get status and 9th bit, then data from buffer error, return -1 andi r18,(1<<FE0)|(1<<DOR0)|(1<<UPE0) breq USART_ReceiveNoError ldi ldi ...
Page 183
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.7.3 Receive Compete 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 buf- fer. This flag is one when ...
Page 184
Parity Checker The Parity Checker is active when the high USART Parity mode (UPM1n) bit is set. Type of Par- ity Check to be performed (odd or even) is selected by the UPM0n bit. When enabled, the Parity Checker ...
Page 185
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.8 Asynchronous Data Reception The USART includes a clock recovery and a data recovery unit for handling asynchronous data reception. The clock recovery logic is used for synchronizing the internally generated baud rate clock to the incoming asynchronous serial ...
Page 186
Figure 19-6. Sampling of Data and Parity Bit RxD Sample (U2X = 0) Sample (U2X = 1) The decision of the logic level of the received bit is taken by doing a majority voting of the logic value to the ...
Page 187
ATmega165A/165PA/325A/325PA/3250A/3250PA slow Table 19-2 that Normal Speed mode has higher toleration of baud rate variations. Table 19-2. # (Data+Parity Bit) Table 19-3. # (Data+Parity Bit) The recommendations of the maximum receiver baud rate ...
Page 188
In this case an UBRRn value that gives an acceptable low error can be used if possible. 19.9 Multi-processor Communication Mode Setting the Multi-processor Communication mode (MPCMn) bit ...
Page 189
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.10 Examples of Baud Rate Setting For standard crystal and resonator frequencies, the most commonly used baud rates for asyn- chronous operation can be generated by using the UBRRn settings in values which yield an actual baud rate differing ...
Page 190
Table 19-5. Examples of UBRRn Settings for Commonly Used Oscillator Frequencies (Continued 3.6864MHz osc Baud U2Xn = 0 U2Xn = 1 Rate (bps) UBRRn Error UBRRn 2400 95 0.0% 191 4800 47 0.0% 95 9600 23 0.0% 47 ...
Page 191
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 Table 19-6. Examples of UBRRn Settings for Commonly Used Oscillator Frequencies (Continued 8.0000MHz osc Baud U2Xn = 0 U2Xn = 1 Rate (bps) UBRRn Error UBRRn 2400 207 0.2% 416 4800 103 0.2% 207 9600 51 0.2% ...
Page 192
Table 19-7. Examples of UBRRn Settings for Commonly Used Oscillator Frequencies (Continued 16.0000MHz osc Baud U2Xn = 0 U2Xn = 1 Rate (bps) UBRRn Error UBRRn 2400 416 -0.1% 832 4800 207 0.2% 416 9600 103 0.2% 207 ...
Page 193
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.11 Register Description 19.11.1 UDRn – USART I/O Data Register Bit (0xC6) Read/Write Initial Value 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 ...
Page 194
UDREn is set after a reset to indicate that the Transmitter is ready. • Bit 4 – FEn: Frame Error n This bit is set if the next character in the receive buffer had a Frame Error when received. I.e., ...
Page 195
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 • Bit 5 – UDRIEn: USART Data Register Empty Interrupt Enable 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, ...
Page 196
Receiver will generate a parity value for the incoming data and compare it to the UPM0n setting mismatch is detected, the UPEn Flag in UCSRnA will be set. Table 19-9. UPMn1 • Bit 3 – USBSn: Stop Bit ...
Page 197
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 19.11.5 UBRRnL and UBRRnH – USART Baud Rate Registers Bit (0xC5) (0xC4) Read/Write Initial Value • Bit 15:12 – Reserved These bits are reserved for future use. For compatibility with future devices, these bit must be written to zero ...
Page 198
USI – Universal Serial Interface 20.1 Features • Two-wire Synchronous Data Transfer (Master or Slave) • Three-wire Synchronous Data Transfer (Master or Slave) • Data Received Interrupt • Wakeup from Idle Mode • In Two-wire Mode: Wake-up from All ...
Page 199
ATmega165A/165PA/325A/325PA/3250A/3250PA/6 The 4-bit counter can be both read and written via the data bus, and can generate an overflow interrupt. Both the Serial Register and the counter are clocked simultaneously by the same clock source. This allows the counter to ...
Page 200
Figure 20-3. Three-wire Mode, Timing Diagram CYCLE USCK USCK DO The Three-wire mode timing is shown in USCK cycle reference. One bit is shifted into the USI Shift Register (USIDR) for each of these cycles. The USCK timing is shown ...