Other Parts Discussed in Thread: TM4C123GH6PM
Hello,
I have some questions regarding the PWM configuration on a Tiva Launchpad TM4C123GH6PM. I am using as my sources (1) the "TM4C123G Launchpad Workshop Workbook" and (2) the "Tivaware Peripheral Driver Libary" documents.
In (2), this is the example of using the API functionality:
// Configure the PWM generator for count down mode with immediate updates
// to the parameters.
PWMGenConfigure(PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
// Set the period. For a 50 KHz frequency, the period = 1/50,000, or 20
// microseconds. For a 20 MHz clock, this translates to 400 clock ticks.
// Use this value to set the period.
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 400);
// Set the pulse width of PWM0 for a 25% duty cycle.
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 100);
// Set the pulse width of PWM1 for a 75% duty cycle.
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 300);
// Start the timers in generator 0.
PWMGenEnable(PWM_BASE, PWM_GEN_0);
// Enable the outputs.
PWMOutputState(PWM_BASE, (PWM_OUT_0_BIT | PWM_OUT_1_BIT), true);
What is being done here is quite clear to me.A 50 KHz PWM frequency is desired and when the board's clock is tuned at 20 MHz, this translates to 400 clock ticks. This value is configured with PWMGenPeriodSet.
Then, the duty cycle is configured with PWMPulseWidthSet. With 100 clock ticks is the 25% of the period, and 300 clicks is the 75% of the period.
Finally the PWM generator and output states are configured. There ends the example of document (2)-Tivaware driver library.
Lets see the PWM configuration in the example in document (1)-Launchpad workshop.
Firstly, there is a PWM frequency definition:
#define PWM_FREQUENCY 55
The board's clock is set to 40 MHz and then the PWM clock is set to 40 MHz / 64 == 625 kHz:
ROM_SysCtlPWMClockSet(SYSCTL_PWMDIV_64);
The various peripherals are configured and then the PWM generator is configured with: PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN);
... and the PWM period is set:
ui32PWMClock = SysCtlClockGet() / 64;
ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1;
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, ui32Load);
Here, the ui32PWMClock will be 40.000.000 / 64 = 625.000 (625 kHz).
The ui32Load will be (625.000 / 55) - 1 = 11362.
This is not in accordance to the example presented in (2). According to (2), if we have the system clock tuned at 40 MHz and we want to have a PWM frequency of 55 kHz, then the PWM period should be 1/55.000 = 18.2 microseconds. Each tick with a 40 MHz period occurs every 1/40.000.000 = 25ns. So at 0.0182 sec there should be 18.2μs / 25ns = 727 ticks. This is quite different from the 11362 set in the example. Am I correct, or am I missing something here?
Finally, the duty cycle is set, the PWM generator is enabled and the outputs are enabled:
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust * ui32Load / 1000);
ROM_PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, true);
ROM_PWMGenEnable(PWM1_BASE, PWM_GEN_0);
Regarding the configuration of the duty cycle, this is set at ui8Adjust * ui32Load / 1000 = 83 * 11362 / 1000 = 943.
So, to summarize, my questions regarding (1) are:
1) Why is it that since we configure a PWM signal with 625 kHz with SysCtlPWMClockSet(SYSCTL_PWMDIV_64);
a frequency of 55 kHz is later used?
2) If we need to have a PWM signal with 55 kHz frequency, why set the ui32Load to 11362 instead of 727?
3) The configuration of the duty cycle in (1) is very poorly explained. Values like 83 and 1000 are just being put there.
* This is more of a remark than a question.
Now I wouldn't believe that the stuff referred in the workshop example are wrong, but (1) and (2) are different (to my mind there should be more consistency in the examples presented) and the example in (1) is far less understandable than the example in (2). Can someone shed some light to what I am missing out?