xiyue521 发表于 2021-5-11 01:02

【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】定时器PWM模式与舵机应用

本帖最后由 xiyue521 于 2021-5-11 01:12 编辑

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:24px;">1.PWM的配置和函数也不多主要有两个:&nbsp; </span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:24px;">&nbsp;rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);<br />
&nbsp; &nbsp;rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);</span></p>

<p class="imagemiddle"><span style="font-size:24px;">还有要注意的是这个周期的单位是ns,使用&nbsp; &nbsp;rt_device_find(PWM_DEV_NAME);找设备时,名字的编写,像Timer5是t5pwm,可以用list_device查看:</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:24px;"></span></p>

<p class="imagemiddle" style="text-align: center;">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:24px;">经过逻辑分析仪测试得占空比是高电平的脉宽:</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:24px;"></span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;">2.因为很多pwm与一些硬件共用,所以本次实验选择:</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;">通过settings配置PWM:</span></p>

<p class="imagemiddle" style="text-align: center;"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;">list_device查看设备名:(设备名一定要写对不然就不能正常使用了),虽然手册没有提供,但可以通过控制台查看,也是很方便!</span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:24px;">3.在使用lpwm1和lpwm2时发现了一个现象,就是他们周期是一样的,所以猜测他们应该是同一个定时器的3个通道,占空比可以不一样</span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;">4.接下来就是到舵机的应用了,舵机主要是控制高电平脉宽来控制角度,当然频率一般50hz,但是这个pwm最低只有100hz,可以用。</span></p>

<p class="imagemiddle"><span style="font-size:24px;">0.5ms&mdash;&mdash;&mdash;&mdash;-0度;<br />
1.0ms&mdash;&mdash;&mdash;&mdash;45度;<br />
1.5ms&mdash;&mdash;&mdash;&mdash;90度;<br />
2.0ms&mdash;&mdash;&mdash;&ndash;135度;<br />
2.5ms&mdash;&mdash;&mdash;&ndash;180度;</span></p>

<p class="imagemiddle"><span style="font-size:24px;">MSH_CMD_EXPORT用于将函数调到help控制台便于调试用,</span></p>

<p class="imagemiddle"><span style="font-size:24px;">所以这里我写了三个测试函数为45,,90,135三个角度。</span></p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;">help查看:</span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;"></span></p>

<p class="imagemiddle"><span style="font-size:24px;">效果很不错!</span></p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle"><span style="font-size:24px;">代码:</span></p>

<pre>
<code>#define PWM_DEV_CHANNEL   1   
struct rt_device_pwm*pwm_dev1,*pwm_dev2;      /* PWM设备句柄 */
ALIGN(RT_ALIGN_SIZE)
static uint8_t PWM_Thread_Stack;
static void PWM_Thread_Entry(void *para);
static struct rt_thread pwm_thread;
rt_uint32_t period, pulse;
void Pwm_Init(void){


   pwm_dev1 = (struct rt_device_pwm *)rt_device_find("lpwm1");
   RT_ASSERT(pwm_dev1 != RT_NULL);

   rt_pwm_set(pwm_dev1, PWM_DEV_CHANNEL, 2000000, 200000);

   rt_pwm_enable(pwm_dev1, PWM_DEV_CHANNEL);

   pwm_dev2 = (struct rt_device_pwm *)rt_device_find("lpwm2");
   RT_ASSERT(pwm_dev2 != RT_NULL);

   rt_pwm_set(pwm_dev2, PWM_DEV_CHANNEL, 10000000, 1500000);

   rt_pwm_enable(pwm_dev2, PWM_DEV_CHANNEL);



}

static void PWM_Thread_Entry(void *para){
    Pwm_Init();

}

int Pwm_Thread_Init(void){

   rt_thread_init(&amp;pwm_thread, "pwm_thread", PWM_Thread_Entry, RT_NULL,
         &amp;PWM_Thread_Stack, sizeof(PWM_Thread_Stack), 10, 10);

   rt_thread_startup(&amp;pwm_thread);
   return 0;
}
INIT_APP_EXPORT(Pwm_Thread_Init);



void test45(void)
{

    rt_pwm_set(pwm_dev2, PWM_DEV_CHANNEL, 10000000, 1000000);



}void test90(void)
{

    rt_pwm_set(pwm_dev2, PWM_DEV_CHANNEL, 10000000, 1500000);



}void test135(void)
{

    rt_pwm_set(pwm_dev2, PWM_DEV_CHANNEL, 10000000, 2000000);



}
MSH_CMD_EXPORT(test45,45);MSH_CMD_EXPORT(test90,90);MSH_CMD_EXPORT(test135,135);

</code></pre>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p class="imagemiddle">&nbsp;</p>

<p>&nbsp;</p>

w494143467 发表于 2021-5-11 15:24

<p>舵机也是属于非常好玩的一个外设的,感谢分享,期待后续的测评!!!</p>

火辣西米秀 发表于 2021-5-11 16:31

<p>lpwm1和lpwm2频率应该可以设置</p>

<p>占空比可以根据需要调整</p>

freebsder 发表于 2021-5-12 09:51

<p>这芯片的pwm有足够多的配置模式吗?</p>

xiyue521 发表于 2021-5-16 00:22

freebsder 发表于 2021-5-12 09:51
这芯片的pwm有足够多的配置模式吗?

<p>没有了,手册也讲很少</p>

freebsder 发表于 2021-5-16 21:34

xiyue521 发表于 2021-5-16 00:22
没有了,手册也讲很少

<p>呵呵,看起来差距还是蛮大的,英飞凌,nxp他们的手册里pwm的各种模式丰富的不是一般。</p>

xiyue521 发表于 2021-5-17 17:50

<p>看那个数据手册说明也挺少,只能靠rtt的底层驱动</p>

xiyue521 发表于 2021-5-17 17:51

<p>看那个数据手册说明也挺少,只能靠rtt的底层驱动</p>
页: [1]
查看完整版本: 【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】定时器PWM模式与舵机应用