找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1488|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

32超聲波測高oled不顯示 誰能幫我看下

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:260280 發(fā)表于 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空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表