20160813 超音波センサー サンプル
Dependencies: mbed
main.cpp@0:c7074b60eeab, 2016-08-13 (annotated)
- Committer:
- takahashikento
- Date:
- Sat Aug 13 09:33:07 2016 +0000
- Revision:
- 0:c7074b60eeab
0813 ???????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takahashikento | 0:c7074b60eeab | 1 | /* ヘッダーファイル読み込み */ |
takahashikento | 0:c7074b60eeab | 2 | #include "mbed.h" |
takahashikento | 0:c7074b60eeab | 3 | |
takahashikento | 0:c7074b60eeab | 4 | /* ピンの機能設定 */ |
takahashikento | 0:c7074b60eeab | 5 | Serial PC (USBTX,USBRX); //USB :シリアル通信 |
takahashikento | 0:c7074b60eeab | 6 | DigitalOut USSTriger (p11); //P11 :超音波センサ トリガ出力 |
takahashikento | 0:c7074b60eeab | 7 | Timer ActiveTime; |
takahashikento | 0:c7074b60eeab | 8 | |
takahashikento | 0:c7074b60eeab | 9 | /* 割り込み処理宣言 */ |
takahashikento | 0:c7074b60eeab | 10 | Ticker TrigerTiming; //Trigerピン :インターバルタイマ |
takahashikento | 0:c7074b60eeab | 11 | InterruptIn USSEcho (p12); //p12 :超音波センサ エコー入力 |
takahashikento | 0:c7074b60eeab | 12 | |
takahashikento | 0:c7074b60eeab | 13 | /* 関数宣言 */ |
takahashikento | 0:c7074b60eeab | 14 | void init(void); |
takahashikento | 0:c7074b60eeab | 15 | void Output_Monitor(unsigned short Value); |
takahashikento | 0:c7074b60eeab | 16 | |
takahashikento | 0:c7074b60eeab | 17 | |
takahashikento | 0:c7074b60eeab | 18 | /* グローバル変数宣言*/ |
takahashikento | 0:c7074b60eeab | 19 | unsigned short USSDistance; //USSDistance:超音波センサ測定距離 |
takahashikento | 0:c7074b60eeab | 20 | |
takahashikento | 0:c7074b60eeab | 21 | /* main関数開始*/ |
takahashikento | 0:c7074b60eeab | 22 | int main() { |
takahashikento | 0:c7074b60eeab | 23 | char val; |
takahashikento | 0:c7074b60eeab | 24 | init(); |
takahashikento | 0:c7074b60eeab | 25 | while(1) { |
takahashikento | 0:c7074b60eeab | 26 | if( PC.readable() ){ |
takahashikento | 0:c7074b60eeab | 27 | Output_Monitor( USSDistance ); |
takahashikento | 0:c7074b60eeab | 28 | val = PC.getc(); |
takahashikento | 0:c7074b60eeab | 29 | } |
takahashikento | 0:c7074b60eeab | 30 | } |
takahashikento | 0:c7074b60eeab | 31 | } |
takahashikento | 0:c7074b60eeab | 32 | |
takahashikento | 0:c7074b60eeab | 33 | /*************************************************** |
takahashikento | 0:c7074b60eeab | 34 | * @brief 60ms毎の割り込みでUSSTrigerに10usのON出力 |
takahashikento | 0:c7074b60eeab | 35 | * @param なし |
takahashikento | 0:c7074b60eeab | 36 | * @return なし |
takahashikento | 0:c7074b60eeab | 37 | * @date 2014/12/16 新規作成 |
takahashikento | 0:c7074b60eeab | 38 | **************************************************/ |
takahashikento | 0:c7074b60eeab | 39 | void Triger (){ |
takahashikento | 0:c7074b60eeab | 40 | USSTriger = 1; |
takahashikento | 0:c7074b60eeab | 41 | wait_us(10); |
takahashikento | 0:c7074b60eeab | 42 | USSTriger = 0; |
takahashikento | 0:c7074b60eeab | 43 | } |
takahashikento | 0:c7074b60eeab | 44 | |
takahashikento | 0:c7074b60eeab | 45 | /*************************************************** |
takahashikento | 0:c7074b60eeab | 46 | * @brief USSEcho立ち上がりでの割り込み |
takahashikento | 0:c7074b60eeab | 47 | * @brief Hiの場合ActiveTimeタイマスタート |
takahashikento | 0:c7074b60eeab | 48 | * @param なし |
takahashikento | 0:c7074b60eeab | 49 | * @return なし |
takahashikento | 0:c7074b60eeab | 50 | * @date 2014/12/16 新規作成 |
takahashikento | 0:c7074b60eeab | 51 | **************************************************/ |
takahashikento | 0:c7074b60eeab | 52 | void RiseEcho(){ |
takahashikento | 0:c7074b60eeab | 53 | ActiveTime.start(); |
takahashikento | 0:c7074b60eeab | 54 | } |
takahashikento | 0:c7074b60eeab | 55 | |
takahashikento | 0:c7074b60eeab | 56 | |
takahashikento | 0:c7074b60eeab | 57 | /*************************************************** |
takahashikento | 0:c7074b60eeab | 58 | * @brief USSEcho立ち下がりでの割り込み |
takahashikento | 0:c7074b60eeab | 59 | * @brief Lowの場合ActiveTimeタイマ停止+値読み取り |
takahashikento | 0:c7074b60eeab | 60 | * @param なし |
takahashikento | 0:c7074b60eeab | 61 | * @return なし |
takahashikento | 0:c7074b60eeab | 62 | * @date 2014/12/16 新規作成 |
takahashikento | 0:c7074b60eeab | 63 | **************************************************/ |
takahashikento | 0:c7074b60eeab | 64 | void FallEcho(){ |
takahashikento | 0:c7074b60eeab | 65 | unsigned long ActiveWidth; |
takahashikento | 0:c7074b60eeab | 66 | ActiveTime.stop(); |
takahashikento | 0:c7074b60eeab | 67 | ActiveWidth = ActiveTime.read(); |
takahashikento | 0:c7074b60eeab | 68 | USSDistance = ActiveWidth * 0.0170; |
takahashikento | 0:c7074b60eeab | 69 | ActiveTime.reset(); |
takahashikento | 0:c7074b60eeab | 70 | } |
takahashikento | 0:c7074b60eeab | 71 | |
takahashikento | 0:c7074b60eeab | 72 | /*************************************************** |
takahashikento | 0:c7074b60eeab | 73 | * @brief 各種機能のプロパティ設定 |
takahashikento | 0:c7074b60eeab | 74 | * @param なし |
takahashikento | 0:c7074b60eeab | 75 | * @return なし |
takahashikento | 0:c7074b60eeab | 76 | * @date 2014/12/13 新規作成 |
takahashikento | 0:c7074b60eeab | 77 | **************************************************/ |
takahashikento | 0:c7074b60eeab | 78 | void init(void){ |
takahashikento | 0:c7074b60eeab | 79 | TrigerTiming.attach( Triger , 0.060 ); //USSTriger周期 60ms |
takahashikento | 0:c7074b60eeab | 80 | USSEcho.rise( RiseEcho ); //USSEcho立ち上がり時割り込み |
takahashikento | 0:c7074b60eeab | 81 | USSEcho.fall( FallEcho ); //USSEcho立ち下がり時割り込み |
takahashikento | 0:c7074b60eeab | 82 | } |
takahashikento | 0:c7074b60eeab | 83 | |
takahashikento | 0:c7074b60eeab | 84 | /*************************************************** |
takahashikento | 0:c7074b60eeab | 85 | * @brief Parameterの値をPC画面に出力 |
takahashikento | 0:c7074b60eeab | 86 | * @param Value : 画面に出力する値 |
takahashikento | 0:c7074b60eeab | 87 | * @return なし |
takahashikento | 0:c7074b60eeab | 88 | * @date 2014/12/14 新規作成 |
takahashikento | 0:c7074b60eeab | 89 | **************************************************/ |
takahashikento | 0:c7074b60eeab | 90 | void Output_Monitor(unsigned long Value){ |
takahashikento | 0:c7074b60eeab | 91 | PC.printf("%d[cm]\r\n",Value); |
takahashikento | 0:c7074b60eeab | 92 | } |