STM32 timer matrix function implementation

Keywords: stm32 linear algebra

catalog:

1, What is a timer matrix

2, Timer matrix principle

3, Timer matrix code implementation

4, Timer matrix verification

1, What is a timer matrix

In product development, we often encounter some different timing requirements, such as key shaking elimination, LED flashing once per second and several times every few seconds, and some messy timing requirements.

The timer matrix is the pain point to solve these different timing requirements.

The set of LED timing, key timing and other function timing is called timer matrix.

2, Timer matrix principle

  In order to ensure the timing accuracy, the MCU timer is generally used to give the timer matrix a reference timing, similar to systick. For example, 50us is interrupted once, which can meet most timing requirements.

For example, if the LED flashes once per second, it needs to change the LED control pin level once every 500ms. According to the MCU timer 50us benchmark timing, 500000 / 50 = 10000 interrupts are required for 500ms.

For another example, key scanning needs to be performed once in 10ms, then 10000 / 50 = 200. MCU timer interrupt 200 times to execute a key scan.

Some timers need cyclic timing from the beginning to the end of the program, such as key scanning.

But some are disposable. For example, I want the light to go out automatically after 2 seconds. There is no need for this timer to count all the time, wasting resources.

Therefore, we need to flexibly create, delete and arbitrarily timed timer matrix functions.

Then we use the high-level usage of C language to encapsulate these functions, making it more humanized and more convenient to use.

3, Timer matrix code implementation

1. Create a timer structure

/********************   hal_time.h   **************/
// Timer ID
typedef enum
{
	T_LED,					//LED timer
	
	T_SUM,
}TIMER_ID_TYPEDEF;
	
// Timer completion flag
typedef enum
{
	T_SUCCESS,//0
	T_FAIL,	//1
}TIMER_RESULT_TYPEDEF;

// Timer status
typedef enum
{
	T_STA_INVAILD,
	T_STA_STOP,      // Timer stop
	T_STA_START,     // Timer start
}TIMER_STATE_TYPEDEF;

// Timer structure
typedef struct
{
	TIMER_STATE_TYPEDEF state;      // 0: timer not started 1 - timer started
	unsigned short CurrentCount;    // Current timing value
	unsigned short Period;          // Timing cycle
	void (*func)(void);              // Function pointer
}Stu_TimerTypedef;

2. Write timer initialization function hal_timeInit

volatile Stu_TimerTypedef Stu_Timer[T_SUM]; // Placed in the interrupt function processing, it is generally modified with volatile

// Initialization timer
void hal_TimeInit(void)
{
	unsigned char i;
	hal_timer4Config();
	
	for(i=0;i<T_SUM;i++)
	{
		Stu_Timer[i].state = T_STA_STOP;
		Stu_Timer[i].CurrentCount = 0;
		Stu_Timer[i].Period = 0;
		Stu_Timer[i].func = 0;
	}
}

3. Writing the timer function hal_CreatTimer

/*******************************************************************************
* Function Name  : hal_CreatTimer(TIMER_ID_TYPEDEF id,void (*proc)(void), unsigned short Period,unsigned char state)
* Description    : Create timer 
* Input          : - id: Timer ID
*									- (*proc)() Function pointer 
*									- Period Timing period, unit: 50us
* 								- state Timer initial state
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
void hal_CreatTimer(TIMER_ID_TYPEDEF id,void (*proc)(void),unsigned short Period,TIMER_STATE_TYPEDEF state)
{
	Stu_Timer[id].state = state;
	Stu_Timer[id].CurrentCount = 0;
	Stu_Timer[id].Period = Period;
	Stu_Timer[id].func = proc;
}

4. Writing timer matrix timing scheduling function hal_TimerHandle, put this function in the MCU timer interrupt

/*******************************************************************************
* Function Name  : static void Hal_TimerHandle(void)
* Description    : Timer interrupt timing function
* Input          : None
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
static void hal_TimerHandle(void)
{
	unsigned char i;
	
	for(i=0;i<T_SUM;i++)
	{
		if((Stu_Timer[i].func)&&(Stu_Timer[i].state==T_STA_START))
		{
			Stu_Timer[i].CurrentCount++;
			if(Stu_Timer[i].CurrentCount >= Stu_Timer[i].Period)
			{
				 Stu_Timer[i].state = T_STA_STOP;
				 Stu_Timer[i].CurrentCount = Stu_Timer[i].CurrentCount;
				 Stu_Timer[i].func();
			}
		}
	}
}


// timer interrupt 
void TIM4_IRQHandler(void)
{
	TIM_ClearFlag(TIM4, TIM_FLAG_Update);
	hal_TimerHandle();
}

5. Writing timer reset function hal_ResetTimer

/*******************************************************************************
* Function Name  : hal_ResetTimer(TIMER_ID_TYPEDEF id,TIMER_STATE_TYPEDEF sta)
* Description    : Reset timer status and timing time
* Input          : - id: Timer ID
*								 	 - sta Timer status
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
TIMER_RESULT_TYPEDEF hal_ResetTimer(TIMER_ID_TYPEDEF id,TIMER_STATE_TYPEDEF sta)
{
	if(Stu_Timer[id].func)		//Determine whether the timer exists
	{
		Stu_Timer[id].state = sta;
		Stu_Timer[id].CurrentCount = 0;
		 
		return T_SUCCESS;
	}else
	{
		return T_FAIL;
	}
}

6. Write other auxiliary functions hal_CtrlTimerAction,hal_DeleteTimer,hal_GetTimerState

/*******************************************************************************
* Function Name  : unsigned char hal_CtrlTimerAction(TIMER_ID_TYPEDEF id,TIMER_STATE_TYPEDEF sta)
* Description    : Control timer action 
* Input          : - id: Timer ID
*								 	 - sta Timer status
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
TIMER_RESULT_TYPEDEF hal_CtrlTimerAction(TIMER_ID_TYPEDEF id,TIMER_STATE_TYPEDEF sta)
{
	if(Stu_Timer[id].func)   //Determine whether the timer exists
	{
		Stu_Timer[id].state = sta;
		return T_SUCCESS;
	}
	else
	{
		return T_FAIL;
	}
}

/*******************************************************************************
* Function Name  : TIMER_STATE_RESULT_TYPEDEF	hal_GetTimerState(TIMER_ID_TYPEDEF id)
* Description    : Get timer status
* Input          : - id: Timer ID
*								 	 - sta Timer status
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
TIMER_STATE_TYPEDEF	hal_GetTimerState(TIMER_ID_TYPEDEF id)
{
	if(Stu_Timer[id].func)		//Determine whether the timer exists
	{
		return Stu_Timer[id].state;
	 
	}else
	{
		return T_STA_INVAILD;
	}
}
/*******************************************************************************
* Function Name  : hal_DeleteTimer(TIMER_ID_TYPEDEF id)
* Description    : Delete timer 
* Input          : - id: Timer ID
*								 
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
TIMER_RESULT_TYPEDEF hal_DeleteTimer(TIMER_ID_TYPEDEF id)
{
	if(Stu_Timer[id].func)
	{
		Stu_Timer[id].state = T_STA_STOP;
	 
		Stu_Timer[id].CurrentCount = 0;
		//Stu_Timer[id].Period = 0;
		Stu_Timer[id].func = 0;
		return T_SUCCESS;
	}else
	{
		return T_FAIL;
	}
}

/*******************************************************************************
* Function Name  : hal_ResetTimer(TIMER_ID_TYPEDEF id,TIMER_STATE_TYPEDEF sta)
* Description    : Reset timer status and timing time
* Input          : - id: Timer ID
*								 	 - sta Timer status
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
TIMER_RESULT_TYPEDEF hal_ResetTimer(TIMER_ID_TYPEDEF id,TIMER_STATE_TYPEDEF sta)
{
	if(Stu_Timer[id].func)		//Determine whether the timer exists
	{
		Stu_Timer[id].state = sta;
		Stu_Timer[id].CurrentCount = 0;
		 
		return T_SUCCESS;
	}else
	{
		return T_FAIL;
	}
}

4, Timer matrix function verification

Create a timer so that the LED flashes once per second.

Note:

1. You must create and add timers before using them.

2. If cycle timing is required, Hal will be called after the timer callback function ends_ Resettimer causes the timer to reset and start.

 

Posted by ineedhelpbigtime on Tue, 12 Oct 2021 17:25:13 -0700