199 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			199 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "base.h"
 | 
						|
#include "timer.h"
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
static TIMER_UserStruct g_timer[9]={0};
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
/*
 | 
						|
@初始化通用定时器
 | 
						|
@TIM2\TIM3\TIM4\TIM5\TIM6\TIM7\TIM12\TIM13\TIM14
 | 
						|
*/
 | 
						|
int TIMER_InitNormal (TIMER_InitStruct *init)
 | 
						|
{
 | 
						|
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
 | 
						|
	NVIC_InitTypeDef NVIC_InitStructure;
 | 
						|
	u16 arr;u16 psc;
 | 
						|
	if (init==0) return -1;
 | 
						|
	if (init->Cycle>60000)//定时时间大于60毫秒
 | 
						|
	{
 | 
						|
		//psc=84*500-1;			//每100us计数器加1
 | 
						|
		psc=90*100-1;
 | 
						|
		arr=init->Cycle/100-1;
 | 
						|
	}
 | 
						|
	else 
 | 
						|
	{
 | 
						|
		//u16 t=60000/(init->Cycle*84)+1;
 | 
						|
		//psc=84/t-1;
 | 
						|
		u16 t=60000/(init->Cycle*90)+1;
 | 
						|
		psc=90/t-1;
 | 
						|
		arr=t*init->Cycle-1;
 | 
						|
	}
 | 
						|
	
 | 
						|
	u32 timer=(u32)(init->Tim-TIM2)/(TIM3-TIM2);
 | 
						|
	if (init->UpdataCall)
 | 
						|
	{
 | 
						|
		g_timer[timer].UpdataCall=init->UpdataCall;
 | 
						|
		g_timer[timer].Inited=1;
 | 
						|
	}
 | 
						|
	
 | 
						|
	RCC_APB1PeriphClockCmd(1<<timer,ENABLE); 
 | 
						|
	
 | 
						|
  TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
 | 
						|
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;  //定时器分频
 | 
						|
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
 | 
						|
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
 | 
						|
	
 | 
						|
	TIM_TimeBaseInit(init->Tim,&TIM_TimeBaseInitStructure);//初始化TIM
 | 
						|
	
 | 
						|
	TIM_ITConfig(init->Tim,TIM_IT_Update,ENABLE); //允许定时器更新中断
 | 
						|
	TIM_Cmd(init->Tim,ENABLE); //使能定时器3
 | 
						|
	
 | 
						|
	u8 irq_num=0;
 | 
						|
	if (init->Tim==TIM2)
 | 
						|
	{
 | 
						|
		irq_num=TIM2_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM3)
 | 
						|
	{
 | 
						|
		irq_num=TIM3_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM4)
 | 
						|
	{
 | 
						|
		irq_num=TIM4_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM5)
 | 
						|
	{
 | 
						|
		irq_num=TIM5_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM6)
 | 
						|
	{
 | 
						|
		//irq_num=TIM6_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM7)
 | 
						|
	{
 | 
						|
		irq_num=TIM7_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM12)
 | 
						|
	{
 | 
						|
		//irq_num=TIM12_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM13)
 | 
						|
	{
 | 
						|
		//irq_num=TIM13_IRQn;
 | 
						|
	}
 | 
						|
	else if (init->Tim==TIM14)
 | 
						|
	{
 | 
						|
		//irq_num=TIM14_IRQn;
 | 
						|
	}
 | 
						|
	
 | 
						|
	
 | 
						|
	
 | 
						|
	NVIC_InitStructure.NVIC_IRQChannel=irq_num; //定时器中断
 | 
						|
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
 | 
						|
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
 | 
						|
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
 | 
						|
	NVIC_Init(&NVIC_InitStructure);
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
void TIMER_DeInit (TIM_TypeDef* TIMx)
 | 
						|
{
 | 
						|
	TIM_DeInit (TIMx);
 | 
						|
	u32 timer=(u32)(TIMx-TIM2)/(TIM3-TIM2);
 | 
						|
	
 | 
						|
	RCC_APB1PeriphClockCmd(1<<timer,DISABLE); 
 | 
						|
	g_timer[timer].UpdataCall=0;
 | 
						|
	g_timer[timer].Inited=0;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
void TIM2_IRQHandler (void)
 | 
						|
{
 | 
						|
	const TIM_TypeDef *timer=TIM2;
 | 
						|
	const u8 timer_num=0;
 | 
						|
	if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //溢出中断
 | 
						|
	{
 | 
						|
		if (g_timer[timer_num].Inited==1)
 | 
						|
		{
 | 
						|
			g_timer[timer_num].UpdataCall();
 | 
						|
		}
 | 
						|
		TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update);  //清除中断标志位
 | 
						|
	}
 | 
						|
	
 | 
						|
}
 | 
						|
void TIM3_IRQHandler (void)
 | 
						|
{
 | 
						|
	const TIM_TypeDef *timer=TIM3;
 | 
						|
	const u8 timer_num=1;
 | 
						|
	if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //溢出中断
 | 
						|
	{
 | 
						|
		if (g_timer[timer_num].Inited==1)
 | 
						|
		{
 | 
						|
			g_timer[timer_num].UpdataCall();
 | 
						|
		}
 | 
						|
		TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update);  //清除中断标志位
 | 
						|
	}
 | 
						|
}
 | 
						|
void TIM4_IRQHandler (void)
 | 
						|
{
 | 
						|
	const TIM_TypeDef *timer=TIM4;
 | 
						|
	const u8 timer_num=2;
 | 
						|
	if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //溢出中断
 | 
						|
	{
 | 
						|
		if (g_timer[timer_num].Inited==1)
 | 
						|
		{
 | 
						|
			g_timer[timer_num].UpdataCall();
 | 
						|
		}
 | 
						|
		TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update);  //清除中断标志位
 | 
						|
	}
 | 
						|
}
 | 
						|
void TIM5_IRQHandler (void)
 | 
						|
{
 | 
						|
	const TIM_TypeDef *timer=TIM5;
 | 
						|
	const u8 timer_num=3;
 | 
						|
	if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //溢出中断
 | 
						|
	{
 | 
						|
		if (g_timer[timer_num].Inited==1)
 | 
						|
		{
 | 
						|
			g_timer[timer_num].UpdataCall();
 | 
						|
		}
 | 
						|
		TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update);  //清除中断标志位
 | 
						|
	}
 | 
						|
}
 | 
						|
void TIM7_IRQHandler (void)
 | 
						|
{
 | 
						|
	const TIM_TypeDef *timer=TIM7;
 | 
						|
	const u8 timer_num=5;
 | 
						|
	if(TIM_GetITStatus((TIM_TypeDef *)timer,TIM_IT_Update)==SET) //溢出中断
 | 
						|
	{
 | 
						|
		if (g_timer[timer_num].Inited==1)
 | 
						|
		{
 | 
						|
			g_timer[timer_num].UpdataCall();
 | 
						|
		}
 | 
						|
		TIM_ClearITPendingBit((TIM_TypeDef *)timer,TIM_IT_Update);  //清除中断标志位
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 |