This post was last edited by Min丨Dayu on 2019-8-30 12:46
This content is originally created by EEWORLD forum user Min Da. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source
This post is to answer the help post I sent yesterday. Here is a small record. 2019.08.30 Help post link: https://en.eeworld.com/bbs/forum.php?mod=viewthread&tid=1088275&extra=
Output single PWM
The routine in the MSP430Ware package—timer_a_ex1_pwmSingle can output a single PWM wave with a duty cycle of 0.75 and a frequency of ** (if you forget, you can use an oscilloscope to measure it). The output pin is P2.0. After the code is successfully burned, press the reset button of the microcontroller and use an oscilloscope to observe the waveform to verify whether the code is correct.
By referring to Table 4-1 of the MSP430F5529 data sheet, P1.4 also has PWM output capability. Now I want to modify the original routine P2.0 pin to P1.4 pin to output PWM.
Configuring peripherals is a routine, which I personally summarize into three steps: configuring peripheral clocks; configuring GPIO ports; initializing peripheral modules.
Configure peripheral clocks: Each peripheral module must have clock control to work, so before using each peripheral module, you must understand the maximum clock frequency of the peripheral. The peripheral clock is obtained by dividing the system clock. After knowing the system clock frequency, you need to pay attention to the actual clock frequency of the peripheral. You must be able to read the clock tree when learning to use each microcontroller.
Configure GPIO port: GPIO (General-purpose Input Output) is a general-purpose digital input and output port. The GPIO peripheral is the only bridge that communicates MUC with the outside world.
Initialize peripheral modules: Each peripheral module has a lot of registers. If you want to achieve a certain goal through these registers, you need to set them in advance. For example, if a person needs to go to Beijing and is required to arrive at Tiananmen Square in 5 days, this person can have many different options. Arriving at Tiananmen Square in Beijing can be considered as the goal, and various different options can be considered as register configurations. If there are only the first two steps without configuring registers, that is, this person cannot arrive at Tiananmen Square and the goal cannot be achieved.
Now let's configure the PWM module to help understand the three-step method.
Step 1. Configure the PWM module clock
Step 2. Configure GPIO port
Step 3. Initialize the PWM module
MSP430F5529 does not have an independent PWM module, so there is no PWM module clock. Here we need to mention DSP28335, this MUC is also from TI. It has an independent PWM module, which includes a time base module, a comparison module, an action module, etc. Back to here, the MSP430F5529 peripheral configuration can slightly modify steps 1 and 3 and merge them after step 2.
Configure the PWM module of MSP430F5529 in two steps
Step 1. Configure GPIO port
Step 2. Initialize Timer_A register
Step 1: Configure P1.4 as PWM output pin
GPIO_setAsPeripheralModuleFunctionOutputPin(
GPIO_PORT_P1,
GPIO_PIN4);
|
Step 2: Check the data sheet to see that P1.4 corresponds to the TimerA register, so configure the register to PWM mode
//Generate PWM - Timer runs in Up mode
Timer_A_outputPWMParam param = {0}; //Upward counting mode. param is a structure variable.
param.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;//Timer_A clock source
param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1; // No frequency division
param.timerPeriod = TIMER_PERIOD; //PWM period count value
param.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_3; //Look up the table to know that P1.4 corresponds to CCR3
param.compareOutputMode = TIMER_A_OUTPUTMODE_RESET_SET; //Automatic reload mode
param.dutyCycle = DUTY_CYCLE; //duty cycle count value
Timer_A_outputPWM(TIMER_A0_BASE, m); //Look up the table to know that P1.4 corresponds to TimerA0
|
Summary: There are a total of 3 places in the routine code that need to be modified.
GPIO_setAsPeripheralModuleFunctionOutputPin(
GPIO_PORT_P1,
GPIO_PIN4);
|
param.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_3; //Look up the table to know that P1.4 corresponds to CCR3
|
Timer_A_outputPWM(TIMER_A0_BASE, m); //Look up the table to know that P1.4 corresponds to TimerA0
|
The source code is attached:
timer_a_ex1_pwmSingle.rar
(589.76 KB, downloads: 12)