GPIO_InitTypeDef GPIO_InitStructure;
tiM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void TIM1_GPIO_TConfig(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_12);
}
void TIMx_NVIC_TConfig(void)
{
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&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 TIMx_TConfig(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = 1;
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision =0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ITRxExternalClockConfig(TIM1,TIM_TS_ETRF);
TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0); ±ÖÓ´¥·¢Ä£Ê
TIM_ClearITPendingBit(TIM1, TIM_IT_Update );
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
TIM_SetCounter(TIM1,0);
TIM_Cmd(TIM1,ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period =0xFFFF-1;
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}
void MODE_select(void)
{
if(modeSlect==MODE_T)
{
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = 1;
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ITRxExternalClockConfig(TIM1,TIM_TS_ETRF);
TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0);
//TIM_SelectInputTrigger(TIM1, TIM_TS_ETRF);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update );
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);//ÔÊÐí¸üÐÂÖÐ¶Ï ,ÔÊÐíCC1IE²¶»ñÖжÏ
TIM_SetCounter(TIM1,0);
TIM_Cmd(TIM1,ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period =0xFFFF-1;
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}
else if(modeSlect==MODE_D)
{
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period=0xFFFF-1;
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ITRxExternalClockConfig(TIM1,TIM_TS_ETRF);
TIM_ETRClockMode2Config(TIM1,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0);
//TIM_SelectInputTrigger(TIM1, TIM_TS_ETRF);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update );
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
TIM_SetCounter(TIM1,0);
TIM_Cmd(TIM1,ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period =2000;
TIM_TimeBaseStructure.TIM_Prescaler =36000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}
}
void TIM1_UP_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
if(MODE_T==modeSlect)
{
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM3, ENABLE);
if(Index==1)
{
time_1=flowCount*65535+TIM_GetCounter(TIM3);
}
if(Index==2)
{
time_2=flowCount*65535+TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3,0);
Index=0;
flowCount=0;
freq=(2*72000000.0/(time_2-time_1));
flag=1;
}
Index++;
}
if(MODE_D==modeSlect)
{
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM3, ENABLE);
Index_1++;
}
}
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
if(MODE_T==modeSlect)
{
flowCount++;
}
if(MODE_D==modeSlect)
{
nCount=(u32)TIM_GetCounter(TIM1);
TIM_SetCounter(TIM1,0);
TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE);
freq=65535*Index_1+nCount;
Index_1=0;
flag=1;
}
}
}
一周热门 更多>