Welcome to MSPM0G3519 Development Board repository
7. PWM Breathing Light
PWM Breathing Light
PWM Breathing Light1. Learning ObjectivesPWM PrinciplePWM Basic Parameters2. Hardware Setup3. Experiment Steps1. Open SYSCONFIG Configuration Tool2. PWM Parameter Configuration3. Write Program4. Compile4. Program Analysis5. Experimental Phenomena
1. Learning Objectives
Learn the principle of PWM breathing light.
Output a PWM signal through TIMG6_C0 to control the LED indicator light, making it brighten from dim to bright and then dim from bright to dim, similar to human breathing.
PWM Principle
PWM stands for Pulse Width Modulation. It is a very effective technique for digitally controlling analog circuits using microprocessor digital outputs. With its advantages of simple control, flexibility, and good dynamic response, it has become the most widely used control method in power electronics technology. Its application areas include measurement, communication, power control and conversion, motor control, servo control, dimming, switching power supplies, and even some audio amplifiers. Therefore, learning PWM has very important practical significance.
Actually, we can understand it this way: PWM is a method of digitally encoding analog signal levels using high-resolution counters. By using a high-resolution counter, the duty cycle of a square wave is modulated to encode a specific analog signal level. The PWM signal remains digital because at any given moment, the full-scale DC supply is either completely on (ON) or completely off (OFF). The voltage or current source is applied to the analog load through a repetitive pulse sequence of on (ON) or off (OFF). When on, the DC supply is connected to the load; when off, the supply is disconnected. As long as the bandwidth is sufficient, any analog value can be encoded using PWM.
PWM Basic Parameters
PWM is pulse width modulation with two very important parameters: frequency and duty cycle.
Frequency: The frequency of PWM is the reciprocal of the entire period, which is the number of times the PWM signal repeats per unit time. It represents how many signal cycles are completed per second. Higher frequency means shorter signal period and more changes.
Duty Cycle: Duty cycle refers to the proportion of high level in one cycle. Usually expressed as a percentage. It reflects the "width" of the PWM signal, which is the ratio of the high-level duration to the entire period time. Higher duty cycle means higher average output voltage.
2. Hardware Setup
Earlier we introduced that the MSPM0G series has 7 timers in total, which can be divided into 2 types: general-purpose timers (TIMG) and advanced control timers (TIMA). The PWM function is implemented based on the timer. From the user manual, you can learn that MSPM0G3519 has 7 timers. Which timer has PWM functionality needs to be referred to the pin description in the data manual. Each PWM channel corresponds to a pin on the microcontroller. This pin is not uniquely fixed. There may be one or two pins corresponding to the same channel. For example, TIMG_C0 corresponds to PA5 and PA12, meaning both PA5 and PA12 pins can be configured as timer channel 0. When using, you can choose either one for configuration.


This case uses LED1's PB2 pin, configured as timer 6 channel 0, to generate a PWM signal controlling the light on/off.

PWM has two modes. One is edge-aligned PWM, which means the timer uses down-counting mode. The output channel outputs high level when the timer count starts. When the count value equals the output channel's compare value (CC0 and CCP0, CC1 and CCP1 in the figure), the output channel outputs low level. This continues until the timer count reaches 0, completing one cycle of pulse output.

The other mode is center-aligned mode, which means the timer uses up/down counting mode. The output channel outputs low level when the timer starts. It toggles the level once when the timer counts up and down to the equal compare value of the output channel.

3. Experiment Steps
Achieve breathing light effect through driving PWM signal. By changing the PWM signal's duty cycle, you can adjust the current flowing through the LED, thereby achieving LED brightness gradient. Generally, the human eye does not perceive obvious flickering at refresh rates above 80Hz because the flickering cannot be detected at high frequencies. Larger duty cycle means brighter LED; smaller duty cycle means dimmer LED. Therefore, at a fixed frequency, adjusting the duty cycle controls the LED brightness, thereby achieving the breathing light effect. This case will use the onboard LED light (PB2) to achieve breathing light effect, gradually brightening then gradually dimming.
1. Open SYSCONFIG Configuration Tool
Create a new empty project empty in CCS.


Find and open the empty.syscfg file in the left workspace of CCS.

In SYSCONFIG, select MCU peripherals on the left, find the TIMER-PWM option and click to enter. Click ADD in TIMER-PWM to add the PWM peripheral under the timer.

2. PWM Parameter Configuration
Here set channel 0 to 4kHz frequency, 0% duty cycle PWM. Period count value is 1000, compare value is also set to 1000. This way, starting from 1000 and counting down, whenever it encounters 1000, the PWM state toggles once.

Parameter description:
Name: Custom name. Here set to PWM_LED
PWM Profiles: Configure PWM template. Default is fine.
Timer Clock Source: Timer clock source. Select BUSCLK (bus clock), default frequency is 32MHz.
Timer Clock Divider: Timer clock divider. Set to 8 divider.
Calculated Timer Clock Source: Shows the clock frequency after division.
Timer Clock Prescale: Timer prescaler. We configured TIMG6, a 16-bit timer, which supports prescaler. No prescaler set here.
Calculated Clock Freguency (Hz): Shows the frequency after division and prescaling.
PWM Period Count: PWM period count value. Here set to 1000.
Calculated PwM Frequency(Hz): Software automatically calculates PWM frequency, showing 4000.
Start Timer: Whether to start timer. Checked here. Timer PWM output starts on power-up.
PWM Mode: PWM mode. Configured as Edge-aligned Down Counting edge-aligned mode with down-counting mode.
PWM Channel(s): PWM channel. Configured to only enable channel 0.
Enable Channel Complimentary Output: Whether to enable channel complementary output. Not enabled here.
Initial Value: Default output state of PWM channel. Configured as default low level output.
Counter Compare Value: PWM compare value. Set to 1000, meaning PWM duty cycle setting range is 0-999.
Desired Duty cycle (%): PWM duty cycle. Default duty cycle 0 is used here.
Actual Duty cycle (%): Shows the actual duty cycle automatically calculated by software.
Invert Channel: Whether to enable inverted output. Not enabled here.
Channel Update Mode: Channel update mode. Configured as Capture Compare value has immediate effect - update immediately when compare value is reached.

LED1's control pin is PB2. PB2 has timer 6 channel 0, so select TIMG6, PB2/50 here.

We have already assigned PB2 of LED1 to PWM. It cannot be used for GPIO peripheral anymore and needs to be removed. Currently, only one button configuration remains in the GPIO peripheral in the example.
Save the configuration in the .syscfg file using the shortcut key Ctrl+S.
3. Write Program
In the empty.c file, write the following code
x#include "ti_msp_dl_config.h"
volatile unsigned int delay_times = 0;
//Precise millisecond delay with tick timervoid delay_ms(unsigned int ms){ delay_times = ms; while( delay_times != 0 );}
int main(void){ int i = 0;
SYSCFG_DL_init();
while (1) {
// Breathing light gradually brightens for (i = 0; i <= 999; i++) { // Set LED Brightness DL_TimerG_setCaptureCompareValue(PWM_LED_INST,i,GPIO_PWM_LED_C0_IDX); delay_ms(1); // Delay to control the speed of brightness change } // Breathing light dimming process for (i = 999; i > 0; i--) { // Set LED Brightness DL_TimerG_setCaptureCompareValue(PWM_LED_INST,i,GPIO_PWM_LED_C0_IDX); delay_ms(1); // Delay to control the speed of brightness change } }}
//Tick timer interrupt service functionvoid SysTick_Handler(void){ if( delay_times != 0 ) { delay_times--; }}4. Compile

Compilation was successful. You can now download the program to the development board.
4. Program Analysis
empty.c

After initializing the hardware, enter the breathing light loop. Use PWM signal duty cycle to control LED brightness. The breathing light gradually brightens and gradually dims using two for loops, adjusting the PWM duty cycle from 0 to 999 and then from 999 to 0 progressively. delay_ms(1) sets the brightness change interval to 1 millisecond. You can control the breathing light change speed by adjusting the delay function parameter.
5. Experimental Phenomena
After the program is downloaded, you can see the LED light slowly brighten and dim, showing a breathing light effect.
