打印

STM32测距小程序求帮助

[复制链接]
1272|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
diannaodanshi|  楼主 | 2015-3-28 23:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
菜鸟一枚,在做一个利用HC-SR04模块测距的小程序。想法就是利用滴答定时器。代码如下:
int main(void)
{        double a;
        GPIO_InitTypeDef GPIO_InitStructure;
        /* 1us中断一次 */
        SysTick_Init();
       
                LED_GPIO_Config();               
                                                                                                                                          
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;       

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       
        GPIO_Init(GPIOB, &GPIO_InitStructure);       
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;       
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
         
        GPIO_Init(GPIOB, &GPIO_InitStructure);       
       
        USART1_Config();       
       
        printf("\r\n this program show the distance \r\n");
       
        for(;;)
        {        a=0;
                GPIO_ResetBits(GPIOB,GPIO_Pin_10);
                Delay_us(20);       
                GPIO_SetBits(GPIOB,GPIO_Pin_10);
               
                while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0) {
                        Delay_us(10);
                        a=a+1;}
               
                printf("\r\n distance %.1f\r\n",a/100000*170*100);                                 
                Delay_ms(1000);                /* 1s 测试距离一次*/
                LED1_TOGGLE;
        }   
}

但是就测距结果来看,只能通过串口输出3.9,4.1这两个固定的数值。求问大神该程序是哪里的问题,应该如何改正。感激不尽!

相关帖子

沙发
dirtwillfly| | 2015-3-29 17:34 | 只看该作者

我记得:HC-SR04模块ECHO的高电平的持续时间是超声波发送到返回的时间。

使用特权

评论回复
板凳
diannaodanshi|  楼主 | 2015-3-29 21:26 | 只看该作者
dirtwillfly 发表于 2015-3-29 17:34
我记得:HC-SR04模块ECHO的高电平的持续时间是超声波发送到返回的时间。

太感谢了,问题解决了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

4

帖子

0

粉丝