For Hepta-Sat Lite

Dependencies:   mbed

Committer:
goro56
Date:
Mon Nov 01 07:44:54 2021 +0000
Revision:
0:c92d21a4e8c2
Lite Geomagnetic

Who changed what in which revision?

UserRevisionLine numberNew contents of line
goro56 0:c92d21a4e8c2 1 #include "mbed.h"
goro56 0:c92d21a4e8c2 2 I2C i2c(PB_7, PB_6);
goro56 0:c92d21a4e8c2 3 Serial pc(USBTX, USBRX, 9600);
goro56 0:c92d21a4e8c2 4 const int addr_accel_gyro = 0xD0;
goro56 0:c92d21a4e8c2 5 const int addr_compus = 0x18;
goro56 0:c92d21a4e8c2 6
goro56 0:c92d21a4e8c2 7
goro56 0:c92d21a4e8c2 8 char cmd[2];
goro56 0:c92d21a4e8c2 9 char data[1];
goro56 0:c92d21a4e8c2 10 char adata[1];
goro56 0:c92d21a4e8c2 11 char mxl[1], mxh[1], myl[1], myh[1], mzl[1], mzh[1] , st[1];
goro56 0:c92d21a4e8c2 12 double mg_x, mg_y, mg_z, MX, MY, MZ;
goro56 0:c92d21a4e8c2 13
goro56 0:c92d21a4e8c2 14 int main() {
goro56 0:c92d21a4e8c2 15 pc.printf("Start detectiong magnetism\r\n");
goro56 0:c92d21a4e8c2 16 wait(1.0);
goro56 0:c92d21a4e8c2 17 i2c.frequency(100000);
goro56 0:c92d21a4e8c2 18
goro56 0:c92d21a4e8c2 19 cmd[0]=0x6B;
goro56 0:c92d21a4e8c2 20 cmd[1]=0x00;
goro56 0:c92d21a4e8c2 21 i2c.write(addr_accel_gyro,cmd,2);
goro56 0:c92d21a4e8c2 22 cmd[0] = 0x37;
goro56 0:c92d21a4e8c2 23 cmd[1] = 0x02;
goro56 0:c92d21a4e8c2 24 i2c.write(addr_accel_gyro,cmd,2);
goro56 0:c92d21a4e8c2 25
goro56 0:c92d21a4e8c2 26 //write address of compus
goro56 0:c92d21a4e8c2 27 adata[0] = 0x0A;
goro56 0:c92d21a4e8c2 28 adata[1] = 0x12;
goro56 0:c92d21a4e8c2 29 i2c.write(addr_compus,adata,2);
goro56 0:c92d21a4e8c2 30 i2c.stop();
goro56 0:c92d21a4e8c2 31 wait(1);
goro56 0:c92d21a4e8c2 32
goro56 0:c92d21a4e8c2 33 while(1) {
goro56 0:c92d21a4e8c2 34
goro56 0:c92d21a4e8c2 35 //=========== x ===========
goro56 0:c92d21a4e8c2 36 //read mxl
goro56 0:c92d21a4e8c2 37 adata[0] = 0x03;
goro56 0:c92d21a4e8c2 38 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 39 i2c.read(addr_compus|0x01,mxl,1);
goro56 0:c92d21a4e8c2 40
goro56 0:c92d21a4e8c2 41 //read mxh
goro56 0:c92d21a4e8c2 42 adata[0] = 0x04;
goro56 0:c92d21a4e8c2 43 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 44 i2c.read(addr_compus|0x01,mxh,1);
goro56 0:c92d21a4e8c2 45
goro56 0:c92d21a4e8c2 46
goro56 0:c92d21a4e8c2 47 //=========== y ===========
goro56 0:c92d21a4e8c2 48 adata[0] = 0x05;
goro56 0:c92d21a4e8c2 49 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 50 i2c.read(addr_compus|0x01,myl,1);
goro56 0:c92d21a4e8c2 51
goro56 0:c92d21a4e8c2 52 adata[0] = 0x06;
goro56 0:c92d21a4e8c2 53 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 54 i2c.read(addr_compus|0x01,myh,1);
goro56 0:c92d21a4e8c2 55
goro56 0:c92d21a4e8c2 56
goro56 0:c92d21a4e8c2 57 //=========== z ===========
goro56 0:c92d21a4e8c2 58 adata[0] = 0x07;
goro56 0:c92d21a4e8c2 59 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 60 i2c.read(addr_compus|0x01,mzl,1);
goro56 0:c92d21a4e8c2 61
goro56 0:c92d21a4e8c2 62 adata[0] = 0x08;
goro56 0:c92d21a4e8c2 63 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 64 i2c.read(addr_compus|0x01,mzh,1);
goro56 0:c92d21a4e8c2 65
goro56 0:c92d21a4e8c2 66 //status
goro56 0:c92d21a4e8c2 67 adata[0] = 0x09;
goro56 0:c92d21a4e8c2 68 i2c.write(addr_compus,adata,1);
goro56 0:c92d21a4e8c2 69 i2c.read(addr_compus|0x01,st,1);
goro56 0:c92d21a4e8c2 70
goro56 0:c92d21a4e8c2 71
goro56 0:c92d21a4e8c2 72
goro56 0:c92d21a4e8c2 73 mg_x = short((mxh[0]<<8)|(mxl[0]));
goro56 0:c92d21a4e8c2 74 mg_y = short((myh[0]<<8)|(myl[0]));
goro56 0:c92d21a4e8c2 75 mg_z = short((mzh[0]<<8)|(mzl[0]));
goro56 0:c92d21a4e8c2 76 MX = mg_x*0.15;
goro56 0:c92d21a4e8c2 77 MY = mg_y*0.15;
goro56 0:c92d21a4e8c2 78 MZ = mg_z*0.15;
goro56 0:c92d21a4e8c2 79 pc.printf("MX= %f,MY= %f,MZ= %f\r\n",MX,MY,MZ);
goro56 0:c92d21a4e8c2 80
goro56 0:c92d21a4e8c2 81 wait(0.5);
goro56 0:c92d21a4e8c2 82
goro56 0:c92d21a4e8c2 83 }
goro56 0:c92d21a4e8c2 84
goro56 0:c92d21a4e8c2 85 }