[ModusToolbox™] 【英飞凌CYW20829测评】篇3 PWM呼吸灯测试

[复制链接]
911|1
 楼主| abner_ma 发表于 2024-7-22 20:11 | 显示全部楼层 |阅读模式
    CYW20829的PWM(脉冲宽度调制)配置是一个涉及硬件和软件设置的过程,旨在通过调整PWM信号的占空比来控制LED或其他外设的亮度或功率。
一、软件环境准备
  • 开发工具:使用Eclipse IDE for Modustoolbox soffware 作为开发工具,这是英飞凌官方推荐的开发环境。



  • 软件安装:从英飞凌官网下载并安装ModusToolbox,同时确保安装了适用于CYW20829的SDK和相关工具链。





二、项目创建与配置
  • 新建项目:在ModusToolbox中创建一个新的项目,并输入工程名称,选择对应的芯片型号(CYW20829)。
  • 硬件配置
    • 使用Device Configurator(设备配置工具)来配置硬件资源,包括GPIO、PWM和Timer等。
    • 将需要控制的LED(如CYBSP_USER_LED1)配置为PWM输出,用于实现呼吸灯效果。
    • 如有需要,将其他LED(如CYBSP_USER_LED2)配置为普通GPIO输出,用于周期闪烁或其他用途。


三、PWM配置
  • 外设配置
    • 在外设配置窗口中,设置PWM的相关参数,包括时钟源、分辨率(通常是一个较大的数,如10000,用于更精细地控制占空比)等。
    • 确保TCPWM(定时器/计数器/PWM)模块被正确配置并启用。

  • PWM结构体声明
    • 在代码中声明PWM结构体变量,如cyhal_pwm_t pwm_led1_control;,用于控制特定的LED。
  • PWM初始化
    • 使用cyhal_pwm_init_cfg()函数初始化PWM,并传入相应的配置参数和硬件抽象层(HAL)配置。
    • 调用cyhal_pwm_start()函数启动PWM。


四、定时器配置
  • 定时器结构体声明
    • 声明定时器结构体变量,如cyhal_timer_t timer_obj;,并定义一个计数器变量(如uint16_t timer_count = 2000;)用于控制PWM占空比的变化周期。
  • 定时器初始化
    • 使用cyhal_timer_init_cfg()函数初始化定时器,并配置其回调函数(ISR,中断服务例程)。
    • 在回调函数中,根据计数器的值调整PWM的占空比,并在必要时重置计数器或切换其他LED的状态。
  • 定时器启动
    • 调用cyhal_timer_start()函数启动定时器。
      1. <div style="background-color: rgb(255, 255, 255); padding-left: 2px;"><div style="color: rgb(0, 0, 0); font-family: Consolas; font-size: 10pt; white-space: pre;"><p><span style="color:#7f0055;font-weight:bold;">void</span> <span style="font-weight: bold;">handle_error</span>(<span style="color:#005032;">cy_rslt_t</span> status)</p><p>{</p><p>    <span style="color:#7f0055;font-weight:bold;">if</span> (CY_RSLT_SUCCESS != status)</p><p>    {</p><p>        <span style="color:#3f7f5f;">/* Halt the CPU while debugging */</span></p><p>        CY_ASSERT(0);</p><p>    }</p><p>}</p></div></div>

      1. void check_status(char *message, cy_rslt_t status)
      2. {
      3.     if (CY_RSLT_SUCCESS != status)
      4.     {
      5.         printf("\r\n=====================================================\r\n");
      6.         printf("\nFAIL: %s\r\n", message);
      7.         printf("Error Code: 0x%08" PRIX32 "\n", status);
      8.         printf("\r\n=====================================================\r\n");

      9.         while(true);
      10.     }
      11. }
      12. int main(void)
      13. {
      14.     /* PWM object */
      15.     cyhal_pwm_t pwm_led_control;
      16.     /* API return code */
      17.     cy_rslt_t result;

      18. #if defined(CY_DEVICE_SECURE)
      19.     cyhal_wdt_t wdt_obj;
      20.     /* Clear watchdog timer so that it doesn't trigger a reset */
      21.     result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
      22.     CY_ASSERT(CY_RSLT_SUCCESS == result);
      23.     cyhal_wdt_free(&wdt_obj);
      24. #endif

      25.     /* Initialize the device and board peripherals */
      26.     result = cybsp_init();
      27.     handle_error(result);

      28.     /* Enable global interrupts */
      29.     __enable_irq();

      30.     /* Initialize the retarget-io to use the debug UART port */
      31.     result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
      32.                                  CY_RETARGET_IO_BAUDRATE);
      33.     handle_error(result);

      34.     /* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
      35.     printf("\x1b[2J\x1b[;H");
      36.     printf("****************** "
      37.            "HAL: PWM square wave "
      38.            "****************** \r\n\n");

      39.     /* In this example, PWM output is routed to the user LED on the kit.
      40.        See HAL API Reference document for API details. */

      41.     /* Initialize the PWM */
      42.     result = cyhal_pwm_init(&pwm_led_control, CYBSP_USER_LED, NULL);
      43.     check_status("API cyhal_pwm_init failed with error code", result);

      44.     /* Set the PWM output frequency and duty cycle */
      45.     result = cyhal_pwm_set_duty_cycle(&pwm_led_control, PWM_DUTY_CYCLE,
      46.                                       PWM_FREQUENCY);
      47.     check_status("API cyhal_pwm_set_duty_cycle failed with error code", result);

      48.     /* Start the PWM */
      49.     result = cyhal_pwm_start(&pwm_led_control);
      50.     check_status("API cyhal_pwm_start failed with error code", result);

      51.     printf("PWM started successfully. Entering the sleep mode...\r\n");

      52.     for (;;)
      53.     {
      54.         /* Put the CPU into sleep mode to save power */
      55.         cyhal_syspm_sleep();
      56.     }
      57. }



本帖子中包含更多资源

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

×
逢dududu必shu 发表于 2024-8-17 00:59 | 显示全部楼层
“Peripheral Driver”配置工具,选择PWM。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:项目经理
简介:资深嵌入式开发工程师

104

主题

191

帖子

3

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