This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430 I2C HTS221 Humidity Temperature sensor

Other Parts Discussed in Thread: MSP430F5242
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(); }

  • Cannot seem to get the formating correct above. Always looks different after I post. So, I will put my description down here.
    Maybe TI can provide me a tutorial on how to use this forum:)

    Above is some very very crude code I wrote to test an ST HTS221 Humidity sensor on an MSP430F5242 I2C port.
    I did not use the I2C interrupts. I just wanted to turn the sensor on and read the Humidity and temperature and check that it is working. My
    next step is to clean up the code, but I thought this might help someone else get started who is trying to do the same thing. Make sure you set the port pins on the
    MSP430 to the correct state before setting up the I2C port. I also included a scope image of the I2C output. I could not fit it all on the screen,
    but its enough to see what is happeneing. Took me awhile to figure this out so I hope this helps somone else save a litle time.
    Also, I measure 47.48512 %RH and 25.02273C, which is pretty close to what my calibrated sensor close by was reading.

    Also, I did find the code that does the conversion using the cal numbers as part of a library I found on the web. It helped me figure out how to use the cal numbers and calculate the linear interpolation. It was not much help with the I2C stuff.

    Peace.

  • Thanks for sharing  Gary! This can be useful tot eh community as a whole!

    Regards,
    JH

**Attention** This is a public forum