[AIROC™ 蓝牙] 【英飞凌CYW20829测评】第5篇 基于TCPWM的方波采样实验

[复制链接]
 楼主| 小宏121 发表于 2024-7-17 23:46 | 显示全部楼层 |阅读模式
<
本帖最后由 小宏121 于 2024-7-17 23:48 编辑

这里是官方的输出PWM方波的例程地址
GitHub - Infineon/mtb-example-hal-pwm-square-wave: This code example generates a square wave using the TCPWM resource configured as a PWM. An LED connected to the PWM output pin blinks at 2 Hz.

Infineon-AN238557_-_AIROC_CYW20829_application_developer_guide-ApplicationNotes-v03_00-EN.pdf
开发指导手册里面有方波例程的链接


基于 "HAL_PWM_Square_Wave" 的例程的测试
可以看到LED灯0.5秒闪烁
例程的PWM频率和占空比配置

  1. /* PWM Frequency = 2Hz */
  2. #define PWM_FREQUENCY (2u)
  3. /* PWM Duty-cycle = 50% */

根据开发板的原理图可以知道,LED1的R42接到MCU的IO


使用线怼到R42,线的另一端接到示波器的采样探针

可以看到示波器的测量频率为2Hz,占空比为50%


现在修改代码的输出频率为20KHz,占空比为80%
现在LED灯看不出闪烁

  1. /* PWM Frequency = 2Hz */
  2. #define PWM_FREQUENCY (20000u)
  3. /* PWM Duty-cycle = 50% */
  4. #define PWM_DUTY_CYCLE (80.0f)

示波器的采样到的波形和修改后设置的差不多


HAL_PWM_Square_Wave的代码

  1. /*******************************************************************************
  2. * File Name:   main.c
  3. *
  4. * Description: This is the source code for the PWM square wave code example
  5. *              for ModusToolbox.
  6. *
  7. * Related Document: See README.md
  8. *
  9. ********************************************************************************
  10. * Copyright 2019-2024, Cypress Semiconductor Corporation (an Infineon company) or
  11. * an affiliate of Cypress Semiconductor Corporation.  All rights reserved.
  12. *
  13. * This software, including source code, documentation and related
  14. * materials ("Software") is owned by Cypress Semiconductor Corporation
  15. * or one of its affiliates ("Cypress") and is protected by and subject to
  16. * worldwide patent protection (United States and foreign),
  17. * United States copyright laws and international treaty provisions.
  18. * Therefore, you may use this Software only as provided in the license
  19. * agreement accompanying the software package from which you
  20. * obtained this Software ("EULA").
  21. * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
  22. * non-transferable license to copy, modify, and compile the Software
  23. * source code solely for use in connection with Cypress's
  24. * integrated circuit products.  Any reproduction, modification, translation,
  25. * compilation, or representation of this Software except as specified
  26. * above is prohibited without the express written permission of Cypress.
  27. *
  28. * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
  31. * reserves the right to make changes to the Software without notice. Cypress
  32. * does not assume any liability arising out of the application or use of the
  33. * Software or any product or circuit described in the Software. Cypress does
  34. * not authorize its products for use in any products where a malfunction or
  35. * failure of the Cypress product may reasonably be expected to result in
  36. * significant property damage, injury or death ("High Risk Product"). By
  37. * including Cypress's product in a High Risk Product, the manufacturer
  38. * of such system or application assumes all risk of such use and in doing
  39. * so agrees to indemnify Cypress against all liability.
  40. *******************************************************************************/

  41. /*******************************************************************************
  42. * Header Files
  43. *******************************************************************************/
  44. #include "cybsp.h"
  45. #include "cyhal.h"
  46. #include "cy_retarget_io.h"
  47. #include <inttypes.h>


  48. /*******************************************************************************
  49. * Macros
  50. *******************************************************************************/
  51. /* PWM Frequency = 2Hz */
  52. #define PWM_FREQUENCY (2u)
  53. /* PWM Duty-cycle = 50% */
  54. #define PWM_DUTY_CYCLE (50.0f)


  55. /*******************************************************************************
  56. * Global Variables
  57. *******************************************************************************/


  58. /*******************************************************************************
  59. * Function Prototypes
  60. *******************************************************************************/


  61. /*******************************************************************************
  62. * Function Definitions
  63. *******************************************************************************/

  64. /*******************************************************************************
  65. * Function Name: handle_error
  66. ********************************************************************************
  67. * Summary:
  68. *  User defined error handling function.
  69. *
  70. * Parameters:
  71. *  status - status for evaluation.
  72. *
  73. * Return:
  74. *  void
  75. *
  76. *******************************************************************************/
  77. void handle_error(cy_rslt_t status)
  78. {
  79.     if (CY_RSLT_SUCCESS != status)
  80.     {
  81.         /* Halt the CPU while debugging */
  82.         CY_ASSERT(0);
  83.     }
  84. }


  85. /*******************************************************************************
  86. * Function Name: check_status
  87. ********************************************************************************
  88. * Summary:
  89. *  Prints the message and waits forever when an error occurs.
  90. *
  91. * Parameters:
  92. *  message - message to print if status is non-zero.
  93. *  status - status for evaluation.
  94. *
  95. * Return:
  96. *  void
  97. *
  98. *******************************************************************************/
  99. void check_status(char *message, cy_rslt_t status)
  100. {
  101.     if (CY_RSLT_SUCCESS != status)
  102.     {
  103.         printf("\r\n=====================================================\r\n");
  104.         printf("\nFAIL: %s\r\n", message);
  105.         printf("Error Code: 0x%08" PRIX32 "\n", status);
  106.         printf("\r\n=====================================================\r\n");

  107.         while(true);
  108.     }
  109. }


  110. /*******************************************************************************
  111. * Function Name: main
  112. ********************************************************************************
  113. * Summary:
  114. * This is the main function for the CPU. It configures the PWM and puts the CPU
  115. * in Sleep mode to save power.
  116. *
  117. * Parameters:
  118. *  void
  119. *
  120. * Return:
  121. *  int
  122. *
  123. *******************************************************************************/
  124. int main(void)
  125. {
  126.     /* PWM object */
  127.     cyhal_pwm_t pwm_led_control;
  128.     /* API return code */
  129.     cy_rslt_t result;

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

  137.     /* Initialize the device and board peripherals */
  138.     result = cybsp_init();
  139.     handle_error(result);

  140.     /* Enable global interrupts */
  141.     __enable_irq();

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

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

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

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

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

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

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

  164.     for (;;)
  165.     {
  166.         /* Put the CPU into sleep mode to save power */
  167.         cyhal_syspm_sleep();
  168.     }
  169. }


  170. /* [] END OF FILE */



本帖子中包含更多资源

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

×
ym0sly 发表于 2024-7-30 11:07 | 显示全部楼层
根据开发板的原理图可以知道,LED1的R42接到MCU的IO
梵蒂冈是神uy 发表于 2024-7-30 17:29 | 显示全部楼层
基于 "HAL_PWM_Square_Wave" 的例程的测试
盗铃何须掩耳 发表于 2024-7-31 14:16 | 显示全部楼层
这是PWM输出实验啊
我想看大海 发表于 2024-7-31 14:29 | 显示全部楼层
我以为是MCU采样PWM,原来是示波器采样
原来是wjc 发表于 2025-2-28 23:23 | 显示全部楼层
PWM的输出通常是一个方波信号,通过调整占空比和频率来控制输出信号的特性。
g36xcv 发表于 2025-3-25 15:57 | 显示全部楼层
通过Infineon硬件平台来生成PWM信号,这是控制外设非常常见的操作方式。
小夏天的大西瓜 发表于 2025-3-25 22:52 | 显示全部楼层
PWM信号常见的测试
小小蚂蚁举千斤 发表于 2025-3-26 23:35 | 显示全部楼层
基于TCPWM的方波采样
星辰大海不退缩 发表于 2025-3-27 16:51 | 显示全部楼层
方波处理是如何实现的?
suncat0504 发表于 2025-3-27 22:25 | 显示全部楼层
测试这种波,还是得需要外部测试设备
suncat0504 发表于 2025-3-27 22:25 | 显示全部楼层
在铁片上焊接导线,需要有功底才行,
suncat0504 发表于 2025-3-27 22:27 | 显示全部楼层
为啥不通过软件,改变输出GPIO呢?这可比焊接短接导线靠谱。
LOVEEVER 发表于 2025-3-27 22:39 | 显示全部楼层
方波具体是如何发生的额?
呐咯密密 发表于 2025-3-28 16:47 | 显示全部楼层
这是用什么外设采集的,定时器吗
szt1993 发表于 2025-3-29 22:05 | 显示全部楼层
示波器的采样过程
lxs0026 发表于 2025-4-25 18:25 | 显示全部楼层
首先要配置PWM的时钟和GPIO管脚,确保PWM信号可以输出。PWM资源一般会通过定时器(例如TCPWM)来实现。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

21

帖子

0

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