Skip to content

Pulse width modulated output (PWM)


Introduction

PWM (Pulse Width Modulation) is a digital coding method for analog signal level. It uses pulses with different frequencies and square waves with different duty cycles to code a specific analog signal level, so that a series of pulses with equal amplitude can be obtained at the output end, and these pulses can be used to replace the required waveform.


API

Function name Description
pwm_ Init Initialize PWM device
pwm_ Start Start PWM device
pwm_ Stop Stop the PWM device
pwm_ set_ Duty Set PWM duty cycle
pwm_ set_ restart_ Md Select the effective re counting mode


Example

(1) The following code configures and starts PWM0,% 50 duty cycle, 1000 frequency

scu_ set_ device_ gate(HAL_PWM5_BASE,ENABLE);
dpmu_ set_ io_ reuse(PB4,SECOND_FUNCTION);                // Set pin function reuse
dpmu_ set_ io_ direction(PB4,DPMU_IO_DIRECTION_OUTPUT); // Set pin function to output mode
dpmu_ set_ io_ pull(PB4,DPMU_IO_PULL_DISABLE);          // Set Close Up and Down
pwm_ init_ t init;
init. clk_ sel = 0;
init. freq = 1000;
init. duty = 50;
init. duty_ max = 100;
pwm_ init(PWM5,init);
pwm_ stop(PWM5);
pwm_ start(PWM5);

(2) The IP address of this chip adds the option of effective mode of re counting. For example, after initialization, it is necessary to switch between different duty cycles. It is hoped that after the PWN wave of the previous duty cycle is completely output, the re counting of the next duty cycle will take effect. Please refer to the following example code.

scu_ set_ device_ gate(HAL_PWM5_BASE,ENABLE);
dpmu_ set_ io_ reuse(PB4,SECOND_FUNCTION);                // Set pin function reuse
dpmu_ set_ io_ direction(PB4,DPMU_IO_DIRECTION_OUTPUT); // Set pin function to output mode
dpmu_ set_ io_ pull(PB4,DPMU_IO_PULL_DISABLE);          // Set Close Up and Down
pwm_ init_ t init;
init. clk_ sel = 0;
init. freq = 1000;
init. duty = 50;
init. duty_ max = 100;
pwm_ init(PWM5,init);
pwm_ set_ restart_ md(PWM5,1);        // The configuration will not take effect until the PWM wave in progress is completely output
pwm_ stop(PWM5);
pwm_ start(PWM5);

//After 1s delay
pwm_ set_ Duty (PWM5,30100)//Switch to duty cycle of 30%

The configuration will not take effect until the PWM wave in progress is completely output. The waveform results are as follows:

PWM波形图

The configured re counting takes effect immediately, pwm_ set_ restart_ Md (PWM0,0), waveform results are as follows:

PWM波形图


(3) 2 sets of PWM realize complementary output function

scu_set_device_gate(HAL_PWM5_BASE,ENABLE);
dpmu_set_io_reuse(PB4,SECOND_FUNCTION);                //Set pin function multiplexing
dpmu_set_io_direction(PB4,DPMU_IO_DIRECTION_OUTPUT);   //Set the pin function to output mode
dpmu_set_io_pull(PB4,DPMU_IO_PULL_DISABLE);            //Set Close Up and Down

pwm_init_t init;
init.clk_sel = 0;
init.freq = 16000;
init.duty = 50;
init.duty_max = 100;
pwm_init(PWM5,init);
pwm_stop(PWM5);
scu_set_device_gate(HAL_PC_BASE,ENABLE);
scu_set_device_gate(HAL_PWM0_BASE,ENABLE);
dpmu_set_io_reuse(PC4,FORTH_FUNCTION);                 //Set pin function multiplexing
dpmu_set_io_direction(PC4,DPMU_IO_DIRECTION_OUTPUT);   //Set the pin function to output mode
dpmu_set_io_pull(PC4,DPMU_IO_PULL_DISABLE);            //Set Close Up and Down
init.clk_sel = 0;
init.freq = 16000;
init.duty = 50;
init.duty_max = 100;
pwm_init(PWM0,init);
pwm_stop(PWM0);
{
    volatile uint32_t i = 0;
    taskENTER_CRITICAL();
    pwm_start(PWM5);
    for (i = 0;i < 0x210;i++);
    pwm_start(PWM0);
    taskEXIT_CRITICAL();
}

Frequently Asked Questions

-Relation between PWM frequency and PWM maximum duty cycle:

  • When freq is 8200000Hz, the duty_ Max can be configured up to 10
  • When freq is 820000Hz, the duty_ Max can be configured up to 100
  • When freq is 82000Hz, the duty_ Max can be configured up to 1000