最近的设计上需要用到STM32F103,很久没用过这个芯片了。还是写个调试记录方便以后查阅。
关于定时器倒是比较简单
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, 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(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
timenum++;timecheck++;timeDis++;timeADC++;
}
}
我只需要一个定时器,直接复制这个就完事了。需要注意的是,void TIM3_Int_Init(u16 arr,u16 psc) 函数有两个输入的值。其中psc设置计数频率,如果写TIM3_Int_Init(4999,7199) ,我这里是72M的频率,所以计数频率就是72M除以psc+1,得出10000。计数周期的时间就是0.1ms。计数的次数是0.1*(arr+1),结果为500ms。得出500ms进入中断一次。
|