標(biāo)題: 32超聲波測(cè)高oled不顯示 誰(shuí)能幫我看下 [打印本頁(yè)]

作者: 鄒連中    時(shí)間: 2018-7-23 10:40
標(biāo)題: 32超聲波測(cè)高oled不顯示 誰(shuí)能幫我看下
#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++;
        }
       
}





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1