Microchip® Advanced Software Framework

Quick Start Guide for TC - Callback

In this use case, the TC will be used to generate a PWM signal, with a varying duty cycle.

Here the pulse width is increased each time the timer count matches the set compare value. The TC module will be set up as follows:

  • GCLK generator 0 (GCLK main) clock source
  • 16-bit resolution on the counter
  • No prescaler
  • Normal PWM wave generation
  • GCLK reload action
  • Don't run in standby
  • No inversion of waveform output
  • No capture enabled
  • Count upward
  • Don't perform one-shot operations
  • No event input enabled
  • No event action
  • No event generation enabled
  • Counter starts on 0

Quick Start

Prerequisites

There are no prerequisites for this use case.

Code

Add to the main application source file, before any functions:

  • SAM D21 Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
  • SAM D20 Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
  • SAM R21 Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
  • SAM D11 Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
  • SAM L21 Xplained Pro.
    #define PWM_MODULE EXT2_PWM_MODULE
    #define PWM_OUT_PIN EXT2_PWM_0_PIN
    #define PWM_OUT_MUX EXT2_PWM_0_MUX
  • SAM L22 Xplained Pro.
    #define PWM_MODULE EXT3_PWM_MODULE
    #define PWM_OUT_PIN EXT3_PWM_0_PIN
    #define PWM_OUT_MUX EXT3_PWM_0_MUX
  • SAM DA1 Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
  • SAM HA1G16A Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
  • SAM C21 Xplained Pro.
    #define PWM_MODULE EXT1_PWM_MODULE
    #define PWM_OUT_PIN EXT1_PWM_0_PIN
    #define PWM_OUT_MUX EXT1_PWM_0_MUX
    Add to the main application source file, outside of any functions: Copy-paste the following callback function code to your user application:
    struct tc_module *const module_inst)
    {
    static uint16_t i = 0;
    i += 128;
    }
    Copy-paste the following setup code to your user application:
    void configure_tc(void)
    {
    struct tc_config config_tc;
    config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
    config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
    config_tc.counter_16_bit.compare_capture_channel[0] = 0xFFFF;
    config_tc.pwm_channel[0].enabled = true;
    config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
    config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
    tc_init(&tc_instance, PWM_MODULE, &config_tc);
    }
    {
    }
    Add to user application initialization (typically the start of main()):

Workflow

  1. Create a module software instance structure for the TC module to store the TC driver state while it is in use.
    Note
    This should never go out of scope as long as the module is in use. In most cases, this should be global.
  2. Configure the TC module.
    1. Create a TC module configuration struct, which can be filled out to adjust the configuration of a physical TC peripheral.
      struct tc_config config_tc;
    2. Initialize the TC configuration struct with the module's default values.
      Note
      This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
    3. Alter the TC settings to configure the counter width, wave generation mode, and the compare channel 0 value.
      config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
      config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM;
      config_tc.counter_16_bit.compare_capture_channel[0] = 0xFFFF;
    4. Alter the TC settings to configure the PWM output on a physical device pin.
      config_tc.pwm_channel[0].enabled = true;
      config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN;
      config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX;
    5. Configure the TC module with the desired settings.
      tc_init(&tc_instance, PWM_MODULE, &config_tc);
    6. Enable the TC module to start the timer and begin PWM signal generation.
  3. Configure the TC callbacks.
    1. Register the Compare Channel 0 Match callback functions with the driver.
    2. Enable the Compare Channel 0 Match callback so that it will be called by the driver when appropriate.

Use Case

Code

Copy-paste the following code to your user application:

while (true) {
}

Workflow

  1. Enter an infinite loop while the PWM wave is generated via the TC module.
    while (true) {
    }