twe-liteを用いた実験用プログラム。回転数を回転数側から受信する目的で書いた。
Fork of twe-lite_main by
Diff: main.cpp
- Revision:
- 0:dd2e7831df58
- Child:
- 1:107266d60757
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Feb 23 04:20:23 2018 +0000 @@ -0,0 +1,68 @@ +#include "mbed.h" + +#define DATA_SIZE 10 + +DigitalOut myled(LED1); + +Serial pc(USBTX,USBRX); +Serial twe(p9,p10); + +Ticker flipper; + +char rpm_array[3] = "00";//実は末尾にnullがあるので、要素が4つでも配列では5つの箱を用意している +char dam; + +int rpm=0; + + +void send(){ + + char DATA[DATA_SIZE] = {':', '0', '1', '0', '1', '3', '9', 'X', '\r', '\n'}; + //04 + /*:は送信時に必要なヘッダー。78は相手のID(0x78)。01は送信コマンドとして必要。 + 39は任意の値。Xはチェックサムの省略形。 \r\nは改行コマンド*/ + + for (int i = 0 ; i < DATA_SIZE ; i++){ + twe.putc(DATA[i]); + myled=1; + + } + + } + + +int main() { + pc.baud(115200); +twe.baud(115200); + +flipper.attach(&send, 0.05); + + + while(1) { + // pc.putc(twe.getc()); + + + if(twe.getc()==':'){ + if(twe.getc()=='0'){ + if(twe.getc()=='1'){ + dam=twe.getc(); + dam=twe.getc(); + + for (int i = 0; i <= 2; i++) { + rpm_array[i] =twe.getc();//高度の受信rpm+1000の値を文字列として受け取る + } + + rpm = atoi(rpm_array); + + pc.printf("%d\r\n",rpm); + + }//if(twe.getc()=='0') + }//if(twe.getc()==':') +} + + +}//while + + + +}//main \ No newline at end of file