/* * main.c */ int main(void) { float fTemperature, fHumidity; int32_t i32IntegerPart; int32_t i32FractionPart; g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); //confiugre the GPIO pins PinoutSet(false,false); ConfigureUART(); //configure I2C pins SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C8); GPIOPinConfigure(GPIO_PA2_I2C8SCL); GPIOPinConfigure(GPIO_PA3_I2C8SDA); GPIOPinTypeI2C(GPIO_PORTA_BASE,GPIO_PIN_2); GPIOPinTypeI2C(GPIO_PORTA_BASE,GPIO_PIN_3); //enable interrupts IntMasterEnable(); //initialize I2C I2CMInit(&g_sI2CInst,I2C8_BASE,INT_I2C8,0xff,0xff,g_ui32SysClock); IntPrioritySet(INT_I2C7,0xE0); //initialize the sensors SHT21Init(&g_sSHT21Inst, &g_sI2CInst, SHT21_I2C_ADDRESS, SHT21AppCallback,&g_sSHT21Inst); //delay for 20 ms SysCtlDelay(g_ui32SysClock / (50 * 3)); while(1){ // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Write the command to start a humidity measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_RH, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Wait 33 milliseconds before attempting to get the result. Datasheet // claims this can take as long as 29 milliseconds. // SysCtlDelay(g_ui32SysClock / (30 * 3)); // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Get the raw data from the sensor over the I2C bus. // SHT21DataRead(&g_sSHT21Inst, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Get a copy of the most recent raw data in floating point format. // SHT21DataHumidityGetFloat(&g_sSHT21Inst, &fHumidity); // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Write the command to start a temperature measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_T, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Wait 100 milliseconds before attempting to get the result. Datasheet // claims this can take as long as 85 milliseconds. // SysCtlDelay(g_ui32SysClock / (10 * 3)); // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Read the conversion data from the sensor over I2C. // SHT21DataRead(&g_sSHT21Inst, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Get the most recent temperature result as a float in celcius. // SHT21DataTemperatureGetFloat(&g_sSHT21Inst, &fTemperature); // // Convert the floats to an integer part and fraction part for easy // print. Humidity is returned as 0.0 to 1.0 so multiply by 100 to get // percent humidity. // fHumidity *= 100.0f; i32IntegerPart = (int32_t) fHumidity; i32FractionPart = (int32_t) (fHumidity * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print the humidity value using the integers we just created. // UARTprintf("Humidity %3d.%03d\t", i32IntegerPart, i32FractionPart); // // Perform the conversion from float to a printable set of integers. // i32IntegerPart = (int32_t) fTemperature; i32FractionPart = (int32_t) (fTemperature * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print the temperature as integer and fraction parts. // UARTprintf("Temperature %3d.%03d\n", i32IntegerPart, i32FractionPart); // // Delay for one second. This is to keep sensor duty cycle // to about 10% as suggested in the datasheet, section 2.4. // This minimizes self heating effects and keeps reading more accurate. // SysCtlDelay(g_ui32SysClock / 3); } return 0; }
void SHT21AppCallback(void *pvCallbackData, unsigned int ui8Status) { float fTemperature, fHumidity; unsigned char tempString[30]={0}; if(ui8Status == I2CM_STATUS_SUCCESS&&sensorTurn==3) { if(flag==0) { // // Get the most recent temperature result as a float in celcius. // SHT21DataTemperatureGetFloat(&g_sSHT21Inst, &fTemperature); flag=(flag+1)%3; // // Write the command to start a humidity measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_RH, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Perform the conversion from float to a printable set of integers. // SHT21_i32IntegerPart2 = (int32_t) fTemperature; SHT21_i32FractionPart2 = (int32_t) (fTemperature * 1000.0f); SHT21_i32FractionPart2 = SHT21_i32FractionPart2 - (SHT21_i32IntegerPart2 * 1000); if(SHT21_i32FractionPart2 < 0) { SHT21_i32FractionPart2 *= -1; } // // // // Print the temperature as integer and fraction parts. // // // sprintf(tempString,"Temperature %3d.%03d\n\r", SHT21_i32IntegerPart2, SHT21_i32FractionPart2); // CLI_Write(tempString); } else { SHT21DataRead(&g_sSHT21Inst, SHT21AppCallback, &g_sSHT21Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Get a copy of the most recent raw data in floating point format. // SHT21DataHumidityGetFloat(&g_sSHT21Inst, &fHumidity); // // Convert the floats to an integer part and fraction part for easy // print. Humidity is returned as 0.0 to 1.0 so multiply by 100 to get // percent humidity. // fHumidity *= 100.0f; SHT21_i32IntegerPart1 = (int32_t) fHumidity; SHT21_i32FractionPart1 = (int32_t) (fHumidity * 1000.0f); SHT21_i32FractionPart1 = SHT21_i32FractionPart1 - (SHT21_i32IntegerPart1 * 1000); if(SHT21_i32FractionPart1 < 0) { SHT21_i32FractionPart1 *= -1; } // // // // // Print the humidity value using the integers we just created. // // //sprintf(tempString,"Humidity %3d.%03d\n\r", SHT21_i32IntegerPart1, SHT21_i32FractionPart1); //CLI_Write(tempString); sensorTurn=(sensorTurn+1)%NumberOfSensor; TimerEnable(TIMER1_BASE, TIMER_A); flag=(flag+1)%3; // // Write the command to start a temperature measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_T, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); } } }
void printHumidityData(void){ float fTemperature, fHumidity; int32_t i32IntegerPart; int32_t i32FractionPart; // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Write the command to start a humidity measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_RH, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Wait 33 milliseconds before attempting to get the result. Datasheet // claims this can take as long as 29 milliseconds. // ROM_SysCtlDelay(g_ui32SysClock / (30 * 3)); // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Get the raw data from the sensor over the I2C bus. // SHT21DataRead(&g_sSHT21Inst, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Get a copy of the most recent raw data in floating point format. // SHT21DataHumidityGetFloat(&g_sSHT21Inst, &fHumidity); // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Write the command to start a temperature measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_T, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Wait 100 milliseconds before attempting to get the result. Datasheet // claims this can take as long as 85 milliseconds. // ROM_SysCtlDelay(g_ui32SysClock / (10 * 3)); // // Turn on D2 to show we are starting a transaction with the sensor. // This is turned off in the application callback. // LEDWrite(CLP_D1 | CLP_D2 , CLP_D2); // // Read the conversion data from the sensor over I2C. // SHT21DataRead(&g_sSHT21Inst, SHT21AppCallback, &g_sSHT21Inst); // // Wait for the I2C transactions to complete before moving forward. // SHT21AppI2CWait(__FILE__, __LINE__); // // Get the most recent temperature result as a float in celcius. // SHT21DataTemperatureGetFloat(&g_sSHT21Inst, &fTemperature); // // Convert the floats to an integer part and fraction part for easy // print. Humidity is returned as 0.0 to 1.0 so multiply by 100 to get // percent humidity. // fHumidity *= 100.0f; i32IntegerPart = (int32_t) fHumidity; i32FractionPart = (int32_t) (fHumidity * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print the humidity value using the integers we just created. // UARTprintf("Humidity %3d.%03d\t", i32IntegerPart, i32FractionPart); // // Perform the conversion from float to a printable set of integers. // i32IntegerPart = (int32_t) fTemperature; i32FractionPart = (int32_t) (fTemperature * 1000.0f); i32FractionPart = i32FractionPart - (i32IntegerPart * 1000); if(i32FractionPart < 0) { i32FractionPart *= -1; } // // Print the temperature as integer and fraction parts. // UARTprintf("Temperature %3d.%03d\n", i32IntegerPart, i32FractionPart); // // Delay for one second. This is to keep sensor duty cycle // to about 10% as suggested in the datasheet, section 2.4. // This minimizes self heating effects and keeps reading more accurate. // ROM_SysCtlDelay(g_ui32SysClock / 3); }