找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1190|回复: 0
打印 上一主题 下一主题
收起左侧

32超声波测高oled不显示 谁能帮我看下

[复制链接]
跳转到指定楼层
楼主
ID:260280 发表于 2018-7-23 10:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "include.h"

float Current_Height=0;
/*******************************************************************************
*oˉêyÔ-Dí:                void Ultrasonic_Init(void)
*1|¡¡¡¡Äü:                3¬éù2¨òy½Å3õê¼»ˉ
*******************************************************************************/
void Ultrasonic_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE);
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOD, &GPIO_InitStructure);       
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPD ;
        GPIO_Init(GPIOD, &GPIO_InitStructure);
        GPIO_ResetBits(GPIOD, GPIO_Pin_9);
}

/*******************************************************************************
*oˉêyÔ-Dí:                void Ultrasonic_Ranging(void)
*1|¡¡¡¡Äü:                3¬éù2¨2a¾à,·μ»Ø¸ßμçƽ걼ä
*******************************************************************************/
void Ultrasonic_Ranging(void)
{
        static u8 count=0;
        u16 error=0;
        static float Last_Height=0;
        u16 Ultrasonic_count=0;
       
        if(count==10||count==0)
        {
                GPIO_SetBits(GPIOD, GPIO_Pin_9);
                Delay_us(15);
                GPIO_ResetBits(GPIOD, GPIO_Pin_9);
                while(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_8)==0)
                {       
                        if(error>4000)
                        {
                                break;
                        }
                        else
                        {
                                error++;
                        }
                }
                if(error<3500)
                {
                        while(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_8)==1)
                        {
                               
                                if(Ultrasonic_count>28950)
                                {
                                        break;
                                }
                                else
                                {
                                        Ultrasonic_count++;
                                }
                        }
                }
                Current_Height=Ultrasonic_count;
                Current_Height=Current_Height/96.5;
               
                if(fabs(Current_Height-Last_Height)<20)
                {
                        Last_Height=Current_Height;                       
                }
                count=1;
        }
        else
        {
                Current_Height=Last_Height;
                count++;
        }
       
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表