【雅特力AT32WB415系列蓝牙BLE 5.0 MCU】PWM 呼吸灯
<div class='showpostmsg'><p style="text-align: center;"><strong>【雅特力AT32WB415系列蓝牙BLE 5.0 MCU】PWM 呼吸灯</strong></p><p style="text-align: center;"><strong>很荣幸能获得雅特力AT32WB415系列蓝牙BLE 5.0 MCU的评测机会,为此按照我以前的评测的习惯,附上我的<a href="https://github.com/kings669/My_AT32WB415_Demo" target="_blank">Github</a>:,所有评测代码均开源分享。</strong></p>
<p style="text-align: center;">如果因为网络问题无法进入Github可以在Gitee中下载,可能会存在没有及时更新。Gitee:<a href="https://gitee.com/king-a-fei/My_AT32WB415_Demo">My_AT32WB415_Demo: 雅特力科技AT32WB415系列学习,从各个外设入手,学习各个功能。 (gitee.com)</a></p>
<p><strong>一、PWM分析</strong></p>
<p><strong> </strong></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p> 查看一下灯PB9的引脚,判断是否有PWM产生。</p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle"> 我们再来分析一下AT32的PWM的配置,我们可以看到,Tme4是通用定时器,产生PWM。</p>
<p class="imagemiddle"> 控制PWM输出的是有Cxdt来的,我们再库函数中也能看到。</p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle"> </p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle"> 具体的pwm的原理就不多说,大家感兴趣的可以参考STM32的教程:<a href="https://blog.csdn.net/qq_42866708/article/details/113355329?spm=1001.2014.3001.5502">(13条消息) STM32控制舵机讲解,从入门到放弃。_KING_阿飞的博客-CSDN博客_stm32控制舵机的例程详细解释</a></p>
<p class="imagemiddle"><strong>二、代码配置</strong></p>
<p class="imagemiddle"> 我们首先看结构体里面,</p>
<pre>
<code class="language-cpp">/**
* <a href="home.php?mod=space&uid=159083" target="_blank">@brief </a> tmr output config type
*/
typedef struct
{
tmr_output_control_mode_type oc_mode; /*!< output channel mode */
confirm_state oc_idle_state; /*!< output channel idle state */
confirm_state occ_idle_state; /*!< output channel complementary idle state */
tmr_output_polarity_type oc_polarity; /*!< output channel polarity */
tmr_output_polarity_type occ_polarity; /*!< output channel complementary polarity */
confirm_state oc_output_state; /*!< output channel enable */
confirm_state occ_output_state; /*!< output channel complementary enable */
} tmr_output_config_type;
</code></pre>
<p> oc_mode 配置模式</p>
<p> oc_idle_state; <strong>输出通道的空闲状态</strong><br />
occ_idle_state; <strong>输出互补通道空闲状态</strong><br />
oc_polarity; <strong>输出通道极性</strong><br />
occ_polarity; <strong>输出互补通道极性</strong><br />
oc_output_state; <strong>输出通过使能</strong></p>
<p> occ_output_state <strong>输出互补通道使能</strong></p>
<p> </p>
<p><b>具体配置:</b></p>
<pre>
<code class="language-cpp">gpio_init_type gpio_init_struct;
tmr_output_config_type tmr_oc_init_structure;
uint16_t div_value = 0;
/* tmr4 clock enable */
crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);
/* gpioa gpiob clock enable */
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_9;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init(GPIOB, &gpio_init_struct);
/* compute the div value */
div_value = (uint16_t)(system_core_clock / 24000000) - 1;
/* tmr4 time base configuration */
tmr_base_init(TMR4, 665, div_value);
tmr_cnt_dir_set(TMR4, TMR_COUNT_UP);
tmr_clock_source_div_set(TMR4, TMR_CLOCK_DIV1);
tmr_output_default_para_init(&tmr_oc_init_structure);
tmr_oc_init_structure.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
tmr_oc_init_structure.oc_idle_state = FALSE;
tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
tmr_oc_init_structure.oc_output_state = TRUE;
tmr_output_channel_config(TMR4, TMR_SELECT_CHANNEL_4, &tmr_oc_init_structure);
tmr_channel_value_set(TMR4, TMR_SELECT_CHANNEL_4, 83);
tmr_output_channel_buffer_enable(TMR4, TMR_SELECT_CHANNEL_4, TRUE);
tmr_period_buffer_enable(TMR4, TRUE);
/* tmr enable counter */
tmr_counter_enable(TMR4, TRUE);</code></pre>
<pre>
<code class="language-cpp">uint16_t period, pulse, dir;
period = 665;
dir = 1;
pulse = 0;
while(1){
vTaskDelay(50);
if (dir)
{
pulse += 30;
}
else
{
pulse -= 30;
}
if (pulse >= period)
{
dir = 0;
}
if (0 == pulse)
{
dir = 1;
}
tmr_channel_value_set(TMR4, TMR_SELECT_CHANNEL_4, pulse);
}</code></pre>
<p> </p>
<p><strong>三、效果展示</strong></p>
<p style="text-align: center;">549c921acc64ddbd821e0225e397de72<br />
</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> 这三个灯配合非常棒,感谢分享。
页:
[1]