STM32 TIMER2的使用
扫描二维码
随时随地手机看文章
配置定时100us的配置如下:
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period=1;//59999;//1199;//9999;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_Prescaler=3499;//分频35999
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_IT_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,DISABLE);
//((1+TIM_Prescaler )/f_soc)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1秒
time = ((1+3499)/72M)*(1+1) =100us :f_soc =72MHz
看STM32的时钟树可以知道 Timer的f_soc= APB1_Clk * APB1_Prescaler
APB1_Clk = AHB_Clk /APB1_Prescaler
AHB_CLk = Sysclk /AHB_Prescaler
我这里的时钟设置 Sysclk = 72MHz
AHB_Prescaler = 1 => AHB_Clk = 72MHz
因为 APB1_CLK_max = 36MHz
所以 APB1_Prescaler =2
于是 Timer2 的f_soc = 72MHz