0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LPC812MAXと超音波距離センサーHC-SR04で距離をシリアル出力 (mbed2リビジョン144)(動いた)

Last updated at Posted at 2022-09-04

x @sasakisanさんの記事を非営利、研究目的で参考にして移植しました。

x mbed2リビジョン144
x リビジョン変更が出来る人むけ

目的
HC_SR04からのPWMの受信

o_con326.jpg

o_con724.jpg

o_con723.jpg

いろいろあって今完成(2022/09/06 18:05)




//ser_test1_HC_SR04_812_1


#include "mbed.h"


//         TX    RX
Serial pc(P0_4, P0_0); //812


//10の割り算 0から1028までは、正しい。主に0から999
#define DIV10(n) ((n*205)>>11)


#define HIGH 1
DigitalOut TRIG(P0_17);   //green
DigitalIn  ECHO(P0_7);    //red


//タイマーの設定
Timer t;

unsigned long times; //測定時間
int distance;        //長さ


//PA_5のオン時間をusで測る 今のところ引数は、無効
int pulseIn(int pin1,int pu1,int timeout1)
{

    t.reset();//タイマーの開始

    //タイムアウトの設定
    //timeout1 = 32000000;
    timeout1 = 100000;    //28ms
    //timeout1 = 1000;   //degbu

    //while(PA_5 == 0) {} //オフの間は、無限ループ

loop_s1:
    timeout1--;
    //                                     5432109876543210
    if (     (~(LPC_GPIO_PORT->PIN0))  & 0b0000000010000000     ) {
        if ( timeout1 ) {
            goto loop_s1;
        }
    }


    int sd = t.read_us();//測定開始

    //タイムアウトの設定
    //timeout1 = 32000000;
    timeout1 = 100000;     //28ms
    //timeout1 = 1000;   //degbu

    //while(PA_5 != 0) {} //オンの間は、無限ループ

loop_in1:
    timeout1--;
    //                               5432109876543210
    if (   (LPC_GPIO_PORT->PIN0) & 0b0000000010000000   ) {
        if( timeout1 ) {
            goto loop_in1;
        }
    }


    return(t.read_us()-sd);//測定終了

}//pulseIn


int b=800; //マスターに返す値

//プロトタイプ宣言
void rx_Event();


//メインルーチン
int main()
{

    //タイマーの開始
    t.start();

    //GPIOの初期化
    TRIG = 0;

    //無限ループ
    while(1) {

        //距離のリード
        rx_Event();

        //0.5秒の待ち
        wait_ms(500);

    }//while

}//main


void rx_Event()
{

    //I2Cスレーブの送信データの表示 num debug
    int d = b;
    char data_read[4]; //バッファーの定義
    data_read[3] = 0;
    data_read[2] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
    d = DIV10(d);
    data_read[1] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
    data_read[0] = '0' +  DIV10(d);                  // '0'+(d/10)
    pc.putc(data_read[0]);
    pc.putc(data_read[1]);
    pc.putc(data_read[2]);
    pc.putc('\r');
    pc.putc('\n');

    // 超音波を発生させる
    TRIG=1;
    wait_us(10);
    TRIG=0;

    // 超音波を受け取る
    times = pulseIn( 5, HIGH,2000000);

    //times = (unsigned long)7700/2; //debug
    //times = 1177;//debug 1ms 20.0cm
    //times = 942;//debug 1ms 16.0cm

    //時間を距離に変換する 音は、1秒間に約300メートル進む
    //distance = (int)(times * 0.017);
    distance = (int)((times * ((unsigned long)(17))) / 100);

    //80センチメートル以上の時は、80にする。
    if (  distance >= 800    ) {
        b = 800;
    } else {
        b = distance;
    }

}//rx_Event







たぶんぜんぜんうごかない(2022/09/05 07:33)



//ser_test1_HC_SR04_812_1


#include "mbed.h"


//10の割り算 0から1028までは、正しい。主に0から999
#define DIV10(n) ((n*205)>>11)


#define HIGH 1
DigitalOut TRIG(P0_17);   //green
DigitalIn  ECHO(P0_7);    //red


//タイマーの設定
Timer t;

unsigned long times; //測定時間
int distance;        //長さ


//PA_5のオン時間をusで測る 今のところ引数は、無効
int pulseIn(int pin1,int pu1,int timeout1)
{

    t.reset();//タイマーの開始

    //timeout1 = 32000000;
    timeout1 = 100000;    //28ms
    //timeout1 = 1000;   //degbu

    //while(PA_5 == 0) {}

loop_s1:
    timeout1--;
    //                                     5432109876543210
    if (     (~(LPC_GPIO_PORT->PIN0))  & 0b0000000010000000     ) {
        if ( timeout1 ) {
            goto loop_s1;
        }
    }


    int sd = t.read_us();//測定開始

    //timeout1 = 32000000;
    timeout1 = 100000;     //28ms
    //timeout1 = 1000;   //degbu

    //while(PA_5 != 0) {}

loop_in1:
    timeout1--;
    //                               5432109876543210
    if (   (LPC_GPIO_PORT->PIN0) & 0b0000000010000000   ) {
        if( timeout1 ) {
            goto loop_in1;
        }
    }


    return(t.read_us()-sd);//測定終了

}//pulseIn


unsigned char b=200; //マスターに返す値

//プロトタイプ宣言
void rx_Event();


//メインルーチン
int main()
{

    //タイマーの開始
    t.start();

    //GPIOの初期化
    TRIG = 0;

    //無限ループ
    while(1) {

        //距離のリード
        rx_Event();

        //0.5秒の待ち
        //wait_ms(500);

    }//while

}//main


void rx_Event()
{

    //ソフトウェアシリアルから距離を出力
    //pc_putc(b);

    
        //I2Cスレーブの送信データの表示 num debug
        int d = b; //buf[0];
        //int d = b; //+ 60;
        //int d = ir_length;
        char data_read[4]; //バッファーの定義
        data_read[3] = 0;
        data_read[2] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
        d = DIV10(d);
        data_read[1] = '0' + (  d - (DIV10(d) * 10)  );  // '0'+(d%10)
        data_read[0] = '0' +  DIV10(d);                  // '0'+(d/10)
        pc.putc(data_read[0]);
        pc.putc(data_read[1]);
        pc.putc(data_read[2]);
        pc.putc('\r');
        pc.putc('\n');

    // 超音波を発生させる
    TRIG=1;
    wait_us(10);
    TRIG=0;

    // 超音波を受け取る
    times = pulseIn( 5, HIGH,2000000);

    //times = (unsigned long)7700/2; //debug
    //times = 1177;//debug 1ms 20.0cm
    //times = 942;//debug 1ms 16.0cm



    //distance = (int)(times * 0.017);
    distance = (int)((times * ((unsigned long)(17))) / 100);

    b = distance;

}//rx_Event



o_con833.jpg

-12-

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?