之前有写过一个测量频率的,精度挺高,不过只能测到10kHz精度就不行了,还想测高一些,网上查了用两个定时器的办法,于是尝试了好几天,就是不行,希望各位大神帮我看看,学习一下。
int main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
TIM_Configuration();
GPIO_Configuration();
USARTx_Config();
TIM_Cmd(TIM2,ENABLE);//ʹÄÜÍⲿ¼ÆÊý
TIM_Cmd(TIM3,ENABLE);//¿ªÆô1s¶¨Ê±
while (1)
{
if(Flag==1)
{
Flag=0;
Freq=Counter+i*9999;
if(Freq>300)
{
Freq=1.0052*Freq;//-1.4475;//Îó²î¾ÀÕý
}
printf("\r\n Freq = %lf Hz \r\n",Freq);
i=0;
TIM_Cmd(TIM3,DISABLE);//1s¶¨Ê±
TIM_Cmd(TIM2,DISABLE);//ʧÄÜÍⲿ¼ÆÊý
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2,ENABLE);//ʹÄÜÍⲿ¼ÆÊý
TIM_Cmd(TIM3,ENABLE);//¿ªÆô1s¶¨Ê±
}
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
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);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 9999; //×Ô¶¯ÖØ×°Öµ
TIM_TimeBaseStructure.TIM_Prescaler =7199; //Ô¤·ÖƵֵ, ʹTIMx_CLK=72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÊäÈëʱÖÓ²»·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//ÏòÉϼÆÊý
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //TIM2_TimeBase
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
//TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);//ÍⲿʱÖÓ´¥·¢ÊäÈë ÉÏÉýÑØÓÐЧ ʱÖÓ²»·ÖƵ
//TIM_SelectInputTrigger(TIM2,TIM_TS_ETRF); //TIM_TS_ETRF Íⲿ´¥·¢ÊäÈë
//TIM_SetCounter(TIM2,0);
//TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2,TIM_TS_ETRF);
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_SetCompare1(TIM2, 0);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 9999; //Tout= ((9999+1)*(7199+1))/Tclk;
TIM_TimeBaseStructure.TIM_Prescaler =7199; //TIMx_CLK=72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
NVIC_Configuration();
TIM_Cmd(TIM3,DISABLE); //TIM3_TimeBase
TIM_Cmd(TIM2,DISABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //PA2 ÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
中断服务函数:
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3|TIM_IT_Update);
i++;
}
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
Counter=TIM_GetCounter(TIM2);
Flag=1;
}
谢谢大家了
|
|