Microchip® Advanced Software Framework

Use case #1

In this use case the PWM module is configured with overflow interrupt.

Setup steps

Example code

Add to application C-file:

* struct pwm_config pwm_cfg;
*
* void my_callback(void)
* {
* do_something();
* }
* void pwm_init(void)
* {
*
*
* pwm_init(&pwm_cfg, PWM_TCE0, PWM_CH_A, 75);
* pwm_overflow_int_callback(&pwm_cfg, my_callback);
* }

Workflow

  1. Define config struct for PWM module:
    struct pwm_config pwm_cfg;
  2. Define a callback function in the application which does whatever task you want it to do:
    void my_callback(void)
    {
    do_something();
    }
  3. Initialize interrupt controller module:
  4. Initialize sysclock module:
  5. Enable global interrupts:
  6. Initialize config struct and set up PWM with frequency of 75 Hz:
    pwm_init(&pwm_cfg, PWM_TCE0, PWM_CH_A, 75);
    Note
    Since the timer/counter PWM_TCE0 and channel PWM_CH_A is used, the PWM will be output on port E, pin 0. See pwm_tc_t and pwm_channel_t for more information on what port/pin is used for different timer/counters.
    Attention
    This step must not be skipped or the initial content of the structs will be unpredictable, possibly causing misconfiguration.
  7. Set callback function on PWM TC channel overflow:
    pwm_overflow_int_callback(&pwm_cfg, my_callback);

Usage steps

Example code

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

pwm_start(&pwm_cfg, 50);

Workflow

  1. Start PWM with 50% duty cycle:
    pwm_start(&pwm_cfg, 50);