江科大學(xué)習(xí)筆記 _5.外部時(shí)鐘源中斷配置

?

外部時(shí)鐘計(jì)數(shù):
1.?RCC時(shí)鐘開(kāi)啟
Timer2掛載在APB1總線(xiàn)上
RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM2EN, ENABLE);
?
2.?選擇時(shí)基單元的時(shí)鐘源
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x0F); //使用外部時(shí)鐘源PA0
定時(shí)器2的外部輸入為PA0引腳,STM32定義,可以查看手冊(cè)
?
所以 GPIO PA0也要配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
????
????GPIO_InitTypeDef GPIO_InitStruct;
????GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
????GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
????GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
????GPIO_Init(GPIOA,&GPIO_InitStruct);
?
3.?配置時(shí)基單元
????TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
????TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//濾波分頻
????TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
????TIM_TimeBaseInitStruct.TIM_Period = 10000 - 1;
????TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;
????TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
?
?TIM_ClearFlag(TIM2, TIM_FLAG_Update);//防止初始化完就進(jìn)中斷的問(wèn)題
?
4.?配置輸出中斷控制,允許更新中斷到輸出NVIC
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
?
?
5.?配置NVIC,打開(kāi)定時(shí)器同通道,優(yōu)先級(jí)
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
????
????NVIC_InitTypeDef NVIC_InitStruct;
????NVIC_InitStruct.NVIC_IRQChannel = ?TIM2_IRQn;
????NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
????NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
????NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
?
6.?運(yùn)行控制
TIM_Cmd(TIM2,ENABLE);
?
?
中斷服務(wù)函數(shù)
void TIM2_IRQHandler(void)
{
????
????if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
????{
????????
????????Num++;
????????
????????TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
????}
}
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?