[AIROC™ 蓝牙] 【英飞凌CYW20829测评】3.定时器+呼吸灯实验

[复制链接]
 楼主| yuyy1989 发表于 2024-6-20 22:42 | 显示全部楼层 |阅读模式
<
在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闪烁
  1. cyhal_timer_t timer_obj;
  2. uint16_t timer_count = 1000;
  3. static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
  4. {
  5.     (void)callback_arg;
  6.     (void)event;

  7.     if(timer_count > 0)
  8.         timer_count -= 1;
  9.     if(timer_count == 0)
  10.     {
  11.         timer_count = 1000;
  12.         cyhal_gpio_toggle(CYBSP_USER_LED1);
  13.         cyhal_gpio_toggle(CYBSP_USER_LED2);
  14.     }
  15. }
  16. void app_timer_init(void)
  17. {
  18.     cyhal_timer_init_cfg(&timer_obj, &tcpwm_0_group_1_cnt_0_hal_config);
  19.     cyhal_timer_register_callback(&timer_obj, isr_timer, NULL);
  20.     cyhal_timer_enable_event(&timer_obj, CYHAL_TIMER_IRQ_TERMINAL_COUNT,3, true);
  21.     cyhal_timer_start(&timer_obj);
  22. }

在main函数中调用app_timer_init,编译烧录,效果如下

接下来配置PWM,实现呼吸灯效果,在Device Configurator的pins标签中选择LED,Digital Output选择PWM

切换到外设标签,这时相关的TCPWM会有个标识,勾选这两个,选择PWM,参数保持默认就好

在main.c中初始化PWM,并实现2个LED交替呼吸的效果,设置占空比的参数是浮点数范围0~100
  1. cyhal_pwm_t pwm_led1_control;
  2. cyhal_pwm_t pwm_led2_control;
  3. static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
  4. {
  5.     (void)callback_arg;
  6.     (void)event;

  7.     if(timer_count > 0)
  8.         timer_count -= 1;
  9.     if(timer_count == 0)
  10.     {
  11.         timer_count = 2000;
  12.     }
  13.     if(timer_count < 1000)
  14.     {
  15.         cyhal_pwm_set_duty_cycle(&pwm_led1_control, timer_count/10.0,10000);
  16.         cyhal_pwm_set_duty_cycle(&pwm_led2_control, (1000-timer_count)/10.0,10000);
  17.     }
  18.     else
  19.     {
  20.         cyhal_pwm_set_duty_cycle(&pwm_led1_control, (2000-timer_count)/10.0,10000);
  21.         cyhal_pwm_set_duty_cycle(&pwm_led2_control, (timer_count-1000)/10.0,10000);
  22.     }
  23. }
  24. void app_pwm_init(void)
  25. {
  26.     cyhal_pwm_init_cfg(&pwm_led1_control,&tcpwm_0_group_1_cnt_3_hal_config);
  27.     cyhal_pwm_init_cfg(&pwm_led2_control,&tcpwm_0_group_1_cnt_5_hal_config);
  28.     cyhal_pwm_start(&pwm_led1_control);
  29.     cyhal_pwm_start(&pwm_led2_control);
  30. }

在main函数中调用app_pwm_init,编译烧录,效果如下

开发板上还有个RGB的LED

用PWM实现颜色渐变效果,配置方法和上面一样就不重复介绍了,代码如下
  1. cyhal_pwm_t pwm_led1_control;
  2. cyhal_pwm_t pwm_led2_control;
  3. cyhal_pwm_t pwm_ledr_control;
  4. cyhal_pwm_t pwm_ledg_control;
  5. cyhal_pwm_t pwm_ledb_control;
  6. uint8_t rgb_step = 0;
  7. uint16_t rgb_pwm = 0;
  8. static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
  9. {
  10.     (void)callback_arg;
  11.     (void)event;

  12.     if(timer_count > 0)
  13.         timer_count -= 1;
  14.     if(timer_count == 0)
  15.     {
  16.         timer_count = 2000;
  17.     }
  18.     if(timer_count < 1000)
  19.     {
  20.         cyhal_pwm_set_duty_cycle(&pwm_led1_control, timer_count/10.0,10000);
  21.         cyhal_pwm_set_duty_cycle(&pwm_led2_control, (1000-timer_count)/10.0,10000);
  22.     }
  23.     else
  24.     {
  25.         cyhal_pwm_set_duty_cycle(&pwm_led1_control, (2000-timer_count)/10.0,10000);
  26.         cyhal_pwm_set_duty_cycle(&pwm_led2_control, (timer_count-1000)/10.0,10000);
  27.     }
  28.     rgb_pwm += 1;
  29.     if(rgb_pwm == 1000)
  30.     {
  31.         rgb_pwm = 0;
  32.         rgb_step += 1;
  33.         if(rgb_step > 2)
  34.             rgb_step = 0;
  35.     }
  36.     switch(rgb_step)
  37.     {
  38.     case 0:
  39.         cyhal_pwm_set_duty_cycle(&pwm_ledr_control, rgb_pwm/10.0,10000);
  40.         cyhal_pwm_set_duty_cycle(&pwm_ledg_control, 0,10000);
  41.         cyhal_pwm_set_duty_cycle(&pwm_ledb_control, (1000-rgb_pwm)/10.0,10000);
  42.         break;
  43.     case 1:
  44.         cyhal_pwm_set_duty_cycle(&pwm_ledg_control, rgb_pwm/10.0,10000);
  45.         cyhal_pwm_set_duty_cycle(&pwm_ledb_control, 0,10000);
  46.         cyhal_pwm_set_duty_cycle(&pwm_ledr_control, (1000-rgb_pwm)/10.0,10000);
  47.         break;
  48.     case 2:
  49.         cyhal_pwm_set_duty_cycle(&pwm_ledb_control, rgb_pwm/10.0,10000);
  50.         cyhal_pwm_set_duty_cycle(&pwm_ledr_control, 0,10000);
  51.         cyhal_pwm_set_duty_cycle(&pwm_ledg_control, (1000-rgb_pwm)/10.0,10000);
  52.         break;
  53.     }
  54. }
  55. void app_timer_init(void)
  56. {
  57.     cyhal_timer_init_cfg(&timer_obj, &tcpwm_0_group_1_cnt_0_hal_config);
  58.     cyhal_timer_register_callback(&timer_obj, isr_timer, NULL);
  59.     cyhal_timer_enable_event(&timer_obj, CYHAL_TIMER_IRQ_TERMINAL_COUNT,3, true);
  60.     cyhal_timer_start(&timer_obj);
  61. }
  62. void app_pwm_init(void)
  63. {
  64.     cyhal_pwm_init_cfg(&pwm_ledr_control,&tcpwm_0_group_0_cnt_1_hal_config);
  65.     cyhal_pwm_init_cfg(&pwm_ledg_control,&tcpwm_0_group_0_cnt_0_hal_config);
  66.     cyhal_pwm_init_cfg(&pwm_ledb_control,&tcpwm_0_group_1_cnt_1_hal_config);
  67.     cyhal_pwm_init_cfg(&pwm_led1_control,&tcpwm_0_group_1_cnt_3_hal_config);
  68.     cyhal_pwm_init_cfg(&pwm_led2_control,&tcpwm_0_group_1_cnt_5_hal_config);
  69.     cyhal_pwm_start(&pwm_led1_control);
  70.     cyhal_pwm_start(&pwm_led2_control);
  71.     cyhal_pwm_start(&pwm_ledr_control);
  72.     cyhal_pwm_start(&pwm_ledg_control);
  73.     cyhal_pwm_start(&pwm_ledb_control);
  74. }

最终效果


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
longguangyong 发表于 2024-6-21 06:01 来自手机 | 显示全部楼层
谢谢分享,
lidi911 发表于 2024-6-21 07:42 来自手机 | 显示全部楼层
用PWM控制LED灯呼吸,效果很nice。
一路向北lm 发表于 2024-6-21 08:16 | 显示全部楼层
用PWM控制LED灯呼吸,效果很nice,不知道能不能用keil
 楼主| yuyy1989 发表于 2024-6-21 08:44 | 显示全部楼层
本帖最后由 yuyy1989 于 2024-6-21 08:45 编辑
一路向北lm 发表于 2024-6-21 08:16
用PWM控制LED灯呼吸,效果很nice,不知道能不能用keil

可以,但是配置很麻烦,参考官方文档 Infineon-ModusToolbox_3.2_b_Keil_uVision_User_Guide-UserManual-v01_00-EN.pdf链接https://www.infineon.com/dgdl/Infineon-ModusToolbox_3.2_b_Keil_uVision_User_Guide-UserManual-v01_00-EN.pdf?fileId=8ac78c8c8d2fe47b018e0ea94b0e78f8
lulugl 发表于 2024-6-21 10:33 | 显示全部楼层
学习了学习了!
jf101 发表于 2024-6-28 11:10 | 显示全部楼层
很详细的配置文档
和下土 发表于 2024-6-30 16:42 | 显示全部楼层
过配置1个16位的定时器实现1ms的计时来学习TCPWM模块
pnews88 发表于 2024-7-22 15:54 | 显示全部楼层
源代码分享一下啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:同飞软件研发工程师
简介:制冷系统单片机软件开发,使用PID控制温度

161

主题

815

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部