Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include"mbed.h" 00002 00003 #define ADXL345_I2C_READ 0xA7 00004 #define ADXL345_I2C_WRITE 0xA6 00005 #define ADXL345_I2C_ADDRESS 0x53 00006 #define ADXL345_DEVID_REG 0x00 00007 #define ADXL345_DATAX0_REG 0x32 00008 #define ADXL345_POWER_CTL_REG 0x2D 00009 #define MeasurementMode 0x08 00010 00011 I2C adxl345(PA_10,PA_09); 00012 Serial uart0(PA_13,PA_14); 00013 //DigitalOut led(LED1); 00014 int16_t xyz[3] = {0, 0, 0}; 00015 00016 int main() { 00017 char config[2]; 00018 char data[6]; 00019 config[0] = ADXL345_DEVID_REG; 00020 char output; 00021 uart0.baud(9600); 00022 adxl345.write( ADXL345_I2C_WRITE , config, 1); 00023 adxl345.read( ADXL345_I2C_READ , &output, 1); 00024 uart0.printf("ADXL345 device id is 0x%x.\r\n",output); 00025 config[0] = ADXL345_POWER_CTL_REG; 00026 config[1] = MeasurementMode; 00027 adxl345.write( ADXL345_I2C_WRITE, config, 2); //set power 00028 00029 config[0]=ADXL345_DATAX0_REG; 00030 while (1) 00031 { 00032 adxl345.write( ADXL345_I2C_WRITE, config, 1); 00033 adxl345.read( ADXL345_I2C_READ , data, 6); 00034 xyz[0]=(int)data[1]<<8|(int)data[0]; 00035 xyz[1]=(int)data[3]<<8|(int)data[2]; 00036 xyz[2]=(int)data[5]<<8|(int)data[4]; 00037 uart0.printf("%6i, %6i, %6i\n", xyz[0], xyz[1], xyz[2]); 00038 wait(0.1); 00039 } 00040 }
Generated on Tue Jul 19 2022 05:48:15 by
1.7.2