【中科蓝讯AB32VG1 RISC-V板“碰上”RTT测评】定时器PWM模式与舵机应用
[复制链接]
本帖最后由 xiyue521 于 2021-5-11 01:12 编辑
1.PWM的配置和函数也不多主要有两个:
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
还有要注意的是这个周期的单位是ns,使用 rt_device_find(PWM_DEV_NAME);找设备时,名字的编写,像Timer5是t5pwm,可以用list_device查看:
经过逻辑分析仪测试得占空比是高电平的脉宽:
2.因为很多pwm与一些硬件共用,所以本次实验选择:
通过settings配置PWM:
list_device查看设备名:(设备名一定要写对不然就不能正常使用了),虽然手册没有提供,但可以通过控制台查看,也是很方便!
3.在使用lpwm1和lpwm2时发现了一个现象,就是他们周期是一样的,所以猜测他们应该是同一个定时器的3个通道,占空比可以不一样
4.接下来就是到舵机的应用了,舵机主要是控制高电平脉宽来控制角度,当然频率一般50hz,但是这个pwm最低只有100hz,可以用。
0.5ms————-0度;
1.0ms————45度;
1.5ms————90度;
2.0ms———–135度;
2.5ms———–180度;
MSH_CMD_EXPORT用于将函数调到help控制台便于调试用,
所以这里我写了三个测试函数为45,,90,135三个角度。
help查看:
效果很不错!
代码:
#define PWM_DEV_CHANNEL 1
struct rt_device_pwm *pwm_dev1,*pwm_dev2; /* PWM设备句柄 */
ALIGN(RT_ALIGN_SIZE)
static uint8_t PWM_Thread_Stack[1024];
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(&pwm_thread, "pwm_thread", PWM_Thread_Entry, RT_NULL,
&PWM_Thread_Stack[0], sizeof(PWM_Thread_Stack), 10, 10);
rt_thread_startup(&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);
|