x あまり正確では、ない
x mbed2リビジョン144
x リビジョンを変更できる人むけ
目的
シェフの気まぐれ
周波数も
シリアルに出力する。
レジスター直読み込みで高速化した。
内部タイマーを使いusでオン時間を測る
シリアル出力
テストプログラム
//ser_pwm_in_812_2
#include "mbed.h"
// TX RX
Serial pc(P0_4, P0_0); //812
//10の割り算 0から9999までは、正しい。
#define DIV10(n) (((n*409)+((n*154)>>8))>>12)
#define HIGH 1
DigitalIn ECHO(P0_7); //red
//タイマーの設定
Timer t;
unsigned long times; //測定時間
//int distance; //長さ
unsigned long hz;
//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
//PA_5のオン時間をusで測る 今のところ引数は、無効
int hzIn(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;
}
}
//タイムアウトの設定
//timeout1 = 32000000;
timeout1 = 100000; //28ms
//timeout1 = 1000; //degbu
//while(PA_5 == 0) {} //オフの間は、無限ループ
loop_in2:
timeout1--;
// 5432109876543210
if ( (~(LPC_GPIO_PORT->PIN0)) & 0b0000000010000000 ) {
if ( timeout1 ) {
goto loop_in2;
}
}
return(t.read_us()-sd);//測定終了
}//hzIn
//メインルーチン
int main()
{
pc.puts("STAR\r\n");
wait_ms(200); //debug
//タイマーの開始
t.start();
int d = 0; //表示変換用
char data_read[5]; //バッファーの定義
//無限ループ
while(1) {
//周期でデータ取得
hz=hzIn(7 ,HIGH,2000000);
//hz=500;
//周波数の計算
//hz=hz*2;
if(hz==0){hz=1;}
hz = 1000000/hz;
//時間の表示 num debug
d = hz;
data_read[4] = 0;
data_read[3] = '0' + ( d - (DIV10(d) * 10) ); // '0'+(d%10)
d = DIV10(d);
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.puts(data_read);
pc.putc('H');
pc.putc('z');
pc.putc(' ');
// PWMを測る
times = pulseIn( 5, HIGH,2000000);
//times = 1234;//debug
if( times >= 9999 ) {
times = 9999;
}
//時間の表示 num debug
d = times;
data_read[4] = 0;
data_read[3] = '0' + ( d - (DIV10(d) * 10) ); // '0'+(d%10)
d = DIV10(d);
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.puts(data_read);
pc.putc('u');
pc.putc('s');
pc.putc('\r');
pc.putc('\n');
//1秒の待ち
wait_ms(1000);
}//while
}//main
-31-