新手入门
- 积分
- 10
- 金钱
- 10
- 注册时间
- 2021-10-15
- 在线时间
- 5 小时
|
5金钱
求助,为什么我的程序测量不了频率,我用信号发生器输入1Hz的方波到STM32F103上,串口助手没有任何反应,我参考一些程序写出来的,大佬们能不能帮我看看我的程序问题在哪?
//timer.c
volatile float frequent_input;
void TIM5_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 Çå3y֮ǰéèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 êäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA0
}
void input_frequent_init(void)
{
TIM_TimeBaseInitTypeDef TIM5_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
NVIC_InitTypeDef TIM5_NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
TIM_ClearITPendingBit(TIM5,TIM_IT_Update|TIM_IT_CC1);
TIM5_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM5_TimeBaseInitStructure.TIM_Prescaler = 143;
TIM5_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM5_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM5_TimeBaseInitStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
TIM5_NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;
TIM5_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
TIM5_NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
TIM5_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&TIM5_NVIC_InitStructure);
TIM_Cmd(TIM5,ENABLE);
TIM_ITConfig(TIM5, TIM_IT_Update|TIM_IT_CC1, ENABLE );
}
u8 TIM5CH1_CAPTURE_STA=0;
u16 TIM5CH1_CAPTURE_VAL1;
u16 TIM5CH1_CAPTURE_VAL2;
void TIM5_IRQHandler()
{
if((TIM5CH1_CAPTURE_STA&0X80)==0)
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH1_CAPTURE_STA&0X20)
{
if((TIM5CH1_CAPTURE_STA&0x1f)==0x1f)
{
TIM5CH1_CAPTURE_STA|=0x80;
}
else
{
TIM5CH1_CAPTURE_STA++;
}
}
}
}
if (TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)
{
if((TIM5CH1_CAPTURE_STA&0X20)&&(!(TIM5CH1_CAPTURE_STA&0X40)))
{
TIM5CH1_CAPTURE_STA|=0X40;
TIM5CH1_CAPTURE_VAL1=TIM_GetCapture4(TIM5)+(TIM5CH1_CAPTURE_STA&0X1f)*65536;
TIM_SetCounter(TIM5,0);
TIM_OC4PolarityConfig(TIM5,TIM_ICPolarity_Rising);
TIM5CH1_CAPTURE_STA&=0Xe0;
}
else if((TIM5CH1_CAPTURE_STA&0X20)==0)
{
TIM5CH1_CAPTURE_STA=0;
TIM5CH1_CAPTURE_VAL1=0;
TIM5CH1_CAPTURE_VAL2=0;
TIM_SetCounter(TIM5,0);
TIM5CH1_CAPTURE_STA|=0X20;
TIM_OC4PolarityConfig(TIM5,TIM_ICPolarity_Falling);
}
else if((TIM5CH1_CAPTURE_STA&0X40)&&(!(TIM5CH1_CAPTURE_STA&0X80)))
{
TIM5CH1_CAPTURE_VAL2=TIM_GetCapture4(TIM5)+(TIM5CH1_CAPTURE_STA&0X1f)*65536;
TIM5CH1_CAPTURE_STA|=0X80;
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC4|TIM_IT_Update);
}
主函数:
extern u8 TIM5CH1_CAPTURE_STA; //
extern u16 TIM5CH1_CAPTURE_VAL1;//
extern u16 TIM5CH1_CAPTURE_VAL2;//
float frequent;
float duty;
char Str_Buf[32];
int main()
{
LED_Init();
input_frequent_init();
// input_duty_init();
POINT_COLOR=BLUE;
LCD_ShowString(60,130,200,16,16,"ADC_CH0_VAL:");
while(1)
{
if((TIM5CH1_CAPTURE_STA&0x80)) //
{
duty=(float)TIM5CH1_CAPTURE_VAL1/(TIM5CH1_CAPTURE_VAL1+TIM5CH1_CAPTURE_VAL2 );
frequent+=500000.0/(TIM5CH1_CAPTURE_VAL1+TIM5CH1_CAPTURE_VAL2 );
TIM5CH1_CAPTURE_STA=0;
// sprintf(Str_Buf,"pl:%.2f %%",frequent);
printf("当前频率为:%d Hz\r\n",duty);
printf("当前频率为:%d Hz\r\n",frequent);
}
}
}
|
|