个人觉得ST的库函数用起来还是挺不错的,之前都是使用自己写的函数,容易改出错,以下是验证过的,TIM1定时100微妙中断一次。
void TIM1_TimeBaseInit(u16 TIM1_Prescaler, TIM1_CounterMode_TypeDef TIM1_CounterMode, u16 TIM1_Period, u8 TIM1_RepetitionCounter) { /* Check parameters */ assert_param(IS_TIM1_COUNTER_MODE_OK(TIM1_CounterMode)); /* Set the Autoreload value */ TIM1->ARRH = (u8)(TIM1_Period >> 8); TIM1->ARRL = (u8)(TIM1_Period); /* Set the Prescaler value */ TIM1->PSCRH = (u8)(TIM1_Prescaler >> 8); TIM1->PSCRL = (u8)(TIM1_Prescaler); /* Select the Counter Mode */ TIM1->CR1 = (u8)(((TIM1->CR1) & (u8)(~(TIM1_CR1_CMS | TIM1_CR1_DIR))) | (u8)(TIM1_CounterMode)); /* Set the Repetition Counter value */ TIM1->RCR = TIM1_RepetitionCounter; } void TIM1_Cmd(FunctionalState NewState) { /* Check the parameters */ assert_param(IS_FUNCTIONALSTATE_OK(NewState)); /* set or Reset the CEN Bit */ if (NewState != DISABLE) { TIM1->CR1 |= TIM1_CR1_CEN; } else { TIM1->CR1 &= (u8)(~TIM1_CR1_CEN); } } void TIM1_ITConfig(TIM1_IT_TypeDef TIM1_IT, FunctionalState NewState) { /* Check the parameters */ assert_param(IS_TIM1_IT_OK(TIM1_IT)); assert_param(IS_FUNCTIONALSTATE_OK(NewState)); if (NewState != DISABLE) { /* Enable the Interrupt sources */ TIM1->IER |= (u8)TIM1_IT; } else { /* Disable the Interrupt sources */ TIM1->IER &= (u8)(~(u8)TIM1_IT); } } void TIM1_TimerInit(u16 Timer1Time) { assert_param(IS_TIM1TIMERTIME_OK(Timer1Time)); TIM1_DeInit();//复位TIM1所有参数 TIM1_TimeBaseInit(TIM1_PRESCALER_16,TIM1_COUNTERMODE_DOWN,Timer1Time,0); //TIM1的预分频器基于一个由16位寄存器 //选择16分频 16M/16=1M 周期为1us TIM1_Cmd(ENABLE);//使能计时器 TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);//使能TIM1中断 } 初始化时,调用 TIM1_TimerInit(100);//定时中断100微妙一次
STM8S TIM1库函数应用,布布扣,bubuko.com
原文地址:http://blog.csdn.net/chuangwu2009/article/details/25977207