[AIROC™ 蓝牙] 【英飞凌CYW20829测评】TCPWM的方波采样

[复制链接]
690|0
 楼主| 单片小菜 发表于 2024-7-27 07:41 | 显示全部楼层 |阅读模式
  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 */
查看示波器的效果


调整占空比和频率,可以实时改变方波的效果。

本帖子中包含更多资源

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

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

本版积分规则

认证:资深专家
简介:丰富的嵌入式软硬件开发管理经验; 丰富的项目管理经验并具备敏锐的市场嗅觉; 丰富的产品的供应链资源及工厂管控能力; 具备很强的产品落地经验(从产品企划到产品量产);

107

主题

2354

帖子

10

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