This is the quick start guide for the Timer Counter (TC) , with step-by-step instructions on how to configure and use the driver for a specific use case.
The code examples can be copied into e.g the main application loop or any other function that will need to control the timer/counters.
Use cases
Timer/counter overflow (interrupt based)
This use case will prepare a timer to trigger an interrupt when the timer overflows. The interrupt is handled by a cutomisable callback function.
We will setup the timer in this mode:
- Normal WGM mode (incrementing timer)
- Use the system clock as clock source
- No prescaling (clock divider set to 1)
- Overflow interrupt after 1000 counts. This will be done by setting the top value to 1000.
Setup steps
Prequisites
For the setup code of this use case to work, the following must be added to the project:
Example code
Add a callback function that will be executed when the overflow interrupt trigger.
static void my_callback(void)
{
}
Add to, e.g., the main loop in the application C-file:
Workflow
- Enable the interrupt controller:
- Enable the clock system:
- Enable timer/counter TCC0
-
- Note
- This will enable the clock system for the module
- Set the callback function for overflow interrupt
-
- Warning
- This function requires that the my_callback function is defined
- Set the desired waveform mode
-
- Note
- In this case, we use normal mode where the timer increments it count value until the TOP value is reached. The timer then reset its count value to 0.
- Set the period
-
- Note
- This will specify the TOP value of the counter. The timer will overflow and reset when this value is reached.
- Set the overflow interrupt level
- Enable interrupts:
- Set the clock source
-
- Warning
- When the clock source is set, the timer will start counting
Usage steps
- None. The timer will run in the background, and the code written in the call back function will execute each time the timer overflows.
Timer/counter compare match (interrupt based)
This use case will prepare a timer to trigger two independent interrupts when it reaches two different compare values. The period of the timer is customizable and the two compare matches will be handled by two separate interrupts implemented in call back functions.
We will setup the timer in this mode:
- Normal WGM mode - incrementing timer
- Use the system clock as clock source
- No prescaling (divider set to 1)
- Period of timer 10000 counts
- Compare match A interrupt trigger after 100 counts
- Compare match B interrupt trigger after 1000 counts
- If compare A and compare B match occurs simultaneously, compare B should have higher priority
Setup steps
Prequisites
For the setup code of this use case to work, the following must be added to the project:
Example code
Add two callback functions that will be executed when compare match A and compare match B occurs
static void my_cca_callback(void)
{
}
static void my_ccb_callback(void)
{
}
Add to, e.g., the main loop in the application C-file:
Workflow
- Enable the interrupt controller:
- Enable the clock system:
- Enable interrupts:
- Enable timer/counter TCC0
-
- Note
- This will enable the clock system for the module
- Set call back function for CCA interrupt
-
- Warning
- This function requires that the call back function is defined
- Set call back function for CCB interrupt
-
- Warning
- This function requires that the call back function is defined
- Set the desired waveform mode
-
- Note
- In this case, we use normal mode where the timer increments it count value until the TOP value is reached. The timer then reset its count value to 0.
- Set the period
-
- Note
- This will specify the TOP value of the counter. The timer will overflow and reset when this value is reached.
- Set compare match value on CCA
- Set compare match value on CCB
- Enable compare channel A and compare channel B -
- Set interrupt level on channel A (low priority, see TC_INT_LEVEL_t)
- Set interrupt level on channel B (medium priority TC_INT_LEVEL_t)
- Set the clock source
-
- Warning
- When the clock source is set, the timer will start counting
Usage steps
- None. The timer will run in the background, and the code written in the call back functions will execute each time a compare match occur.
Timer/counter PWM
This use case will setup a timer in PWM mode. For more details you can also look at the XMEGA PWM service.
We will setup the timer in this mode:
- Normal WGM mode - incrementing timer
- Use the 2MHz oscillator as clock source (default)
- 1Hz PWM frequency (2MHz clock, 1024x prescale, TOP value 1950)
- 10% duty cycle (1:10 ratio between PER and CC register)
- Output the PWM signal to a I/O port
Setup steps
Prequisites
For the setup code of this use case to work, the following must be added to the project:
Example code
Add to, e.g., the main loop in the application C-file:
Workflow
- Ensure that PWM I/O pin is configured as output
-
- Note
- The board_init(); function configures the I/O pins. If this function is not executed, the I/O pin must be configured as output manually
- Enable the clock system:
- Enable timer/counter TCE0
-
- Note
- This will enable the clock system for the module
- Set the desired waveform mode
-
- Note
- In this case, we use normal mode where the timer increments it count value until the TOP value is reached. The timer then reset its count value to 0.
- Set the period
-
- Note
- This will specify the TOP value of the counter. The timer will overflow and reset when this value is reached.
- Set compare match value on CCA
-
- Note
- The PWM duty cycle will be the ratio between PER and CCA, which is set by the tc_write_period() and tc_write_cc() functions. Use tc_write_cc() to change duty cycle run time (e.g to dim a LED). When CCA = 0, the duty cycle will be 0%. When CCA = PER (top value) the duty cycle will be 100%.
- Enable compare channel A -
- Set the clock source
-
- Warning
- When the clock source is set, the timer will start counting
Usage steps