The PWM output mode of GD32F4xx controls the transformation of the lamp

Introduction to PWM principle

PWM basics

1. Introduction to PWM

PWM (Pulse Width Modulation) is a very effective technology that uses the digital output of a microprocessor to control an analog circuit. PWM is a method of digitally encoding the level of an analog signal. Through the use of high-resolution counters, the square wave duty cycle is modulated to encode the level of a specific analog signal. The PWM signal is still digital because at any given moment, full scale DC power is either fully present or not present. For example, our voltage output is 5v, then by changing the duty cycle of PWM, we can achieve the effect of outputting 3.3V or 1.3V within a certain period of time

2. GD32PWM understanding

Screen clip capture time: 2023/5/21 15:18

Basic timers simply cannot use PWM channels

3.PWM basic parameters

PWM is a pulse width debug that has three very important parameters: frequency, duty cycle and period

Frequency: Frequency is the number of times the signal goes from a high level to a low level and back to a high level per second

Duty cycle: Duty cycle refers to the proportion of high level in a cycle

Period: Period refers to a complete PWM signal duration.

4. PWM control method

There is an important conclusion in sampling control theory: when narrow pulses with equal impulse but different shapes are applied to links with inertia, the effects are basically the same.

PWM control technology is based on this conclusion as a theoretical basis to control the on and off of the semiconductor switching device, so that the output terminal can get a series of pulses with equal amplitude and unequal width, and use these pulses to replace sine waves or other pulses. required waveform.

5. PWM application

PWM can be applied to motor speed regulation, power modulation, PID regulation, communication, etc., with simple configuration and strong anti-interference ability. The brightness of the LED lamp can be controlled by PWM, the passive buzzer can be controlled to make a simple sound by the PWM signal, and the coil of the power relay can be used to save energy. It is very important that PWM is used to drive the motor and adjust the speed of the motor.

PWM operation process

By driving PWM to achieve the effect of a breathing light, the change of the duty cycle of the PWM signal can realize the difference of the current flowing through the LED, and realize the gradual change of the brightness of the LED

Ordinary people’s eyes have no sense of flicker at all when the refresh frequency is above 80HZ. Since the flicker is invisible when the frequency is high, the larger the duty cycle, the brighter the LED, and the smaller the duty cycle, the darker the LED. Therefore, when the frequency is off, the brightness of the LED light can be changed with different duty cycles to achieve the effect of a breathing light

1. Configure the PWM process

1. Configure the channel pin GPIO

PA

Enable the clock first

Configure the IO mode mode

Configure IO output set

GPIO multiplexing

2. Configure the timer

Turn on timer clock 1

Configure the prescaler of the clock

Initialize the clock

Initialize structure parameters

Timer initialization

3. Configure the output structure

Timer output structure

The polarity of the output of the Ocpolarity channel, that is, whether it is configured as active low or active high, here it is configured as active high TIMER–OC–POLARITY–HIGH

Outputstate channel output state (also enable channel), generally enable TIMER_CCX_ENABLE enable PWM output to the port

4. Configure the timer output channel

//Output timer channel polarity

timer_ocintpara.ocpolarity=TIMER_OC_POLARITY_HIGH;

//Output timer channel output status

timer_ocintpara.outputnstate=TIMER_CCX_ENABLE;

// configure the output structure

timer_channel_output_config(BSP_PWM_TIMER,TIMER_CH_0, & amp;timer_ocintpara);//initialize the structure

5. Configure the duty cycle of the timer output channel

Void timer_channel_output_pulse_value_config(uint32_t timer_periph,uint16_t channel,uint32_t)

Timer_channel_output_pulse_value_config(BSP_PWM_TIMER, BSP_PWM_TIMER_CH, 5000-1);

//Configure timer channel output pulse

Here the output pulse value is set to 5000-1, the period of the timer we set is 10000-1, and the duty cycle is 5000/1000=50%

Configure peripheral timer channel output duty cycle

Void timer_channel_output_mode_config(uint32_t timer_periph,uint16_t ocmode);

Timer_channel_output_mode_config(BSP_PWM_TIMER, BSP_PWM_TIMER_CH, TIMER_OC_MODE_PWM0);//Configure timer channel output comparison mode

Here it is set to PWM mode 0, which is configured as TIMER_OC_MODE_PWM0

6. Timer auto reload shadow enable

Void timer_auto_reload_shadow_enable(BSP_PWM_TIMER);

2. Breathing light function

The principle of the breathing light is that the LED light gradually brightens and gradually dims, and then continues to cycle. The control of the brightness of the LED light is by changing the duty cycle of the PWM. The larger the duty cycle, the brighter the LED light, and the smaller the duty cycle. , the LED light is dimmer

3. Experimental phenomenon

bsp_pwm.c

#include "bsp_pwm.h"

static void pwm_gpio_config(void)
{
//enable the clock
   rcu_periph_clock_enable(BSP_PWM_RCU);
    //Configure GPIO mode
gpio_mode_set(BSP_PWM_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, BSP_PWM_PIN);
    //Configure GPIO output and speed CPIOA port output is push-pull output 50 GPIO5
gpio_output_options_set(BSP_PWM_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, BSP_PWM_PIN);
\t
//Configure the multiplexing function of GPIO. If PA5 is multiplexed, AF is 1
gpio_af_set(BSP_PWM_PORT, BSP_PWM_AF, BSP_PWM_PIN);
\t
\t

\t
     
}

void pwm_config(uint16_t pre,uint16_t per)
{
//Configure structure parameters
timer_parameter_struct timer_initpara;
//Output initialization output structure
timer_oc_parameter_struct timer_ocintpara;
\t
\t

\t
\t
\t
pwm_gpio_config();
//start the clock
rcu_periph_clock_enable(BSP_PWM_TIMER_RCU);//This is timer 1
    //Configure the prescaler number of the clock 200/4=50 from 200 to 50
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
//Initialize the timer
timer_deinit(BSP_PWM_TIMER);
\t
\t
\t

\t
//Initialize structure parameters
timere_initpara.prescaler=pre-1; //timer 1 second string 20000
     timere_initpara.alignedmode=TIMER_COUNTER_EDGE;
     timer_initpara.counterdirection=TIMER_COUNTER_UP;
     timer_initpara.clockdivision=TIMER_CKDIV_DIV1;
    //If this is timing for 1 second, it will be 10000
timere_initpara.period=per-1;
     timere_initpara.repetitioncounter=0; //Repeat timer Only advanced ones can use this is basic, so it is not needed so it is 0
     
     timer_init(BSP_PWM_TIMER, &timere_initpara);
 
\t

\t
\t
\t
\t
// //Output timer channel polarity
// timer_ocintpara.ocpolarity=TIMER_OC_POLARITY_HIGH;
// //Output timer channel output status
// timer_ocintpara. outputnstate=TIMER_CCX_ENABLE;
//
// //Configure the output structure
// timer_channel_output_config(BSP_PWM_TIMER, BSP_PWM_CHANNEL, & amp;timer_ocintpara);//initialize the structure

/* output structure */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(BSP_PWM_TIMER, BSP_PWM_CHANNEL, & timer_ocintpara);

\t
\t
\t
//Configure the timer channel output pulse value
timer_channel_output_pulse_value_config(BSP_PWM_TIMER,BSP_PWM_CHANNEL,0);
//Configure timer channel mode
timer_channel_output_mode_config(BSP_PWM_TIMER, BSP_PWM_CHANNEL, TIMER_OC_MODE_PWM0);
/ / Configure the state of the timer disabled
timer_channel_output_shadow_config(BSP_PWM_TIMER, BSP_PWM_CHANNEL, TIMER_OC_SHADOW_DISABLE);
\t





\t
//Timer priority output configuration This is only used by advanced timers
//timer_primary_output_config(BSP_PWM_TIMER, ENABLE);
//Configure the timer to automatically reload the shadow enable
timer_auto_reload_shadow_enable(BSP_PWM_TIMER); //If it is an advanced timer, configure the priority of the timer
//Timer enable
timer_enable(BSP_PWM_TIMER);//Timer 1 starts
}
/*breathing light*/
void pwm_breathing_lamp(void)
{
   static uint8_t direct=0;//direction
   static uint16_t value=0;//
\t

  if(direct==0){//gradually brighten
  
value + =300;
if(value>10000)
direct=1;
\t 
  }else{//gradually darken
    value-=300;
    if(value<=0)
     direct=0;
  }

timer_channel_output_pulse_value_config(BSP_PWM_TIMER, BSP_PWM_CHANNEL, value);
  delay_1ms(50);
}


bsp_pwm.h
#ifndef _BSP_PWM_H
#define _BSP_PWM_H


#include "gd32f4xx.h"
#include "systick.h"

#define BSP_PWM_RCU RCU_GPIOA
#define BSP_PWM_PORT GPIOA
#define BSP_PWM_PIN GPIO_PIN_5
#define BSP_PWM_AF GPIO_AF_1



#define BSP_PWM_TIMER_RCU RCU_TIMER1
#define BSP_PWM_TIMER TIMER1
#define BSP_PWM_CHANNEL TIMER_CH_0

void pwm_config(uint16_t pre,uint16_t per);
void pwm_breathing_lamp(void);


#endif