STM32定時(shí)器時(shí)間的計(jì)算方法
STM32中的定時(shí)器有很多用法:
(一)系統(tǒng)時(shí)鐘(SysTick)
設(shè)置非常簡(jiǎn)單,以下是產(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)
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)(來(lái)自大蝦網(wǎng),未經(jīng)檢驗(yàn))
(1)TIMx(1-8),在庫(kù)設(shè)置默認(rèn)的情況下,都是72M的時(shí)鐘;
(2)TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
是重復(fù)計(jì)數(shù),就是重復(fù)溢出多少次才給你來(lái)一個(gè)溢出中斷,
它對(duì)應(yīng)的寄存器叫TIM1 RCR.
如果這個(gè)值不配置,上電的時(shí)候寄存器值可是隨機(jī)的,本來(lái)1秒中斷一次,可能變成N秒中斷一次,讓你超級(jí)頭大!
假設(shè)系統(tǒng)時(shí)鐘是72Mhz,TIM1是由PCLK2(72MHz)得到,TIM2-7是由PCLK1得到
關(guān)鍵是設(shè)定時(shí)鐘預(yù)分頻數(shù),自動(dòng)重裝載寄存器周期的值
定時(shí)器的基本設(shè)置
1、 TIM_TimeBaseStructure.TIM_Prescaler = 7199;//時(shí)鐘預(yù)分頻數(shù) 例如:時(shí)
鐘頻率=72/(時(shí)鐘預(yù)分頻+1)
2、TIM_TimeBaseStructure.TIM_Period = 9999; //自動(dòng)重裝載寄存器周期的值(定時(shí)
時(shí)間) 累計(jì)0xFFFF個(gè)頻率后產(chǎn)生個(gè)更新或者中斷(也是說(shuō)定時(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)燈閃爍
#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)));
}
}
- PC官方版
- 安卓官方手機(jī)版
- IOS官方手機(jī)版