【瑞萨RA8D1开发板,基于M85内核的图形MCU测评】5.基于LVGL实现PWM占空比,控制电机等
<div class='showpostmsg'><div><div>上次已经完成LVGL9.1的移植,本次实现基于LVGL库的电机PWM调制。</div>
<div>本次实验使用LVGL的slider控件,实现比较简单,基于官方例程的仿真效果如下图所示</div>
<div></div>
<div>关键代码如下
<pre>
<code class="language-cpp">static lv_obj_t * label;
static void slider_event_cb(lv_event_t * e)
{
lv_obj_t * slider = lv_event_get_target(e);
/*Refresh the text*/
lv_label_set_text_fmt(label, "%"LV_PRId32, lv_slider_get_value(slider));
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
}
/**
* Create a slider and write its value on a label.
*/
void lv_example_get_started_4(void)
{
/*Create a slider in the center of the display*/
lv_obj_t * slider = lv_slider_create(lv_screen_active());
lv_obj_set_width(slider, 200); /*Set the width*/
lv_obj_center(slider); /*Align to the center of the parent (screen)*/
lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL); /*Assign an event function*/
/*Create a label above the slider*/
label = lv_label_create(lv_screen_active());
lv_label_set_text(label, "0");
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
}</code></pre>
<p> </p>
</div>
<div>本次控制的电机为空心杯有刷电机</div>
<div></div>
<div>电机的控制电路原理图为</div>
<div></div>
<div>电机的速度采用进行PWM的调制驱动,PWM的驱动控制和液晶背光控制是一样的,由于手头没有mos管了没法搭建驱动电路,此处采用液晶背光亮度表征电机转速。</div>
<div></div>
<div></div>
<div>在slider_event_cb函数中实现PWM的可调
<pre>
<code>static void slider_event_cb(lv_event_t * e)
{
lv_obj_t * slider = lv_event_get_target(e);
uint8_t pwm = lv_slider_get_value(slider);
/*Refresh the text*/
lv_label_set_text_fmt(label, "%"LV_PRId32, lv_slider_get_value(slider));
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
/* 避免背光不显示*/
if(pwm<20)
pwm=20;
lcd_set_brightness(pwm);
}</code></pre>
<p> </p>
</div>
<div>效果如下图,滑动实现屏幕亮度或电机速度的调节</div>
<div></div>
</div>
</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]