STM32定時(shí)器定時(shí)時(shí)長計(jì)算公式
T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK
其中,TIMxCLK是其時(shí)鐘頻率,若是晶振為8MHz的,一般為72MHz
STM32定時(shí)器時(shí)間的計(jì)算方法
STM32中的定時(shí)器有很多用法:
(一)系統(tǒng)時(shí)鐘(SysTick)
設(shè)置非常簡單,以下是產(chǎn)生1ms中斷的設(shè)置,和產(chǎn)生10ms延時(shí)的函數(shù):
void RCC_Configuration(void)
{
RCC_ClocksTypeDef RCC_ClockFreq;
SystemInit();//源自system_stm32f10x.c文件,只需要調(diào)用此函數(shù),則可完成RCC的配置。
RCC_GetClocksFreq(&RCC_ClockFreq);
//SYSTICK分頻--1ms的系統(tǒng)時(shí)鐘中斷
if (SysTick_Config(SystemFrequency / 1000))
{
while (1); // Capture error
}
}
void SysTick_Handler(void)//在中斷處理函數(shù)中的程序
{
while(tim)
{
tim--;
}
}
//調(diào)用程序:
Delay_Ms(10);
當(dāng)然,前提是要設(shè)置好,變量tim要設(shè)置成volatile類型的。
(二)第二種涉及到定時(shí)器計(jì)數(shù)時(shí)間(TIMx)
/*TIM3時(shí)鐘配置*/
TIM_TimeBaseStructure.TIM_Prescaler = 2; //預(yù)分頻(時(shí)鐘分頻)72M/(2+1)=24M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
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);
定時(shí)時(shí)間計(jì)算:
TIM_TimeBaseStructure.TIM_Prescaler = 2;
//分頻2 72M/(2+1)/2=24MHz
TIM_TimeBaseStructure.TIM_Period = 65535; //計(jì)數(shù)值65535
((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+2)/72M)*(1+65535)=0.00273秒=366.2Hz */
注意兩點(diǎn)(來自大蝦網(wǎng),未經(jīng)檢驗(yàn))
(1)TIMx(1-8),在庫設(shè)置默認(rèn)的情況下,都是72M的時(shí)鐘;
(2)TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
是重復(fù)計(jì)數(shù),就是重復(fù)溢出多少次才給你來一個溢出中斷,
它對應(yīng)的寄存器叫TIM1 RCR.
如果這個值不配置,上電的時(shí)候寄存器值可是隨機(jī)的,本來1秒中斷一次,可能變成N秒中斷一次,讓你超級頭大!
假設(shè)系統(tǒng)時(shí)鐘是72Mhz,TIM1是由PCLK2(72MHz)得到,TIM2-7是由PCLK1得到
關(guān)鍵是設(shè)定時(shí)鐘預(yù)分頻數(shù),自動重裝載寄存器周期的值
/*每1秒發(fā)生一次更新事件(進(jìn)入中斷服務(wù)程序)。RCC_Configuration()的SystemInit()的
RCC-》CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2表明TIM3CLK為72MHz。因此,每次進(jìn)入中
斷服務(wù)程序間隔時(shí)間為
((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+7199)/72M)*(1+9999)=1秒*/
定時(shí)器的基本設(shè)置
1、 TIM_TimeBaseStructure.TIM_Prescaler = 7199;//時(shí)鐘預(yù)分頻數(shù) 例如:時(shí)
鐘頻率=72/(時(shí)鐘預(yù)分頻+1)
2、TIM_TimeBaseStructure.TIM_Period = 9999; //自動重裝載寄存器周期的值(定時(shí)
時(shí)間) 累計(jì)0xFFFF個頻率后產(chǎn)生個更新或者中斷(也是說定時(shí)時(shí)間到)
3、 TIM_TimeBaseStructure.TIM_CounterMode = TIM1_CounterMode_Up; //定時(shí)器
模式 向上計(jì)數(shù)
4、TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //時(shí)間分割值
5、TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//初始化定時(shí)器2
6、TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //打開中斷 溢出中斷
7、TIM_Cmd(TIM2, ENABLE);//打開定時(shí)器
或者:
TIM_TimeBaseStructure.TIM_Prescaler = 35999;//分頻35999 72M/
(35999+1)/2=1Hz 1秒中斷溢出一次
TIM_TimeBaseStructure.TIM_Period = 2000; //計(jì)數(shù)值2000
((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+35999)/72M)*(1+2000)=1秒*/
STM32通用定時(shí)器的基本定時(shí)器功能實(shí)現(xiàn)燈閃爍
/*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);/*清除更新標(biāo)志位*/
TIM_ARRPreloadConfig(TIM3, DISABLE);/*預(yù)裝載寄存器的內(nèi)容被立即傳送到影子寄存器*/
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
while (1) {
;
}
}
void TIM3_Configuration(void)
{
/*每1秒發(fā)生一次更新事件(進(jìn)入中斷服務(wù)程序)。RCC_Configuration()的SystemInit()的RCC-》CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2表明TIM3CLK為72MHz。
因此,每次進(jìn)入中斷服務(wù)程序間隔時(shí)間為((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);/*此函數(shù)的語句“TIMx-》EGR = TIM_PSCReloadMode_Immediate;”以軟件方式產(chǎn)生更新事件(注:當(dāng)發(fā)生一個更新事件時(shí),所有的寄存器都被更新,硬件同時(shí)(依據(jù)URS位)設(shè)置更新標(biāo)志位(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);
/*注:不用為實(shí)現(xiàn)通用定時(shí)器的基本定時(shí)器功能配置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)));
評論
查看更多