Hepta 9axis Sensor(magnetometer)
Dependencies: mbed HeptaBattery
Fork of Lab7-01_template by
main.cpp@17:251c49305819, 2019-08-19 (annotated)
- Committer:
- HEPTA
- Date:
- Mon Aug 19 06:45:58 2019 +0000
- Revision:
- 17:251c49305819
- Parent:
- 16:2ddb40e12082
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
HEPTA | 16:2ddb40e12082 | 2 | #include "HeptaBattery.h" |
HEPTA | 14:d758d18b2710 | 3 | I2C i2c(p28, p27); |
HEPTA | 14:d758d18b2710 | 4 | Serial pc(USBTX, USBRX); |
HEPTA | 17:251c49305819 | 5 | HeptaBattery battery(p16,p26); |
HEPTA | 14:d758d18b2710 | 6 | const int addr_accel_gyro = 0xD0; |
HEPTA | 14:d758d18b2710 | 7 | char cmd[2]; |
HEPTA | 14:d758d18b2710 | 8 | const int addr_compus = 0x18; |
HEPTA | 14:d758d18b2710 | 9 | short int mxl,mxh,myl,myh,mzl,mzh,st2; |
umeume | 2:1c5cdb2c3e0f | 10 | int main() |
umeume | 2:1c5cdb2c3e0f | 11 | { |
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 | } |