hui wang
/
adxl345
Release V0.1
main.cpp@0:133799070ae9, 2016-07-15 (annotated)
- Committer:
- dfrobot
- Date:
- Fri Jul 15 10:16:17 2016 +0000
- Revision:
- 0:133799070ae9
release V0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dfrobot | 0:133799070ae9 | 1 | #include"mbed.h" |
dfrobot | 0:133799070ae9 | 2 | |
dfrobot | 0:133799070ae9 | 3 | #define ADXL345_I2C_READ 0xA7 |
dfrobot | 0:133799070ae9 | 4 | #define ADXL345_I2C_WRITE 0xA6 |
dfrobot | 0:133799070ae9 | 5 | #define ADXL345_I2C_ADDRESS 0x53 |
dfrobot | 0:133799070ae9 | 6 | #define ADXL345_DEVID_REG 0x00 |
dfrobot | 0:133799070ae9 | 7 | #define ADXL345_DATAX0_REG 0x32 |
dfrobot | 0:133799070ae9 | 8 | #define ADXL345_POWER_CTL_REG 0x2D |
dfrobot | 0:133799070ae9 | 9 | #define MeasurementMode 0x08 |
dfrobot | 0:133799070ae9 | 10 | |
dfrobot | 0:133799070ae9 | 11 | I2C adxl345(PA_10,PA_09); |
dfrobot | 0:133799070ae9 | 12 | Serial uart0(PA_13,PA_14); |
dfrobot | 0:133799070ae9 | 13 | //DigitalOut led(LED1); |
dfrobot | 0:133799070ae9 | 14 | int16_t xyz[3] = {0, 0, 0}; |
dfrobot | 0:133799070ae9 | 15 | |
dfrobot | 0:133799070ae9 | 16 | int main() { |
dfrobot | 0:133799070ae9 | 17 | char config[2]; |
dfrobot | 0:133799070ae9 | 18 | char data[6]; |
dfrobot | 0:133799070ae9 | 19 | config[0] = ADXL345_DEVID_REG; |
dfrobot | 0:133799070ae9 | 20 | char output; |
dfrobot | 0:133799070ae9 | 21 | uart0.baud(9600); |
dfrobot | 0:133799070ae9 | 22 | adxl345.write( ADXL345_I2C_WRITE , config, 1); |
dfrobot | 0:133799070ae9 | 23 | adxl345.read( ADXL345_I2C_READ , &output, 1); |
dfrobot | 0:133799070ae9 | 24 | uart0.printf("ADXL345 device id is 0x%x.\r\n",output); |
dfrobot | 0:133799070ae9 | 25 | config[0] = ADXL345_POWER_CTL_REG; |
dfrobot | 0:133799070ae9 | 26 | config[1] = MeasurementMode; |
dfrobot | 0:133799070ae9 | 27 | adxl345.write( ADXL345_I2C_WRITE, config, 2); //set power |
dfrobot | 0:133799070ae9 | 28 | |
dfrobot | 0:133799070ae9 | 29 | config[0]=ADXL345_DATAX0_REG; |
dfrobot | 0:133799070ae9 | 30 | while (1) |
dfrobot | 0:133799070ae9 | 31 | { |
dfrobot | 0:133799070ae9 | 32 | adxl345.write( ADXL345_I2C_WRITE, config, 1); |
dfrobot | 0:133799070ae9 | 33 | adxl345.read( ADXL345_I2C_READ , data, 6); |
dfrobot | 0:133799070ae9 | 34 | xyz[0]=(int)data[1]<<8|(int)data[0]; |
dfrobot | 0:133799070ae9 | 35 | xyz[1]=(int)data[3]<<8|(int)data[2]; |
dfrobot | 0:133799070ae9 | 36 | xyz[2]=(int)data[5]<<8|(int)data[4]; |
dfrobot | 0:133799070ae9 | 37 | uart0.printf("%6i, %6i, %6i\n", xyz[0], xyz[1], xyz[2]); |
dfrobot | 0:133799070ae9 | 38 | wait(0.1); |
dfrobot | 0:133799070ae9 | 39 | } |
dfrobot | 0:133799070ae9 | 40 | } |