PWM Breathing Lamp Experiment

1、Learning objectives

1.Learn the principle of PWM breathing lamp.

2.The CH4 of the TIM4 outputs a PWM signal to control the LED indicator light from dark to light, and then from light to dark, similar to human breathing.

Introduction to PWM principle:

PWM is an abbreviation for Pulse Width Modulation, which means Pulse Width Modulation (PWM) in Chinese. It is a very effective technology that utilizes the digital output of a microprocessor to control analog circuits. Its advantages such as simple control, flexibility, and good dynamic response have made it the most widely used control method in power electronics technology. Its application fields include measurement, communication, power control and conversion, motor control, servo control, dimming, switching power supplies, and even some audio amplifiers, Therefore, learning PWM has very important practical significance.

In fact, we can also understand that PWM is a method of digitally encoding analog signal levels. Through the use of high-resolution counters, the duty cycle of the square wave is modulated to encode the level of a specific analog signal. The PWM signal is still digital because at any given moment, the full amplitude DC power supply is either completely ON or completely OFF. The voltage or current source is applied to the analog load in an ON or OFF repetitive pulse sequence. When the DC power supply is applied to the load, when it is turned on, it is when the power supply is turned off. As long as the bandwidth is sufficient, any analog value can be encoded using PWM.

2、Hardware construction

The timer pin used in this experiment is PB9, and the corresponding timer channel is TIM4_ For CH4, because the PB9 pin does not have an LED light, in order to observe the effect of the breathing light, we connect the PB9 and PB4 with a DuPont cable and directly use the LED light on the board.

Note: 1. After successfully downloading the program, wire the pins; DuPont line needs to be purchased separately.

image-2023022700001

Wiring reference diagram

Program analysis

The PWM output frequency is constant, changing the value in the CCR register, and a change in this value will result in a change in the PWM output signal duty cycle. The duty cycle is actually the ratio of the high level time to the cycle within a cycle, and changing this ratio can control the time for the io pin to output the high level and low level. Therefore, the control of the LED breathing lamp can be achieved by outputting a PWM signal through the CH4 of the TIM4.

LED initialization code:

PWM initialization code:

Main function code:

Experimental phenomenon

After downloading the program, we can see that the LED lights on the STM32 motherboard slowly turn off, showing a breathing light effect. image-2023022700003