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


* 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 */

查看示波器的效果
 


调整占空比和频率,可以实时改变方波的效果。。
---------------------
作者:单片小菜
链接:https://bbs.21ic.com/icview-3392568-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值