time.c#include "time.h"
/*********提供给外部的API*******************/
void Timer_Init(u16 CountData,u16 FreqData);
unsigned char systime_get(void);
void Refresh(u8 ID);
/***********************************************/
SYSTIME systime = 定义SYSTIME类型变量,并初始化函数指针
{
.get_id=systime_get,
.refresh=Refresh,
.timer_init=Timer_Init
};
/****************************************************/
//函数名:Timer_init
//描述:初始化定时器
//输入:中断时间相关
//输出:null
/****************************************************/
void Timer_Init(u16 CountData,u16 FreqData)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Prescaler = FreqData;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
/****************************************************/
//函数名:systime_get
//描述:获取当前时间,并产生一个注册
//输入:null
//输出:null
/****************************************************/
unsigned char systime_get()
{
if(systime.ID<TimerID_max)
{
systime.last[systime.ID]=systime.now;
systime.ID++;
return systime.ID;
}
else
return 0;
}
/****************************************************/
//函数名:Refresh
//描述:更新当前时间
//输入:获取到的ID
//输出:null
/****************************************************/
void Refresh(u8 ID)
{
systime.last[ID-1]=systime.now;
}
/****************************************************/
//函数名:TIM4_IRQHandler
//描述:1ms定时器
//输入:null
//输出:null
/****************************************************/
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
systime.now++;
}
}
|