[AIROC™ 蓝牙] 【英飞凌CYW20829测评】+ 自制呼吸小灯

[复制链接]
 楼主| lishuihua 发表于 2024-7-21 17:41 | 显示全部楼层 |阅读模式
本帖最后由 lishuihua 于 2024-7-21 16:54 编辑

基本原理是通过改变PWM占空比来实现小灯从暗到亮变化,即呼吸效果。

在上一篇测评的基础上,增加一个TCPWM配置,配置CLOCK SIGNAL为16bit Divider 0 clik,配置PWM Mode为PWM模式,输出PWM到P5[2],即小灯LED4位置。



完整代码如下:
  1. #include "cyhal.h"
  2. #include "cybsp.h"
  3. #include "cy_retarget_io.h"


  4. /*******************************************************************************
  5. * Macros
  6. *******************************************************************************/
  7. /* LED blink timer clock value in Hz  */
  8. #define LED_BLINK_TIMER_CLOCK_HZ          (10000)

  9. /* LED blink timer period value */
  10. #define LED_BLINK_TIMER_PERIOD            (9)


  11. /*******************************************************************************
  12. * Global Variables
  13. *******************************************************************************/
  14. bool timer_interrupt_flag = false;
  15. bool led_blink_active_flag = true;

  16. /* Variable for storing character read from terminal */
  17. uint8_t uart_read_value;

  18. /* Timer object used for blinking the LED */
  19. cyhal_timer_t led_blink_timer;

  20. /* variables related to pwm*/
  21. uint16_t timer_count = 2000;
  22. cyhal_pwm_t pwm_led2_control;

  23. /*******************************************************************************
  24. * Function Prototypes
  25. *******************************************************************************/
  26. void timer_init(void);
  27. static void isr_timer(void *callback_arg, cyhal_timer_event_t event);

  28. /*******************************************************************************
  29. * Function Name: main
  30. ********************************************************************************
  31. * Summary:
  32. * This is the main function. It sets up a timer to trigger a periodic interrupt.
  33. * The main while loop checks for the status of a flag set by the interrupt and
  34. * toggles an LED at 1Hz to create an LED blinky. Will be achieving the 1Hz Blink
  35. * rate based on the The LED_BLINK_TIMER_CLOCK_HZ and LED_BLINK_TIMER_PERIOD
  36. * Macros,i.e. (LED_BLINK_TIMER_PERIOD + 1) / LED_BLINK_TIMER_CLOCK_HZ = X ,Here,
  37. * X denotes the desired blink rate. The while loop also checks whether the
  38. * 'Enter' key was pressed and stops/restarts LED blinking.
  39. *
  40. * Parameters:
  41. *  none
  42. *
  43. * Return:
  44. *  int
  45. *
  46. *******************************************************************************/
  47. int main(void)
  48. {
  49.     cy_rslt_t result;

  50. #if defined (CY_DEVICE_SECURE)
  51.     cyhal_wdt_t wdt_obj;

  52.     /* Clear watchdog timer so that it doesn't trigger a reset */
  53.     result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
  54.     CY_ASSERT(CY_RSLT_SUCCESS == result);
  55.     cyhal_wdt_free(&wdt_obj);
  56. #endif /* #if defined (CY_DEVICE_SECURE) */

  57.     /* Initialize the device and board peripherals */
  58.     result = cybsp_init();

  59.     /* Board init failed. Stop program execution */
  60.     if (result != CY_RSLT_SUCCESS)
  61.     {
  62.         CY_ASSERT(0);
  63.     }

  64.     /* Enable global interrupts */
  65.     __enable_irq();

  66.     /* Initialize retarget-io to use the debug UART port */
  67.     result = cy_retarget_io_init_fc(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
  68.             CYBSP_DEBUG_UART_CTS,CYBSP_DEBUG_UART_RTS,CY_RETARGET_IO_BAUDRATE);

  69.     /* retarget-io init failed. Stop program execution */
  70.     if (result != CY_RSLT_SUCCESS)
  71.     {
  72.         CY_ASSERT(0);
  73.     }

  74.     /* Initialize the User LED */
  75.     init_cycfg_pins();
  76.     result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,
  77.                              CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);

  78.     /* GPIO init failed. Stop program execution */
  79.     if (result != CY_RSLT_SUCCESS)
  80.     {
  81.         CY_ASSERT(0);
  82.     }

  83.     /* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
  84.     printf("\x1b[2J\x1b[;H");

  85.     printf("****************** "
  86.            "HAL: Hello World! Example "
  87.            "****************** \r\n\n");

  88.     printf("Hello World!!!\r\n\n");
  89.     printf("For more projects, "
  90.            "visit our code examples repositories:\r\n\n");

  91.     printf("https://github.com/Infineon/"
  92.            "Code-Examples-for-ModusToolbox-Software\r\n\n");

  93.     /* Initialize timer to toggle the LED */
  94.     timer_init();
  95.     pwm_init();

  96.     printf("LED Breathing.\r\n");

  97.     while(1);

  98. #if 0
  99.     for (;;)
  100.     {
  101.         /* Check if 'Enter' key was pressed */
  102.         if (cyhal_uart_getc(&cy_retarget_io_uart_obj, &uart_read_value, 1)
  103.              == CY_RSLT_SUCCESS)
  104.         {
  105.             if (uart_read_value == '\r')
  106.             {
  107.                 /* Pause LED blinking by stopping the timer */
  108.                 if (led_blink_active_flag)
  109.                 {
  110.                     cyhal_timer_stop(&led_blink_timer);

  111.                     printf("LED blinking paused \r\n");
  112.                 }
  113.                 else /* Resume LED blinking by starting the timer */
  114.                 {
  115.                     cyhal_timer_start(&led_blink_timer);

  116.                     printf("LED blinking resumed\r\n");
  117.                 }

  118.                 /* Move cursor to previous line */
  119.                 printf("\x1b[1F");

  120.                 led_blink_active_flag ^= 1;
  121.             }
  122.         }
  123.         /* Check if timer elapsed (interrupt fired) and toggle the LED */
  124.         if (timer_interrupt_flag)
  125.         {
  126.             /* Clear the flag */
  127.             timer_interrupt_flag = false;

  128.             /* Invert the USER LED state */
  129.             //cyhal_gpio_toggle(CYBSP_USER_LED);
  130.         }
  131.     }
  132. #endif

  133. }

  134. /*******************************************************************************
  135. * Function Name: isr_timer
  136. ********************************************************************************
  137. * Summary:
  138. * This is the interrupt handler function for the timer interrupt.
  139. *
  140. * Parameters:
  141. *    callback_arg    Arguments passed to the interrupt callback
  142. *    event            Timer/counter interrupt triggers
  143. *
  144. * Return:
  145. *  void
  146. *******************************************************************************/
  147. static void isr_timer1(void *callback_arg, cyhal_timer_event_t event)
  148. {
  149.     (void) callback_arg;
  150.     (void) event;

  151.     /* Set the interrupt flag and process it from the main while(1) loop */
  152.     timer_interrupt_flag = true;
  153. }

  154. static void isr_timer(void* callback_arg, cyhal_timer_event_t event)
  155. {
  156.     (void)callback_arg;
  157.     (void)event;

  158.     if(timer_count > 0)
  159.         timer_count -= 1;
  160.     if(timer_count == 0)
  161.     {
  162.         timer_count = 2000;
  163.         //cyhal_gpio_toggle(CYBSP_USER_LED1);
  164.     }
  165.     if(timer_count < 1000)
  166.     {
  167.             cyhal_pwm_set_duty_cycle(&pwm_led2_control,timer_count/10.0,10000);
  168.     }
  169.     else {
  170.             cyhal_pwm_set_duty_cycle(&pwm_led2_control, (2000-timer_count)/10.0,10000);
  171.     }
  172. }

  173. /*******************************************************************************
  174. * Function Name: timer_init
  175. ********************************************************************************
  176. * Summary:
  177. * This function creates and configures a Timer object. The timer ticks
  178. * continuously and produces a periodic interrupt on every terminal count
  179. * event. The period is defined by the 'period' and 'compare_value' of the
  180. * timer configuration structure 'led_blink_timer_cfg'. Without any changes,
  181. * this application is designed to produce an interrupt every 1 second.
  182. *
  183. * Parameters:
  184. *  none
  185. *
  186. * Return :
  187. *  void
  188. *
  189. *******************************************************************************/
  190. void timer_init(void)
  191. {
  192.     cy_rslt_t result;

  193.     const cyhal_timer_cfg_t led_blink_timer_cfg =
  194.     {
  195.         .compare_value = 0,                 /* Timer compare value, not used */
  196.         .period = LED_BLINK_TIMER_PERIOD,   /* Defines the timer period */
  197.         .direction = CYHAL_TIMER_DIR_UP,    /* Timer counts up */
  198.         .is_compare = false,                /* Don't use compare mode */
  199.         .is_continuous = true,              /* Run timer indefinitely */
  200.         .value = 0                          /* Initial value of counter */
  201.     };

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

  205.     /* timer init failed. Stop program execution */
  206.     if (result != CY_RSLT_SUCCESS)
  207.     {
  208.         CY_ASSERT(0);
  209.     }

  210.     /* Configure timer period and operation mode such as count direction,
  211.        duration */
  212.     cyhal_timer_configure(&led_blink_timer, &led_blink_timer_cfg);

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

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

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

  220.     /* Start the timer with the configured settings */
  221.     cyhal_timer_start(&led_blink_timer);
  222. }

  223. void pwm_init(void)
  224. {
  225.      cyhal_pwm_init_cfg(&pwm_led2_control,&tcpwm_0_group_1_cnt_6_hal_config);
  226.      cyhal_pwm_start(&pwm_led2_control);
  227. }



  228. /* [] END OF FILE */
复制代码


本帖子中包含更多资源

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

×
为你转身 发表于 2024-9-30 13:14 来自手机 | 显示全部楼层
在上一篇测评的基础上,增加一个TCPWM配置
chenqianqian 发表于 2024-10-16 08:13 来自手机 | 显示全部楼层
弄一个RGB灯比格一下就提升上去了
szt1993 发表于 2024-10-22 15:52 | 显示全部楼层
改变PWM占空比来实现小灯从暗到亮变化的呼吸效果
sj8zw8 发表于 2025-4-30 23:05 | 显示全部楼层
通过改变PWM占空比,LED的亮度会逐渐变化,从而达到类似呼吸灯效果。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

31

帖子

0

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