x @sasakisanさんの記事を非営利、研究目的で参考にして移植しました。
x mbed2リビジョン144
x リビジョン変更が出来る人むけ
目的
HC_SR04からのPWMの受信
いろいろあって今完成(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
-12-