STM32F4超声波串口打印不出来,大神求救

STM32F4超声波模块测距,想串口助手显示距离打印不出来,新手求救,不仅感谢。程序如下:
cs.c
#include "cs.h"
#include "stm32f4xx.h"
#include "delay.h"
#include "usart.h"

/*¼Ç¼¶¨Ê±Æ÷Òç³ö´ÎÊý*/
uint overcount=0;

/*ÉèÖÃÖжÏÓÅÏȼ¶*/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructer;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

NVIC_InitStructer.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructer.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructer.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructer.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructer);
}

/*³õʼ»¯Ä£¿éµÄGPIOÒÔ¼°³õʼ»¯¶¨Ê±Æ÷TIM2*/
void CH_SR04_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructer;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructer;

RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

/*TRIG´¥·¢ÐźÅ*/
GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructer);

/*ECOH»ØÏìÐźÅ*/
GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructer.GPIO_Pin=GPIO_Pin_9;
GPIO_Init(GPIOB, & GPIO_InitStructer);

/*¶¨Ê±Æ÷TIM2³õʼ»¯*/
TIM_DeInit(TIM2);
TIM_TimeBaseInitStructer.TIM_Period=999;//¶¨Ê±ÖÜÆÚ1000
TIM_TimeBaseInitStructer.TIM_Prescaler=71; //·ÖƵϵÊý72
TIM_TimeBaseInitStructer.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructer.TIM_CounterMode=TIM_CounterMode_Up;
//TIM_TimeBaseInitStructer.TIM_RepetitionCounter
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructer);

TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//¿ªÆô¸üÐÂÖжÏ
NVIC_Config();
TIM_Cmd(TIM2,DISABLE);//¹Ø±Õ¶¨Ê±Æ÷ʹÄÜ

}

float Senor_Using(void)
{
float length=0,sum=0;
u16 tim;
uint i=0;
/*²â5´ËÊý¾Ý¼ÆËãÒ»´Îƽ¾ùÖµ*/
while(i!=5)
{
PBout(8)=1; //À­¸ßÐźţ¬×÷Ϊ´¥·¢ÐźÅ
delay_us(20); //¸ßµçƽÐźų¬¹ý10us
PBout(8)=0;
/*µÈ´ý»ØÏìÐźÅ*/
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==RESET);
TIM_Cmd(TIM2,ENABLE);//»ØÏìÐźŵ½À´£¬¿ªÆô¶¨Ê±Æ÷¼ÆÊý
i+=1; //ÿ´ÎÊÕµ½Ò»´Î»ØÏìÐźÅ+1,ÊÕµ½5´Î¾Í¼ÆËã¾ùÖµ
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)==SET);//»ØÏìÐźÅÏûʧ
TIM_Cmd(TIM2,DISABLE);//¹Ø±Õ¶¨Ê±Æ÷

tim=TIM_GetCounter(TIM2);//»ñÈ¡¼ÆTIM2Êý¼Ä´æÆ÷ÖеļÆÊýÖµ£¬Ò»±ß¼ÆËã»ØÏìÐźÅ

length=(tim+overcount*1000)/58.0;//ͨ¹ý»ØÏìÐźżÆËã¾àÀë

sum=length+sum;
TIM2->CNT=0;  //½«TIM2¼ÆÊý¼Ä´æÆ÷µÄ¼ÆÊýÖµÇåÁã
overcount=0;  //ÖжÏÒç³ö´ÎÊýÇåÁã
delay_ms(100);

}
length=sum/5;
return length;//¾àÀë×÷Ϊº¯Êý·µ»ØÖµ
}

void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//Çå³ýÖжϱêÖ¾
overcount++;
}
}

主函数
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "cs.h"

float ChangeDistance(int cout1)

{

float distance=0;

printf("cou1=%d\n",cout1);

distance=cout1/58.0;

  return distance;  

}

int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2
delay_init(168); //ÑÓʱ³õʼ»¯
uart_init(115200); //´®¿Ú³õʼ»¯²¨ÌØÂÊΪ115200
// int count=0;

// float distance=0;

CH_SR04_Init();

 printf("Senor_Using\n");

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐