PWM Normal and Complementary Output

This sample code guide is designed to help users easily and comprehensively understand PWM sample. This sample demonstrates PWM output, PWM complementary output, and deadzone.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_0 of EVB to channel 0 of the logic analyzer, connect P0_2 of EVB to channel 1 of the logic analyzer, and connect P0_3 of EVB to channel 2 of the logic analyzer.

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define PWM_OUT_PIN              ADC_0

    • #define PWM_OUT_PIN_P            ADC_2

    • #define PWM_OUT_PIN_N            ADC_3

  2. The following macros can be configured to modify the high level time and low level time of PWM.

    • #define PWM_HIGH_LEVEL_CNT                      2000

    • #define PWM_LOW_LEVEL_CNT                       2000

  3. The following macros can be configured to modify deadzone time.

    • #define PWM_DEADZONE_SIZE_CNT                   0x10

  4. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    pwm_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

Press the Reset button on the EVB, the waveforms displayed by channels 0/1/2 of the logic analyzer are shown the figure below.

../../../_images/PWM_Demo_Expected_Result_Diagram.png

PWM Sample Code Expected Result Diagram

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\tim\pwm\pwm_demo.c.

Initialization

Normal Output

  1. Call pwm_create() to create a PWM with a high level time of 2ms and a low level time of 2ms.

  2. Call pwm_pin_config() to configure PWM output pin.

  3. Call pwm_start() to start the PWM.

Complementary Output and Deadzone

  1. Call pwm_create() to create a PWM with a high level time of 2ms, a low level time of 2ms and deadzone enabled.

  2. Define the T_PWM_CONFIG type demo_pwm_deadzone_para, and modify the demo_pwm_deadzone_para parameters. The PWM parameters configuration is shown in the table below.

PWM Parameters

PWM Hardware Parameters

Setting in the demo_pwm_deadzone_para

PWM

PWM High Count

_T_HW_TIMER_PWM::pwm_high_count

2000

PWM Low Count

_T_HW_TIMER_PWM::pwm_low_count

2000

PWM Deadzone Enable

_T_HW_TIMER_PWM::pwm_deadzone_enable

FunctionalState::ENABLE

PWM Clock Source

_T_HW_TIMER_PWM::clock_source

_T_PWM_CLOCK_SOURCE::PWM_CLOCK_1M

Deadzone Time

_T_HW_TIMER_PWM::pwm_deadzone_size

0x10

PWM P Stop State

_T_HW_TIMER_PWM::pwm_p_stop_state

_T_PWM_STOP_STATE::PWM_DEAD_ZONE_STOP_LOW

PWM N Stop State

_T_HW_TIMER_PWM::pwm_n_stop_state

_T_PWM_STOP_STATE::PWM_DEAD_ZONE_STOP_HIGH

  1. Call pwm_config() to configure PWM according to the parameters.

  2. Call pwm_pin_config() to configure PWM output pin.

  3. Call pwm_start() to start the PWM.