最近在搞一个rct6超声波测距有关的项目 结果调了一天 测出来的距离一直都是0 一开始是用外部中断 后来改成了输入捕获 结果还是输出0
这是定时器输入捕获的代码
void TIM3IC_Init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM3);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 65535 - 1;
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1 ;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
TIM_ITConfig(TIM3,TIM_IT_CC2|TIM_IT_CC1,ENABLE);
TIM_SetCounter(TIM2,0);
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE);
}
void TIM3_IRQHandler()
{
if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET)
{
Distance = (TIM_GetCapture2(TIM3)+1) * 340/20000.0;
Flag = 1;
//printf("Distance = %f m\r\n",Distance);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
这是配置超声波引脚的代码
float Distance;
int Flag = 1,ICAP1value,ICAP2value;
void HC_SR04_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void HC_SR04_start(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
Delay_us(20);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
Delay_ms(60);
}
输出结果一直为0 串口方面测试过是没问题的 引脚也可以正常输出 但不知道自己的逻辑错在哪了 希望带佬们指导一下