Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> STM32定時器時間的計算方法

STM32定時器時間的計算方法

日期:2017/2/7 14:32:50      編輯:Linux教程
 

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)));
}
}

Copyright © Windows教程網 All Rights Reserved