21.4 Initialization and Configuration
The following example shows how to initialize PWM Generator 0 with a 25-kHz frequency, a 25% duty cycle on the MnPWM0 pin, and a 75% duty cycle on the MnPWM1 pin. This example assumes the system clock is 20 MHz.
- Enable the PWM clock by setting its corresponding bit in the RCGCPWM register in the System Control module (RCGCPWM) (see Section 4.2.99).
- Enable the clock to the appropriate GPIO module using the RCGCGPIO register in the System Control module (RCGCGPIO) (see Section 4.2.87).
- Enable the appropriate pins in the GPIO module for their alternate function using the GPIOAFSEL register. To determine which GPIOs to configure, see the device-specific data sheet.
- Configure the PMCn fields in the GPIOPCTL register to assign the PWM signals to the appropriate pins (see Section 17.5.22 and the device-specific data sheet).
- Configure the PWM Clock Configuration (PWMCC) register to use the PWM divide (USEPWMDIV) and set the divider (PWMDIV) to divide by 2 (0x0).
- Configure the PWM generator for countdown mode with immediate updates to the parameters.
- Write the PWM0CTL register with a value of 0x0000.0000.
- Write the PWM0GENA register with a value of 0x0000.008C.
- Write the PWM0GENB register with a value of 0x0000.080C.
- Set the period. For a 25-kHz frequency, the period = 1 / 25000, or 40 µs. The PWM clock source is 10 MHz, and the system clock divided by 2. Thus, there are 400 clock ticks per period. Use this value to set the PWM0LOAD register. In count-down mode, set the LOAD field in the PWM0LOAD register to the requested period minus one.
- Write the PWM0LOAD register with a value of 0x0000.018F.
- Set the pulse width of the MnPWM0 pin for a 25% duty cycle.
- Write the PWM0CMPA register with a value of 0x0000.012B.
- Set the pulse width of the MnPWM1 pin for a 75% duty cycle.
- Write the PWM0CMPB register with a value of 0x0000.0063.
- Start the timers in PWM generator 0.
- Write the PWM0CTL register with a value of 0x0000.0001.
- Enable PWM outputs.
- Write the PWMENABLE register with a value of 0x0000.0003.