コウスケ スズキ
/
20224222
i2c 2
Diff: main.cpp
- Revision:
- 1:b38a436a0059
- Parent:
- 0:bf86efb237ca
diff -r bf86efb237ca -r b38a436a0059 main.cpp --- a/main.cpp Fri Apr 22 17:24:36 2022 +0000 +++ b/main.cpp Fri Apr 22 19:03:26 2022 +0000 @@ -7,7 +7,6 @@ 「マスターが受信する」によって、取得するため 1.slave address(スレーブアドレス→内部レジスタ) - 2.ack(受信) 3.data1(8bits) 4.ack=1(送信) @@ -24,9 +23,13 @@ const int addr = 0xD0; //加速度とジャイロのスレーブアドレス -short int xl,xh; //x軸の加速度 -short int yl,yh; //y軸の加速度 -short int zl,zh; +short int axl,axh; //x軸の加速度 +short int ayl,ayh; //y軸の加速度 +short int azl,azh; //z軸の加速度 + +short int gxl,gxh; //xの角速度 +short int gyl,gyh; //yの角速度 +short int gzl,gzh; //zの角速度 int main() { @@ -34,6 +37,7 @@ pc.printf("Start!!\r\n"); while(1) { + //加速度 //x軸加速度取得 //1.slave address(スレーブアドレス→内部レジスタ) @@ -52,7 +56,7 @@ //4.ack=0(送信) i2c.start(); i2c.write(addr|0x01); //ack=1(送信) - xh = i2c.read(0);//data取り出す + axh = i2c.read(0);//data取り出す i2c.stop(); i2c.start(); @@ -60,7 +64,7 @@ i2c.write(0x3d);//data2(61)2 //6.ack=1(送信) i2c.write(addr|0x01); //ack=1(受信) - xl = i2c.read(0);//data取り出す + axl = i2c.read(0);//data取り出す i2c.stop(); //以下2~6を繰り返す。 @@ -74,13 +78,13 @@ i2c.start(); i2c.write(addr|0x01); //ack=1(送信) - yh = i2c.read(0);//data取り出す + ayh = i2c.read(0);//data取り出す i2c.stop(); i2c.start(); i2c.write(0x3f); //data(63)4 i2c.write(addr|0x01); //ack=1(送信) - yl = i2c.read(0);//data取り出す + ayl = i2c.read(0);//data取り出す i2c.stop(); //z軸加速度取得 @@ -91,27 +95,87 @@ i2c.start(); i2c.write(addr|0x01); //ack=1(送信) - zh = i2c.read(0);//data取り出す + azh = i2c.read(0);//data取り出す i2c.stop(); i2c.start(); i2c.write(0x41); //data(65)6 i2c.write(addr|0x01); //ack=1(送信) - zl = i2c.read(0);//data取り出す + azl = i2c.read(0);//data取り出す i2c.stop(); + //ジャイロセンサ + //x + i2c.start(); + i2c.read(false); //ack(受信) + i2c.write(0x42); //data + i2c.stop(); + + i2c.start(); + i2c.write(addr|0x01); //ack=1(送信) + gxh = i2c.read(0);//data取り出す + i2c.stop(); + + i2c.start(); + i2c.write(0x43); //data + i2c.write(addr|0x01); //ack=1(送信) + gxl = i2c.read(0);//data取り出す + i2c.stop(); + + //y + i2c.start(); + i2c.read(false); //ack(受信) + i2c.write(0x44); //data + i2c.stop(); + + i2c.start(); + i2c.write(addr|0x01); //ack=1(送信) + gyh = i2c.read(0);//data取り出す + i2c.stop(); + + i2c.start(); + i2c.write(0x45); //data + i2c.write(addr|0x01); //ack=1(送信) + gyl = i2c.read(0);//data取り出す + i2c.stop(); + + //z + i2c.start(); + i2c.read(false); //ack(受信) + i2c.write(0x46); //data + i2c.stop(); + + i2c.start(); + i2c.write(addr|0x01); //ack=1(送信) + gzh = i2c.read(0);//data取り出す + i2c.stop(); + + i2c.start(); + i2c.write(0x47); //data + i2c.write(addr|0x01); //ack=1(送信) + gzl = i2c.read(0);//data取り出す + i2c.stop(); - double acc_ax = short((xh<<8) | (xl)); - double acc_ay = short((yh<<8) | (yl)); - double acc_az = short((zh<<8) | (zl)); + double acc_ax = short((axh<<8) | (axl)); + double acc_ay = short((ayh<<8) | (ayl)); + double acc_az = short((azh<<8) | (azl)); + + double acc_gx = short((gxh<<8) | (gxl)); + double acc_gy = short((gyh<<8) | (gyl)); + double acc_gz = short((gzh<<8) | (gzl)); double AX = (acc_ax)*2/32768*9.81; double AY = (acc_ay)*2/32768*9.81; double AZ = (acc_az)*2/32768*9.81; + double GX = (acc_gx)*0.0128114995; //0.02562299 + double GY = (acc_gy)*0.0128114995; //0.0128114995(測定レンジ±500) + double GZ = (acc_gz)*0.0128114995; + pc.printf("%f %f %f\r\n",AX,AY,AZ); + pc.printf("%f %f %f\r\n",GX,GY,GZ); //GXがずれている wait(0.01); } } \ No newline at end of file