中级会员
- 积分
- 335
- 金钱
- 335
- 注册时间
- 2014-3-16
- 在线时间
- 111 小时
|
发表于 2017-7-11 22:12:57
|
显示全部楼层
#include "Freq_Measure.h"
#define FREQ_DIV TIM_ExtTRGPSC_OFF //TIM_ExtTRGPSC_DIV4//TIM_ExtTRGPSC_OFF //TIM_ExtTRGPSC_DIV4 //设置频率的输入分频系数
static float Freq=0;
static unsigned int FREQ_DIV_K; //输入分频系数值!
//*******************************************************************************************************
//STM32F205 pa0--ETR--tim2 的IO脚定义处理
void TIM2_ETR_IO_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//------------------------------------------------------------------------
GPIO_InitStructure.GPIO_Pin = ETR_IN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIOA Configuration: PA.0(TIM2 CH1) */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
}
//************************************************************************************************************
//定义TIM4 1秒钟产生内部定时触发
//*************************************************************************
//计算定时参数,用于Tim3,传入要定时的时间,单位是m秒 定时器的时钟倍频器2
//*************************************************************************
unsigned int Get_Timer_TIM4(float ms){
unsigned int tick;
RCC_ClocksTypeDef RCC_Clocks;
//---------------------------------------------------------------
//先计算分频系数!
FREQ_DIV_K=1;
if(FREQ_DIV ==TIM_ExtTRGPSC_DIV2) FREQ_DIV_K=2;
if(FREQ_DIV ==TIM_ExtTRGPSC_DIV4) FREQ_DIV_K=4;
if(FREQ_DIV ==TIM_ExtTRGPSC_DIV8) FREQ_DIV_K=8;
//---------------------------------------------------------------
RCC_GetClocksFreq(&RCC_Clocks);
tick=2*(RCC_Clocks.PCLK1_Frequency/TIM4_DIV/1000)*ms;//得到间隔常数!1000 是秒的转换,TIM4_DIV是分频系数!
return tick;
}
//*****************************************************************************
//捕捉测频
void TIM4_Config(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/**************************** TIM4 Config ***********************************/
/* TIM4 clock enable */
TIM_DeInit(TIM4);
TIM2_ETR_IO_Init();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* TIM4 Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler =TIM4_DIV-1; //分频数,降低频率,降低检测的频率!
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = Get_Timer_TIM4(MEASURE_ms); //xxx毫秒扫描一次!
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //定时1秒中断
//------------------------------------------------------------------------------------------------------
//设置主定是输出到到TIM2
/* Use the TIM4 Update event as TIM4 Trigger Output(TRGO) */
TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_Update);//送出TIM4更新事件UEV做为触发输出(TIM4_CR2寄存器的MMS=’010’)
/* Enable the TIM4 Master Slave Mode */
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);//配置定时器4为主模式
/* Enable and set EXTI Line0 Interrupt to the lowest priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//第2组:最高2位用于指定抢占式优先级,后面2位用于指定响应优先级;
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//rTIM4
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM4, ENABLE);
}
//******************************************************************************************
//1000毫秒 定时查询!
void TIM4_IRQHandler_Lib(void){
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET){
Freq=(float)TIM_GetCapture1(TIM2);
Freq *=FREQ_DIV_K*(1000/MEASURE_ms);
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}
}
//**********************************************************************************************************
// 初始化TIM2 它作为外部脉冲计数器,同时也是由TIM4 内部触发复位控制的从计数器
//**********************************************************************************************************
void TIM2_Config(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
/*---------------------------- TIM2 Configuration ----------------------------*/
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xffffffff;//fCK_PSC/(PSC[15:0]+1)。
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR3); //定时器2选择定时器4获得输入触发(TIM2_SMCR寄存器的TS=011)
/* Use the External Clock as TIM2 Slave Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //配置定时器2使用复位模式(TIM2_SMCR寄存器的SMS=100)
//--------------------------------------------------------------------------------
//设置外部输入: x分频,上升沿,不过滤!
TIM_ETRClockMode2Config(TIM2,FREQ_DIV,TIM_ExtTRGPolarity_Inverted,0);
//TIM_ITRxExternalClockConfig(TIM2,TIM_TS_ITR3);
//----------------------------------------------------------------------------
//TIM4-tigo 同时产生 TIM2的输入扑捉
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_TRC; //TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter=0; //Fsampling=Fdts/4 N=8 滤除32ms的抖动
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2, ENABLE);
}
//-------------------------------------------------------------------------------------------
//初始化 TIM2,TIM4.ETR
void FREQ_Measure_Init(void){
TIM4_Config();
TIM2_Config();
}
//---------------------------------------------------------------------------------------------
//得到频率值
float Get_Freq_Value(void){
return Freq;
}
|
|