![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
I2C通信を使用した角速度測定、LCD表示サンプル
Dependencies: mbed mbed_mpu6050_i2c_raw_register AQM0802
main.cpp@6:c940ad83d2c5, 2019-07-22 (annotated)
- Committer:
- yusaku0125
- Date:
- Mon Jul 22 09:43:17 2019 +0000
- Revision:
- 6:c940ad83d2c5
- Parent:
- 5:8c482bdfb9e8
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
satoshi1204 | 0:abd3a0fd55a9 | 1 | #include "mbed.h" |
yusaku0125 | 4:bf507fe18cb2 | 2 | #include "AQM0802.h" |
satoshi1204 | 0:abd3a0fd55a9 | 3 | #include "mpu6050.h" |
satoshi1204 | 0:abd3a0fd55a9 | 4 | |
satoshi1204 | 0:abd3a0fd55a9 | 5 | Serial pc(USBTX, USBRX); |
yusaku0125 | 4:bf507fe18cb2 | 6 | MPU6050 mpu(D4, D5); |
yusaku0125 | 5:8c482bdfb9e8 | 7 | AQM0802 lcd(D4,D5); |
satoshi1204 | 0:abd3a0fd55a9 | 8 | |
yusaku0125 | 5:8c482bdfb9e8 | 9 | int main() |
yusaku0125 | 5:8c482bdfb9e8 | 10 | { |
yusaku0125 | 4:bf507fe18cb2 | 11 | double gx,gy,gz; //Z角速度情報を格納 |
yusaku0125 | 5:8c482bdfb9e8 | 12 | int i_gz; //Z軸データint型変換格納 |
yusaku0125 | 5:8c482bdfb9e8 | 13 | char str[5]; //LCD表示用文字列 |
yusaku0125 | 6:c940ad83d2c5 | 14 | |
yusaku0125 | 6:c940ad83d2c5 | 15 | |
yusaku0125 | 5:8c482bdfb9e8 | 16 | pc.baud(9600); //通信速度は9600bps |
yusaku0125 | 4:bf507fe18cb2 | 17 | mpu.setMaxScale(MAX_ACC_8G, MAX_GYRO_1000degpersec);//[1000deg/sec]を測定上限とする。 |
yusaku0125 | 5:8c482bdfb9e8 | 18 | lcd.init(); //LCD初期化 |
yusaku0125 | 5:8c482bdfb9e8 | 19 | lcd.locate(1,0); //桁、行 |
yusaku0125 | 5:8c482bdfb9e8 | 20 | lcd.print("GYRO_Z");//1行目に「GYRO_Z」を表示 |
yusaku0125 | 6:c940ad83d2c5 | 21 | |
yusaku0125 | 6:c940ad83d2c5 | 22 | while(true) { |
yusaku0125 | 6:c940ad83d2c5 | 23 | wait(0.2); |
yusaku0125 | 6:c940ad83d2c5 | 24 | //★☆角速度情報の取得☆★ |
yusaku0125 | 4:bf507fe18cb2 | 25 | mpu.readGyroscope(gx, gy, gz);//関数仕様上3軸すべて角速度取得する。 |
yusaku0125 | 5:8c482bdfb9e8 | 26 | i_gz=(int)gz; //doubleは大きすぎるのでint型へ変換 |
yusaku0125 | 6:c940ad83d2c5 | 27 | |
yusaku0125 | 6:c940ad83d2c5 | 28 | //★☆ターミナルへの角速度表示☆★ |
yusaku0125 | 4:bf507fe18cb2 | 29 | pc.printf("gz:%d\r\n", i_gz);//Z軸方向のみ表示する。(他は使わない) |
yusaku0125 | 6:c940ad83d2c5 | 30 | |
yusaku0125 | 6:c940ad83d2c5 | 31 | //★☆LCDへの角速度表示☆★ |
yusaku0125 | 4:bf507fe18cb2 | 32 | lcd.locate(1,1);//桁、行 |
yusaku0125 | 4:bf507fe18cb2 | 33 | lcd.print(" ");//表示クリア |
yusaku0125 | 4:bf507fe18cb2 | 34 | sprintf(str,"%4d",i_gz); |
yusaku0125 | 4:bf507fe18cb2 | 35 | lcd.locate(1,1);//桁、行 |
yusaku0125 | 5:8c482bdfb9e8 | 36 | lcd.print(str); //表示クリア |
satoshi1204 | 0:abd3a0fd55a9 | 37 | } |
satoshi1204 | 0:abd3a0fd55a9 | 38 | } |