- •Introduction
- •Feature
- •Table of Contents
- •1. Description
- •2. Configuration Summary
- •3. Ordering Information
- •4. Block Diagram
- •5. Pin Configurations
- •5.1. Pin Descriptions
- •5.1.3. Port A (PA[7:0])
- •5.1.4. Port B (PB[3:0])
- •5.1.5. RESET
- •6. I/O Multiplexing
- •7. General Information
- •7.1. Resources
- •7.2. Data Retention
- •7.3. About Code Examples
- •8. AVR CPU Core
- •8.1. Overview
- •8.2. Features
- •8.3. Block Diagram
- •8.4. ALU – Arithmetic Logic Unit
- •8.5. Status Register
- •8.6. General Purpose Register File
- •8.8. Stack Pointer
- •8.10. Instruction Execution Timing
- •8.11. Reset and Interrupt Handling
- •8.11.1. Interrupt Response Time
- •8.12. Register Description
- •8.12.1. Configuration Change Protection Register
- •8.12.2. Stack Pointer Register Low and High byte
- •8.12.3. Status Register
- •9. AVR Memories
- •9.1. Overview
- •9.2. Features
- •9.4. SRAM Data Memory
- •9.4.1. Data Memory Access Times
- •9.5. I/O Memory
- •10. Clock System
- •10.1. Overview
- •10.2. Clock Distribution
- •10.3. Clock Subsystems
- •10.4. Clock Sources
- •10.4.1. Calibrated Internal 8MHz Oscillator
- •10.4.2. External Clock
- •10.4.3. Internal 128kHz Oscillator
- •10.4.4. Switching Clock Source
- •10.4.5. Default Clock Source
- •10.5. System Clock Prescaler
- •10.5.1. Switching Prescaler Setting
- •10.6. Starting
- •10.6.1. Starting from Reset
- •10.6.2. Starting from Power-Down Mode
- •10.6.3. Starting from Idle / ADC Noise Reduction / Standby Mode
- •10.7. Register Description
- •10.7.1. Clock Main Settings Register
- •10.7.2. Oscillator Calibration Register
- •10.7.3. Clock Prescaler Register
- •11. Power Management and Sleep Modes
- •11.1. Overview
- •11.2. Features
- •11.3. Sleep Modes
- •11.3.1. Idle Mode
- •11.3.2. ADC Noise Reduction Mode
- •11.3.4. Standby Mode
- •11.4. Power Reduction Register
- •11.5. Minimizing Power Consumption
- •11.5.1. Analog Comparator
- •11.5.2. Analog to Digital Converter
- •11.5.3. Internal Voltage Reference
- •11.5.4. Watchdog Timer
- •11.5.5. Port Pins
- •11.6. Register Description
- •11.6.1. Sleep Mode Control Register
- •11.6.2. Power Reduction Register
- •12. SCRST - System Control and Reset
- •12.1. Overview
- •12.2. Features
- •12.3. Resetting the AVR
- •12.4. Reset Sources
- •12.4.3. External Reset
- •12.4.4. Watchdog System Reset
- •12.5. Watchdog Timer
- •12.5.1. Overview
- •12.5.2. Procedure for Changing the Watchdog Timer Configuration
- •12.5.2.1. Safety Level 1
- •12.5.2.2. Safety Level 2
- •12.5.3. Code Examples
- •12.6. Register Description
- •12.6.1. Watchdog Timer Control Register
- •12.6.2. VCC Level Monitoring Control and Status register
- •12.6.3. Reset Flag Register
- •13. Interrupts
- •13.1. Overview
- •13.2. Interrupt Vectors
- •13.3. External Interrupts
- •13.3.1. Low Level Interrupt
- •13.3.2. Pin Change Interrupt Timing
- •13.4. Register Description
- •13.4.1. External Interrupt Control Register A
- •13.4.2. External Interrupt Mask Register
- •13.4.3. External Interrupt Flag Register
- •13.4.4. Pin Change Interrupt Control Register
- •13.4.5. Pin Change Interrupt Flag Register
- •13.4.6. Pin Change Mask Register 0
- •13.4.7. Pin Change Mask Register 1
- •14. I/O-Ports
- •14.1. Overview
- •14.2. Features
- •14.3. I/O Pin Equivalent Schematic
- •14.4. Ports as General Digital I/O
- •14.4.1. Configuring the Pin
- •14.4.2. Toggling the Pin
- •14.4.4. Reading the Pin Value
- •14.4.5. Digital Input Enable and Sleep Modes
- •14.4.6. Unconnected Pins
- •14.4.7. Program Example
- •14.4.8. Alternate Port Functions
- •14.4.8.1. Alternate Functions of Port A
- •14.4.8.2. Alternate Functions of Port B
- •14.5. Register Description
- •14.5.1. Port A Input Pins Address
- •14.5.2. Port A Data Direction Register
- •14.5.3. Port A Data Register
- •14.5.5. Port B Input Pins Address
- •14.5.6. Port B Data Direction Register
- •14.5.7. Port B Data Register
- •14.5.9. Port Control Register
- •15. USART - Universal Synchronous Asynchronous Receiver Transceiver
- •15.1. Overview
- •15.2. Features
- •15.3. Block Diagram
- •15.4. Clock Generation
- •15.4.1. Internal Clock Generation – The Baud Rate Generator
- •15.4.2. Double Speed Operation (U2X0)
- •15.4.3. External Clock
- •15.4.4. Synchronous Clock Operation
- •15.5. Frame Formats
- •15.5.1. Parity Bit Calculation
- •15.6. USART Initialization
- •15.7. Data Transmission – The USART Transmitter
- •15.7.1. Sending Frames with 5 to 8 Data Bits
- •15.7.2. Sending Frames with 9 Data Bit
- •15.7.3. Transmitter Flags and Interrupts
- •15.7.4. Parity Generator
- •15.7.5. Disabling the Transmitter
- •15.8. Data Reception – The USART Receiver
- •15.8.1. Receiving Frames with 5 to 8 Data Bits
- •15.8.2. Receiving Frames with 9 Data Bits
- •15.8.3. Receive Compete Flag and Interrupt
- •15.8.4. Receiver Error Flags
- •15.8.5. Parity Checker
- •15.8.6. Disabling the Receiver
- •15.8.7. Flushing the Receive Buffer
- •15.9. Asynchronous Data Reception
- •15.9.1. Asynchronous Clock Recovery
- •15.9.2. Asynchronous Data Recovery
- •15.9.3. Asynchronous Operational Range
- •15.9.4. Start Frame Detection
- •15.10. Multi-Processor Communication Mode
- •15.10.1. Using MPCMn
- •15.11. Examples of Baud Rate Setting
- •15.12. Register Description
- •15.12.1. USART I/O Data Register 0
- •15.12.2. USART Control and Status Register 0 A
- •15.12.3. USART Control and Status Register 0 B
- •15.12.4. USART Control and Status Register 0 C
- •15.12.5. USART Control and Status Register 0 D
- •15.12.6. USART Baud Rate 0 Register Low and High byte
- •16. USARTSPI - USART in SPI Mode
- •16.1. Overview
- •16.2. Features
- •16.3. Clock Generation
- •16.4. SPI Data Modes and Timing
- •16.5. Frame Formats
- •16.5.1. USART MSPIM Initialization
- •16.6. Data Transfer
- •16.6.1. Transmitter and Receiver Flags and Interrupts
- •16.6.2. Disabling the Transmitter or Receiver
- •16.7. AVR USART MSPIM vs. AVR SPI
- •16.8. Register Description
- •17.1. Overview
- •17.2. Features
- •17.3. Block Diagram
- •17.4. Definitions
- •17.5. Registers
- •17.6.1. Reusing the Temporary High Byte Register
- •17.7. Timer/Counter Clock Sources
- •17.7.1. Internal Clock Source - Prescaler
- •17.7.2. Prescaler Reset
- •17.7.3. External Clock Source
- •17.8. Counter Unit
- •17.9. Input Capture Unit
- •17.9.1. Input Capture Trigger Source
- •17.9.2. Noise Canceler
- •17.9.3. Using the Input Capture Unit
- •17.10. Output Compare Units
- •17.10.1. Force Output Compare
- •17.10.2. Compare Match Blocking by TCNT0 Write
- •17.10.3. Using the Output Compare Unit
- •17.11. Compare Match Output Unit
- •17.11.1. Compare Output Mode and Waveform Generation
- •17.12. Modes of Operation
- •17.12.1. Normal Mode
- •17.12.2. Clear Timer on Compare Match (CTC) Mode
- •17.12.3. Fast PWM Mode
- •17.12.4. Phase Correct PWM Mode
- •17.12.5. Phase and Frequency Correct PWM Mode
- •17.13. Timer/Counter Timing Diagrams
- •17.14. Register Description
- •17.14.1. Timer/Counter0 Control Register A
- •17.14.2. Timer/Counter0 Control Register B
- •17.14.3. Timer/Counter0 Control Register C
- •17.14.4. Timer/Counter 0 Low and High byte
- •17.14.5. Output Comparte Register A 0 Low and High byte
- •17.14.6. Output Comparte Register B 0 Low and High byte
- •17.14.7. Input Capture Register 0 Low and High byte
- •17.14.8. Timer/Counter0 Interrupt Mask Register
- •17.14.9. Timer/Counter0 Interrupt Flag Register
- •17.14.10. General Timer/Counter Control Register
- •18. AC - Analog Comparator
- •18.1. Overview
- •18.2. Features
- •18.3. Block Diagram
- •18.4. Register Description
- •18.4.1. Analog Comparator Control and Status Register
- •18.4.2. Analog Comparator Control and Status Register 0
- •18.4.3. Digital Input Disable Register 0
- •19. ADC - Analog to Digital Converter
- •19.1. Overview
- •19.2. Features
- •19.3. Block Diagram
- •19.4. Operation
- •19.5. Starting a Conversion
- •19.6. Prescaling and Conversion Timing
- •19.7. Changing Channel or Reference Selection
- •19.8. ADC Input Channels
- •19.9. ADC Voltage Reference
- •19.10. ADC Noise Canceler
- •19.11. Analog Input Circuitry
- •19.12. Analog Noise Canceling Techniques
- •19.13. ADC Accuracy Definitions
- •19.14. ADC Conversion Result
- •19.15. Register Description
- •19.15.1. ADC Multiplexer Selection Register
- •19.15.2. ADC Control and Status Register A
- •19.15.3. ADC Control and Status Register B
- •19.15.4. ADC Data Register Low and High Byte (ADLAR=0)
- •19.15.5. ADC Data Register Low and High Byte (ADLAR=1)
- •19.15.6. Digital Input Disable Register 0
- •20.1. Overview
- •20.2. Features
- •20.3.2. Flash Memory
- •20.3.3. Configuration Section
- •20.3.3.1. Latching of Configuration Bits
- •20.3.4. Signature Section
- •20.3.4.1. Signature Row Summary
- •20.3.4.1.1. Device ID n
- •20.3.4.1.2. Serial Number Byte n
- •20.3.5. Calibration Section
- •20.4. Accessing the NVM
- •20.4.1. Addressing the Flash
- •20.4.2. Reading the Flash
- •20.4.3. Programming the Flash
- •20.4.3.1. Chip Erase
- •20.4.3.2. Erasing the Code Section
- •20.4.3.3. Writing a Code Word
- •20.4.3.4. Erasing the Configuration Section
- •20.4.3.5. Writing a Configuration Word
- •20.4.4. Reading NVM Lock Bits
- •20.4.5. Writing NVM Lock Bits
- •20.5. Self programming
- •20.6. External Programming
- •20.6.1. Entering External Programming Mode
- •20.6.2. Exiting External Programming Mode
- •20.7. Register Description
- •21.1. Overview
- •21.2. Features
- •21.3. Block Diagram
- •21.4. Physical Layer of Tiny Programming Interface
- •21.4.1. Enabling
- •21.4.2. Disabling
- •21.4.3. Frame Format
- •21.4.4. Parity Bit Calculation
- •21.4.5. Supported Characters
- •21.4.6. Operation
- •21.4.7. Serial Data Reception
- •21.4.8. Serial Data Transmission
- •21.4.9. Collision Detection Exception
- •21.4.10. Direction Change
- •21.4.11. Access Layer of Tiny Programming Interface
- •21.4.11.1. Message format
- •21.4.11.2. Exception Handling and Synchronisation
- •21.5. Instruction Set
- •21.5.1. SLD - Serial LoaD from data space using indirect addressing
- •21.5.2. SST - Serial STore to data space using indirect addressing
- •21.5.3. SSTPR - Serial STore to Pointer Register
- •21.5.4. SIN - Serial IN from i/o space using direct addressing
- •21.5.5. SOUT - Serial OUT to i/o space using direct addressing
- •21.5.6. SLDCS - Serial LoaD data from Control and Status space using direct addressing
- •21.5.7. SSTCS - Serial STore data to Control and Status space using direct addressing
- •21.5.8. SKEY - Serial KEY signaling
- •21.7. Control and Status Space Register Descriptions
- •21.7.1. Tiny Programming Interface Identification Register
- •21.7.2. Tiny Programming Interface Physical Layer Control Register
- •21.7.3. Tiny Programming Interface Status Register
- •22. Electrical Characteristics
- •22.1. Absolute Maximum Ratings*
- •22.2. DC Characteristics
- •22.3. Speed
- •22.4. Clock Characteristics
- •22.4.1. Accuracy of Calibrated Internal Oscillator
- •22.4.2. External Clock Drive
- •22.5. System and Reset Characteristics
- •22.6. Analog Comparator Characteristics
- •22.7. ADC Characteristics
- •22.8. Serial Programming Characteristics
- •23. Typical Characteristics
- •23.1. Active Supply Current
- •23.2. Idle Supply Current
- •23.3. Supply Current of I/O Modules
- •23.5. Pin Driver Strength
- •23.6. Pin Threshold and Hysteresis
- •23.7. Analog Comparator Offset
- •23.9. Internal Oscillator Speed
- •23.10. VLM Thresholds
- •23.11. Current Consumption of Peripheral Units
- •23.12. Current Consumption in Reset and Reset Pulsewidth
- •24. Register Summary
- •24.1. Note
- •25. Instruction Set Summary
- •26. Packaging Information
- •27. Errata
- •27.1. ATtiny102
- •27.2. ATtiny104
- •28. Datasheet Revision History
/* Wait for data to be received */ while ( !(UCSRnA & (1<<RXCn)) );
/* Get and return received data from buffer */ return UDRn;
}
16.6.1.Transmitter and Receiver Flags and Interrupts
The RXCn, TXCn, and UDREn flags and corresponding interrupts in USART in MSPIM mode are identical in function to the normal USART operation. However, the receiver error status flags (FE, DOR, and PE) are not in use and is always read as zero.
16.6.2.Disabling the Transmitter or Receiver
The disabling of the transmitter or receiver in USART in MSPIM mode is identical in function to the normal USART operation.
16.7.AVR USART MSPIM vs. AVR SPI
The USART in MSPIM mode is fully compatible with the AVR SPI regarding:
•Master mode timing diagram
•The UCPOLn bit functionality is identical to the SPI CPOL bit
•The UCPHAn bit functionality is identical to the SPI CPHA bit
•The UDORDn bit functionality is identical to the SPI DORD bit
However, since the USART in MSPIM mode reuses the USART resources, the use of the USART in MSPIM mode is somewhat different compared to the SPI. In addition to differences of the control register bits, and that only master operation is supported by the USART in MSPIM mode, the following features differ between the two modules:
•The USART in MSPIM mode includes (double) buffering of the transmitter. The SPI has no buffer
•The USART in MSPIM mode receiver includes an additional buffer level
•The SPI WCOL (Write Collision) bit is not included in USART in MSPIM mode
•The SPI double speed mode (SPI2X) bit is not included. However, the same effect is achieved by setting UBRRn accordingly
•Interrupt timing is not compatible
•Pin control differs due to the master only operation of the USART in MSPIM mode
A comparison of the USART in MSPIM mode and the SPI pins is shown in the table below.
Table 16-3. Comparison of USART in MSPIM mode and SPI pins
USART_MSPIM |
SPI |
Comments |
||
|
|
|
|
|
TxDn |
MOSI |
Master Out only |
||
|
|
|
|
|
RxDn |
MISO |
Master In only |
||
|
|
|
|
|
XCKn |
SCK |
(Functionally identical) |
||
|
|
|
|
|
(N/A) |
|
|
|
Not supported by USART in MSPIM |
SS |
||||
16.8.Register Description
Refer to the USART Register Description.
Atmel ATtiny102 / ATtiny104 [DATASHEET] 123
Atmel-42505D-ATtiny102-ATtiny104_Datasheet_Complete-10/2016
17.TC0 - 16-bit Timer/Counter0 with PWM
17.1.Overview
The 16-bit Timer/Counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement.
A block diagram of the 16-bit Timer/Counter is shown below. 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 Register Description. For the actual placement of I/O pins, refer to the Pin Configurations description.
Related Links
Pin Configurations on page 12
17.2.Features
•True 16-bit Design (i.e., allows 16-bit PWM)
•Two independent Output Compare Units
•Double Buffered Output Compare Registers
•One Input Capture Unit
•Input Capture Noise Canceler
•Clear Timer on Compare Match (Auto Reload)
•Glitch-free, Phase Correct Pulse Width Modulator (PWM)
•Variable PWM Period
•Frequency Generator
•External Event Counter
•Independent interrupt Sources (TOV, OCFA, OCFB, and ICF)
17.3.Block Diagram
The Power Reduction TC0 bit in the Power Reduction Register (PRR0.PRTIM0) must be written to zero to enable the TC0 module.
Atmel ATtiny102 / ATtiny104 [DATASHEET] 124
Atmel-42505D-ATtiny102-ATtiny104_Datasheet_Complete-10/2016
Figure 17-1. 16-bit Timer/Counter Block Diagram |
|
|
|
||
|
|
Count |
|
TOVn |
|
|
|
Clear |
|
(Int.Req.) |
|
|
|
Control Logic |
Clock Select |
|
|
|
|
Direction |
clkTn |
|
|
|
|
|
|
Edge |
Tn |
|
|
|
|
Detector |
|
|
|
|
|
|
|
|
|
TOP |
BOTTOM |
|
|
|
Timer/Counter |
|
|
( From Prescaler ) |
|
|
|
|
|
|
|
|
TCNTn |
= |
= 0 |
|
|
|
|
|
|
||
|
|
|
|
OCnA |
|
|
|
|
|
(Int.Req.) |
|
|
= |
|
|
Waveform |
OCnA |
|
|
|
Generation |
||
|
OCRnA |
|
|
|
|
|
|
|
Fixed |
OCnB |
|
BUS |
|
|
TOP |
(Int.Req.) |
|
= |
|
Values |
Waveform |
|
|
|
|
|
|||
|
|
|
OCnB |
||
DATA |
|
|
Generation |
||
OCRnB |
|
|
|
( From Analog |
|
|
|
|
|
||
|
|
|
ICFn (Int.Req.) |
|
Comparator Ouput ) |
|
|
|
|
|
|
|
ICRn |
|
Edge |
Noise |
|
|
|
Detector |
Canceler |
|
|
|
|
|
ICPn |
||
|
|
|
|
|
|
|
TCCRnA |
TCCRnB |
|
|
|
See the related links for actual pin placement.
17.4.Definitions
Many register and bit references in this section are written in general form:
•n=0 represents the Timer/Counter number
•x=A,B represents the Output Compare Unit A or B
However, when using the register or bit definitions in a program, the precise form must be used, i.e., TCNT0 for accessing Timer/Counter0 counter value.
The following definitions are used throughout the section:
Atmel ATtiny102 / ATtiny104 [DATASHEET] 125
Atmel-42505D-ATtiny102-ATtiny104_Datasheet_Complete-10/2016
Table 17-1. Definitions
Constant |
Description |
|
|
BOTTOM |
The counter reaches the BOTTOM when it becomes zero (0x00 for 8-bit counters, or 0x0000 |
|
for 16-bit counters). |
|
|
MAX |
The counter reaches its Maximum when it becomes 0xF (decimal 15, for 8-bit counters) or |
|
0xFF (decimal 255, for 16-bit counters). |
|
|
TOP |
The counter reaches the TOP when it becomes equal to the highest value in the count |
|
sequence. The TOP value can be assigned to be the fixed value MAX or the value stored in |
|
the OCR0A Register. The assignment is dependent on the mode of operation. |
|
|
17.5.Registers
The Timer/Counter (TCNT0), Output Compare Registers (OCRA/B), and Input Capture Register (ICR0) are all 16-bit registers. Special procedures must be followed when accessing the 16-bit registers. These procedures are described in section Accessing 16-bit Registers.
The Timer/Counter Control Registers (TCCR0A/B/C) are 8-bit registers and have no CPU access restrictions. Interrupt requests (abbreviated to Int.Req. in the block diagram) signals are all visible in the Timer Interrupt Flag Register (TIFR0). All interrupts are individually masked with the Timer Interrupt Mask Register (TIMSK0). TIFR0 and TIMSK0 are not shown in the figure.
The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on the T0 pin. The Clock Select logic block controls which clock source and edge the Timer/Counter uses to increment (or decrement) its value. The Timer/Counter is inactive when no clock source is selected. The output from the Clock Select logic is referred to as the timer clock (clkT0).
The double buffered Output Compare Registers (OCR0A/B) are compared with the Timer/Counter value at all time. The result of the compare can be used by the Waveform Generator to generate a PWM or variable frequency output on the Output Compare pin (OC0A/B). See Output Compare Units. The compare match event will also set the Compare Match Flag (OCF0A/B) which can be used to generate an Output Compare interrupt request.
The Input Capture Register can capture the Timer/Counter value at a given external (edge triggered) event on either the Input Capture pin (ICP0) or on the Analog Comparator pins. The Input Capture unit includes a digital filtering unit (Noise Canceler) for reducing the chance of capturing noise spikes.
The TOP value, or maximum Timer/Counter value, can in some modes of operation be defined by either the OCR0A Register, the ICR0 Register, or by a set of fixed values. When using OCR0A as TOP value in a PWM mode, the OCR0A Register can not be used for generating a PWM output. However, the TOP value will in this case be double buffered allowing the TOP value to be changed in run time. If a fixed TOP value is required, the ICR0 Register can be used as an alternative, freeing the OCR0A to be used as PWM output.
Related Links
ICR0L and ICR0H on page 156
OCR0AL and OCR0AH on page 154
OCR0BL and OCR0BH on page 155
TCNT0L and TCNT0H on page 153
TCCR0A on page 147
TCCR0B on page 150
Atmel ATtiny102 / ATtiny104 [DATASHEET] 126
Atmel-42505D-ATtiny102-ATtiny104_Datasheet_Complete-10/2016
TIFR0 on page 158
TIMSK0 on page 157
AC - Analog Comparator on page 160
17.6.Accessing 16-bit Timer/Counter Registers
The TCNT0, OCR0A/B, and ICR0 are 16-bit registers that can be accessed by the AVR CPU via the 8-bit data bus. The 16-bit register must be accessed byte-wise, using two read or write operations. Each 16-bit timer has a single 8-bit TEMP register for temporary storing of the high byte of the 16-bit access. The same temporary register is shared between all 16-bit registers within each 16-bit timer.
Accessing the low byte triggers the 16-bit read or write operation: When the low byte of a 16-bit register is written by the CPU, the high byte that is currently stored in TEMP and the low byte being written are both copied into the 16-bit register in the same clock cycle. When the low byte of a 16-bit register is read by the CPU, the high byte of the 16-bit register is copied into the TEMP register in the same clock cycle as the low byte is read, and must be read subsequently.
Note: To perform a 16-bit write operation, the low byte must be written before the high byte. For a 16-bit read, the low byte must be read before the high byte.
Not all 16-bit accesses uses the temporary register for the high byte. Reading the OCR0A/B 16-bit registers does not involve using the temporary register.
16-bit Access
The following code examples show how to access the 16-bit Timer Registers assuming that no interrupts updates the temporary register. The same principle can be used directly for accessing the OCR0A/B and ICR0 Registers. Note that when using C, the compiler handles the 16-bit access.
Assembly Code Example(1)
... |
|
; Set TCNT0 to 0x01FF |
|
ldi |
r17,0x01 |
ldi |
r16,0xFF |
out |
TCNT0H,r17 |
out |
TCNT0L,r16 |
; Read |
TCNT0 into r17:r16 |
in |
r16,TCNT0L |
in |
r17,TCNT0H |
... |
|
The assembly code example returns the TCNT0 value in the r17:r16 register pair.
C Code Example(1)
unsigned int i;
...
/* Set TCNT0 to 0x01FF */ TCNT0 = 0x1FF;
/* Read TCNT0 into i */ i = TCNT0;
...
Note:
1.The example code assumes that the part specific header file is included. For I/O Registers located in extended I/O map, “IN”, “OUT”, “SBIS”, “SBIC”, “CBI”, and “SBI” instructions must be replaced with instructions that allow access to extended I/O. Typically “LDS” and “STS” combined with “SBRS”, “SBRC”, “SBR”, and “CBR”.
Atmel ATtiny102 / ATtiny104 [DATASHEET] 127
Atmel-42505D-ATtiny102-ATtiny104_Datasheet_Complete-10/2016
Atomic Read
It is important to notice that accessing 16-bit registers are atomic operations. If an interrupt occurs between the two instructions accessing the 16-bit register, and the interrupt code updates the temporary register by accessing the same or any other of the 16-bit Timer Registers, then the result of the access outside the interrupt will be corrupted. Therefore, when both the main code and the interrupt code update the temporary register, the main code must disable the interrupts during the 16-bit access.
The following code examples show how to perform an atomic read of the TCNT0 Register contents. The OCR0A/B or ICR0 Registers can be ready by using the same principle.
Assembly Code Example(1)
TIM16_ReadTCNT0:
; Save global interrupt flag in r18,SREG
;Disable interrupts cli
;Read TCNT0 into r17:r16
in |
r16,TCNT0L |
in |
r17,TCNT0H |
; Restore global interrupt flag out SREG,r18
ret
The assembly code example returns the TCNT0 value in the r17:r16 register pair.
C Code Example(1)
unsigned int TIM16_ReadTCNT0( void )
{
unsigned char sreg; unsigned int i;
/* Save global interrupt flag */ sreg = SREG;
/* Disable interrupts */ _CLI();
/* Read TCNT0 into i */ i = TCNT0;
/* Restore global interrupt flag */ SREG = sreg;
return i;
}
Note:
1.The example code assumes that the part specific header file is included. For I/O Registers located in extended I/O map, “IN”, “OUT”, “SBIS”, “SBIC”, “CBI”, and “SBI” instructions must be replaced with instructions that allow access to extended I/O. Typically “LDS” and “STS” combined with “SBRS”, “SBRC”, “SBR”, and “CBR”.
Atomic Write
The following code examples show how to do an atomic write of the TCNT0 Register contents. Writing any of the OCR0A/B or ICR0 Registers can be done by using the same principle.
Assembly Code Example(1)
TIM16_WriteTCNT0:
; Save global interrupt flag in r18,SREG
;Disable interrupts cli
;Set TCNT0 to r17:r16
out |
TCNT0H,r17 |
out |
TCNT0L,r16 |
; Restore global interrupt flag
Atmel ATtiny102 / ATtiny104 [DATASHEET] 128
Atmel-42505D-ATtiny102-ATtiny104_Datasheet_Complete-10/2016
