stm32定时器可以这么用
扫描二维码
随时随地手机看文章
STM32中的定时器有很多用法:
(一)系统时钟(SysTick)
设置非常简单,以下是产生1ms中断的设置,和产生10ms延时的函数:
void RCC_Configuration(void)
{
RCC_ClocksTypeDef RCC_ClockFreq;
SystemInit();/源自system_stm32f10x.c文件,只需要调用此函数,则可完成RCC的配置.
RCC_GetClocksFreq(RCC_ClockFreq);
/SYSTICK分频--1ms的系统时钟中断
if (SysTick_Config(SystemFrequency / 1000))
{
while (1); / Capture error
}
}
void SysTick_Handler(void)/在中断处理函数中的程序
{
while(tim)
{
tim--;
}
}
/调用程序:
Delay_Ms(10);
当然,前提是要设置好,变量tim要设置成volatile类型的。
(二)第二种涉及到定时器计数时间(TIMx)
TIM_TimeBaseStructure.TIM_Prescaler = 2;/预分频(时钟分频)72M/(2 1)=24M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;/向上计数
TIM_TimeBaseStructure.TIM_Period = 65535; /装载值18k/144=125hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM3,TIM_TimeBaseStructure);
定时时间计算:
TIM_TimeBaseStructure.TIM_Prescaler = 2;
/分频2 72M/(2 1)/2=24MHz
TIM_TimeBaseStructure.TIM_Period = 65535; /计数值65535
((1 TIM_Prescaler)/72M)*(1 TIM_Period )=((1 2)/72M)*(1 65535)=0.00273秒=366.2Hz*/
注意两点(来自大虾网,未经检验)
(1)TIMx(1-8),在库设置默认的情况下,都是72M的时钟;
(2)TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
是重复计数,就是重复溢出多少次才给你来一个溢出中断,
它对应的寄存器叫TIM1 RCR.
如果这个值不配置,上电的时候寄存器值可是随机的,本来1秒中断一次,可能变成N秒中断一次,让你超级头大!
假设系统时钟是72Mhz,TIM1是由PCLK2(72MHz)得到,TIM2-7是由PCLK1得到
关键是设定时钟预分频数,自动重装载寄存器周期的值
定时器的基本设置
1、 TIM_TimeBaseStructure.TIM_Prescaler = 7199;/时钟预分频数 例如:时钟频率=72/(时钟预分频 1)
2、TIM_TimeBaseStructure.TIM_Period = 9999; /自动重装载寄存器周期的值(定时时间) 累计0xFFFF个频率后产生个更新或者中断(也是说定时时间到)
3、 TIM_TimeBaseStructure.TIM_CounterMode = TIM1_CounterMode_Up;/定时器
模式 向上计数
4、TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; /时间分割值
5、TIM_TimeBaseInit(TIM2,TIM_TimeBaseStructure);/初始化定时器2
6、TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /打开中断 溢出中断
7、TIM_Cmd(TIM2, ENABLE);/打开定时器
或者:
TIM_TimeBaseStructure.TIM_Prescaler = 35999;/分频35999 72M/
(35999 1)/2=1Hz 1秒中断溢出一次
TIM_TimeBaseStructure.TIM_Period = 2000; /计数值2000
((1 TIM_Prescaler )/72M)*(1 TIM_Period)=((1 35999)/72M)*(1 2000)=1秒*/
STM32通用定时器的基本定时器功能实现灯闪烁
#include stm32f10x.h
#include misc.h
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM3_Configuration();
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM3, DISABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
while (1) {
;
}
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 9999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,TIM_TimeBaseStructure);
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(NVIC_InitStructure);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, GPIO_InitStructure);
}
#include stm32f10x_it.h
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 -GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
}
}