Hepta 9axis Sensor(magnetometer)
Dependencies: mbed HeptaBattery
Fork of Lab7-01_template by
main.cpp@15:44477a8ccbbf, 2019-08-17 (annotated)
- Committer:
- HEPTA
- Date:
- Sat Aug 17 08:56:29 2019 +0000
- Revision:
- 15:44477a8ccbbf
- Parent:
- 14:d758d18b2710
- Child:
- 16:2ddb40e12082
Added sentence to make EN pin high
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
HEPTA | 14:d758d18b2710 | 2 | I2C i2c(p28, p27); |
HEPTA | 14:d758d18b2710 | 3 | Serial pc(USBTX, USBRX); |
HEPTA | 15:44477a8ccbbf | 4 | DigitalOut EN(p26); |
HEPTA | 14:d758d18b2710 | 5 | const int addr_accel_gyro = 0xD0; |
HEPTA | 14:d758d18b2710 | 6 | char cmd[2]; |
HEPTA | 14:d758d18b2710 | 7 | const int addr_compus = 0x18; |
HEPTA | 14:d758d18b2710 | 8 | short int mxl,mxh,myl,myh,mzl,mzh,st2; |
umeume | 2:1c5cdb2c3e0f | 9 | int main() |
umeume | 2:1c5cdb2c3e0f | 10 | { |
HEPTA | 15:44477a8ccbbf | 11 | EN = 1; // EN pin : high |
HEPTA | 14:d758d18b2710 | 12 | i2c.frequency(100000); |
HEPTA | 14:d758d18b2710 | 13 | cmd[0]=0x6B; |
HEPTA | 14:d758d18b2710 | 14 | cmd[1]=0x00; |
HEPTA | 14:d758d18b2710 | 15 | i2c.write(addr_accel_gyro,cmd,2); |
HEPTA | 14:d758d18b2710 | 16 | cmd[0] = 0x37; |
HEPTA | 14:d758d18b2710 | 17 | cmd[1] = 0x02; |
HEPTA | 14:d758d18b2710 | 18 | i2c.write(addr_accel_gyro,cmd,2); |
HEPTA | 14:d758d18b2710 | 19 | i2c.stop(); |
HEPTA | 14:d758d18b2710 | 20 | while(1) { |
HEPTA | 14:d758d18b2710 | 21 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 22 | i2c.write(addr_compus); |
HEPTA | 14:d758d18b2710 | 23 | i2c.write(0x0A); |
HEPTA | 14:d758d18b2710 | 24 | i2c.write(0x12); |
HEPTA | 14:d758d18b2710 | 25 | i2c.stop(); |
HEPTA | 14:d758d18b2710 | 26 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 27 | i2c.write(addr_compus); |
HEPTA | 14:d758d18b2710 | 28 | i2c.write(0x03); |
HEPTA | 14:d758d18b2710 | 29 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 30 | i2c.write(addr_compus|0x01); |
HEPTA | 14:d758d18b2710 | 31 | mxl = i2c.read(0); |
HEPTA | 14:d758d18b2710 | 32 | i2c.stop(); |
HEPTA | 14:d758d18b2710 | 33 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 34 | i2c.write(addr_compus); |
HEPTA | 14:d758d18b2710 | 35 | i2c.write(0x04); |
HEPTA | 14:d758d18b2710 | 36 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 37 | i2c.write(addr_compus|0x01); |
HEPTA | 14:d758d18b2710 | 38 | mxh = i2c.read(0); |
HEPTA | 14:d758d18b2710 | 39 | i2c.stop(); |
HEPTA | 14:d758d18b2710 | 40 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 41 | i2c.write(addr_compus); |
HEPTA | 14:d758d18b2710 | 42 | i2c.write(0x09); |
HEPTA | 14:d758d18b2710 | 43 | i2c.start(); |
HEPTA | 14:d758d18b2710 | 44 | i2c.write(addr_compus|0x01); |
HEPTA | 14:d758d18b2710 | 45 | st2 = i2c.read(0); |
HEPTA | 14:d758d18b2710 | 46 | i2c.stop(); |
HEPTA | 14:d758d18b2710 | 47 | double mg_x = short((mxh<<8) | (mxl)); |
HEPTA | 14:d758d18b2710 | 48 | double MX = ( mg_x)*0.15; |
HEPTA | 14:d758d18b2710 | 49 | pc.printf("MX = %f\r\n",MX); |
HEPTA | 14:d758d18b2710 | 50 | wait(0.5); |
HEPTA | 14:d758d18b2710 | 51 | } |
umeume | 2:1c5cdb2c3e0f | 52 | } |