【MSPM0L1306 LaunchPad】8、pwm呼吸灯
本帖最后由 dql2016 于 2023-11-5 22:02 编辑<p>呼吸灯是很多产品常见的设计,尤其是低端产品没有液晶显示屏通过LED来显示工作状态。</p>
<p>使用CCS配置PWM输出十分简单,首先选择例程:</p>
<div style="text-align: center;"></div>
<p> </p>
<p> </p>
<p>看看demo说明</p>
<div style="text-align: center;"></div>
<p> </p>
<p> </p>
<p><br />
PWM的配置,主要是定时器时钟和分频系数、装载值的设置:</p>
<div style="text-align: center;"></div>
<p> </p>
<p>输出管脚连接了LED:</p>
<p> </p>
<div style="text-align: center;"></div>
<p>程序,十分简洁易懂:</p>
<pre>
<code class="language-cpp">
#include "ti_msp_dl_config.h"
int pwm_count = 1800; // initial ccr count based on 10% duty cycle
int dc = 10; // initialized to 10 % duty cycle
int mode = 1; // 1 = up (will indicate you need to increase dc), 0 = down (will
// decrease dc)
int main(void) {
SYSCFG_DL_init();
NVIC_EnableIRQ(PWM_0_INST_INT_IRQN);
DL_TimerG_startCounter(PWM_0_INST);
while (1) {
__WFI();
}
}
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;
}
}
</code></pre>
<p>效果:</p>
<div style="text-align: center;"></div>
<p> </p>
<p>pwm呼吸灯是比测项目,完成的不错嘛</p>
<p>PWM可以不用开启中断,毕竟PWM的频率一般都是比较高的 </p>
页:
[1]