/************************************************************************************************** * Function ADXL345_Write() * ------------------------------------------------------------------------------------------------- * Overview: Function write byte of data to ADXL345 * Input: register address, data * Output: Nothing **************************************************************************************************/ void ADXL345_Write(unsigned short address, unsigned short data1) { I2C2_Start(); // issue I2C start signal I2C2_Write(0x3A); // send byte via I2C (device address + W) I2C2_Write(address); // send byte (address of the location) I2C2_Write(data1); // send data (data to be written) I2C2_Stop(); // issue I2C stop signal }
void TS_Write (unsigned char reg, unsigned char num, unsigned int val) { I2C2_Start(); I2C2_Addr(0x82 | 0); I2C2_Write(reg); /* Register to be written */ while (num--) { I2C2_Write(val & 0xFF); val >>= 8; } I2C2_Stop(); }
/************************************************************************************************** * Function ADXL345_Read() * ------------------------------------------------------------------------------------------------- * Overview: Function read byte of data from ADXL345 * Input: register address * Output: data from addressed register in ADXL345 **************************************************************************************************/ unsigned short ADXL345_Read(unsigned short address) { unsigned short tmp = 0; I2C2_Start(); // issue I2C start signal I2C2_Write(0x3A); // send byte via I2C (device address + W) I2C2_Write(address); // send byte (data address) I2C2_Start(); // issue I2C signal repeated start I2C2_Write(0x3B); // send byte (device address + R) tmp = I2C2_Read(1); // Read the data (NO acknowledge) I2C2_Stop(); // issue I2C stop signal return tmp; }
unsigned int TS_Read (unsigned char reg, unsigned char num) { unsigned int res = 0; I2C2_Start(); I2C2_Addr(0x82 | 0); I2C2_Write(reg); /* Register to be read */ I2C2_Start(); I2C2_Addr(0x82 | 1); while (num--) { res <<= 8; res |= I2C2_Read ((num >= 1)); } I2C2_Stop(); return(res); }