ATMEGA32U4-MUR

Manufacturer Part NumberATMEGA32U4-MUR
DescriptionMCU AVR 16K FLASH 16MHZ 44VQFN
ManufacturerAtmel
SeriesAVR® ATmega
ATMEGA32U4-MUR datasheet
 


Specifications of ATMEGA32U4-MUR

Core ProcessorAVRCore Size8-Bit
Speed16MHzConnectivityI²C, SPI, UART/USART, USB
PeripheralsBrown-out Detect/Reset, POR, PWM, WDTNumber Of I /o26
Program Memory Size32KB (16K x 16)Program Memory TypeFLASH
Eeprom Size1K x 8Ram Size2.5K x 8
Voltage - Supply (vcc/vdd)2.7 V ~ 5.5 VData ConvertersA/D 12x10b
Oscillator TypeExternalOperating Temperature-40°C ~ 85°C
Package / Case44-VQFNLead Free Status / RoHS StatusLead free / RoHS Compliant
1
Page 1
2
Page 2
3
Page 3
4
Page 4
5
Page 5
6
Page 6
7
Page 7
8
Page 8
9
Page 9
10
Page 10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
Page 1/433

Download datasheet (9Mb)Embed
Next
Features
High Performance, Low Power AVR
Advanced RISC Architecture
– 135 Powerful Instructions – Most Single Clock Cycle Execution
– 32 x 8 General Purpose Working Registers
– Fully Static Operation
– Up to 16 MIPS Throughput at 16 MHz
– On-Chip 2-cycle Multiplier
Non-volatile Program and Data Memories
– 16/32K Bytes of In-System Self-Programmable Flash (ATmega16U4/ATmega32U4)
– 1.25/2.5K Bytes Internal SRAM (ATmega16U4/ATmega32U4)
– 512Bytes/1K Bytes Internal EEPROM (ATmega16U4/ATmega32U4)
– Write/Erase Cycles: 10,000 Flash/100,000 EEPROM
– Data retention: 20 years at 85°C/ 100 years at 25°C
– Optional Boot Code Section with Independent Lock Bits
In-System Programming by On-chip Boot Program
True Read-While-Write Operation
All supplied parts are preprogramed with a default USB bootloader
– Programming Lock for Software Security
JTAG (IEEE std. 1149.1 compliant) Interface
– Boundary-scan Capabilities According to the JTAG Standard
– Extensive On-chip Debug Support
– Programming of Flash, EEPROM, Fuses, and Lock Bits through the JTAG Interface
USB 2.0 Full-speed/Low Speed Device Module with Interrupt on Transfer Completion
– Complies fully with Universal Serial Bus Specification Rev 2.0
– Supports data transfer rates up to 12 Mbit/s and 1.5 Mbit/s
– Endpoint 0 for Control Transfers: up to 64-bytes
– 6 Programmable Endpoints with IN or Out Directions and with Bulk, Interrupt or
Isochronous Transfers
– Configurable Endpoints size up to 256 bytes in double bank mode
– Fully independent 832 bytes USB DPRAM for endpoint memory allocation
– Suspend/Resume Interrupts
– CPU Reset possible on USB Bus Reset detection
– 48 MHz from PLL for Full-speed Bus Operation
– USB Bus Connection/Disconnection on Microcontroller Request
– Crystal-less operation for Low Speed mode
Peripheral Features
– On-chip PLL for USB and High Speed Timer: 32 up to 96 MHz operation
– One 8-bit Timer/Counter with Separate Prescaler and Compare Mode
– Two 16-bit Timer/Counter with Separate Prescaler, Compare- and Capture Mode
– One 10-bit High-Speed Timer/Counter with PLL (64 MHz) and Compare Mode
– Four 8-bit PWM Channels
– Four PWM Channels with Programmable Resolution from 2 to 16 Bits
– Six PWM Channels for High Speed Operation, with Programmable Resolution from
2 to 11 Bits
– Output Compare Modulator
– 12-channels, 10-bit ADC (features Differential Channels with Programmable Gain)
– Programmable Serial USART with Hardware Flow Control
– Master/Slave SPI Serial Interface
®
8-Bit Microcontroller
(1)
8-bit
Microcontroller
with
16/32K Bytes of
ISP Flash
and USB
Controller
ATmega16U4
ATmega32U4
Preliminary
7766F–AVR–11/10

ATMEGA32U4-MUR Summary of contents

  • Page 1

    ... MIPS Throughput at 16 MHz – On-Chip 2-cycle Multiplier • Non-volatile Program and Data Memories – 16/32K Bytes of In-System Self-Programmable Flash (ATmega16U4/ATmega32U4) – 1.25/2.5K Bytes Internal SRAM (ATmega16U4/ATmega32U4) – 512Bytes/1K Bytes Internal EEPROM (ATmega16U4/ATmega32U4) – Write/Erase Cycles: 10,000 Flash/100,000 EEPROM – Data retention: 20 years at 85°C/ 100 years at 25°C – ...

  • Page 2

    Byte Oriented 2-wire Serial Interface – Programmable Watchdog Timer with Separate On-chip Oscillator – On-chip Analog Comparator – Interrupt and Wake-up on Pin Change – On-chip Temperature Sensor • Special Microcontroller Features – Power-on Reset and Programmable Brown-out Detection ...

  • Page 3

    ... PB2 (PDO/PCINT3/MISO) PB3 2. Overview The ATmega16U4/ATmega32U4 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 ATmega16U4/ATmega32U4 achieves throughputs approaching 1 MIPS per MHz allowing the system designer to optimize power consumption versus processing speed. ...

  • Page 4

    ... CISC microcontrollers. The ATmega16U4/ATmega32U4 provides the following features: 16/32K bytes of In-System Programmable Flash with Read-While-Write capabilities, 512Bytes/1K bytes EEPROM, 1.25/2.5K bytes SRAM, 26 general purpose I/O lines (CMOS outputs and LVTTL inputs), 32 ...

  • Page 5

    ... The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B has better driving capabilities than the other ports. Port B also serves the functions of various special features of the ATmega16U4/ATmega32U4 as listed on 2.2.4 Port C (PC7,PC6) Port 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit) ...

  • Page 6

    ... Only bits 6 and 7 are present on the product pinout. Port C also serves the functions of special features of the ATmega16U4/ATmega32U4 as listed on page 2.2.5 Port D (PD7..PD0) Port 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port D output buffers have symmetrical drive characteristics with both high sink and source capability ...

  • Page 7

    UVCC USB Pads Internal Regulator Input supply voltage. 2.2.12 UCAP USB Pads Internal Regulator Output supply voltage. Should be connected to an external capac- itor (1µF). 2.2.13 VBUS USB VBUS monitor input. 2.2.14 RESET Reset input. A low level ...

  • Page 8

    ... Resources A comprehensive set of development tools, application notes and datasheets are available for download on http://www.atmel.com/avr. 3.3 Code Examples This documentation contains simple code examples that briefly show how to use various parts of the device. Be aware that not all C compiler vendors include bit definitions in the header files and interrupt handling compiler dependent ...

  • Page 9

    AVR CPU Core 4.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 10

    ... SPI, and other I/O functions. The I/O Memory can be accessed directly the Data Space locations following those of the Register File, 0x20 - 0x5F. In addition, the ATmega16U4/ATmega32U4 has Extended I/O space from 0x60 - 0x0FF in SRAM where only the ST/STS/STD and LD/LDS/LDD instructions can be used. ...

  • Page 11

    Status Register The Status Register contains information about the result of the most recently executed arithme- tic instruction. This information can be used for altering program flow in order to perform conditional operations. Note that the Status Register is ...

  • Page 12

    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. 4.5 General Purpose Register File The Register File is optimized for the ...

  • Page 13

    Figure 4-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). 4.6 Stack Pointer The Stack is mainly used for storing ...

  • Page 14

    Extended Z-pointer Register for ELPM/SPM - RAMPZ Bit Read/Write Initial Value For ELPM/SPM instructions, the Z-pointer is a concatenation of RAMPZ, ZH, and ZL, as shown in Figure 4-4. Note that LPM is not affected by the RAMPZ setting. ...

  • Page 15

    Figure 4-6. Register Operands Fetch ALU Operation Execute 4.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 ...

  • Page 16

    Note that the Status Register is not automatically stored when entering an interrupt routine, nor restored when returning from an interrupt routine. This must be handled by software. When using the CLI instruction to disable interrupts, the interrupts will be ...

  • Page 17

    Assembly Code Example sei sleep; enter sleep, waiting for interrupt ; note: will enter sleep before any pending ; interrupt(s) C Code Example __enable_interrupt(); /* set Global Interrupt Enable */ __sleep(); /* enter sleep, waiting for interrupt */ /* note: ...

  • Page 18

    ... Boot Program section and Application Program section. The Flash memory has an endurance of at least 100,000 write/erase cycles. The ATmega16U4/ATmega32U4 Program Counter (PC bits wide, thus addressing the 32K program memory locations. The operation of Boot Program section and associated Boot Lock bits for software protection are described in detail in 7766F– ...

  • Page 19

    ... SRAM Data Memory Figure 5-2 The ATmega16U4/ATmega32U4 is a complex microcontroller with more peripheral units than can be supported within the 64 location reserved in the Opcode for the IN and OUT instructions. For the Extended I/O space from $060 - $0FF in SRAM, only the ST/STS/STD and LD/LDS/LDD instructions can be used ...

  • Page 20

    ... When using register indirect addressing modes with automatic pre-decrement and post-incre- ment, the address registers X, Y, and Z are decremented or incremented. The 32 general purpose working registers, 64 I/O registers, and the 1.25/2.5Kbytes of internal data SRAM in the ATmega16U4/ATmega32U4 are all accessible through all these addressing modes. The Register File is described in Figure 5-2. ...

  • Page 21

    ... Initial Value • Bits 15..12 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and will always read as zero. • Bits 11..0 – EEAR8..0: EEPROM Address The EEPROM Address Registers – EEARH and EEARL specify the EEPROM address in the 512Bytes/1K bytes EEPROM space ...

  • Page 22

    ... Initial Value • Bits 7..6 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and will always read as zero. • Bits 5, 4 – EEPM1 and EEPM0: EEPROM Programming Mode Bits The EEPROM Programming mode bit setting defines which programming action that will be trig- gered when writing EEPE ...

  • Page 23

    EEPROM write takes place. The following procedure should be followed when writing the EEPROM (the order of steps 3 and 4 is not essential): 1. Wait until EEPE becomes zero. 2. Wait until SELFPRGEN in SPMCSR becomes zero. ...

  • Page 24

    Assembly Code Example EEPROM_write: C Code Example void EEPROM_write(unsigned int uiAddress, unsigned char ucData Note: The next code examples show assembly and C functions for reading the EEPROM. The exam- ples assume that interrupts are controlled so that ...

  • Page 25

    Assembly Code Example EEPROM_read: C Code Example unsigned char EEPROM_read(unsigned int uiAddress Note: 5.3.5 Preventing EEPROM Corruption During periods of low V too low for the CPU and the EEPROM to operate properly. These issues are the same ...

  • Page 26

    ... The I/O space definition of the ATmega16U4/ATmega32U4 is shown in page 408. All ATmega16U4/ATmega32U4 I/Os and peripherals are placed in the I/O space. All I/O loca- tions may be accessed by the LD/LDS/LDD and ST/STS/STD instructions, transferring data between the 32 general purpose working registers and the I/O space. I/O Registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI and CBI instructions ...

  • Page 27

    System Clock and Clock Options 6.1 Clock Systems and their Distribution Figure 6-1 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 ...

  • Page 28

    ADC Clock – clk ADC 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. This gives more accurate ADC conversion results. 6.1.5 ...

  • Page 29

    ... CKDIV8 programmed, resulting in 1.0MHz system clock with an 8 MHz crystal. See 28-5 on page 348 6.2.2 Default Clock Source ATmega16U4RC and ATmega32U4RC The device is shipped with Calibrated Internal RC oscillator (8.0MHz) enabled and with the fuse CKDIV8 programmed, resulting in 1.0MHz system clock. See overview of the default Clock Selection Fuse setting ...

  • Page 30

    C1 and C2 should always be equal for both crystals and resonators. The optimal value of the capacitors depends on the crystal or resonator in use, the amount of stray capacitance, and the electromagnetic noise of the environment. Some initial ...

  • Page 31

    Table 6-4. Oscillator Source / Power Conditions Ceramic resonator, slowly rising power Crystal Oscillator, BOD enabled Crystal Oscillator, fast rising power Crystal Oscillator, slowly rising power Notes: Table 6-5. Power Conditions BOD enabled Fast rising power Slowly rising power Note: ...

  • Page 32

    Note: 6.5 Calibrated Internal RC Oscillator The calibrated internal RC Oscillator by default provides a 8.0 MHz clock. This frequency is nominal value at 3V and 25°C. The device is shipped with the CKDIV8 Fuse programmed. See “System Clock Prescaler” ...

  • Page 33

    ... The temperature sensitivity is quite linear but as said previously depends on the process. To determine its slope, the frequency must be measured at two temperatures. The temperature sensor of the ATmega16U4/ATmega32U4 allows such an operation, that is detailed on 24.6.1 ”Sensor Calibration” on page in function of the temperature measured. Note that this oscillator is used to time EEPROM and Flash write accesses, and these write times will be affected accordingly ...

  • Page 34

    ... Refer to 37 for details. 6.7 Clock Switch The ATmega16U4/ATmega32U4 product includes a Clock Switch controller, that allows user to switch from one clock source to another one by software, in order to control application power and execution time with more accuracy. 6.7.1 Example of use The modification may be needed when the device enters in USB Suspend mode ...

  • Page 35

    Figure 6-4. USB non-Idle CPU Clock Ext External Oscillator RC oscillator Figure 6-5. USB non-Idle CPU Clock Ext External Oscillator RC oscillator 6.8 Clock switch Algorithm 6.8.1 Switch from external clock to RC clock if (Usb_suspend_detected()) { } 7766F–AVR–11/10 Example ...

  • Page 36

    Switch from RC clock to external clock if (Usb_wake_up_detected()) { } 6.8.3 CLKSEL0 – Clock Selection Register 0 Bit Read/Write Initial Value • Bit 7-6 – RCSUT[1:0]: SUT for RC oscillator These 2 bits are the SUT value for ...

  • Page 37

    Bit 7-4 – RCCKSEL[3:0]: CKSEL for RC oscillator Clock configuration for the RC Oscillator. After a reset, this part of the register is loaded with the 0010b value that corresponds to the RC oscillator. Modifying this value by firmware ...

  • Page 38

    The ripple counter that implements the prescaler runs at the frequency of the undivided clock, which may be faster than the CPU's clock frequency. Hence not possible to determine the state of the prescaler - even if it ...

  • Page 39

    ... XTAL1). 6.10.1 Internal PLL The internal PLL in ATmega16U4/ATmega32U4 generates a clock frequency between 32MHz and 96 MHz from nominally 8MHz input. The source of the 8MHz PLL input clock is the output of the internal PLL clock prescaler that generates the 8MHz from the clock source multiplexer output (See face) ...

  • Page 40

    ... When using a 16 MHz clock source, this bit must be set to 1 before enabling PLL (1:2). • Bit 3..2 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and always read as zero. • Bit 1 – PLLE: PLL Enable When the PLLE is set, the PLL is started. Note that the Calibrated 8 MHz Internal RC oscillator is automatically enabled when the PLLE bit is set and with PINMUX (see PLLFRQ register) is set ...

  • Page 41

    Bit 7– PINMUX: PLL Input Multiplexer This bit selects the clock input of the PLL: – PINMUX = 0: the PLL input is connected to the PLL Prescaler, that has the Primary – PINMUX = 1: the PLL input ...

  • Page 42

    PDIV3 The optimal PLL configuration at 5V is: PLL output frequency = 96 MHz, divided by 1.5 to gener- ate the 64 MHz High Speed Timer clock, and divided ...

  • Page 43

    ... To avoid the MCU entering the sleep mode unless it is the programmer’s purpose recommended to write the Sleep Enable (SE) bit to one just before the execution of the SLEEP instruction and to clear it immediately after waking up. 7766F–AVR–11/10 Table 7-1 presents the different clock systems in the ATmega16U4/ATmega32U4 – ...

  • Page 44

    Idle Mode When the SM2..0 bits are written to 000, the SLEEP instruction makes the MCU enter Idle mode, stopping the CPU but allowing the USB, SPI, USART, Analog Comparator, ADC, 2-wire Serial Interface, Timer/Counters, Watchdog, and the interrupt ...

  • Page 45

    Standby Mode When the SM2..0 bits are 110 and an external crystal/resonator clock option is selected, the SLEEP instruction makes the MCU enter Standby mode. This mode is identical to Power-down with the exception that the Oscillator is kept ...

  • Page 46

    Power Reduction Register 0 - PRR0 Bit Read/Write Initial Value • 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 ...

  • Page 47

    Bit 4- PRTIM4: Power Reduction Timer/Counter4 Writing a logic one to this bit shuts down the Timer/Counter4 module. When the Timer/Counter4 is enabled, operation will continue like before the shutdown. • Bit 3 - PRTIM3: Power Reduction Timer/Counter3 Writing ...

  • Page 48

    If the reference is kept on in sleep mode, the output can be used immediately. Refer to age Reference” on page 54 7.8.5 ...

  • Page 49

    ... Reset Sources The ATmega16U4/ATmega32U4 has five sources of reset: • Power-on Reset. The MCU is reset when the supply voltage is below the Power-on Reset threshold (V • External Reset. The MCU is reset when a low level is present on the RESET pin for longer than the minimum pulse length. • ...

  • Page 50

    Figure 8-1. BODLEVEL [2..0] Table 8-1. Symbol V POT V POR V CCRR V RST t RST Notes: 8.0.3 Power-on Reset A Power-on Reset (POR) pulse is generated by an On-chip detection circuit. The detection level is defined in POR ...

  • Page 51

    RESET after V when V Figure 8-2. TIME-OUT INTERNAL Figure 8-3. TIME-OUT INTERNAL 8.0.4 External Reset An External Reset is generated by a low level on the RESET pin. Reset pulses longer than the minimum pulse ...

  • Page 52

    ... Brown-out Detection ATmega16U4/ATmega32U4 has an On-chip Brown-out Detection (BOD) circuit for monitoring the V CC BOD can be selected by the BODLEVEL Fuses. The trigger level has a hysteresis to ensure spike free Brown-out Detection. The hysteresis on the detection level should be interpreted BOT+ Table 8-2. Table 8-3. ...

  • Page 53

    Watchdog 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 t page 55 Figure 8-6. ...

  • Page 54

    ... Reset Flags. 8.1 Internal Voltage Reference ATmega16U4/ATmega32U4 features an internal 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. 8.1.1 Voltage Reference Enable Signals and Start-up Time The voltage reference has a start-up time that may influence the way it should be used ...

  • Page 55

    ... Symbol Note: 8.2 Watchdog Timer ATmega16U4/ATmega32U4 has an Enhanced Watchdog Timer (WDT). The main features are: • Clocked from separate On-chip Oscillator • 3 Operating modes – Interrupt – System Reset – Interrupt and System Reset • Selectable Time-out period from 16ms to 8s • ...

  • Page 56

    This is typically used to prevent system hang-up in case of runaway code. The third mode, Interrupt and System Reset mode, combines the other two modes by first giving an inter- rupt and then switch to System Reset mode. ...

  • Page 57

    Assembly Code Example WDT_off: C Code Example void WDT_off(void Note: Note: If the Watchdog is accidentally enabled, for example by a runaway pointer or brown-out condition, the device will be reset and the Watchdog Timer will stay ...

  • Page 58

    The following code example shows one assembly and one C function for changing the time-out value of the Watchdog Timer. Assembly Code Example WDT_Prescaler_Change: C Code Example void WDT_Prescaler_Change(void Note: Note: The Watchdog Timer should be reset before ...

  • Page 59

    Alternatively, WDIF is cleared by writing a logic one to the flag. When the I-bit in SREG and WDIE are set, the Watchdog Time-out Interrupt is executed. • Bit 6 - WDIE: Watchdog Interrupt Enable When this bit ...

  • Page 60

    Table 8-6. WDP3 7766F–AVR–11/10 Watchdog Timer Prescale Select Number of WDT Oscillator WDP2 WDP1 WDP0 (2048) cycles 0 0 ...

  • Page 61

    ... Interrupts ATmega16U4/ATmega32U4. For a general explanation of the AVR interrupt handling, refer to “Reset and Interrupt Handling” on page 9.1 Interrupt Vectors in ATmega16U4/ATmega32U4 Table 9-1. Vector No 7766F–AVR–11/10 15. Reset and Interrupt Vectors Program (2) Address Source Interrupt Definition External Pin, Power-on Reset, Brown-out Reset, ...

  • Page 62

    Table 9-1. Vector No Notes: Table 9-2 BOOTRST and IVSEL settings. If the program never enables an interrupt source, the Interrupt Vectors are not used, and ...

  • Page 63

    MCU Control Register – MCUCR Bit Read/Write Initial Value • Bit 1 – IVSEL: Interrupt Vector Select When the IVSEL bit is cleared (zero), the Interrupt Vectors are placed at the start of the Flash memory. When this bit ...

  • Page 64

    Assembly Code Example Move_interrupts: C Code Example void Move_interrupts(void 7766F–AVR–11/10 ; Enable change of Interrupt Vectors ldi r16, (1<<IVCE) out MCUCR, r16 ; Move interrupts to Boot Flash section ldi r16, (1<<IVSEL) out MCUCR, r16 ret /* Enable ...

  • Page 65

    I/O-Ports 10.1 Introduction 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 66

    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. 10.2 Ports as General Digital I/O The ports are bi-directional I/O ports ...

  • Page 67

    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 ...

  • Page 68

    Figure 10-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 the ...

  • Page 69

    Assembly Code Example C Code Example unsigned char i; Note: 10.2.5 Digital Input Enable and Sleep Modes As shown in Schmidt-trigger. The signal denoted SLEEP in the figure, is set by the MCU Sleep Controller in Power-down mode, Power-save mode, ...

  • Page 70

    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 inputs ...

  • Page 71

    Note: Table 10-2 ure 10-5 in the modules having the alternate function. Table 10-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 ...

  • Page 72

    MCU Control Register – MCUCR Bit 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 DDxn and PORTxn Registers are ...

  • Page 73

    ... PDO/MISO/PCINT3 – Port B, Bit 3 PDO, SPI Serial Programming Data Output. During Serial Program Downloading, this pin is used as data output line for the ATmega16U4/ATmega32U4. MISO: Master Data input, Slave Data output pin for SPI channel. When the SPI is enabled as a master, this pin is configured as an input regardless of the setting of DDB3. When the SPI is enabled as a slave, the data direction of this pin is controlled by DDB3 ...

  • Page 74

    SCK/PCINT1 – Port B, Bit 1 SCK: Master Clock output, Slave Clock input pin for SPI channel. When the SPI is enabled as a slave, this pin is configured as an input regardless of the setting of DDB1. When ...

  • Page 75

    Table 10-5. Signal Name PUOE PUOV DDOE DDOV PVOE PVOV DIEOE DIEOV DI AIO 10.3.3 Alternate Functions of Port C The Port C alternate function is as follows: Table 10-6. • ICP3/CLKO/OC.4A – Port C, Bit 7 ICP3: If Timer ...

  • Page 76

    OC.3A/OC.4A – Port C, Bit 6 OC.3A: Timer 3 Output Compare A. This pin can be used to generate a PWM signal from Timer 3 module. OC.4A: Timer 4 Output Compare A. This pin can be used to generate ...

  • Page 77

    Alternate Functions of Port D The Port D pins with alternate functions are shown in Table 10-8. Port Pin PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 The alternate pin configuration is as follows: • T0/OC.4D/ADC10 – Port D, ...

  • Page 78

    INT3/TXD1 – Port D, Bit 3 INT3, External Interrupt source 3: The PD3 pin can serve as an external interrupt source to the MCU. TXD1, Transmit Data (Data output pin for the USART1). When the USART1 Transmitter is enabled, ...

  • Page 79

    Table 10-9. Signal Name PUOE PUOV DDOE DDOV PVOE PVOV DIEOE DIEOV DI AIO Table 10-10. Overriding Signals for Alternate Functions in PD3.PD0 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV DIEOE DIEOV DI AIO Note: 7766F–AVR–11/10 Overriding Signals for ...

  • Page 80

    Alternate Functions of Port E The Port E pins with alternate functions are shown in Table 10-11. Port E Pins Alternate Functions Port Pin PE7 PE6 PE5 PE4 PE3 PE2 PE1 PE0 • INT6/AIN0 – Port E, Bit 6 ...

  • Page 81

    PF7(TDI), PF5(TMS), and PF4(TCK) will be activated even if a Reset occurs. Table 10-13. Port F Pins Alternate Functions Port Pin PF7 PF6 PF5 PF4 PF3 PF2 PF1 PF0 • TDI, ADC7 – Port ...

  • Page 82

    Table 10-14. Overriding Signals for Alternate Functions in PF7..PF4 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV DIEOE DIEOV DI AIO Table 10-15. Overriding Signals for Alternate Functions in PF1..PF0 Signal Name PUOE PUOV DDOE DDOV PVOE PVOV DIEOE ...

  • Page 83

    Port B Data Direction Register – DDRB Bit Read/Write Initial Value 10.4.3 Port B Input Pins Address – PINB Bit Read/Write Initial Value 10.4.4 Port C Data Register – PORTC Bit Read/Write Initial Value 10.4.5 Port C Data Direction ...

  • Page 84

    Port E Data Register – PORTE Bit Read/Write Initial Value 10.4.11 Port E Data Direction Register – DDRE Bit Read/Write Initial Value 10.4.12 Port E Input Pins Address – PINE Bit Read/Write Initial Value 10.4.13 Port F Data Register ...

  • Page 85

    External Interrupts The External Interrupts are triggered by the INT6, INT3:0 pin or any of the PCINT7..0 pins. Observe that, if enabled, the interrupts will trigger even if the INT[6;3:0] or PCINT7..0 pins are configured as outputs. This feature ...

  • Page 86

    ... Initial Value • Bit 7..6 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and always read as zero. • Bits 5, 4 – ISC61, ISC60: External Interrupt 6 Sense Control Bits The External Interrupt 6 is activated by the external pin INT6 if the SREG I-flag and the corre- sponding interrupt mask in the EIMSK is set ...

  • Page 87

    ... Bit 3..0 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and always read as zero. 11.0.3 External Interrupt Mask Register – EIMSK Bit Read/Write Initial Value • Bits 7..0 – INT6, INT3 – INT0: External Interrupt Request Enable When an INT[6 ...

  • Page 88

    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 EIMSK are set (one), ...

  • Page 89

    Timer/Counter0, Timer/Counter1, and Timer/Counter3 Prescalers Timer/Counter0, 1, and 3 share the same prescaler module, but the Timer/Counters can have different prescaler settings. The description below applies to all Timer/Counters used as a general name ...

  • Page 90

    ... Since the edge detector uses ExtClk clk_I/O I/O Clear Synchronization Synchronization TIMER/COUNTERn CLOCK SOURCE clk ATmega16U4/ATmega32U4 products. “Tn” only refers to T3 input is not available on the either inputs TSM – – R ...

  • Page 91

    ... 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 (clk 7766F–AVR–11/10 “Pinout ATmega16U4/ATmega32U4” on page “8-bit Timer/Counter Register Description” on page Count Clear ...

  • Page 92

    The double buffered Output Compare Registers (OCR0A and OCR0B) are compared with the Timer/Counter value at all times. The result of the compare can be used by the Waveform Gen- erator to generate a PWM or variable frequency output on ...

  • Page 93

    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 is selected (CS02 the ...

  • Page 94

    Figure 13-3. Output Compare Unit, Block Diagram The OCR0x Registers are 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 dou- ble buffering is ...

  • Page 95

    Similarly, do not write the TCNT0 value equal to BOTTOM when the counter is down-counting. The setup of the OC0x should be performed before setting the Data Direction Register for the port pin to output. The easiest way of ...

  • Page 96

    Compare Output Mode and Waveform Generation The Waveform Generator uses the COM0x1:0 bits differently in Normal, CTC, and PWM modes. For all modes, setting the COM0x1 tells the Waveform Generator that no action on the OC0x Register ...

  • Page 97

    Figure 13-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 98

    PWM mode is shown in togram for illustrating the single-slope operation. The diagram includes non-inverted and inverted PWM outputs. The small horizontal line marks on the TCNT0 slopes represent Com- pare Matches between OCR0x and TCNT0. Figure 13-6. Fast PWM ...

  • Page 99

    OC0A toggle in CTC mode, except the double buffer feature of the Out- put Compare unit is enabled in the fast PWM mode. 13.6.4 Phase Correct PWM Mode The phase correct PWM mode (WGM02:0 = ...

  • Page 100

    OC0A pin to toggle on Compare Matches if the WGM02 bit is set. This option is not available for the OC0B pin (See be visible on the port pin if the data direction for the port pin ...

  • Page 101

    Figure 13-9. Timer/Counter Timing Diagram, with Prescaler (f clk clk (clk TCNTn TOVn Figure 13-10 mode and PWM mode, where OCR0A is TOP. Figure 13-10. Timer/Counter Timing Diagram, Setting of OCF0x, with Prescaler (f clk clk (clk TCNTn OCRnx OCFnx ...

  • Page 102

    Timer/Counter Register Description 13.8.1 Timer/Counter Control Register A – TCCR0A Bit Read/Write Initial Value • Bits 7:6 – COM01A:0: Compare Match Output A Mode These bits control the Output Compare pin (OC0A) behavior. If one or both of ...

  • Page 103

    Table 13-4 rect PWM mode. Table 13-4. COM0A1 Note: • Bits 5:4 – COM0B1:0: Compare Match Output B Mode These bits control the Output Compare pin (OC0B) behavior. If one or both of the COM0B1:0 bits ...

  • Page 104

    ... Note: • Bits 3, 2 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and will always read as zero. • Bits 1:0 – WGM01:0: Waveform Generation Mode Combined with the WGM02 bit found in the TCCR0B Register, these bits control the counting ...

  • Page 105

    Timer/Counter Control Register B – TCCR0B Bit Read/Write Initial Value • 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 ...

  • Page 106

    Table 13-9. 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 feature allows software control of ...

  • Page 107

    ... Initial Value • Bits 7..3, 0 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and will always read as zero. • Bit 2 – OCF0B: Timer/Counter 0 Output Compare B Match Flag The OCF0B bit is set when a Compare Match occurs between the Timer/Counter and the data in OCR0B – ...

  • Page 108

    ... The Power Reduction Timer/Counter1 bit, PRTIM1, in page 46 The Power Reduction Timer/Counter3 bit, PRTIM3, in page 46 7766F–AVR–11/10 “Pinout ATmega16U4/ATmega32U4” on page “16-bit Timers/Counters (Timer/Counter1 and Timer/Counter3)” 108. must be written to zero to enable Timer/Counter1 module. must be written to zero to enable Timer/Counter3 module. ATmega16/32U4 Figure 14-1 ...

  • Page 109

    ... OCRnA = OCRnB = OCRnC ICRn TCCRnA 1. Refer to “Pinout ATmega16U4/ATmega32U4” on page 10-6 on page 75 for Timer/Counter1 and 3 and 3 pin placement and description only refers to T1 since T3 input is not available on the ATmega16U4/ATmega32U4 . product ATmega16/32U4 (1) TOVn (Int.Req.) Control Logic Clock Select TCLK Edge Detector TOP ...

  • Page 110

    The double buffered Output Compare Registers (OCRnA/B/C) are compared with the Timer/Counter value at all time. The result of the compare can be used by the Waveform Gener- ator to generate a PWM or variable frequency output on the Output ...

  • Page 111

    Assembly Code Examples C Code Examples Note: The assembly code example returns the TCNTn value in the r17:r16 register pair important to notice that accessing 16-bit registers are atomic operations interrupt occurs between the two instructions ...

  • Page 112

    Assembly Code Example TIM16_ReadTCNTn: C Code Example unsigned int TIM16_ReadTCNTn( void ) { } Note: The assembly code example returns the TCNTn value in the r17:r16 register pair. 7766F–AVR–11/10 (1) ; Save global interrupt flag in r18,SREG ; Disable interrupts ...

  • Page 113

    The following code examples show how atomic write of the TCNTn Register contents. Writing any of the OCRnA/B/C or ICRn Registers can be done by using the same principle. Assembly Code Example TIM16_WriteTCNTn: C Code Example void ...

  • Page 114

    Counter Unit The main part of the 16-bit Timer/Counter is the programmable 16-bit bi-directional counter unit. Figure 14-2 Figure 14-2. Counter Unit Block Diagram Signal description (internal signals): Count Direction Clear clk TOP BOTTOM The 16-bit counter is mapped ...

  • Page 115

    The Timer/Counter Overflow Flag (TOVn) is set according to the mode of operation selected by the WGMn3:0 bits. TOVn can be used for generating a CPU interrupt. 14.5 Input Capture Unit The Timer/Counter incorporates an input capture unit that can ...

  • Page 116

    Reading the 16-bit value in the Input Capture Register (ICRn) is done by first reading the low byte (ICRnL) and then the high byte (ICRnH). When the low byte is read the high byte is copied into the high byte ...

  • Page 117

    Using the Input Capture unit in any mode of operation when the TOP value (resolution) is actively changed during operation, is not recommended. Measurement of an external signal’s duty cycle requires that the trigger edge is changed after each capture. ...

  • Page 118

    The OCRnx Register is double buffered when using any of the twelve Pulse Width Modulation (PWM) modes. For the Normal and Clear Timer on Compare (CTC) modes of operation, the double buffering is disabled. The double buffering synchronizes the update ...

  • Page 119

    Compare Match Output Unit The Compare Output mode (COMnx1:0) bits have two functions. The Waveform Generator uses the COMnx1:0 bits for defining the Output Compare (OCnx) state at the next compare match. Secondly the COMnx1:0 bits control the OCnx ...

  • Page 120

    PWM refer to page 131. A change of the COMnx1:0 bits state will have effect at the first compare match after the bits are written. ...

  • Page 121

    Figure 14-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 OCFnA or ICFn Flag according to the register used to define ...

  • Page 122

    The PWM resolution for fast PWM can be fixed to 8-, 9-, or 10-bit, or defined by either ICRn or OCRnA. The minimum resolution allowed is 2-bit (ICRn or OCRnA set to 0x0003), and the max- imum resolution is 16-bit ...

  • Page 123

    When the OCRnA I/O location is written the value written will be put into the OCRnA Buffer Register. The OCRnA Compare Register will then be updated with the value in the Buffer Register at the next ...

  • Page 124

    OCRnA set to MAX). The PWM resolu- tion in bits can be calculated by using the following equation: In phase correct PWM mode the counter is incremented until the counter value ...

  • Page 125

    Since the OCRnx update occurs at TOP, the PWM period starts and ends at TOP. This implies that the length of the falling slope is determined by the previous TOP value, while the length of the rising slope is ...

  • Page 126

    OCRnA set to MAX). The PWM resolution in bits can be calculated using the following equation: In phase and frequency correct PWM mode the counter is incremented until the counter value matches either ...

  • Page 127

    Using the ICRn Register for defining TOP works well when using fixed TOP values. By using ICRn, the OCRnA Register is free to be used for generating a PWM output on OCnA. However, if the base PWM frequency is actively ...

  • Page 128

    Figure 14-11. Timer/Counter Timing Diagram, Setting of OCFnx, with Prescaler (f Figure 14-12 frequency correct PWM mode the OCRnx Register is updated at BOTTOM. The timing diagrams will be the same, but TOP should be replaced by BOTTOM, TOP-1 by ...

  • Page 129

    Figure 14-13. Timer/Counter Timing Diagram, with Prescaler (f clk clk (clk TCNTn (CTC and FPWM) TCNTn (PC and PFC PWM) TOVn and ICF n as TOP) OCRnx (Update at TOP) 14.10 16-bit Timer/Counter Register Description 14.10.1 Timer/Counter1 Control Register A ...

  • Page 130

    When the OCnA, OCnB or OCnC is connected to the pin, the function of the COMnx1:0 bits is dependent of the WGMn3:0 bits setting. the WGMn3:0 bits are set to a normal or a CTC mode (non-PWM). Table 14-2. COMnA1/COMnB1/ ...

  • Page 131

    Table 14-4. COMnA1/COMnB/ COMnC1 Note: • Bit 1:0 – WGMn1:0: Waveform Generation Mode Combined with the WGMn3:2 bits found in the TCCRnB Register, these bits control the counting sequence of the counter, the source for maximum (TOP) counter value, and ...

  • Page 132

    Table 14-5. Waveform Generation Mode Bit Description (Continued) WGMn2 WGMn1 Mode WGMn3 (CTCn) (PWMn1 Note: 1. The CTCn and PWMn1:0 bit definition names are obsolete. Use the location of these bits ...

  • Page 133

    Table 14-6. CSn2 external pin modes are used for the Timer/Countern, transitions on the Tn pin will clock the counter even if the pin is configured as an output. This feature ...

  • Page 134

    Timer/Counter1 – TCNT1H and TCNT1L Bit Read/Write Initial Value 14.10.8 Timer/Counter3 – TCNT3H and TCNT3L Bit Read/Write Initial Value The two Timer/Counter I/O locations (TCNTnH and TCNTnL, combined TCNTn) give direct access, both for read and for write operations, ...

  • Page 135

    Output Compare Register 3 A – OCR3AH and OCR3AL Bit Read/Write Initial Value 14.10.13 Output Compare Register 3 B – OCR3BH and OCR3BL Bit Read/Write Initial Value 14.10.14 Output Compare Register 3 C – OCR3CH and OCR3CL Bit Read/Write ...

  • Page 136

    Timer/Counter1 Interrupt Mask Register – TIMSK1 Bit Read/Write Initial Value 14.10.18 Timer/Counter3 Interrupt Mask Register – TIMSK3 Bit Read/Write Initial Value • Bit 5 – ICIEn: Timer/Countern, Input Capture Interrupt Enable When this bit is written to one, and ...

  • Page 137

    Timer/Counter3 Interrupt Flag Register – TIFR3 Bit Read/Write Initial Value • Bit 5 – ICFn: Timer/Countern, Input Capture Flag This flag is set when a capture event occurs on the ICPn pin. When the Input Capture Register (ICRn) is ...

  • Page 138

    ... A lock feature allows user to update the PWM registers and A simplified block diagram of the Timer/Counter4 is shown in of the I/O pins, refer to register and bit locations are listed in the 7766F–AVR–11/10 “Pinout ATmega16U4/ATmega32U4” on page “Register Description” on page ATmega16/32U4 Figure 15-1. For actual placement 3 ...

  • Page 139

    Figure 15-1. Timer/Counter4 Block Diagram TOV4 T/C INT. MASK REGISTER (TIMSK4) TIMER/COUNTER4 (TCNT4) COMPARE REGISTER A 8-BIT OUTPUT COMPARE REGISTER A (OCR4A) 15.2.1 Speed The maximum speed of the Timer/Counter4 is 64 MHz. However supply voltage below 4 ...

  • Page 140

    Registers The Timer/Counter (TCNT4) and Output Compare Registers (OCR4A, OCR4B, OCR4C and OCR4D) are 8-bit registers that are used as a data source to be compared with the TCNT4 con- tents. The OCR4A, OCR4B and OCR4D registers determine the ...

  • Page 141

    Figure 15-2. Timer/Counter4 Synchronization Register Block Diagram. PLLTM1:0 != '00' CK PCK (clk TMR SYNC MODE ASYNC MODE 15.2.5 Definitions Many register and bit references in this section are written in general form. A lower case “n” replaces the Timer/Counter ...

  • Page 142

    Counter Unit The main part of the Timer/Counter4 is the programmable bi-directional counter unit. 3 shows a block diagram of the counter and its surroundings. Figure 15-3. Counter Unit Block Diagram Signal description (internal signals): count direction clear clk ...

  • Page 143

    Output Compare Unit The comparator continuously compares TCNT4 with the Output Compare Registers (OCR4A, OCR4B, OCR4C and OCR4D). Whenever TCNT4 equals to the Output Compare Register, the comparator signals a match. A match will set the Output Compare Flag ...

  • Page 144

    Figure 15-5. Effects of Unsynchronized OCR Latching 15.4.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 (FOC4x) bit. Forcing Compare Match will ...

  • Page 145

    Dead Time Generator The Dead Time Generator is provided for the Timer/Counter4 PWM output pairs to allow driving external power control switches safely. The Dead Time Generator is a separate block that can be used to insert dead times ...

  • Page 146

    The length of the counting period is user adjustable by selecting the dead time prescaler setting by using the DTPS41:40 control bits, and selecting then the dead time value in I/O register DT4. The DT4 register consists of two 4-bit ...

  • Page 147

    Figure 15-9. Compare Match Output Unit, Schematic clk I/O 15.6.1 Compare Output Mode and Waveform Generation The Waveform Generator uses the COM4x1:0 bits differently in Normal mode and PWM modes. For all modes, setting the COM4x1 tells the ...

  • Page 148

    Enhanced Compare/PWM mode When the bit ENHC4 of TCCR4E register is set, the Enhanced Compare/PWM mode is enabled. This mode allows user to add an accuracy bit to Output Compare Register OCR4A, OCR4B and OCR4D. Like explained previously, a ...

  • Page 149

    Setting OCR4A = 0x85 (= b’10000101’) signifies that the true value of “Compare A” 15.7 Synchronous update To avoid unasynchronous and incoherent values in a cycle synchronous update of one of several values is necessary, all values ...

  • Page 150

    Waveform Output is cleared on the Compare Match. In inverting Compare Output Mode the Waveform Output is set on Compare Match. The timing diagram for the Normal mode is shown in that is shown as a histogram in the timing ...

  • Page 151

    The Output Compare Pin configurations in Normal Mode are described in Table 15-2. COM4x1 15.8.2 Fast PWM Mode The fast Pulse Width Modulation or fast PWM mode (PWM4x = 1 and WGM40 = 0) provides a ...

  • Page 152

    The Timer/Counter Overflow Flag (TOV4) is set each time the counter reaches TOP. If the inter- rupt is enabled, the interrupt handler routine can be used for updating the compare value.4In fast PWM mode, the compare unit allows generation of ...

  • Page 153

    Match between TCNT4 and OCR4x while upcounting, and set on the Compare Match while down-counting. In inverting Output Compare mode, the operation is inverted. In complementary Compare Output Mode, the Waveform Output is cleared on the Compare Match and set ...

  • Page 154

    The N variable represents the number of steps in dual-slope operation. The value of N equals to the TOP value. The extreme values for the OCR4C Register represent special cases when generating a PWM waveform output in the Phase and ...

  • Page 155

    TOP value. The counter is then cleared at the following timer clock cycle. The TCNT4 value is in the timing diagram shown as a histogram for illustrating the single- slope operation. The timing diagram includes Output ...

  • Page 156

    Table 15-5. COM4A1 1 1 COM4D1 15.9 Timer/Counter Timing Diagrams The Timer/Counter is a synchronous design and the timer clock (clk clock enable signal in the following figures. The figures include information on when Interrupt Flags ...

  • Page 157

    Figure 15-18. Timer/Counter Timing Diagram, Setting of OCF1x, with Prescaler (f clk PCK clk Tn (clk /8) PCK TCNTn OCRnx OCFnx Figure 15-19. Timer/Counter Timing Diagram, with Prescaler (f clk PCK clk Tn (clk /8) PCK TCNTn TOVn 15.10 Fault ...

  • Page 158

    Fault Protection Trigger Source The main trigger source for the Fault Protection unit is the external interrupt pin (INT0). Alterna- tively the Analog Comparator output can be used as trigger source for the Fault Protection unit. The Analog Comparator ...

  • Page 159

    Accessing 10-Bit Registers If 10-bit values are written to the TCNTn and OCRnA/B/C/D registers, the 10-bit registers can be byte accessed by the AVR CPU via the 8-bit data bus using two read or write operations. The 10-bit registers ...

  • Page 160

    It is important to notice that accessing 10-bit registers are atomic operations interrupt occurs between the two instructions accessing the 10-bit register, and the interrupt code updates the TC4H register by accessing the same or any other of ...

  • Page 161

    The following code examples show how atomic write of the TCNTn register contents. Writing any of the OCRnA/B/C/D registers can be done by using the same principle. Assembly Code Example TIM1_WriteTCNTn: C Code Example void TIM1_WriteTCNTn( unsigned ...

  • Page 162

    Register Description 15.12.1 TCCR4A – Timer/Counter4 Control Register A Bit Read/Write Initial value • Bits 7,6 - COM4A1, COM4A0: Comparator A Output Mode, Bits 1 and 0 These bits control the behavior of the Waveform Output (OCW4A) and the ...

  • Page 163

    Table 15-8 are set to Phase and Frequency Correct PWM Mode. Table 15-8. COM1A1.. Table 15-9 are set to single-slope PWM6 Mode. In the PWM6 Mode the same Waveform Output (OCW4A) is used for generating all ...

  • Page 164

    Data Direction Register (DDR) bit corresponding to the OC4B pin must be set in order to enable the output driver. The function of the COM4B1:0 bits depends on the PWM4B and WGM40 bit settings. 11 shows the COM4B1:0 ...

  • Page 165

    The automatic action programmed in COM4A1 and COM4A0 takes place compare match had occurred, but no interrupt is generated. The FOC4A bit is always read as zero. • Bit 2 - FOC4B: Force Output Compare Match ...

  • Page 166

    Table 15-14. Division factors of the Dead Time prescaler DTPS41 • Bits CS43, CS42, CS41, CS40: Clock Select Bits and 0 The Clock Select bits and ...

  • Page 167

    Bits 5,4 - COM4B1S, COM4B0S: Comparator B Output Mode, Bits 1 and 0 These bits are the shadow bits of the COM4A1 and COM4A0 bits that are described in the sec- tion “TCCR4A – Timer/Counter4 Control Register A” on ...

  • Page 168

    Table 15-18. Compare Output Mode, Phase and Frequency Correct PWM Mode COM4D1.. • Bit 1 - FOC4D: Force Output Compare Match 4D The FOC4D bit is only active when the PWM4D bit specify a non-PWM mode. ...

  • Page 169

    Bit 3 - FPAC4: Fault Protection Analog Comparator Enable When written logic one, this bit enables the Fault Protection function in Timer/Counter4 to be triggered by the Analog Comparator. The comparator output is in this case directly connected to ...

  • Page 170

    ... The temporary Timer/Counter4 register is an 2-bit read/write register. • Bits 7:3- Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and always reads as zero. • Bits 2- TC410: Additional MSB bits for 11-bit accesses in Enhanced PWM mode If 10-bit accuracy is used, the Timer/Counter4 High Byte Register (TC4H) is used for temporary storing the MSB bits (TC49, TC48) of the 10-bit accesses ...

  • Page 171

    Bits 1:0 - TC49, TC48: Two MSB bits of the 10-bit accesses If 10-bit accuracy is used, the Timer/Counter4 High Byte Register (TC4H) is used for temporary storing the MSB bits (TC49, TC48) of the 10-bit accesses. The same ...

  • Page 172

    The output compare register 8-bit read/write register. The Timer/Counter Output Compare Register C contains data to be continuously compared with Timer/Counter4, and a compare match will clear TCNT4. This register has the same function in Normal mode ...

  • Page 173

    Bit 5 - OCIE4B: Timer/Counter4 Output Compare Interrupt Enable When the OCIE4B bit is set (one) and the I-bit in the Status Register is set (one), the Timer/Counter4 Compare Match B interrupt is enabled. The corresponding interrupt at vector ...

  • Page 174

    DT4 – Timer/Counter4 Dead Time Value Bit Read/Write Initial value The dead time value register is an 8-bit read/write register. The dead time delay of all Timer/Counter4 channels are adjusted by the dead time value regis- ter, DT4. The ...

  • Page 175

    Output Compare Modulator (OCM1C0A) 16.1 Overview The Output Compare Modulator (OCM) allows generation of waveforms modulated with a carrier frequency. The modulator uses the outputs from the Output Compare Unit C of the 16-bit Timer/Counter1 and the Output Compare ...

  • Page 176

    When the modulator is enabled the type of modulation (logical AND or OR) can be selected by the PORTB7 Register. Note that the DDRB7 controls the direction of the port independent of the COMnx1:0 bit setting. 16.2.1 Timing Example Figure ...

  • Page 177

    ... Serial Peripheral Interface – SPI The Serial Peripheral Interface (SPI) allows high-speed synchronous data transfer between the ATmega16U4/ATmega32U4 and peripheral devices or between several AVR devices. The ATmega16U4/ATmega32U4 SPI includes the following features: • Full-duplex, Three-wire Synchronous Data Transfer • Master or Slave Operation • ...

  • Page 178

    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 179

    When the SPI is enabled, the data direction of the MOSI, MISO, SCK, and SS pins is overridden according to Functions” on page Table 17-1. Pin MOSI MISO SCK SS Note: The following code examples show how to initialize the ...

  • Page 180

    Assembly Code Example SPI_MasterInit: SPI_MasterTransmit: Wait_Transmit: C Code Example void SPI_MasterInit(void void SPI_MasterTransmit(char cData Note: 7766F–AVR–11/10 (1) ; Set MOSI and SCK output, all others input r17,(1<<DD_MOSI)|(1<<DD_SCK) ldi out DDR_SPI,r17 ; Enable SPI, Master, set clock ...

  • Page 181

    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: 17.1 SS Pin ...

  • Page 182

    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 ...

  • Page 183

    SPIF in SPSR will become set. The user will then have to set MSTR to re-enable SPI Mas- ter mode. • Bit 3 – CPOL: Clock Polarity When this bit is written to one, SCK is high when idle. ...

  • Page 184

    ... SPI Data Register. • Bit 5..1 – Res: Reserved Bits These bits are reserved bits in the ATmega16U4/ATmega32U4 and will always read as zero. • Bit 0 – SPI2X: Double SPI Speed Bit When this bit is written logic one the SPI speed (SCK Frequency) will be doubled when the SPI is in Master mode (see clock periods ...

  • Page 185

    Table 17-5. CPOL=0, CPHA=0 CPOL=0, CPHA=1 CPOL=1, CPHA=0 CPOL=1, CPHA=1 Figure 17-3. SPI Transfer Format with CPHA = 0 Figure 17-4. SPI Transfer Format with CPHA = 1 7766F–AVR–11/10 CPOL Functionality Leading Edge Sample (Rising) Setup (Rising) Sample (Falling) Setup ...

  • Page 186

    USART The Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART highly flexible serial communication device. The main features are: • Full Duplex Operation (Independent Serial Receive and Transmit Registers) • Asynchronous or Synchronous Operation • Flow ...

  • Page 187

    ... Double Speed (asynchronous mode only) is controlled by the U2Xn found in the 7766F–AVR–11/10 (1) UBRR[H:L] BAUD RATE GENERATOR UDR (Transmit) TRANSMIT SHIFT REGISTER RECEIVE SHIFT REGISTER UDR (Receive) UCSRA 1. See “Pinout ATmega16U4/ATmega32U4” on page pin placement. ATmega16/32U4 Clock Generator OSC SYNC LOGIC PIN XCK CONTROL Transmitter TX ...

  • Page 188

    UCSRnA Register. When using synchronous mode (UMSELn = 1), the Data Direction Register for the XCKn pin (DDR_XCKn) controls whether the clock source is internal (Master mode) or external (Slave mode). The XCKn pin is only active when using synchronous ...

  • Page 189

    Table 18-1 ing the UBRRn value for each mode of operation using an internally generated clock source. Table 18-1. Operating Mode Asynchronous Normal mode (U2Xn = 0) Asynchronous Double Speed mode (U2Xn = 1) Synchronous Master mode Note: BAUD f ...

  • Page 190

    External Clock External clocking is used by the synchronous slave modes of operation. The description in this section refers to External clock input from the XCKn pin is sampled by a synchronization register to minimize the chance of meta-stability. ...

  • Page 191

    A frame starts with the start bit followed by the least significant data bit. Then the next data bits total of nine, are succeeding, ending with the most significant bit. If enabled, the parity bit is inserted ...

  • Page 192

    USART Initialization The USART has to be initialized before any communication can take place. The initialization pro- cess normally consists of setting the baud rate, setting frame format and enabling the Transmitter or the Receiver depending on the usage. ...

  • Page 193

    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 overrid- den by ...

  • Page 194

    Sending Frames with 9 Data Bit 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. The following ...

  • Page 195

    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 ...

  • Page 196

    UDRn will be masked to zero. The USART has to be initialized before the function can be used. Assembly Code Example USART_Receive: C Code Example unsigned char USART_Receive( void ) { } Note: ...

  • Page 197

    Assembly Code Example USART_Receive: USART_ReceiveNoError: C Code Example unsigned int USART_Receive( void ) { } Note: The receive function example reads all the I/O Registers into the Register File before any com- putation is done. This gives an optimal receive ...

  • Page 198

    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 unread ...

  • Page 199

    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 ...

  • Page 200

    Figure 18-5. Start Bit Sampling Sample (U2X = 0) Sample (U2X = 1) 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 ...