- 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);
- }
最终效果