/* Initialize board hardware. */
BOARD_InitBootClocks();
gpioSetup();
i2cSetup();
timerSetup();
/*Get initial temp/rh reading*/
rhInitData(&humidity, &temperature);
/*Start Periodic Interrupt Timer*/
PIT_StartTimer(PIT, kPIT_Chnl_0);
while(1) {
if (rhPitIsrFlag){ //rhPitIsrFlag is set in timer ISR for every 200 ms
/*reset RH timer flag*/
rhPitIsrFlag = false;
/*checks which iteration of RH module command cycle*/
//at every loop, either send measure command or process data, the frequency is low enough
//for the data to be ready at the next loop
switch (rhPitIsrCount){ //rhPitIsrCount is initialized as 0
/*1: Write measure command to RH/Temp sensor*/
case 0U:
measureCommand();
break;
/*2: Read Value from RH/Temp sensor measurement*/
case 1U:
receiveMeasurement(&humidity, &temperature);
break;
}//END SWITCH CASE
/*Switch Value of case iteration from 0 to 1 or from 1 to 0*/
rhPitIsrCount = !rhPitIsrCount;
}
****************************************************************************
void measureCommand(){
/*Write a measure command to the SHT35 RH/Temp Module over i2c*/
i2cWrite(RH_I2C_CH, rhWrite);
}
****************************************************************************
*****************************************************************************
void receiveMeasurement(float *humidity, float *temperature){
/*Read data packet received from SHT35*/
i2cRead(RH_I2C_CH, rhDataReceive);
/*obtain RH data from first half of data packet*/
rhMes = (((uint16_t)(rhDataReceive[0] & 0x3F)) << 8) + (uint16_t)rhDataReceive[1];
/*calculate RH% value and store in in "humidity"*/
*humidity = 100*((float)rhMes)/16382.0;
/*obtain Temperature data from second half of the data packet*/
tempMes = (((uint16_t)rhDataReceive[2]) << 6) + (((uint16_t)rhDataReceive[3])>>2);
*temperature = -40.0 + (165.0*((float)tempMes))/16382.0;
}
*******************************************************************************
*******************************************************************************
void i2cWrite(uint8_t ch, uint8_t *dat){
i2c_master_transfer_t masterXfer;
memset(&masterXfer, 0, sizeof(masterXfer));
masterXfer.direction = kI2C_Write;
masterXfer.subaddress = 0;
masterXfer.subaddressSize = 0;
masterXfer.data = dat;
masterXfer.slaveAddress = RH_ADDRESS;
masterXfer.dataSize = RH_TRANSFER_LENGTH;
masterXfer.flags = RH_STOP_FLAG_MASK;
I2C_MasterTransferBlocking(RH_I2C, &masterXfer);
}
********************************************************************************
********************************************************************************
void i2cRead(uint8_t ch, uint8_t *dat){
i2c_master_transfer_t masterXfer;
memset(&masterXfer, 0, sizeof(masterXfer));
masterXfer.direction = kI2C_Read;
masterXfer.subaddress = 0;
masterXfer.subaddressSize = 0;
masterXfer.data = dat;
masterXfer.flags = kI2C_TransferDefaultFlag;
masterXfer.slaveAddress = RH_ADDRESS;
masterXfer.dataSize = RH_RECEIVE_LENGTH;
I2C_MasterTransferBlocking(RH_I2C, &masterXfer);
}
}
************************************************************************************