Jelajahi Sumber

增加定时器非堵塞延时处理函数

woshiashuai 2 tahun lalu
induk
melakukan
4b3adbbcf2
2 mengubah file dengan 29 tambahan dan 3 penghapusan
  1. 28 3
      system_Template/TIM.c
  2. 1 0
      system_Template/TIM.h

+ 28 - 3
system_Template/TIM.c

@@ -3,16 +3,18 @@
 static void TIM1_Init(uint16_t arr, uint16_t psc);
 static void TIM2_Init(uint16_t arr, uint16_t psc);
 static void TIM3_Init(uint16_t arr, uint16_t psc);
+static void Overtime_Handle(uint16_t *counter, uint8_t *overTime_flag, uint16_t overTime, void (* callback)(void));
 
 TIMClassStruct TIMClass = {
 	.TIM1_Init = TIM1_Init,
 	.TIM2_Init = TIM2_Init,
-	.TIM3_Init = TIM3_Init
+	.TIM3_Init = TIM3_Init,
+	.Overtime_Handle = Overtime_Handle
 };
 
 //#define use_TIM1
 #define use_TIM2
-//#define use_TIM3
+#define use_TIM3
 
 /* 在别的地方复现此函数  TIM1中断函数
 void TIM1_UP_IRQHandler(void)
@@ -141,7 +143,7 @@ void TIM3_Init(uint16_t arr, uint16_t psc)
 	// 设置中断来源
 	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;	
 	// 设置主优先级为 0
-	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;	 
+	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;	 
 	// 设置抢占优先级为3
 	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	
 	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
@@ -168,4 +170,27 @@ void TIM3_Init(uint16_t arr, uint16_t psc)
 	TIM_Cmd(TIM3, ENABLE);
 	#endif
 }
+/**
+  * @brief  定时器非堵塞延时处理函数
+  * @param  counter: 对应计数器地址
+  * @param  overTime_flag: 对应超时标志地址
+  * @param  overTime: 超时时间
+  * @param  callback: 超时结束的回调函数
+  * @retval None
+  * @note	None
+  */
+static void Overtime_Handle(uint16_t *counter, uint8_t *overTime_flag, uint16_t overTime, void (* callback)(void))
+{
+	if(*overTime_flag)
+	{
+		*counter = *counter + 1;
+		if((*counter >= overTime))
+		{
+			*overTime_flag = 0;
+			*counter = 0;
+			callback();
+		}
+	}
+
+}
 

+ 1 - 0
system_Template/TIM.h

@@ -7,6 +7,7 @@ typedef struct {
 	void (* TIM1_Init)(uint16_t arr, uint16_t psc);
 	void (* TIM2_Init)(uint16_t arr, uint16_t psc);
 	void (* TIM3_Init)(uint16_t arr, uint16_t psc);
+	void (* Overtime_Handle)(uint16_t *counter, uint8_t *overTime_flag, uint16_t overTime, void (* callback)(void));
 } TIMClassStruct;
 
 extern TIMClassStruct TIMClass;