STR712,48M主频工作,设置如下:
第一种程序设计:
TIM2->CR2= 0x4000; //TIM1_CR2 比较模式
TIM_PrescalerConfig(TIM2, 23); //配置分频器及设置定时比较模式及定时中断
TIM2->CR1= 0x8040; //TIM1_CR1
TIM2->OCAR=0x4e20; //TIM1_OCAR
TIM2->CNTR=0xFFFF; //TIM1_CNTR
TIM_ITConfig(TIM2, TIM_OCAIE_mask, ENABLE);
EIC_IRQChannelConfig(T2TIMI_IRQChannel, ENABLE);
EIC_IRQChannelPriorityConfig(T2TIMI_IRQChannel, T2TIMIPriority);
TIM_CounterConfig(TIM2, TIM_START);
第二种程序设计:
void TIM_Init( TIM_TypeDef *TIMx )
{
TIMx->CR1 = 0x0000;
TIMx->CR2 = 0x0000;
TIMx->SR = 0x0000;
}
u16 TIM_GetOCiRForTimer(TIM_TypeDef* TIMx, u16 ms)
{
u8 CC = 0;
u32 PCLK2 = 0;
u16 Xpulse = 0;
CC = TIM_PrescalerValue(TIMx);
PCLK2 = RCCU_FrequencyValue(RCCU_FCLK);
Xpulse =(u16)((PCLK2*ms)/((CC+1)*1000));
return Xpulse;
}
/*
* 设置基准精度定时器
*/
void TIM_BaseTimerSet(TIM_TypeDef* TIMx, u16 ms, TIM_Channels channel)
{
u16 XpulseLength;
TIM_PrescalerConfig(TIMx, 23); //配置分频器及设置定时比较模式及定时中断
XpulseLength = TIM_GetOCiRForTimer(TIMx, ms);
TIM_OCMPModeConfig(TIMx,channel,XpulseLength,TIM_TIMING,TIM_HIGH);
if(TIMx==TIM2&&channel==TIM_CHANNEL_A) //配置相应定时器中断允许及开始发生
{
TIM_ITConfig(TIM2, TIM_OCAIE_mask, ENABLE);
EIC_IRQChannelConfig(T2TIMI_IRQChannel, ENABLE);
EIC_IRQChannelPriorityConfig(T2TIMI_IRQChannel, T2TIMIPriority);
TIM_CounterConfig(TIM2, TIM_START);
}
}
TIM_Init(TIM2); //初始化定时器0
TIM_BaseTimerSet(TIM2,20,TIM_CHANNEL_A); //配置基准定时器0为20MS定时器
两种方法设计的程序,结果都是产生20MS的定时,定时中断程序如下:
void T2TIMI_IRQHandler(void)
{
TIM_CounterConfig(TIM2,TIM_CLEAR);
TIM2->SR&=~TIM_TO_IT; //清除TIM2中断标志
TIM2->SR&=~TIM_OCA_IT; //清除TIM2中断标志
if(g_sec>0) //1秒计时单元递减
{
g_sec--;
}
if(g_sec==0)
{
g_sec=50; //1s计时:20ms * 50 = 1s
sec_flag = 1;
Event_Second(); //1S事件处理,1s累加计时
}
}
在Event_Second();中,进行以秒为单位的时间累加,现在发现,时间每增加1个小时,总是比电脑慢1S钟左右,我调整了初始配置:
void TIM_CounterConfig ( TIM_TypeDef *TIMx, TIM_CounterOperations Xoperation)
{
switch ( Xoperation )
{
case TIM_START :
TIMx->CR1 |= TIM_EN_Mask;
break;
case TIM_STOP :
TIMx->CR1 &= ~TIM_EN_Mask;
break;
case TIM_CLEAR :
TIMx->CNTR = 0x1234+0x1234;
break;
}
}
如上TIMx->CNTR = 0x1234+0x1234;原为TIMx->CNTR = 0x1234;但是没有任何改变,想请教版主和高手,帮忙分析一下,谢谢 |