STM32定时器时间的计算方法

时间:2023-01-31 23:34:23

本文出自:https://wenku.baidu.com/view/e3bdfb7601f69e31433294c4.html


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)

/*TIM3时钟配置*/
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 */

注意两点(来自大虾网,未经检验)
1TIMx(1-8),在库设置默认的情况下,都是72M的时钟;
2TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
是重复计数,就是重复溢出多少次才给你来一个溢出中断,
它对应的寄存器叫TIM1 RCR.
如果这个值不配置,上电的时候寄存器值可是随机的,本来1秒中断一次,可能变成N秒中断一次,让你超级头大!

 

假设系统时钟是72MhzTIM1是由PCLK272MHz)得到,TIM2-7是由PCLK1得到

关键是设定时钟预分频数,自动重装载寄存器周期的值

/*1秒发生一次更新事件(进入中断服务程序)RCC_Configuration()SystemInit()

RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2表明TIM3CLK72MHz。因此,每次进入中

断服务程序间隔时间为

((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1*/

 定时器的基本设置

   1、   TIM_TimeBaseStructure.TIM_Prescaler = 7199;//时钟预分频数  例如:时

钟频率=72/(时钟预分频+1)

   2TIM_TimeBaseStructure.TIM_Period = 9999; //自动重装载寄存器周期的值(定时

时间)    累计0xFFFF个频率后产生个更新或者中断(也是说定时时间到)

   3、  TIM_TimeBaseStructure.TIM_CounterMode =  TIM1_CounterMode_Up; //定时器

模式向上计数

    

     4TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //时间分割值

     5TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//初始化定时器2

     6TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);  //打开中断  溢出中断  

     7TIM_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通用定时器的基本定时器功能实现灯闪烁

/*MAIN.C*/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "misc.h"

/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);

/* Private functions ---------------------------------------------------------*/


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)
{

/*1秒发生一次更新事件(进入中断服务程序)RCC_Configuration()SystemInit()RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2表明TIM3CLK72MHz

因此,每次进入中断服务程序间隔时间为((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1*/


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);/*此函数的语句"TIMx->EGR = TIM_PSCReloadMode_Immediate;"以软件方式产生更新事件(:当发生一个更新事件时,所有的寄存器都被更新,硬件同时(依据URS)设置更新标志位(TIMx_SR寄存器中的UIF))*/
}

void RCC_Configuration(void)
{
SystemInit();
  
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

/* GPIOC clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

}


void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the TIM3 gloabal Interrupt*/
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);

/*:不用为实现通用定时器的基本定时器功能配置Pin*/
}

/*stm32f10x_it.c*/

/* Includes ------------------------------------------------------------------*/

#include "stm32f10x_it.h"

void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
   /* Clear TIM3 update interrupt */
   TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
   GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));