通用定时器是一个通过可编程预分频器驱动的16 位自动装载计数器构成。
先初始化
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1; //自动装载
TIM_TimeBaseStructure.TIM_Prescaler = 7200; //72M分频率到10KHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down; //向下计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
延迟函数
void Delay(vu32 nCount)
{
u16 TIMCounter = nCount;
TIM_Cmd(TIM2, ENABLE);
TIM_SetCounter(TIM2, TIMCounter);
while (TIMCounter>1)
{
TIMCounter = TIM_GetCounter(TIM2);
}
TIM_Cmd(TIM2, DISABLE);
}
当 nCount =10000时候,精确延时1秒。
用户377235 2013-2-26 09:26