SLAAE79 March 2023 MSPM0G1105 , MSPM0G1106 , MSPM0G1107 , MSPM0G1505 , MSPM0G1506 , MSPM0G1507 , MSPM0G3105 , MSPM0G3106 , MSPM0G3107 , MSPM0G3505 , MSPM0G3506 , MSPM0G3507 , MSPM0L1105 , MSPM0L1106 , MSPM0L1303 , MSPM0L1304 , MSPM0L1304-Q1 , MSPM0L1305 , MSPM0L1305-Q1 , MSPM0L1306 , MSPM0L1306-Q1 , MSPM0L1343 , MSPM0L1344 , MSPM0L1345 , MSPM0L1346
Description
The PWM duty cycle directly correlates to the brightness of the LED. When using an LED as an indicator or a light source in an application, you can use a PWM signal to drive the LED brightness and power consumption. The timer modules in the MPSM0 can be used to generate PWM signals with varying frequency and duty cycles. This example code dims and brightens the LED in a heartbeat manner to display the full range of PWM duty cycles that can be used to drive an LED.
Figure 1-1 displays a functional block diagram of the peripherals used in this example.
Required peripherals
This application requires one timer, one device pin, and an onboard LED.
Sub-block Functionality | Peripheral Use | Notes |
---|---|---|
PWM generation |
(1x) Timer G |
Called PWM_0_INST in code |
IOMUX sub-block |
1 pin |
(1x) PWM output |
Compatible devices
Based on the requirements in Table 1-1, this example is compatible with the devices in Table 1-2. The corresponding EVM may be used for prototyping.
Compatible Devices | EVM |
---|---|
MSPM0Lxxx |
|
MSPM0Gxxx |
Design steps
Design considerations
Software flowchart
Figure 1-2 shows the operations performed by application to change the duty cycle of the PWM output.
Application code
In the application code, the PWM duty cycle is increased by 1% each time the timer triggers an interrupt until it reaches 90% and then decreased by 1% until the duty cycle reaches 10%, which generates a heartbeat effect. This application PWM output has 2000 bits of resolution; therefore, increasing or decreasing the pwm_count variable by 20 changes the duty cycle by 1%. Depending on an application requirements, different scaling can be required.
void PWM_0_INST_IRQHandler(void){
switch (DL_TimerG_getPendingInterrupt(PWM_0_INST)){
case DL_TIMER_IIDX_LOAD:
if (dc <= 10){mode = 1;} // if reached lowest dc (10%), increase dc
else if (dc >= 90){mode = 0;} // if reached highest dc (90%), decrease dc
if (mode){pwm_count -= 20; dc += 1;} // up
if (!mode){pwm_count += 20; dc -= 1;} // down
DL_TimerG_setCaptureCompareValue(PWM_0_INST, pwm_count, DL_TIMER_CC_1_INDEX); // update ccr1 value
break;
default:
break;
}
}
Results
Additional Resources