Microchip® Advanced Software Framework

Quick Start Guide for the XMEGA TC Driver

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)
{
// User code to execute when the overflow occurs here
//Important to clear Interrupt Flag
}

Add to, e.g., the main loop in the application C-file:

Workflow

  1. Enable the interrupt controller:
  2. Enable the clock system:
  3. Enable timer/counter TCC0
    • tc_enable(&TCC0);
      Note
      This will enable the clock system for the module
  4. Set the callback function for overflow interrupt
  5. 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.
  6. Set the period
    • tc_write_period(&TCC0, 1000);
      Note
      This will specify the TOP value of the counter. The timer will overflow and reset when this value is reached.
  7. Set the overflow interrupt level
  8. Enable interrupts:
  9. Set the clock source
    • tc_write_clock_source(&TCC0, TC_CLKSEL_DIV1_gc);
      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)
{
// User code here to execute when a channel A compare match occurs
}
static void my_ccb_callback(void)
{
// User code here to execute when a channel B compare match occurs
}

Add to, e.g., the main loop in the application C-file:

Workflow

  1. Enable the interrupt controller:
  2. Enable the clock system:
  3. Enable interrupts:
  4. Enable timer/counter TCC0
    • tc_enable(&TCC0);
      Note
      This will enable the clock system for the module
  5. Set call back function for CCA interrupt
  6. Set call back function for CCB interrupt
  7. 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.
  8. Set the period
    • tc_write_period(&TCC0, 10000);
      Note
      This will specify the TOP value of the counter. The timer will overflow and reset when this value is reached.
  9. Set compare match value on CCA
  10. Set compare match value on CCB
  11. Enable compare channel A and compare channel B -
  12. Set interrupt level on channel A (low priority, see TC_INT_LEVEL_t)
  13. Set interrupt level on channel B (medium priority TC_INT_LEVEL_t)
  14. Set the clock source
    • tc_write_clock_source(&TCC0, TC_CLKSEL_DIV1_gc);
      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:

tc_enable(&TCE0);
tc_write_period(&TCE0, 1950);
tc_write_cc(&TCE0, TC_CCA, 195);
tc_write_clock_source(&TCE0, TC_CLKSEL_DIV1024_gc);

Workflow

  1. 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
  2. Enable the clock system:
  3. Enable timer/counter TCE0
    • tc_enable(&TCE0);
      Note
      This will enable the clock system for the module
  4. 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.
  5. Set the period
    • tc_write_period(&TCE0, 1950);
      Note
      This will specify the TOP value of the counter. The timer will overflow and reset when this value is reached.
  6. Set compare match value on CCA
    • tc_write_cc(&TCC0, TC_CCA, 195);
      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%.
  7. Enable compare channel A -
  8. Set the clock source
    • tc_write_clock_source(&TCE0, TC_CLKSEL_DIV1024_gc);
      Warning
      When the clock source is set, the timer will start counting

Usage steps