之初探定时器
时间:10-02
整理:3721RD
点击:
stm32好久没怎么鼓捣了,最近想写个定时器程序,查看下历程没有最简单的定时器,我就复制例程中比较基础的进行修改。看了下stm32f412参考手册,感觉f412的定时器功能很全,而且我简单的看了下f412的时钟树,感觉挺强大比我看到的f103的时钟。
因为需求是最基本的定时器产生50us的中断,所以采用定时器6,最后研读代码时发现定时器的时钟源没有在参考手册上找到详细的介绍,后来发现412的数据手册上有简单的介绍定时器的时钟源由APB1和APB2决定的其中APB1最高时钟是50MHz而APB2最高事100Hz。
- <div>/*定时器频率 50us 进一次中断*/
- #define timerINTERRUPT_FREQUENCY 20000
- /* 中断优先级 */
- #define timerHIGHEST_PRIORITY 1
- /* 系统调用 */
- volatile uint32_t ulHighFrequencyTimerTicks = 0UL;
- void HAL_TIM6_Init(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;</div><div>
- /* Compute the prescaler value
- To get TIM6 counter clock at 20000Hz, the prescaler is computed as follows:
- Prescaler = (TIM6CLK / TIM6 counter clock) - 1
- Prescaler = ((SystemCoreClock /2) /20000Hz) - 1*/</div><div>
- TIM_DeInit(TIM6);
- TIM_TimeBaseStructure.TIM_Period = timerINTERRUPT_FREQUENCY;
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;//TIM_CKD_div1 means no division
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
- TIM_ARRPreloadConfig(TIM6, ENABLE);
- /* Enable the TIM6 global Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = timerHIGHEST_PRIORITY;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* TIM Interrupts enable */
- TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
- /* TIM6 enable counter */
- TIM_Cmd(TIM6, ENABLE);
- }
- void TIM6_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)
- {
- ulHighFrequencyTimerTicks++;
- TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
- }
- }</div>