Esempio n. 1
0
// Periodically reads the IMU and outputs the raw content via UART2
void i2c_task(void *pvParameters)
{
	char str[64];
	c_common_i2c_readBytes(ADXL345_ADDR, 0x00, 1, &ADXL345_ID);

	// Accelerometer increase G-range (+/- 16G)
	c_common_i2c_writeByte(ADXL345_ADDR, 0x31, 0b00001011);

    //  ADXL345 POWER_CTL
    c_common_i2c_writeByte(ADXL345_ADDR, 0x2D, 0);
    c_common_i2c_writeByte(ADXL345_ADDR, 0x2D, 16);
    c_common_i2c_writeByte(ADXL345_ADDR, 0x2D, 8);

	while(1) {
	    // Read x, y, z acceleration, pack the data.
		c_common_i2c_readBytes(ADXL345_ADDR, ADXL345_X_ADDR, 6, sensorBuffer);
	    accRaw[0] = ((int)sensorBuffer[0] | ((int)sensorBuffer[1] << 8)) * -1;
	    accRaw[1] = ((int)sensorBuffer[2] | ((int)sensorBuffer[3] << 8)) * -1;
	    accRaw[2] = (int)sensorBuffer[4] | ((int)sensorBuffer[5] << 8);

	    sprintf(str, "Accel: %d %d %d\n\r", accRaw[0], accRaw[1], accRaw[2]);
	    c_common_usart_puts(USART2, str);

		vTaskDelay(100/portTICK_RATE_MS);
	}
}
Esempio n. 2
0
/** \brief Escreve apenas um bit em um dado endereço de um dispositivo.
 *
 * @param I2Cx  I2C a ser utilizada.
 * @param device Endereço do dispositivo no barramento.
 * @param address Endereço do byte no qual o bit será escrito.
 * @param bit Posição do bit a ser escrito (0..7).
 * @param value Valor do bit a ser escrito (0 ou 1).
 */
void c_common_i2c_writeBit(I2C_TypeDef* I2Cx ,uint8_t device, uint8_t address, uint8_t bit, bool value) {
	uint8_t byteBuffer;
	c_common_i2c_readBytes(I2Cx, device, address, 1, &byteBuffer);
	byteBuffer = (value == 0)? (byteBuffer & (1<<bit)) : (byteBuffer | (1<<bit));
	c_common_i2c_writeByte(I2Cx, device, address,byteBuffer);
}