STM32 realizes a variety of LED effects

catalogue

1, Purpose of LED effects

2, Implementation principle of various special effects of LED

3, Code implementation

1, Purpose of LED effects

Realize the functions of LED with different flashing effects.

2, Implementation principle of various special effects of LED

First think about the output waveform, and then convert the waveform into data, because whether it is a single chip microcomputer or any processor, the final processing must be data.

For example, if I want to control the LED light to flash once a second, the waveform of the MCU pin controlling the LED is as follows:

  The cycle of 500ms high level and 500ms low level can realize the effect that the LED lights flash once per second

The waveform is represented by data in the form of array, which is:

[1500,0500,1500,0500, cycle / single flag]

1 represents pin output high level       500 represents 500 ms of continuous comment

The cycle / word flag can be represented by the largest number. For example, if the array is two bytes, 0xFFFF can be used to represent the cycle and 0xFFFE can be used to represent a single time. It can be easily handled by cooperating with the timer program.

For another complex example, let the LED flash twice every 5 seconds and cycle once. Then the waveform is as follows

  100ms high level + 100ms low level + 100ms high level realize LED flash twice, and then 5000ms low level, one cycle.

That is to express the waveform by data in the form of array

[1100,0100,1100,05000, cycle / word flag]

1 represents pin output high level     10 represents the level delay of 100ms, the same is true for 5000.

3, Code implementation

1. The LED effects and led types are encapsulated by enumeration for easy calling

#define LED_EFFECT_END 	 0xFFFE / / single execution
#define LED_EFFECT_AGN 	 0xFFFF / / loop execution

// LED type
typedef enum
{
	LED1,//0
	BUZ,//1
	LED_SUM 
}LED_TYPEDEF;

// LED command
typedef enum
{
	LED_DARK,
	LED_LIGHT,
	LED_LIGHT_100MS,
	LED_BLINK1,
	LED_BLINK2,
	LED_BLINK3,
	LED_BLINK4,
	 
}LED_EFFECT_TEPEDEF;

2. Encapsulate the LED command contents with an array

unsigned short Led_Dark[] = {0,10,LED_EFFECT_END};
unsigned short Led_Light[] = {1,10,LED_EFFECT_END};
unsigned short Led_Light100ms[] = {1,10,0,10,LED_EFFECT_END};	
unsigned short Led_Blink1[] = {1,10,0,10,LED_EFFECT_AGN,2};
unsigned short Led_Blink2[] = {1,10,0,10,1,10,0,10,1,10,0,200,LED_EFFECT_AGN,6};
unsigned short Led_Blink3[] = {1,30,0,30,LED_EFFECT_AGN,2};
unsigned short Led_Blink4[] = {1,50,0,50,LED_EFFECT_AGN,2};

3. LED initialization

void hal_LedInit(void)
{
	unsigned char i;
	hal_ledConfig();
	hal_CreatTimer(T_LED,hal_LedHandle,200,T_STA_START); // Create a 10ms LED timer as a benchmark
	
	for(i=0;i<LED_SUM;i++) // Number of LED s traversed
	{
		LedLoadFlag[i] = 0; // Set the LED status to idle
		pLed[i] = (unsigned short *)Led_Dark;  // Turn led_ The dark array is cast to a pointer and the first address value is assigned
		LedTimer[i] = *(pLed[i]+1);   // Assigns a value to the second address of the array
		QueueEmpty(LedCmdBuff[i]);   // Empty queue
	}
	
	LedMsgInput(LED1,LED_BLINK2,1);  // Let the LED lamp achieve a certain effect and execute it immediately
}

First, we create a 10ms timer as the benchmark, and then use the for statement to traverse and initialize all the numbers of LED s.

3. LED execution function initialization

void LedMsgInput(unsigned char type,LED_EFFECT_TEPEDEF cmd,unsigned char clr)
{
	unsigned char bLedCMD;
	if(type >= LED_SUM) // Judge whether the current LED traversal is completed
	{
		return;
	}
	bLedCMD = cmd;  
	if(clr)   // Judge whether to execute immediately 1 - execute the current special effect immediately 0 - do not execute immediately
	{
		QueueEmpty(LedCmdBuff[type]);  
		LedLoadFlag[type] = 0;    // Set queue to idle state
	}
	QueueDataIn(LedCmdBuff[type],&bLedCMD,1); // List
}

4. System task function

// Task function
void hal_LedProc(void)
{
	unsigned char i;
	unsigned char cmd;
	
	// Traverse all LED s
	for(i=0;i<LED_SUM;i++)
	{
		if((QueueDataLen(LedCmdBuff[i])>0) && (LedLoadFlag[i]==0)) // Determine whether the queue exists and run flag
		{
			QueueDataOut(LedCmdBuff[i], &cmd); // Out of line
			LedLoadFlag[i] = 1; 
			switch(cmd)
				{
					case LED_DARK:
						pLed[i] = (unsigned short *)Led_Dark; // Points to the first address of the waveform array
						LedTimer[i] = *(pLed[i]+1);  // Points to the next address of the waveform array
	
					break;
					
					case LED_LIGHT:
						pLed[i] = (unsigned short *)Led_Light;
						LedTimer[i] = *(pLed[i]+1);
					break;
					
					case LED_LIGHT_100MS:
						pLed[i] = (unsigned short *)Led_Light100ms;
						LedTimer[i] = *(pLed[i]+1);
					break;
					
					case LED_BLINK1:
						pLed[i] = (unsigned short *)Led_Blink1;
						LedTimer[i] = *(pLed[i]+1);
					break;
					
					case LED_BLINK2:
						pLed[i] = (unsigned short *)Led_Blink2;
						LedTimer[i] = *(pLed[i]+1);
					break;
					
					case LED_BLINK3:
						pLed[i] = (unsigned short *)Led_Blink3;
						LedTimer[i] = *(pLed[i]+1);
					break;
					
					case LED_BLINK4:
						pLed[i] = (unsigned short *)Led_Blink4;
						LedTimer[i] = *(pLed[i]+1);
					break;
					
					 
				}
		}
	}
}

5. LED interrupt function

static void hal_LedHandle(void)
{
	unsigned char i;
	
	for(i=0;i<LED_SUM;i++)  // Number of LED s traversed
	{
		if(LedTimer[i])
		{
			LedTimer[i]--;   // Counter--
		}
		if(!LedTimer[i])     // Set arrival time
		{
			
			// The timing is up
			if(*(pLed[i]+2) == LED_EFFECT_END)  // First judge whether the next effect is a single execution flag bit
			{
				LedLoadFlag[i] = 0;   // Set to LED idle
			}
			else
			{
				pLed[i]+=2;  
				if(*pLed[i]==LED_EFFECT_AGN)   // Judge whether it is circular execution
				{
					pLed[i] = pLed[i] - (*(pLed[i]+1)*2);  // Re point the first address of the array to pLed
				}
				LedTimer[i] = *(pLed[i]+1);
			}
		}
		hal_LedDrive[i](*pLed[i]); // Execute LED driver function
	}
	hal_ResetTimer(T_LED,T_STA_START); // reset timer 
}

Link: https://pan.baidu.com/s/1oZbqbz8IQD4d6MWc4QsGYQ  
Extraction code: nrue

Tags: stm32

Posted on Tue, 12 Oct 2021 21:34:20 -0400 by obsidian