14
AVR1306
8045A-AVR-02/08
5. Start the TC by selecting a clock source.
6. Wait for Input Capture Interrupt Flag A in INTFLAGS to be set.
7. Read input capture value from the CCA[H:L] register into ‘thisCapture’.
8. Check bit 15 of ‘thisCapture’.
9. If bit 15 of ‘thisCapture’ is 1
a. Save ‘thisCapture’ in ‘lastRising’
10. else
a. Calculate total period:
6.4 Using a Timer/Counter for PWM Generation
Task: Configure TCC0 for pulse width modulation output with varying duty cycle on
channel A.
1. Configure PC0 for output by setting bit 0 in PORTC.DIR.
2. Select the timer period by setting the PER[H:L] register.
3. Select a waveform generation mode by setting the WGMODE[2:0] bits in
4. Enable Compare Channel A by setting the CCAEN bit in CTRLB.
5. Start the TC by selecting a clock source (CLKSEL[3:0] in CTRLA).
6. Calculate the desired compare value.
7. Write the new compare value to CCA[H:L].
8. Wait for the TC Overflow Flag to be set. (OVFIF in INTFLAGS).
9. Clear the TC Overflow flag.
10. Go to step 6.
Using this sequence, the compare value will be updated once every PWM period.
6.5 Event Counting
Task: Configure TCC0 to count the number of switch presses on PC0, using event
channel 0. Generate interrupt after 5 key presses that toggle PC1.
1. Configure PC0 as input, sense on falling edge.
2. Configure PC1 as output.
3. Select PC0 as multiplexer input for event channel 0.
4. Enable filtering on event channel 0.
5. Set the period of TCC0 to 4 (to generate overflow interrupt after 5 switch presses).
6. Enable TCC0 overflow interrupt at low level.
7. Select event channel 0 as clock source for the TC.
In the TCC0 overflow interrupt service routine, toggle PC1.
In this configuration, the CNT[H:L] register will contain the number of times a switch
connected to PC0 has been pressed. After 5 presses, an overflow interrupt service
handler will be triggered, toggling PC1 while CNT[H:L] wraps around to 0.