Preface#
If you haven't read the "First Step Configuration" and "MSPM0 Tutorial: Calling SYSCONFIG and Configuring GPIO Outputs and Inputs" written by the author before, it is strongly recommended that you read these two articles first to avoid configuration errors. If you have already completed the SYSCONFIG configuration, please continue reading.
Configuring Timer PWM Mode in SYSCONFIG#
Open Keil and open the SysConfig tool in empty.syscfg.
First, let's take a look at the introduction of SYSCONFIG for this PWM configuration.
Here's a brief translation:
Your description is about the function and configuration options of the Pulse Width Modulation (PWM) driver module. PWM is a common digital signal control technique that can be used to generate edge-aligned waveforms with a specified duty cycle (the ratio of the high-level part of the pulse period to the entire pulse period).
In the basic configuration, users can:
- Configure the clock source: specify the clock source of the PWM waveform, which will affect the frequency of the PWM waveform.
- Configure timer settings: adjust the period of the PWM waveform, usually achieved by configuring an internal timer.
- Configure PWM:
- Select PWM mode: such as edge-aligned mode or center-aligned mode.
- Configure PWM channels: including comparison values, duty cycle, and other parameters, which will affect the output PWM waveform.
In the advanced configuration, users can:
- Configure cross-triggering: some complex applications may require multiple PWM channels to change state simultaneously, and cross-triggering can be used in this case.
This module supports different types of timers in MSPM0, including TIMGx and TIMAx. The functions of MSPM0L130x are described in detail in the table below. The specific parameters and functions may vary depending on the device, so you need to refer to the data sheet of the device you have selected for more detailed information.
The introduction is very detailed, let's start configuring directly.
Click ADD to add PWM.
Set the relevant configuration items, and then click Start Timer.
Then go to PWM settings, let's introduce the configuration items.
PWM Mode
Two modes can be set.
Edge-Aligned and Center-Aligned.
In Edge-Aligned mode:
- The timer is configured in countdown mode. Each time it is loaded, the output signal is set to high. Once the comparison value is reached (which determines the duty cycle), the corresponding output signal is set to low. Therefore, the rise time (from low level to high level) of each PWM channel is the same, while the fall time (from high level to low level) varies depending on the duty cycle.
In Center-Aligned mode:
- The timer is configured in up-down counting mode. When the count value rises and exceeds the comparison value, the output is set to high. When the count value falls and reaches the comparison value, the output is set to low. Therefore, the center point of each PWM channel is when the LOAD value of the timer is in the counter.
These two modes have their own advantages and disadvantages, suitable for different application scenarios. Edge-Aligned mode is simple to implement and suitable for PWM waveforms with higher frequencies. Center-Aligned mode can generate symmetrical PWM waveforms, which is beneficial for reducing current ripple in loads such as motors, and improving system efficiency and performance.
Configure two PWM channels.
Click File->Save.
Then compile in Keil to update the initialization configuration in dl_config.c.
Write the main function.
unsigned int utick = 0; // Tick timer interrupt count
void SysTick_Handler(void)
{
SysTick->CTRL &= ~(1 << 16); /* Clear the tick timer interrupt flag */
utick++; // Tick timer interrupt count
}
typedef enum CompareMode
{
CompareUp = 0,
CompareDown
} CompareMode;
int main(void)
{
int pretime = 0;
unsigned short CompareValue0 = 100;
CompareMode comparemode = CompareUp;
SYSCFG_DL_init();
NVIC_EnableIRQ(UART1_INT_IRQn);
pretime = utick; // Get current time
while (1)
{
switch (comparemode)
{
case CompareUp:
if (utick > 100 + pretime)
{
DL_TimerG_setCaptureCompareValue(PWM_0_INST, CompareValue0 += 50, DL_TIMER_CC_0_INDEX);
pretime = utick; // Update pretime
if (CompareValue0 > 700)
{
comparemode = CompareDown; // Update comparemode to CompareDown
}
}
break;
case CompareDown:
if (utick > 100 + pretime)
{
DL_TimerG_setCaptureCompareValue(PWM_0_INST, CompareValue0 -= 50, DL_TIMER_CC_0_INDEX);
pretime = utick; // Update pretime
if (CompareValue0 < 100)
{
comparemode = CompareUp; // Update comparemode to CompareUp
}
}
break;
default:
// Handle other cases here, if necessary.
break;
}
}
}
Demonstration#
Reproduction please indicate the source.