我用之前的定时器TIM2程序移植到新板子(新板子就增加几个IO外接),想先驱动LED看看,可发现LED就是不能亮灭,在原先板子上正常。程序如下:
int main(void)
{
RCC_GetClocksFreq(&RCC_Clocks); //获取系统时钟频率,外部晶振8.00M,倍频后系统时钟8.000*8/2=32.00M
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组配置
LED_Init(); //LED指示灯引脚初始化
TIMER2_delayinit(65535,31); //TIM2延时初始化,1us
TIM3_Counter_init(); //TIM3脉冲计时初始化
while(1)
{
LED_ON(); //打开LED
for(int i=0;i<1000;i++) //延时1s
TIMER2_usdelay(1000);
LED_OFF(); //关闭LED
}
}
在IAR上debug发现程序一直在 for(int i=0;i<1000;i++) 里跳不出去,以为1000过大不容易发现,改成i<10并单步执行,还是跳不出去。不知怎么回事?
//使用timer2 进行准确延时1us;
void TIMER2_delayinit(u32 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr; /*该数值为计数器计数次数*/
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清除更新标志
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM2, DISABLE); /* TIM counter disable*/
}
//TIM2延时
void TIMER2_usdelay(uint32_t usdelay)
{
uint32_t tmp;
tmp= 65535 - usdelay;
TIM_SetCounter(TIM2, tmp);
TIM_Cmd(TIM2, ENABLE);
while(TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) == RESET);
TIM_Cmd(TIM2, DISABLE);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
}
|