TIM1测频得到的数值了但是我的时钟定义的是72M为什么是36M中间我都没有设置分频
[复制链接]
/*******************TIM1定义***************************? void Timer_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure;
//TIM1时基设定--捕获填充频率 TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period=0xFFFF; //ARR的值 TIM_TimeBaseStructure.TIM_Prescaler=0; TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样不分频,频率72M TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 // TIM_TimeBaseStructure.TIM_RepetitionCounter=TIM_EncoderMode_TI2; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_PrescalerConfig(TIM1,0,TIM_PSCReloadMode_Immediate);//时钟分频系数0,系统时钟72MHZ TIM_ARRPreloadConfig(TIM1, ENABLE); //使能ARR预装载缓冲器 //定时器1捕获设置 TIM_ICInitStructure.TIM_Channel=TIM_Channel_1; 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=0; TIM_ICInit(TIM1,&TIM_ICInitStructure); //清中断,以免一启用中断后立即产生中断 TIM_ClearFlag(TIM1, TIM_IT_Update);// TIM_FLAG_Update //使能TIM1溢出中断源 TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //TIM_ITConfig(TIM1, TIM_FLAG_CC1OF, ENABLE); TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);//使能捕获中断 TIM_GenerateEvent(TIM1,TIM_EventSource_Update); TIM_Cmd(TIM1, ENABLE); //使能定时器1 }
void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_DeInit(GPIOA); /* TIM2 channel 2 pin (PA.01) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); }
/****************定时器中断服务函数 *******************************/ //要想得到足够大的测频范围,只要定义相应的OV_count,cap_diff足够大就可以了 //建议freq_1array[]可以设置成double类型的变量,那样可以有小数频率! void TIM1_CC_IRQHandler(void) { //捕获处理 if(TIM_GetITStatus(TIM1,TIM_IT_CC1)!=RESET) { TIM_ClearITPendingBit(TIM1,TIM_IT_CC1); if(1==cap_count) //捕获频率 { OV_count=0; old_cap=TIM_GetCapture1(TIM1);//TIM_GetCounter
} else if(cap_count>10) //捕获十次 { new_cap=TIM_GetCapture1(TIM1);// TIM_GetCounter cap_diff=new_cap-old_cap+65535*OV_count; freq_1array=720000000.0/cap_diff;//这里72000000*10是对10次捕获求平均值 *100是求小数后两位 cap_count=1; OV_count=0; } cap_count++; } }
//TIM1溢出中断服务子程序:TIM1_FLAG_CC1OF void TIM1_UP_IRQHandler(void) { TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清中断 OV_count++; }
void main(void) { /* config the sysclock to 72m */ SystemInit(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1| RCC_APB2Periph_AFIO,ENABLE); Timer_Configuration(); GPIO_Configuration(); NVIC_Configuration(); while(1); }
要把中断中的 freq_1array=720000000.0/cap_diff 改成360000000.0才正确 为什么呢。。
/**************"stm32f10x.h"**********************************/ #if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) /* #define SYSCLK_FREQ_HSE HSE_VALUE */ #define SYSCLK_FREQ_24MHz 24000000 #else /* #define SYSCLK_FREQ_HSE HSE_VALUE */ /* #define SYSCLK_FREQ_24MHz 24000000 */ /* #define SYSCLK_FREQ_36MHz 36000000 */ /* #define SYSCLK_FREQ_48MHz 48000000 */ /* #define SYSCLK_FREQ_56MHz 56000000 */ #define SYSCLK_FREQ_72MHz 72000000 #endif
[ 本帖最后由 pk3725069 于 2012-3-14 18:06 编辑 ]
|