stm32测方波频率
时间:10-02
整理:3721RD
点击:
利用stm32测方波的频率,以下是我编写的下横须,请问大神该程序有什么问题?屏幕上不能显示方波的频率也就是检测不到。
- void TIM3_Cap_Init(u32 arr,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
-
- TIM_DeInit(TIM3);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP ;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- GPIO_PinRemapConfig( GPIO_FullRemap_TIM3,ENABLE );
- //GPIO_PinRemapConfig( GPIO_FullRemap_TIM3,ENABLE );
-
-
- TIM_TimeBaseStructure.TIM_Prescaler=psc;
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period=arr;
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_div1;
-
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
- //?TIM2?
- 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(TIM3, &TIM_ICInitStructure);
-
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
-
- TIM_ICInit(TIM3, &TIM_ICInitStructure);
-
- TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
- // TIM2_CH1_Cap_DMAInit();
- TIM_Cmd(TIM3,ENABLE );
-
- 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);
-
- }
- static double rising;
- static u32 rising_last;
- static u32 falling;
- void TIM3_IRQHandler(void)
- {
- if(TIM3->SR&TIM_FLAG_CC3)//TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)//?1?
- {
- rising=TIM3->CCR3-rising_last;
- rising_last=TIM3->CCR3;
- return;
- }
- if(TIM3->SR&TIM_FLAG_CC4)//TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
- {
- falling=TIM3->CCR4-rising_last;
- return;
- }
- TIM3->SR=0;
- }
-
- char vol[6]={"0000Hz"};
- int main(void)
- {
- //OLED_Init();
- uint32_t f;
- Lcd_Init();
- LCD_LED_SET;
- Lcd_Clear(GRAY0);
- TIM3_Cap_Init(65535,71);
- while(1){
- f = (float) (9000000/rising);
- vol[0] = (f/1000);//?y?ò2¨0.885 ?±á÷*0.948*
- //vol[1] = '.';
- vol[2] = ((f%1000)/100);
- vol[3] = ((f%100)/10);
- vol[4] = (f%10);
- Gui_DrawFont_GBK16(10,10,BLUE,GRAY0,"The frequence is");
- Gui_DrawFont_Num32(25,40,BLUE,GRAY0,vol[0]);
- //Gui_DrawFont_GBK16(50,56,BLUE,GRAY0,"μ?");
- Gui_DrawFont_Num32(75-16,40,BLUE,GRAY0,vol[2]);
- Gui_DrawFont_Num32(100-16,40,BLUE,GRAY0,vol[3]);
- Gui_DrawFont_Num32(125-16,40,BLUE,GRAY0,vol[4]);
- Gui_DrawFont_GBK16(170-16,55,BLUE,GRAY0,"Hz");
- }
- }
网上搜索下应该有相关的历程
我有做过这个,你需要参考吗
需要,谢谢大神。麻烦你发送文件到我的qq,1622585951