打印
[ModusToolbox™]

【英飞凌CYW20829测评】篇3 PWM呼吸灯测试

[复制链接]
365|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
    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()函数启动定时器。
    • <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>

    • void check_status(char *message, cy_rslt_t status)
      {
          if (CY_RSLT_SUCCESS != status)
          {
              printf("\r\n=====================================================\r\n");
              printf("\nFAIL: %s\r\n", message);
              printf("Error Code: 0x%08" PRIX32 "\n", status);
              printf("\r\n=====================================================\r\n");

              while(true);
          }
      }
      int main(void)
      {
          /* PWM object */
          cyhal_pwm_t pwm_led_control;
          /* API return code */
          cy_rslt_t result;

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

          /* Initialize the device and board peripherals */
          result = cybsp_init();
          handle_error(result);

          /* Enable global interrupts */
          __enable_irq();

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

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

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

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

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

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

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

          for (;;)
          {
              /* Put the CPU into sleep mode to save power */
              cyhal_syspm_sleep();
          }
      }



5.png (443.32 KB )

5.png

使用特权

评论回复
沙发
逢dududu必shu| | 2024-8-17 00:59 | 只看该作者
“Peripheral Driver”配置工具,选择PWM。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

73

主题

138

帖子

3

粉丝