/******************************************************************
文檔名:Timer
編譯器:MDK5.24.0
M C U:STM32F103RB
使用庫:stm32f10x_xxx V3.5.0
作 用:配置TIM2 TIM3 TIM4 TIM5的base。椋恪
說 明:例程寫在STM32F103RB上 應該適用于103全系列
******************************************************************/
#include "Config.h"
#include "stm32f10x.h"
TIME_TYDEDEFS time_structs;
/******************************************************************
函數名: void Timer_RCC_APB1_Config(uint32_t RCC_APB1Periph_Tim,uint32_t RCC_APB1Periph_Port)
參 數:
uint32_t RCC_APB1Periph_Tim APB1上的Tim時鐘
uint32_t RCC_APB1Periph_Port 相關端口
返回值:無
作 用:配置TIM3的RCC和GPIOA時鐘
說 明:
******************************************************************/
void Timer_RCC_APB1_Config(uint32_t RCC_APB1Periph_Tim,uint32_t RCC_APB1Periph_Port)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_Tim, ENABLE); //時鐘使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_Port, ENABLE); //使能GPIOA時鐘
}
/******************************************************************
函數名: void Timer_RCC_APB2_Config(uint32_t RCC_APB2Periph_Tim,uint32_t RCC_APB1Periph_Port)
參 數:
uint32_t RCC_APB2Periph_Tim APB1上的Tim時鐘
uint32_t RCC_APB1Periph_Port 相關端口
返回值:無
作 用:配置掛在
說 明:
******************************************************************/
void Timer_RCC_APB2_Config(uint32_t RCC_APB2Periph_Tim,uint32_t RCC_APB1Periph_Port)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_Tim, ENABLE); //時鐘使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_Port, ENABLE); //使能GPIOA時鐘
}
/******************************************************************
函數名:extern void Timer3_Init(u16 arr,u16 psc);
參 數:
u16 arr 重裝值
u16 psc 預分頻值
返回值:無
作 用:初始化timer3
說 明:主頻72MHZ 周期為(0.1ms * arr)
Timerx_Init(5000,7199); 為500ms
TIMclk = 1/10000 s 相當于一步 100us
******************************************************************/
void Timer_Base_Init(TIM_TypeDef* TIMx,u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 計數到5000為500ms
TIM_TimeBaseStructure.TIM_Prescaler = (psc-1); //設置用來作為TIMx時鐘頻率除數的預分頻值 10Khz的計數頻率
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設置時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的參數初始化TIMx的時間基數單位
}
/******************************************************************
函數名:void Timer3_Cap_IO_Config(void)
參 數:無
返回值:無
作 用:
說 明:
******************************************************************/
void Timer_Cap_IO_Config(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin,GPIOMode_TypeDef GPIO_Mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //需要修改
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_Init(GPIOx, &GPIO_InitStructure);
}
/******************************************************************
函數名:void Timer_OC_Init(TIM_TypeDef* TIMx,u16 channle)
參 數:
TIM_TypeDef* TIMx 定時器選擇 x=0到17
u16 channle 通道選擇
返回值:無
作 用:使能捕獲比較輸出模塊
說 明:
******************************************************************/
void Timer_OC_Init(TIM_TypeDef* TIMx,u16 channle)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_Pulse = 0; //設置待裝入捕獲比較寄存器的脈沖值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根據TIM_OCInitStruct中指定的參數初始化外設TIMx
TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable); //使能TIMx在CCR2上的預裝載寄存器
}
/******************************************************************
函數名:void Timer3_Cap_IC_Config(u16 channle,u16 tim_icfilter,u16 tim_icpolarity)
參 數:
u16 channle 通道值
u16 tim_icfilter 濾波值0x00 -0x0f可選
u16 tim_icpolarity TIM_ICPolarity_Rising 上升沿
返回值:無 TIM_ICPolarity_Falling 下降沿
作 用: TIM_ICPolarity_BothEdge 雙沿
說 明:配置輸入捕獲的方式
******************************************************************/
void Timer_Cap_IC_Config(TIM_TypeDef* TIMx,u16 channle,u16 tim_icfilter,u16 tim_icpolarity)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = channle; //CC1S=01 選擇輸入端 IC1映射到TI1上
TIM_ICInitStructure.TIM_ICPolarity = tim_icpolarity; //上升沿捕獲
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置輸入分頻,不分頻
TIM_ICInitStructure.TIM_ICFilter = 0x0F;//IC1F=0000 配置輸入濾波器 不濾波
TIM_ICInit(TIMx, &TIM_ICInitStructure);
}
/******************************************************************
函數名:void Timer3_NVIC_Config(void)
參 數:無
返回值:無
作 用:配置TIM3 NVIC
說 明:
******************************************************************/
void Timer_NVIC_Config(TIM_TypeDef* TIMx,FunctionalState NewState)
{
u8 IRQ ;
NVIC_InitTypeDef NVIC_InitStructure;
if(TIMx == TIM2)
{
IRQ = TIM2_IRQn;
}
else if(TIMx == TIM3)
{
IRQ = TIM3_IRQn;
}
else if(TIMx == TIM4)
{
IRQ = TIM4_IRQn;
}
NVIC_InitStructure.NVIC_IRQChannel = IRQ; //TIM中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占優先級0級
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優先級3級
NVIC_InitStructure.NVIC_IRQChannelCmd = NewState; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);
}
/******************************************************************
函數名: void Timer3_RCC_Config(void)
參 數:無
返回值:無
作 用:配置TIM3的RCC和GPIOA時鐘
說 明:
******************************************************************/
void Timer2_Config(void)
{
Timer_RCC_APB1_Config(RCC_APB1Periph_TIM2,RCC_APB2Periph_GPIOA) ;
Timer_Base_Init(TIM2,TIM_CYCLE_10US,TIM_UNIT_1US); //20ms
// Timer2_Cap_IO_Config(GPIOA,GPIO_Pin_1);
// Timer2_Cap_IC_Config(TIM_Channel_2,0x00,TIM_ICPolarity_Falling);
// Timer2_Cap_IO_Config(GPIOA,GPIO_Pin_2);
// Timer2_Cap_IC_Config(TIM_Channel_3,0x00,TIM_ICPolarity_Falling);
Timer_NVIC_Config(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE ); //只使能UPdate 中斷
TIM2_START(); //使能TIMx外設
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
memset(&time_structs.time2_struct,0,sizeof(time_structs.time2_struct));
}
/******************************************************************
函數名: void TIM2_IRQHandler(void)
參 數:無
返回值:無
作 用:配置TIM3的RCC和GPIOA時鐘
說 明:
******************************************************************/
void TIM2_IRQHandler(void) //TIM3中斷
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //檢查指定的TIM中斷發生與否:TIM 中斷源
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除TIMx的中斷待處理位:TIM 中斷源
time_structs.time2_struct.timer_base_count++;
EPS8266_01_Time_Delay(TIM2,time_structs.time2_struct.timer_base_count);
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) // 識別前進
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2 ); //
time_structs.time2_struct.timer_cc2_count ++;
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) //檢查指定的TIM中斷發生與否:TIM 中斷源
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3 ); //清除TIMx的中斷待處理位:TIM 中斷源
time_structs.time2_struct.timer_cc3_count ++;
}
}
|