本帖最后由 小宏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频率和占空比配置
- /* PWM Frequency = 2Hz */
- #define PWM_FREQUENCY (2u)
- /* PWM Duty-cycle = 50% */
根据开发板的原理图可以知道,LED1的R42接到MCU的IO
使用线怼到R42,线的另一端接到示波器的采样探针
可以看到示波器的测量频率为2Hz,占空比为50%
现在修改代码的输出频率为20KHz,占空比为80%
现在LED灯看不出闪烁
- /* PWM Frequency = 2Hz */
- #define PWM_FREQUENCY (20000u)
- /* PWM Duty-cycle = 50% */
- #define PWM_DUTY_CYCLE (80.0f)
示波器的采样到的波形和修改后设置的差不多
HAL_PWM_Square_Wave的代码
- /*******************************************************************************
- * File Name: main.c
- *
- * Description: This is the source code for the PWM square wave code example
- * for ModusToolbox.
- *
- * Related Document: See README.md
- *
- ********************************************************************************
- * Copyright 2019-2024, Cypress Semiconductor Corporation (an Infineon company) or
- * an affiliate of Cypress Semiconductor Corporation. All rights reserved.
- *
- * This software, including source code, documentation and related
- * materials ("Software") is owned by Cypress Semiconductor Corporation
- * or one of its affiliates ("Cypress") and is protected by and subject to
- * worldwide patent protection (United States and foreign),
- * United States copyright laws and international treaty provisions.
- * Therefore, you may use this Software only as provided in the license
- * agreement accompanying the software package from which you
- * obtained this Software ("EULA").
- * If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
- * non-transferable license to copy, modify, and compile the Software
- * source code solely for use in connection with Cypress's
- * integrated circuit products. Any reproduction, modification, translation,
- * compilation, or representation of this Software except as specified
- * above is prohibited without the express written permission of Cypress.
- *
- * Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
- * reserves the right to make changes to the Software without notice. Cypress
- * does not assume any liability arising out of the application or use of the
- * Software or any product or circuit described in the Software. Cypress does
- * not authorize its products for use in any products where a malfunction or
- * failure of the Cypress product may reasonably be expected to result in
- * significant property damage, injury or death ("High Risk Product"). By
- * including Cypress's product in a High Risk Product, the manufacturer
- * of such system or application assumes all risk of such use and in doing
- * so agrees to indemnify Cypress against all liability.
- *******************************************************************************/
- /*******************************************************************************
- * Header Files
- *******************************************************************************/
- #include "cybsp.h"
- #include "cyhal.h"
- #include "cy_retarget_io.h"
- #include <inttypes.h>
- /*******************************************************************************
- * Macros
- *******************************************************************************/
- /* PWM Frequency = 2Hz */
- #define PWM_FREQUENCY (2u)
- /* PWM Duty-cycle = 50% */
- #define PWM_DUTY_CYCLE (50.0f)
- /*******************************************************************************
- * Global Variables
- *******************************************************************************/
- /*******************************************************************************
- * Function Prototypes
- *******************************************************************************/
- /*******************************************************************************
- * Function Definitions
- *******************************************************************************/
- /*******************************************************************************
- * Function Name: handle_error
- ********************************************************************************
- * Summary:
- * User defined error handling function.
- *
- * Parameters:
- * status - status for evaluation.
- *
- * Return:
- * void
- *
- *******************************************************************************/
- void handle_error(cy_rslt_t status)
- {
- if (CY_RSLT_SUCCESS != status)
- {
- /* Halt the CPU while debugging */
- CY_ASSERT(0);
- }
- }
- /*******************************************************************************
- * Function Name: check_status
- ********************************************************************************
- * Summary:
- * Prints the message and waits forever when an error occurs.
- *
- * Parameters:
- * message - message to print if status is non-zero.
- * status - status for evaluation.
- *
- * Return:
- * void
- *
- *******************************************************************************/
- 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);
- }
- }
- /*******************************************************************************
- * Function Name: main
- ********************************************************************************
- * Summary:
- * This is the main function for the CPU. It configures the PWM and puts the CPU
- * in Sleep mode to save power.
- *
- * Parameters:
- * void
- *
- * Return:
- * int
- *
- *******************************************************************************/
- 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();
- }
- }
- /* [] END OF FILE */
|