微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 通用定时器与滴答定时器

STM32 通用定时器与滴答定时器

时间:11-19 来源:互联网 点击:
STM32 通用定时器滴答定时器

  • 前言:STM32包括TIM1/TIM8两个高级定时器,TIM2~TIM5四个通用定时器,TIM6/TIM7两个基本定时器,还有使用非常方便的系统滴答定时器(systick),基本操作为:配置(使能时钟/设置工作方式及初值/配置中断/开启中断和时钟/使能定时器)——>计数——>产生中断。

一、通用定时器使用

void TimerConfig(void){TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;// 使能时钟/复位/自动重装载设置/时钟预分频设置/采样分频/up/清除中断/开启时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);TIM_DeInit(TIM2);  TIM_TimeBaseStructure.TIM_Period = 2000 - 1;TIM_TimeBaseStructure.TIM_Prescaler = (36000 - 1);TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);TIM_ClearFlag(TIM2,TIM_FLAG_Update);TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);TIM_Cmd(TIM2,ENABLE);// 中断设置(组1/通道2/抢占式中断优先级0/响应式中断优先级0/使能中断)NVIC_InitTypeDef NVIC_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);}void TIM2_IRQHandler(void){if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET){// 清除中断标志TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);timer_flag++;}}
二、滴答定时器

void SysTmerConfig(void){if (SysTick_Config(SystemCoreClock / CLOCK_SECOND)){while (1);}}void SysTick_Handler(void){sys_timer_flag++;}


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top