HEPTA-Sat Training 2019
/
Lab4-02_Detect_acceleration_x-axis
Lab4
main.cpp@27:e25138fe99eb, 2019-10-31 (annotated)
- Committer:
- HeptaSatTraining2019
- Date:
- Thu Oct 31 08:18:53 2019 +0000
- Revision:
- 27:e25138fe99eb
- Parent:
- 25:864883d3fe57
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
HeptaSatTraining2019 | 24:ca9f33c96136 | 2 | #include "HEPTA_EPS.h" |
umeume | 2:1c5cdb2c3e0f | 3 | |
umeume | 2:1c5cdb2c3e0f | 4 | Serial pc(USBTX,USBRX); |
HeptaSatTraining2019 | 21:92c25e853b87 | 5 | HEPTA_EPS eps(p16,p26); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 6 | I2C i2c(p28, p27); |
umeume | 2:1c5cdb2c3e0f | 7 | |
HeptaSatTraining2019 | 24:ca9f33c96136 | 8 | const int addr_accel_gyro = 0xD0; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 9 | char cmd[2]; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 10 | short int xl,xh,yl,yh,zl,zh; |
umeume | 2:1c5cdb2c3e0f | 11 | int main() |
umeume | 2:1c5cdb2c3e0f | 12 | { |
HeptaSatTraining2019 | 24:ca9f33c96136 | 13 | i2c.frequency(100000); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 14 | cmd[0]=0x6B; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 15 | cmd[1]=0x00; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 16 | i2c.write(addr_accel_gyro,cmd,2); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 17 | cmd[0] = 0x37; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 18 | cmd[1] = 0x02; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 19 | i2c.write(addr_accel_gyro,cmd,2); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 20 | i2c.stop(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 21 | while(1) { |
HeptaSatTraining2019 | 24:ca9f33c96136 | 22 | i2c.start(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 23 | i2c.write(addr_accel_gyro); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 24 | i2c.write(0x3b);//read adress |
HeptaSatTraining2019 | 24:ca9f33c96136 | 25 | i2c.start(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 26 | i2c.write(addr_accel_gyro|0x01); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 27 | xh = i2c.read(0); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 28 | i2c.stop(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 29 | i2c.start(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 30 | i2c.write(addr_accel_gyro); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 31 | i2c.write(0x3c); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 32 | i2c.start(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 33 | i2c.write(addr_accel_gyro|0x01); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 34 | xl = i2c.read(0); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 35 | i2c.stop(); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 36 | double acc_ax = short((xh<<8) | (xl)); |
HeptaSatTraining2019 | 25:864883d3fe57 | 37 | double AX = (acc_ax)*2/32768*9.81; |
HeptaSatTraining2019 | 24:ca9f33c96136 | 38 | pc.printf("AX = %f\r\n",AX); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 39 | wait(0.5); |
HeptaSatTraining2019 | 24:ca9f33c96136 | 40 | } |
umeume | 2:1c5cdb2c3e0f | 41 | } |