在CYW20829中定时器(Timer)、计数器(Counter)和PWM合称为TCPWM模块,使用16位或32位的计数器实现,可以配置为定时器、计数器、PWM或正交译码器功能,接下来通过配置1个16位的定时器实现1ms的计时来学习TCPWM模块的使用。
继续使用Device Configurator来配置,先看看时钟树
TCPWM模块的输入时钟是96MHz,切换到外设标签,在Digital中找到16位的TCPWM,点击前面的方框
在弹出的窗口中选择Timer
预分频选择16,周期填入5999,开启溢出中断
保存后点击预览,记住这个变量,一会要用到
回到main.c中,添加定时器变量,实现初始化和中断配置,初始化时就会用到刚才记下的变量,在中断中控制LED闪烁
cyhal_timer_t timer_obj;
uint16_t timer_count = 1000;
static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
{
(void)callback_arg;
(void)event;
if(timer_count > 0)
timer_count -= 1;
if(timer_count == 0)
{
timer_count = 1000;
cyhal_gpio_toggle(CYBSP_USER_LED1);
cyhal_gpio_toggle(CYBSP_USER_LED2);
}
}
void app_timer_init(void)
{
cyhal_timer_init_cfg(&timer_obj, &tcpwm_0_group_1_cnt_0_hal_config);
cyhal_timer_register_callback(&timer_obj, isr_timer, NULL);
cyhal_timer_enable_event(&timer_obj, CYHAL_TIMER_IRQ_TERMINAL_COUNT,3, true);
cyhal_timer_start(&timer_obj);
}
在main函数中调用app_timer_init,编译烧录,效果如下
接下来配置PWM,实现呼吸灯效果,在Device Configurator的pins标签中选择LED,Digital Output选择PWM
切换到外设标签,这时相关的TCPWM会有个标识,勾选这两个,选择PWM,参数保持默认就好
在main.c中初始化PWM,并实现2个LED交替呼吸的效果,设置占空比的参数是浮点数范围0~100
cyhal_pwm_t pwm_led1_control;
cyhal_pwm_t pwm_led2_control;
static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
{
(void)callback_arg;
(void)event;
if(timer_count > 0)
timer_count -= 1;
if(timer_count == 0)
{
timer_count = 2000;
}
if(timer_count < 1000)
{
cyhal_pwm_set_duty_cycle(&pwm_led1_control, timer_count/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_led2_control, (1000-timer_count)/10.0,10000);
}
else
{
cyhal_pwm_set_duty_cycle(&pwm_led1_control, (2000-timer_count)/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_led2_control, (timer_count-1000)/10.0,10000);
}
}
void app_pwm_init(void)
{
cyhal_pwm_init_cfg(&pwm_led1_control,&tcpwm_0_group_1_cnt_3_hal_config);
cyhal_pwm_init_cfg(&pwm_led2_control,&tcpwm_0_group_1_cnt_5_hal_config);
cyhal_pwm_start(&pwm_led1_control);
cyhal_pwm_start(&pwm_led2_control);
}
在main函数中调用app_pwm_init,编译烧录,效果如下
开发板上还有个RGB的LED
用PWM实现颜色渐变效果,配置方法和上面一样就不重复介绍了,代码如下
cyhal_pwm_t pwm_led1_control;
cyhal_pwm_t pwm_led2_control;
cyhal_pwm_t pwm_ledr_control;
cyhal_pwm_t pwm_ledg_control;
cyhal_pwm_t pwm_ledb_control;
uint8_t rgb_step = 0;
uint16_t rgb_pwm = 0;
static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
{
(void)callback_arg;
(void)event;
if(timer_count > 0)
timer_count -= 1;
if(timer_count == 0)
{
timer_count = 2000;
}
if(timer_count < 1000)
{
cyhal_pwm_set_duty_cycle(&pwm_led1_control, timer_count/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_led2_control, (1000-timer_count)/10.0,10000);
}
else
{
cyhal_pwm_set_duty_cycle(&pwm_led1_control, (2000-timer_count)/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_led2_control, (timer_count-1000)/10.0,10000);
}
rgb_pwm += 1;
if(rgb_pwm == 1000)
{
rgb_pwm = 0;
rgb_step += 1;
if(rgb_step > 2)
rgb_step = 0;
}
switch(rgb_step)
{
case 0:
cyhal_pwm_set_duty_cycle(&pwm_ledr_control, rgb_pwm/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_ledg_control, 0,10000);
cyhal_pwm_set_duty_cycle(&pwm_ledb_control, (1000-rgb_pwm)/10.0,10000);
break;
case 1:
cyhal_pwm_set_duty_cycle(&pwm_ledg_control, rgb_pwm/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_ledb_control, 0,10000);
cyhal_pwm_set_duty_cycle(&pwm_ledr_control, (1000-rgb_pwm)/10.0,10000);
break;
case 2:
cyhal_pwm_set_duty_cycle(&pwm_ledb_control, rgb_pwm/10.0,10000);
cyhal_pwm_set_duty_cycle(&pwm_ledr_control, 0,10000);
cyhal_pwm_set_duty_cycle(&pwm_ledg_control, (1000-rgb_pwm)/10.0,10000);
break;
}
}
void app_timer_init(void)
{
cyhal_timer_init_cfg(&timer_obj, &tcpwm_0_group_1_cnt_0_hal_config);
cyhal_timer_register_callback(&timer_obj, isr_timer, NULL);
cyhal_timer_enable_event(&timer_obj, CYHAL_TIMER_IRQ_TERMINAL_COUNT,3, true);
cyhal_timer_start(&timer_obj);
}
void app_pwm_init(void)
{
cyhal_pwm_init_cfg(&pwm_ledr_control,&tcpwm_0_group_0_cnt_1_hal_config);
cyhal_pwm_init_cfg(&pwm_ledg_control,&tcpwm_0_group_0_cnt_0_hal_config);
cyhal_pwm_init_cfg(&pwm_ledb_control,&tcpwm_0_group_1_cnt_1_hal_config);
cyhal_pwm_init_cfg(&pwm_led1_control,&tcpwm_0_group_1_cnt_3_hal_config);
cyhal_pwm_init_cfg(&pwm_led2_control,&tcpwm_0_group_1_cnt_5_hal_config);
cyhal_pwm_start(&pwm_led1_control);
cyhal_pwm_start(&pwm_led2_control);
cyhal_pwm_start(&pwm_ledr_control);
cyhal_pwm_start(&pwm_ledg_control);
cyhal_pwm_start(&pwm_ledb_control);
}
最终效果
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
共1人点赞
|