This library enables users to communicate with the ADXL345 accelerometer through the I2C bus on the mbed. The API names are similar and work nearly the same way as those made in the SPI libraries for the ADXL345.
Dependencies: mbed
Revision 3:d030b74d1d71, committed 2019-05-24
- Comitter:
- RobotManYt
- Date:
- Fri May 24 14:36:05 2019 +0000
- Parent:
- 2:73154596c54b
- Commit message:
- l;
Changed in this revision
ADXL345_I2C.cpp | Show annotated file Show diff for this revision Revisions of this file |
ADXL345_I2C.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 73154596c54b -r d030b74d1d71 ADXL345_I2C.cpp --- a/ADXL345_I2C.cpp Wed May 08 21:07:31 2019 +0000 +++ b/ADXL345_I2C.cpp Fri May 24 14:36:05 2019 +0000 @@ -56,13 +56,12 @@ ADXL345_I2C::ADXL345_I2C(PinName sda, PinName scl) : i2c_(sda, scl) { //400kHz, allowing us to use the fastest data rates. - i2c_.frequency(400000); + i2c_.frequency(100000); // initialize the BW data rate char tx[2]; tx[0] = ADXL345_BW_RATE_REG; - tx[1] = ADXL345_1600HZ; //value greater than or equal to 0x0A is written into the rate bits (Bit D3 through Bit D0) in the BW_RATE register + tx[1] = ADXL345_100HZ; //value greater than or equal to 0x0A is written into the rate bits (Bit D3 through Bit D0) in the BW_RATE register i2c_.write( ADXL345_I2C_WRITE , tx, 2); - //Data format (for +-16g) - This is done by setting Bit D3 of the DATA_FORMAT register (Address 0x31) and writing a value of 0x03 to the range bits (Bit D1 and Bit D0) of the DATA_FORMAT register (Address 0x31). char rx[2]; @@ -84,6 +83,10 @@ z[0] = ADXL345_OFSZ_REG ; z[1] = 0xFE; i2c_.write( ADXL345_I2C_WRITE , z, 2); + + // LCD init + char init[7] = {LCD_CMD, LCD_CLEAR, LCD_CMD, LCD_CURSOR, 0, LCD_CMD, LCD_ON}; + i2c_.write(LCD, init, 7); } @@ -142,6 +145,51 @@ } +void ADXL345_I2C::writeEEPROM(char *data, uint8_t lenght){ + i2c_.write(EEPROM_WRITE, data, lenght, 0); +} + + +void ADXL345_I2C::LCDprint(float Xaxes = 0, float Yaxes = 0, float Zaxes = 0){ + + // X + stringstream ss; + ss << Xaxes; + string x = "X : " + ss.str(); + uint8_t length = x.size(); + char data[length + 2]; + data[0] = LCD_CURSOR; + data[1] = 0; + for(uint8_t pop = 0; pop < length; pop++){ + data[pop + 2] = x[pop]; + } + i2c_.write(LCD, data, length); + + // Y + ss << Yaxes; + string y = "Y : " + ss.str(); + length = y.size(); + data[length + 2]; + data[0] = LCD_CURSOR; + data[1] = 0x40; + for(uint8_t pop = 0; pop < length; pop++){ + data[pop + 2] = y[pop]; + } + i2c_.write(LCD, data, length); + + // Z + ss << Zaxes; + string z = "z : " + ss.str(); + length = z.size(); + data[length + 2]; + data[0] = LCD_CURSOR; + data[1] = 0x14; + for(uint8_t pop = 0; pop < length; pop++){ + data[pop + 2] = z[pop]; + } + i2c_.write(LCD, data, length); +} + char ADXL345_I2C::getDeviceID() { return SingleByteRead(ADXL345_DEVID_REG);
diff -r 73154596c54b -r d030b74d1d71 ADXL345_I2C.h --- a/ADXL345_I2C.h Wed May 08 21:07:31 2019 +0000 +++ b/ADXL345_I2C.h Fri May 24 14:36:05 2019 +0000 @@ -55,6 +55,9 @@ * Includes */ #include "mbed.h" +#include "math.h" +#include "string" +#include "sstream" /** * Defines @@ -125,6 +128,17 @@ +#define EEPROM_READ 0xA6 +#define EEPROM_WRITE 0XA7 + +#define LCD 0x50 +#define LCD_ON 0x41 +#define LCD_OFF 0x42 +#define LCD_CURSOR 0X45 +#define LCD_CLEAR 0x51 +#define LCD_CMD 0xFE + + @@ -148,6 +162,11 @@ */ void getOutput(int* readings); + void writeEEPROM(char *data, uint8_t lenght); + + //LCD + void LCDprint(float Xaxes, float Yaxes, float Zaxes); + /** * Read the device ID register on the device. *