当前位置:网站首页>STM32 uses time delay to realize breathing lamp register version

STM32 uses time delay to realize breathing lamp register version

2022-06-24 19:23:00 Me-Space

stm32 Use time delay to realize breathing lamp ( register )

The experiment uses STM32F103C8T6 Realization , Programming with registers .
Breathing lights : The brightness of the light is constantly changing from light to dark under control , And then change from dark to light , It feels like people are breathing .
In general use PWM Pulse realization , The principle is to change the time of high level ( Duty cycle ), So as to achieve the effect of breathing lamp . Here the delay is used to simulate PWM pulse .
PWM The principle is shown in the figure below :
 Insert picture description here
The hardware description is shown in the figure below :
 Insert picture description here

Implementation code

1. Macro definition

#define LED1_ON GPIOA->ODR |= (1 << 0); //LED Light on 
#define LED1_OFF GPIOA->ODR &= ~(1 << 0);//LED The light goes out 

2.GPIO Mouth initialization

void Led_Init(void)
{
    
	// Turn on the clock 
	RCC->APB2ENR |= (1 << 2);
	// Zero clearing 
	GPIOA->CRL &= ~(0XF << 0);
	// General push-pull output 
	GPIOA->CRL |= (0X3 << 0);
	//LED The lamp is off by default 
	GPIOA->ODR &= ~(0XFF << 0); 
}

3. Breathing lamp implementation code

void Breathe _Led(void)
{
    
	int i = 0;
	int num = 5000;// Cycle time 
	// From dark to light 
	for(i = 0; i < num; i++)
	{
    
		LED1_ON;
		delay_us(i);// High level time   Microsecond delay function 
		LED1_OFF;
		delay_us(num - i);// Low level time 
	}
	// Change from light to dark 
	for(i = num; i > 0; i--)
	{
    
		LED1_ON;
		delay_us(i);// High level time 
		LED1_OFF;
		delay_us(num - i);// Low level time 
	}
}

4. The main function

int main(void)
{
    
	Led_Init();//LED Lamp initialization  
	while (1)
	{
    
		Breathe_Led();// Breathing lights 
	}
}

If there is anything wrong, please point it out .

原网站

版权声明
本文为[Me-Space]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211331530919.html