[AIROC™ 蓝牙] 【英飞凌CYW20829测评】-4-PWM呼吸灯

[复制链接]
 楼主| 南来之风 发表于 2024-7-20 08:31 | 显示全部楼层 |阅读模式
<
本帖最后由 南来之风 于 2024-7-20 15:50 编辑

开发板上有两个用户LED,丝印分别是USER LED1和USER LED2,电路原理图如下:


本次实验计划使用PWM内部连接到USER LED2,实现呼吸灯效果;在定时器内通过GPIO正常驱动USER LED1,周期2s。



接着通过BSP Configurators -> Device Configurator 4.20进行外设配置和引脚等配置界面。

配置USER LED2内部连接到TCPWM信号:


时钟分配:


PWM参数配置:


点击SAVE后,自动检测配置是否有错误,如果没有错误,自动生成代码。

  1. const cy_stc_tcpwm_pwm_config_t tcpwm_0_group_1_cnt_5_config =
  2. {
  3.     .pwmMode = CY_TCPWM_PWM_MODE_PWM,
  4.     .clockPrescaler = CY_TCPWM_PWM_PRESCALER_DIVBY_1,
  5.     .pwmAlignment = CY_TCPWM_PWM_LEFT_ALIGN,
  6.     .deadTimeClocks = 0,
  7.     .runMode = CY_TCPWM_PWM_CONTINUOUS,
  8.     .period0 = 32768,
  9.     .period1 = 32768,
  10.     .enablePeriodSwap = false,
  11.     .compare0 = 16384,
  12.     .compare1 = 16384,
  13.     .enableCompareSwap = false,
  14.     .interruptSources = (CY_TCPWM_INT_ON_TC & 0U) | (CY_TCPWM_INT_ON_CC0 & 0U) | (CY_TCPWM_INT_ON_CC1 & 0U),
  15.     .invertPWMOut = CY_TCPWM_PWM_INVERT_DISABLE,
  16.     .invertPWMOutN = CY_TCPWM_PWM_INVERT_DISABLE,
  17.     .killMode = CY_TCPWM_PWM_STOP_ON_KILL,
  18.     .swapInputMode = tcpwm_0_group_1_cnt_5_INPUT_DISABLED & 0x3U,
  19.     .swapInput = CY_TCPWM_INPUT_0,
  20.     .reloadInputMode = tcpwm_0_group_1_cnt_5_INPUT_DISABLED & 0x3U,
  21.     .reloadInput = CY_TCPWM_INPUT_0,
  22.     .startInputMode = tcpwm_0_group_1_cnt_5_INPUT_DISABLED & 0x3U,
  23.     .startInput = CY_TCPWM_INPUT_0,
  24.     .killInputMode = tcpwm_0_group_1_cnt_5_INPUT_DISABLED & 0x3U,
  25.     .killInput = CY_TCPWM_INPUT_0,
  26.     .countInputMode = tcpwm_0_group_1_cnt_5_INPUT_DISABLED & 0x3U,
  27.     .countInput = CY_TCPWM_INPUT_1,
  28.     .swapOverflowUnderflow = false,
  29.     .immediateKill = false,
  30.     .tapsEnabled = 45,
  31.     .compare2 = 16384,
  32.     .compare3 = 16384,
  33.     .enableCompare1Swap = false,
  34.     .compare0MatchUp = true,
  35.     .compare0MatchDown = false,
  36.     .compare1MatchUp = true,
  37.     .compare1MatchDown = false,
  38.     .kill1InputMode = tcpwm_0_group_1_cnt_5_INPUT_DISABLED & 0x3U,
  39.     .kill1Input = CY_TCPWM_INPUT_0,
  40.     .pwmOnDisable = CY_TCPWM_PWM_OUTPUT_HIGHZ,
  41.     .trigger0Event = CY_TCPWM_CNT_TRIGGER_ON_DISABLED,
  42.     .trigger1Event = CY_TCPWM_CNT_TRIGGER_ON_DISABLED,
  43.     .reloadLineSelect = false,
  44.     .line_out_sel = CY_TCPWM_OUTPUT_PWM_SIGNAL,
  45.     .linecompl_out_sel = CY_TCPWM_OUTPUT_INVERTED_PWM_SIGNAL,
  46.     .line_out_sel_buff = CY_TCPWM_OUTPUT_PWM_SIGNAL,
  47.     .linecompl_out_sel_buff = CY_TCPWM_OUTPUT_INVERTED_PWM_SIGNAL,
  48.     .deadTimeClocks_linecompl_out = 0,
  49. #if defined (CY_IP_MXS40TCPWM)
  50.     .hrpwm_enable = false,
  51.     .hrpwm_input_freq = CY_TCPWM_HRPWM_FREQ_80MHZ_OR_100MHZ,
  52.     .kill_line_polarity = CY_TCPWM_LINEOUT_AND_LINECMPOUT_IS_LOW,
  53.     .deadTimeClocksBuff = 0,
  54.     .deadTimeClocksBuff_linecompl_out = 0,
  55.     .buffer_swap_enable = false,
  56.     .glitch_filter_enable = false,
  57.     .gf_depth = CY_GLITCH_FILTER_DEPTH_SUPPORT_VALUE_0,
  58.     .dithering_mode = CY_TCPWM_DITHERING_DISABLE,
  59.     .period_dithering_value = 128,
  60.     .duty_dithering_value = 128,
  61.     .limiter = CY_TCPWM_DITHERING_LIMITER_7,
  62. #endif /* defined (CY_IP_MXS40TCPWM) */
  63. };
接下来在主函数中进行相关初始化与应用程序编写:
首先是定时器设定为1ms一次中断
  1. void timer_init(void)
  2. {
  3.     cy_rslt_t result;

  4.     const cyhal_timer_cfg_t led_blink_timer_cfg =
  5.     {
  6.         .compare_value = 0,                 /* Timer compare value, not used */
  7.         .period = LED_BLINK_TIMER_PERIOD,   /* Defines the timer period */
  8.         .direction = CYHAL_TIMER_DIR_UP,    /* Timer counts up */
  9.         .is_compare = false,                /* Don't use compare mode */
  10.         .is_continuous = true,              /* Run timer indefinitely */
  11.         .value = 0                          /* Initial value of counter */
  12.     };

  13.     /* Initialize the timer object. Does not use input pin ('pin' is NC) and
  14.      * does not use a pre-configured clock source ('clk' is NULL). */
  15.     result = cyhal_timer_init(&led_blink_timer, NC, NULL);

  16.     /* timer init failed. Stop program execution */
  17.     if (result != CY_RSLT_SUCCESS)
  18.     {
  19.         CY_ASSERT(0);
  20.     }

  21.     /* Configure timer period and operation mode such as count direction,
  22.        duration */
  23.     cyhal_timer_configure(&led_blink_timer, &led_blink_timer_cfg);

  24.     /* Set the frequency of timer's clock source */
  25.     cyhal_timer_set_frequency(&led_blink_timer, LED_BLINK_TIMER_CLOCK_HZ);

  26.     /* Assign the ISR to execute on timer interrupt */
  27.     cyhal_timer_register_callback(&led_blink_timer, isr_timer, NULL);

  28.     /* Set the event on which timer interrupt occurs and enable it */
  29.     cyhal_timer_enable_event(&led_blink_timer, CYHAL_TIMER_IRQ_TERMINAL_COUNT,
  30.                               7, true);

  31.     /* Start the timer with the configured settings */
  32.     cyhal_timer_start(&led_blink_timer);
  33. }


接下来进行pwm初始化:
  1. static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
  2. {
  3.     (void)callback_arg;
  4.     (void)event;

  5.     if(timer_count > 0)
  6.         timer_count -= 1;
  7.     if(timer_count == 0)
  8.     {
  9.         timer_count = 2000;
  10.         cyhal_gpio_toggle(CYBSP_USER_LED1);
  11.     }
  12.     if(timer_count < 1000)
  13.     {
  14.             cyhal_pwm_set_duty_cycle(&pwm_led2_control,timer_count/10.0,10000);
  15.     }
  16.     else {
  17.             cyhal_pwm_set_duty_cycle(&pwm_led2_control, (2000-timer_count)/10.0,10000);
  18.     }
  19. }


最终效果如下:



本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

290

帖子

2

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