STM32 breathing light (based on PWM pulse width modulation)

Table of contents

 

 

1. PWM pulse width modulation

1. What is PWM

 2. How to generate PWM signal

2. CubuMX configures the timer to output PWM

1. Chip selection (stm32f103c8t6)

2. Clock configuration

3. Configure timer output PWM

 4. Export project

3. Program engineering code analysis

1. TIM1_channel1 initialization

2. The interrupt callback function realizes the breathing light

4. Experimental effect display

V. Summary

6. Reference link


 

1. PWM pulse width modulation

1. What is PWM

        PWM (Pulse Width Modulation) is pulse width modulation, referred to as pulse width modulation. It is a very effective technology that uses the digital output of the microprocessor to control the analog circuit; it is an analog control method that modulates the bias of the transistor base or MOS transistor gate according to the change of the corresponding load. To realize the change of the conduction time of the transistor or MOS tube, thereby realizing the change of the output of the switching regulated power supply.

 

        PWM is to control the on-off of the switching device of the inverter circuit, so that the output terminal gets a series of pulses with equal amplitude, and these pulses are used to replace the sine wave or the required waveform. It can also be understood that PWM is a method of digitally encoding the level of an analog signal. 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, full-scale DC power is either completely on (ON) or not at all (OFF). A voltage or current source is applied to an analog load in a repetitive pulse sequence of ON or OFF. Any analog value can be encoded using PWM as long as the bandwidth is sufficient.

PWM Pulse Width Modulation Schematic

    9c1f631663414ef585fe8956553b8dd1.png

 

 2. How to generate PWM signal

(1) The PWM signal can be output directly through the internal module of the chip. The premise is that the I/O port has an integrated module, and only a few simple steps are required. This kind of functional module with PWM output is easier to program. At the same time the data is more accurate.

(2) But if there is no PWM function module inside the IC, or if the requirements are not very high, you can use the I/O port to set some parameters to output PWM signals, because PWM signals are actually a combination of a series of high and low levels. The specific method is to add a timer to the I/O. For the frequency of the PWM signal you require to output is consistent with your timer, use the timer interrupt to count, but this method is generally not used, unless the requirements for accuracy, frequency, etc. are not Highly achievable.
 

2. CubuMX configures the timer to output PWM

1. Chip selection (stm32f103c8t6)

500323da1a214062887880674bc8fe4b.png

 

2. Clock configuration

Select crystal/ceramic oscillator as external high speed clock (HSE) input

35e0f3ddb37948118bc94a16bcbcdccf.png

System clock number configuration

38c2bcdf031b477bb46d49214426d6aa.png

3. Configure timer output PWM

Configure PA8 as PWM output IO port

a0cb8f1a783b4ccaa83eadf855ff2ab2.png

 4. Export project

15d45cf98e424041a7c704e2222e4af6.png

3. Program engineering code analysis

1. TIM1_channel1 initialization

 MX_TIM1_Init(); //Timer 1 initialization function

6e3f88e1ac5a486b9bef065fd8b6ed74.png

 

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); //Start tim1 channel 1 timer

Only when the timer interrupt timer is turned on will it start working and counting

2. The interrupt callback function realizes the breathing light

 __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1,i); //PWM output duty ratio setting function

  MX_GPIO_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);				//开启tim1通道1计时器
   
  /* USER CODE END 2 */
	int i=0,flag=0;
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	
	 if(flag==0)			//0状态占空比按+10的步长逐渐增大
	 {
		 i+=10;
		 if(i>400)
			flag=1;
	 }
	 else				   //1状态占空比按-10的步长逐渐减小
	 {
		i-=10;
		if(i<5)
			flag=0;
	 }
	 /*PAB2总线时钟为72Mhz,经过72分频后为1Mhz,计数周期RCC为65535
	  __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1,i);	    //设置该PWM输出占空比为变量i
	 
    /* USER CODE BEGIN 3 */
  }

 Project code file portal: project code

4. Experimental effect display

PWM_LED

V. Summary

        The breathing light mainly changes the IO output voltage through PWM pulse width modulation to realize the gradual change of LED brightness. PWM is a very useful thing, and it may be often encountered or used in future engineering designs. So I hope that future readers can learn about PWM through this small experiment.

6. Reference link

Use STM32 to output PWM waveform

PWM (Pulse Width Modulation) Signal Principle

STM32CUBEMX_PWM-based breathing light

 

Guess you like

Origin blog.csdn.net/qq_52791446/article/details/127576192