stm32 高階定時器1配置注意
阿新 • • 發佈:2019-02-11
void TIM1_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //①時鐘 TIM1 使能
//定時器 TIM1 初始化
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 向上計數
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //高階定時器1是用定時器功能配置這個才可以是正常的計數頻率一開始的72mhz 值得注意的地方
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //②初始化 TIM1
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //③允許更新中斷
//中斷優先順序 NVIC 設定
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1 中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先佔優先順序 1 級
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優先順序 3 級
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
NVIC_Init(&NVIC_InitStructure); //④初始化 NVIC 暫存器
TIM_Cmd(TIM1, ENABLE); //⑤使能 TIM1
}
void TIM1_UP_IRQHandler(void) //TIM1 中斷
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //檢查 TIM1 更新中斷髮生與否
{
TIM_ClearFlag(TIM1, TIM_IT_Update ); //清除 TIM1 更新中斷標誌
LED1=!LED1;
}
}
上面紅字必須注意 因為高階定時器的時基是四個 比通用定時器多一個:
順便再來一個通用定時器的配置: void TIM4_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //時鐘使能 //定時器TIM4初始化 TIM_TimeBaseStructure.TIM_Period = arr; //設定在下一個更新事件裝入活動的自動重灌載暫存器週期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設定用來作為TIMx時鐘頻率除數的預分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設定時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據指定的引數初始化TIMx的時間基數單位 TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM4中斷,允許更新中斷 //中斷優先順序NVIC設定 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先佔優先順序0級 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優先順序3級 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //初始化NVIC暫存器 TIM_Cmd(TIM4, ENABLE); //使能TIMx } //定時器4中斷服務程式 void TIM4_IRQHandler(void) //TIM3中斷 { if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //檢查TIM4更新中斷髮生與否 { TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx更新中斷標誌 LED0=!LED0; // Get_Angle(); } }
順便再來一個通用定時器的配置: void TIM4_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //時鐘使能 //定時器TIM4初始化 TIM_TimeBaseStructure.TIM_Period = arr; //設定在下一個更新事件裝入活動的自動重灌載暫存器週期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設定用來作為TIMx時鐘頻率除數的預分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設定時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據指定的引數初始化TIMx的時間基數單位 TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM4中斷,允許更新中斷 //中斷優先順序NVIC設定 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先佔優先順序0級 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優先順序3級 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //初始化NVIC暫存器 TIM_Cmd(TIM4, ENABLE); //使能TIMx } //定時器4中斷服務程式 void TIM4_IRQHandler(void) //TIM3中斷 { if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //檢查TIM4更新中斷髮生與否 { TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx更新中斷標誌 LED0=!LED0; // Get_Angle(); } }