STM32F051R8T6的通用定時(shí)器配置
void TIM3_Configuration(void) //每0.5ms發(fā)生一次更新事件(進(jìn)入中斷服務(wù)程序).
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3); //重新將Timer設(shè)置為缺省值
TIM_InternalClockConfig(TIM3); //采用內(nèi)部時(shí)鐘給TIM3提供時(shí)鐘源
TIM_ARRPreloadConfig(TIM3, DISABLE); //禁止ARR預(yù)裝載緩沖器 預(yù)裝載寄存器的內(nèi)容被立即傳送到影子寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 24-1; //預(yù)分頻系數(shù)為24-1,這樣計(jì)數(shù)器計(jì)數(shù)頻率為48MHz/24 = 2MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設(shè)置時(shí)鐘分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //設(shè)置計(jì)數(shù)器模式為向上計(jì)數(shù)模式
TIM_TimeBaseStructure.TIM_Period = 1000; //設(shè)置計(jì)數(shù)溢出大小,每計(jì)1000個(gè)數(shù)就產(chǎn)生一個(gè)更新事件
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //將配置應(yīng)用到TIM3中
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清除溢出中斷標(biāo)志
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能TIM3的更新中斷
TIM_Cmd(TIM3, ENABLE); //開(kāi)啟定時(shí)器3
//Enable the TIM3 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//---------------------------------0.5ms中斷一次---------------------------------------
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3 TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//添加代碼
}
}
評(píng)論