Actions

EmSys

Tm4clib-pwm-programming-examples

From EdWiki

PWM Programming Example

The following example shows how to use the PWM API to initialize the PWM0 with a 50 KHz frequency, and with a 25% duty cycle on PWM0 and a 75% duty cycle on PWM1.

  1. #include "cortex.h"
  2. #include "pwm.h"
  3.  
  4. /* Configure the PWM generator for count down mode with immediate updates to the parameters. */
  5. pwm_gen_configure(PWM0, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
  6.  
  7. /* Set the period. For a 50 KHz frequency, the period = 1/50,000, or 20 microseconds.
  8.    For a 20 MHz clock, this translates to 400 clock ticks.
  9.    Use this value to set the period.
  10. */
  11. pwm_gen_set_period(PWM0, PWM_GEN_0, 400);
  12.  
  13. /* Set the pulse width of PWM0 for a 25% duty cycle. */
  14. pwm_set_pulse_width(PWM0, PWM_OUT_0, 100);
  15.  
  16. /* Set the pulse width of PWM1 for a 75% duty cycle. */
  17. pwm_set_pulse_width(PWM0, PWM_OUT_1, 300);
  18.  
  19. /* Start the timers in generator 0. */
  20. pwm_gen_enable(PWM0, PWM_GEN_0);
  21.  
  22. /* Enable the outputs. */
  23. pwm_output_state(PWM0, (PWM_OUT_0_BIT | PWM_OUT_1_BIT), true);