stm32之定時(shí)器徹底研究
相比較而言,直接操作定時(shí)器比較簡(jiǎn)潔,對(duì)著寄存器看十分明了。而使用庫(kù)文件有一點(diǎn)暈頭轉(zhuǎn)向。
(個(gè)人觀點(diǎn))
程序如下:(以下程序在DX32的例程修改而來(lái),使用的是比較古老的3.0固件庫(kù))
1、timer.c文件
#include "STM32Libstm32f10x.h"
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 4000;
u16 CCR2_Val = 2000;
u16 CCR3_Val = 1000;
u16 CCR4_Val = 500;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 4000;
u16 CCR2_Val = 2000;
u16 CCR3_Val = 1000;
u16 CCR4_Val = 500;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 10000; //計(jì)滿值
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //預(yù)分頻,此值+1為分頻的除數(shù)
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; //輸出比較非主動(dòng)模式
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //極性為正
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //禁止OC1重裝載,其實(shí)可以省掉這句,因?yàn)槟J(rèn)是4路都不重裝的.
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM3_Configuration(u16 p,u16 psc)
{
RCC->APB1ENR|=1<<1;//TIM3時(shí)鐘使能
//自動(dòng)裝載寄存器
TIM3->ARR=p; //設(shè)定定時(shí)器自動(dòng)重裝值
//PSC預(yù)分頻寄存器
TIM3->PSC=psc; //設(shè)定定時(shí)器的分頻系數(shù)
TIM3->DIER|=1<<0; //允許更新中斷
TIM3->DIER|=1<<6; //允許觸發(fā)中斷
TIM3->CR1|=0X01; //使能定時(shí)器3(這里面包括計(jì)數(shù)方向?yàn)橄蛏嫌?jì)數(shù))
}
#if 0
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM3->DIER|=1<<6; //允許觸發(fā)中斷
TIM3->CR1|=0X01; //使能定時(shí)器3(這里面包括計(jì)數(shù)方向?yàn)橄蛏嫌?jì)數(shù))
}
#if 0
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 10000; //計(jì)滿值
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //預(yù)分頻,此值+1為分頻的除數(shù)
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
#else
void TIM_Configuration(u16 p,u16 psc)
{
RCC->APB1ENR|=1<<2;//TIM4時(shí)鐘使能
//自動(dòng)裝載寄存器
TIM4->ARR=p; //設(shè)定定時(shí)器自動(dòng)重裝值
//PSC預(yù)分頻寄存器
TIM4->PSC=psc; //設(shè)定定時(shí)器的分頻系數(shù)
TIM4->DIER|=1<<0; //允許更新中斷
TIM4->DIER|=1<<6; //允許觸發(fā)中斷
TIM4->CR1|=0X01; //使能定時(shí)器3(這里面包括計(jì)數(shù)方向?yàn)橄蛏嫌?jì)數(shù))
}
#endif
TIM4->DIER|=1<<6; //允許觸發(fā)中斷
TIM4->CR1|=0X01; //使能定時(shí)器3(這里面包括計(jì)數(shù)方向?yàn)橄蛏嫌?jì)數(shù))
}
#endif
上程序中,定時(shí)器2被配置成多路捕獲模式,定時(shí)器3是直接操作寄存器進(jìn)行配置的。
定時(shí)器4用了兩種配置方式,使用固件庫(kù)和直接操作寄存器。可以切換。效果一樣。
需要注意的是,stm32103RBT6的通用定時(shí)器只有2、3、4.(沒(méi)有5)
2、stm32f10x_it.c文件
unsigned int cnt=0;
unsigned int flag=0;
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
//可添加功能塊......
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
//可添加功能塊......
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
//可添加功能塊......
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
//可添加功能塊......
}
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//flag=1;//計(jì)時(shí)滿標(biāo)志位置位
//cnt++;//每TIM_Period計(jì)時(shí)滿變量加一
}
}
unsigned int flag=0;
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
//可添加功能塊......
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
//可添加功能塊......
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
//可添加功能塊......
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
//可添加功能塊......
}
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//flag=1;//計(jì)時(shí)滿標(biāo)志位置位
//cnt++;//每TIM_Period計(jì)時(shí)滿變量加一
}
}
void TIM3_IRQHandler(void)
{
if(TIM3->SR&0X0001)
{
cnt++;
flag=1;
}
TIM3->SR&=~(1<<0);
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
cnt++;
flag=1;
}
}
可以看到,定時(shí)器2有多個(gè)處理的事件,四個(gè)通道的計(jì)數(shù)溢出和定時(shí)器的總溢出。具體事件根據(jù)應(yīng)用來(lái)配置。
另外,以上代碼只是對(duì)三個(gè)通用定時(shí)器進(jìn)行測(cè)試,具體應(yīng)用根據(jù)情況來(lái)定。
3、NVIC.c文件
#include "STM32Libstm32f10x.h"
//設(shè)置所有的中斷允許
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
關(guān)鍵詞:
stm32定時(shí)器主從模
相關(guān)推薦
技術(shù)專區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線
- 開(kāi)關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開(kāi)發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車電子
- 轉(zhuǎn)換器
- 電源管理
- 信號(hào)放大器
評(píng)論