STM32 realizes the effect of simulating breathing light - non-PWM-STM32CubeMx project generation - manual control of breathing light dimming

Table of contents

I. Overview

2. Implementation principle

3. Code implementation

Four. Summary


I. Overview

        In the previous issue, we explained the effect of using timers to achieve PWM dimming. In this article, we mainly explain the simple implementation of breathing lights. We will not discuss related knowledge such as duty cycle here. (The source code is finally available for free)


2. Implementation principle

        We mentioned that the timer function to achieve the breathing light effect depends on sending out PWM waves to control the cycle time of high and low levels, and change the intensity of light according to the persistent effect of vision.

        Then, on the premise of not using the timer, we need to manually control its duty cycle, then first we need to determine the cycle duration, the point is: the cycle duration cannot , The persistence effect is not achieved, because we need to use an additional method (described below) .

        So far, after the idea is clear, we can use the code to realize it.


3. Code implementation

        First, we create a new STM32CubeMx project and customize any GPIO port as a breathing light.

        Configure clock related parameters, generate and open the project.

        Then we customize a Delay() function, the function body is as follows:

void Delay(int i)
{
	while(i--);
}

        Let's delve into the meaning of this Delay() function. Unlike Hal_Delay(), Hal_Delay is the number of milliseconds to delay the response. Obviously, the number of milliseconds does not allow us to achieve the effect of visual persistence, so we must choose a method higher than milliseconds. The way, this way is to use the speed of the execution statement of the function.

        Next, we define a cycle as the speed of 1000 statements. In this cycle, we change the time for the LED to light up from the execution time of 1 statement to the execution time of 1000 statements, and then change it back, and then execute this cycle Operation, you can achieve the breathing light effect!

while (1)
  {
    /* USER CODE END WHILE */
		
    /* USER CODE BEGIN 3 */
		for(int i=0;i<5000;i++)
		{
				HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_RESET);
				Delay(i);
				HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);
				Delay(5000-i);
		}
		for(int i=5000;i>0;i--)
		{
				HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_RESET);
				Delay(i);
				HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);
				Delay(5000-i);
		}
}

        After the program is compiled, burn it into the development board.

Four. Summary

        This article only introduces a simple method to realize the breathing light. The timer breathing light has been explained in the previous article, but in fact it is not much different from the timer breathing light, only the clock cycle is different. That's all, in essence, the breathing light effect is realized by controlling the duty cycle.

Guess you like

Origin blog.csdn.net/qq_39724355/article/details/127466238