Hi guys,
I'm new bie in Embedded System. I have a temperature and humdity sensor - AOSONG AM2320 which is comminication with my CC1310 via I2C. This sensor require receive a command to get data follow this structure: START + (I 2 C address + W) + function code (0x03) + start address + number of registers + STOP.
I'm using I2C_transfer to sent 3 bytes (function code, start address and number of register) to sensor but I2C_transfer is not OK. I read many time in datasheet and CC13xx Driver Library and TI-RTOS but I can't find how to get data from my sensor. Plz some body help me.
Here is my code but it's not work:
Void taskFxn(UArg arg0, UArg arg1) {
unsigned int i;
uint16_t temperature;
uint16_t humidity;
uint8_t txBuffer[3];
uint8_t rxBuffer[8];
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_100kHz;
i2c = I2C_open(CC1310_LAUNCHXL_I2C0, &i2cParams);
if (i2c == NULL) {
System_abort("Error Initializing I2C\n");
} else {
System_printf("I2C Initialized!\n");
}
//Read data from AM2320 sensor
txBuffer[0] = 0x03;
txBuffer[1] = 0x00;
txBuffer[2] = 0x04;
i2cTransaction.slaveAddress = 0x5c;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 3;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.readCount = 8;
if (I2C_transfer(i2c, &i2cTransaction)) {
/* Extract degrees C from the received data */
temperature = rxBuffer[2];
temperature = (temperature << 8);
temperature |= rxBuffer[3];
System_printf("Temperature: %d (C)\n", temperature);
/* Extract humidity RH from the received data */
humidity = rxBuffer[4];
humidity = (humidity << 8);
humidity |= rxBuffer[5];
System_printf("Humidity: %d (RH)\n", humidity);
} else {
System_printf("I2C Bus fault\n");
}
/* Deinitialized I2C */
I2C_close( i2c);
System_printf("I2C closed!\n");
System_flush();
}