void ConfigUSCIB0_I2C(void)
{ //Before doing anything else, put the UCS_B0 port into reset UCB0CTL1 |= UCSWRST; UCB0CTL0 = (UCMST | UCMODE_3 | UCSYNC); //Select ACLK as input clk but still keep the port in reset UCB0CTL1 |= UCSSEL__ACLK; UCB0BR0 = 38; //3.801088MHz / 38 = 100KHz UCB0BR1 = 0; //upper divisor //take the port out of reset UCB0CTL1 &= ~UCSWRST; }
void HUM_TEMP_IO_Read(void)
{
// TURN SENSOR ON
UCB0I2CSA = 0x5F; // Slave Address is 0x5Fh (If include Read/Write bit BEh (write) and BFh (read)
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
UCB0TXBUF = 0x20; // CTRL_REG1 address
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
while (UCB0IFG & UCTXIFG);
while (!(UCB0IFG & UCTXIFG));
UCB0TXBUF = 0x84; // Turn on Sensor PD = 1 BDU = 1
while (UCB0IFG & UCTXIFG);
while (!(UCB0IFG & UCTXIFG));
UCB0CTL1 |= UCTXSTP; // I2C stop condition
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
__no_operation();
__no_operation();
// SET FOR ONE SHOT
UCB0I2CSA = 0x5F; // Slave Address is 0x5Fh (If include Read/Write bit BEh (write) and BFh (read)
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
UCB0TXBUF = 0x21; // CTRL_REG2 address
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
while (UCB0IFG & UCTXIFG);
while (!(UCB0IFG & UCTXIFG));
UCB0TXBUF = 0x01; // One shot mode
while (UCB0IFG & UCTXIFG);
while (!(UCB0IFG & UCTXIFG));
UCB0CTL1 |= UCTXSTP; // I2C stop condition
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
__no_operation();
__no_operation();
while(1)
{
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
UCB0TXBUF = 0x27; // Status Reg addres
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
UCB0CTL1 &= ~UCTR; //Read mode
UCB0CTL1 |= UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
//UCB0IFG &= ~UCRXIFG; // Clear USCI_B0 RX int flag
UCB0CTL1 |= UCTXSTP; // I2C stop condition
while (!(UCB0IFG & UCRXIFG));
Hum_Temp_Status_Reg = UCB0RXBUF;
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
__no_operation();
__no_operation();
if(Hum_Temp_Status_Reg == 0x03)break;
}
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
UCB0TXBUF = 0xB0; // Status Reg addres
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
//UCB0IFG &= ~UCRXIFG; // Clear USCI_B0 RX int flag
for (i = 0; i< 16; i++)
{
if (i == 15)
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
}
while (!(UCB0IFG & UCRXIFG)); //wait for read buffer full
Read_Buffer_Array[i] = UCB0RXBUF; // Load RXBuffer, start getting next one.
}
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
__no_operation();
__no_operation();
H0_rH_x2 = Read_Buffer_Array[0];
H1_rH_x2 =Read_Buffer_Array[1];
T0_degC_x8 = Read_Buffer_Array[2];
T1_degC_x8 = Read_Buffer_Array[3];
T0_msb = Read_Buffer_Array[5] & 0x03;
T1_msb = (Read_Buffer_Array[5] & 0x0C) >> 2;
H0_T0_OUT = (INT16) ((INT16) Read_Buffer_Array[7] << 8 | Read_Buffer_Array[6]);
H1_T0_OUT = (INT16) ((INT16) Read_Buffer_Array[11] << 8 | Read_Buffer_Array[10]);
T0_OUT = (INT16) ((INT16) Read_Buffer_Array[13] << 8 | Read_Buffer_Array[12]);
T1_OUT = (INT16) ((INT16) Read_Buffer_Array[15] << 8 | Read_Buffer_Array[14]);
outT0 = ((INT16) T0_msb << 8) | T0_degC_x8;
outT1 = ((INT16) T1_msb << 8) | T1_degC_x8;
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
UCB0TXBUF = 0xA8; // Status Reg addres
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Wait for start condition and data to send
//UCB0IFG &= ~UCRXIFG; // Clear USCI_B0 RX int flag
for (i = 0; i< 4; i++)
{
if (i == 3)
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
}
while (!(UCB0IFG & UCRXIFG)); //wait for read buffer full
Read_Buffer_Array[i] = UCB0RXBUF; // Load RXBuffer, start getting next one.
}
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
__no_operation();
__no_operation();
humidityCount = (INT16) ((INT16)Read_Buffer_Array[1] << 8 | Read_Buffer_Array[0]);
temperatureCount = (INT16) ((INT16)Read_Buffer_Array[3] << 8 | Read_Buffer_Array[2]);
HTS221_humidity = (((float)humidityCount - (float)H0_T0_OUT)/((float)H1_T0_OUT - (float)H0_T0_OUT))*((float)H1_rH_x2 - (float)H0_rH_x2)/2 + (float) H0_rH_x2/2;
HTS221_temperature = (((float)temperatureCount - (float)T0_OUT)/((float)T1_OUT - (float)T0_OUT))*((float)outT1 -(float)outT0)/8 + (float) outT0/8;
__no_operation();
__no_operation();
}
