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); } } }
/* * 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 initI2C(void) { uint8_t ui8Mask; SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C7); GPIOPinConfigure(GPIO_PD0_I2C7SCL); GPIOPinConfigure(GPIO_PD1_I2C7SDA); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // ISL29023 // GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_5); GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_5); GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_5, GPIO_FALLING_EDGE); IntEnable(INT_GPIOE); I2CMInit(&g_sI2CInst, I2C7_BASE, INT_I2C7, 0xff, 0xff, g_SysClock); // // Initialize the SHT21. // SHT21Init(&g_sSHT21Inst, &g_sI2CInst, SHT21_I2C_ADDRESS, SHT21AppCallback, &g_sSHT21Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the TMP006 // TMP006Init(&g_sTMP006Inst, &g_sI2CInst, TMP006_I2C_ADDRESS, TMP006AppCallback, &g_sTMP006Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the BMP180. // BMP180Init(&g_sBMP180Inst, &g_sI2CInst, BMP180_I2C_ADDRESS, BMP180AppCallback, &g_sBMP180Inst); SysCtlDelay(g_SysClock / (100 * 3)); IntPrioritySet(INT_I2C7, 0x00); IntPrioritySet(INT_GPIOM, 0x00); IntPrioritySet(INT_TIMER0A, 0x80); IntPrioritySet(INT_TIMER1A, 0x40); IntPrioritySet(INT_GPIOE, 0x80); IntPrioritySet(INT_UART0, 0x80); // // Initialize the ISL29023 Driver. // ISL29023Init(&g_sISL29023Inst, &g_sI2CInst, ISL29023_I2C_ADDRESS, ISL29023AppCallback, &g_sISL29023Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Configure the ISL29023 to measure ambient light continuously. Set a 8 // sample persistence before the INT pin is asserted. Clears the INT flag. // Persistence setting of 8 is sufficient to ignore camera flashes. // ui8Mask = (ISL29023_CMD_I_OP_MODE_M | ISL29023_CMD_I_INT_PERSIST_M | ISL29023_CMD_I_INT_FLAG_M); ISL29023ReadModifyWrite(&g_sISL29023Inst, ISL29023_O_CMD_I, ~ui8Mask, (ISL29023_CMD_I_OP_MODE_ALS_CONT | ISL29023_CMD_I_INT_PERSIST_8), ISL29023AppCallback, &g_sISL29023Inst); // // Configure the upper threshold to 80% of maximum value // g_sISL29023Inst.pui8Data[1] = 0xCC; g_sISL29023Inst.pui8Data[2] = 0xCC; ISL29023Write(&g_sISL29023Inst, ISL29023_O_INT_HT_LSB, g_sISL29023Inst.pui8Data, 2, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // SysCtlDelay(g_SysClock / (100 * 3)); // // Configure the lower threshold to 20% of maximum value // g_sISL29023Inst.pui8Data[1] = 0x33; g_sISL29023Inst.pui8Data[2] = 0x33; ISL29023Write(&g_sISL29023Inst, ISL29023_O_INT_LT_LSB, g_sISL29023Inst.pui8Data, 2, ISL29023AppCallback, &g_sISL29023Inst); // // Wait for transaction to complete // SysCtlDelay(g_SysClock / (100 * 3)); // // Write the command to start a humidity measurement. // SHT21Write(&g_sSHT21Inst, SHT21_CMD_MEAS_RH, g_sSHT21Inst.pui8Data, 0, SHT21AppCallback, &g_sSHT21Inst); // // Wait for transaction to complete // SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the MPU9150 Driver. // MPU9150Init(&g_sMPU9150Inst, &g_sI2CInst, MPU9150_I2C_ADDRESS, MPU9150AppCallback, &g_sMPU9150Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Write application specifice sensor configuration such as filter settings // and sensor range settings. // g_sMPU9150Inst.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; g_sMPU9150Inst.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; g_sMPU9150Inst.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_2G); MPU9150Write(&g_sMPU9150Inst, MPU9150_O_CONFIG, g_sMPU9150Inst.pui8Data, 3, MPU9150AppCallback, &g_sMPU9150Inst); SysCtlDelay(g_SysClock / (100 * 3)); // // Initialize the DCM system. 50 hz sample rate. // accel weight = .2, gyro weight = .8, mag weight = .2 // CompDCMInit(&g_sCompDCMInst, 1.0f / 50.0f, 0.2f, 0.6f, 0.2f); SysCtlDelay(g_SysClock / (100 * 3)); ui32CompDCMStarted = 0; // // Print the basic outline of our data table. Done once and then kept as we // print only the data. // // CLI_Write("\033[2J\033[H"); // CLI_Write("MPU9150 9-Axis Simple Data Application Example\n\r\n\r"); // CLI_Write("\033[20GX\033[31G|\033[43GY\033[54G|\033[66GZ\n\r\n\r"); // CLI_Write("Accel\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("Gyro\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("Mag\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("\n\033[20GRoll\033[31G|\033[43GPitch\033[54G|\033[66GYaw\n\r\n\r"); // CLI_Write("Eulers\033[8G|\033[31G|\033[54G|\n\r\n\r"); // CLI_Write("\n\033[17GQ1\033[26G|\033[35GQ2\033[44G|\033[53GQ3\033[62G|" // "\033[71GQ4\n\r\n\r"); // CLI_Write("Q\033[8G|\033[26G|\033[44G|\033[62G|\n\r\n\r"); TimerEnable(TIMER1_BASE, TIMER_A); }
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); }