首页 新闻 会员 周边 捐助

大神帮忙看看哪里出错了,尝试的第二次方法

0
悬赏园豆:20 [待解决问题]

头文件:

ifndef csb_h

define csb_h

include "sys.h"

void csb_init(void);
void starttimer(void);
void endtimer(void);
u32 gettime(void);

endif

csb.c:

include "csb.h"

u8 conter=0;//ÖжÏÒç³ö´ÎÊý
void csb_init(void)
{
GPIO_InitTypeDef GPIO_InitT;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitT;
TIM_ICInitTypeDef TIM_ICInitT;
NVIC_InitTypeDef NVIC_InitT;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);

GPIO_InitT.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitT.GPIO_Pin =GPIO_Pin_1;
GPIO_InitT.GPIO_Speed =GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&GPIO_InitT);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);//ÖÃ0


GPIO_InitT.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_InitT.GPIO_Pin =GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitT);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);//ÖÃ0

TIM_TimeBaseInitT.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseInitT.TIM_CounterMode =TIM_CounterMode_Up;
TIM_TimeBaseInitT.TIM_Period =65535-1;
TIM_TimeBaseInitT.TIM_Prescaler =71;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitT);

NVIC_InitT.NVIC_IRQChannel =TIM5_IRQn;
NVIC_InitT.NVIC_IRQChannelCmd =ENABLE;
NVIC_InitT.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitT.NVIC_IRQChannelSubPriority =0;
NVIC_Init(&NVIC_InitT);

TIM_ITConfig(TIM5,TIM_IT_Update ,ENABLE);

TIM_Cmd(TIM5,DISABLE);

}
void TIM5_IRQHandler()
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)==1)
{
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
conter++;
}
}
void starttimer(void)
{
TIM_SetCounter(TIM5,0);
conter=0;
TIM_Cmd(TIM5,ENABLE);
}
void endtimer(void)
{
TIM_Cmd(TIM5,DISABLE);
}
u32 gettime(void)
{
u32 t=0;
t=conter * 65535;
t=t+TIM_GetCounter(TIM5);
TIM_SetCounter(TIM5,0);
return t;
}

主函数:

include "csb.h"

include "usart.h"

include "delay.h"

define TRIG PBout(1)

define ECHO GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)

int main(void)
{
u32 time;
float distance=0;
float sum=0;
u8 i;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(9600);
csb_init();
while(i!=5)
{
TRIG=1;
delay_us(20);
TRIG=0;
while(ECHO==0);
starttimer();
while(ECHO==1);
endtimer();
time=gettime();
delay_ms(50);
distance=((float)time/58.0);
sum=sum+distance;
i=i+1;
}
distance=sum/5.0;
printf("distance:%f cm\r\n",distance);
}

实验现象:打开串口,没有数值显示。但是按下复位键,会依次出现3个数值,并且拔下TRIG线,按复位键还是会有数值出现

Cherishkk的主页 Cherishkk | 初学一级 | 园豆:179
提问于:2018-05-15 14:15
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册