コード例 #1
0
/*
 * 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;
}
コード例 #2
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);
}
コード例 #3
0
//*****************************************************************************
//
// Main 'C' Language entry point.
//
//*****************************************************************************
int
main(void)
{

	const uint8_t bufLength=10;
	char inputBuf[1];

	//
	// Configure the system frequency.
	//
	g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
			SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
			SYSCTL_CFG_VCO_480), 120000000);

	//
	// Configure the device pins for this board.
	//
	PinoutSet(false, false);

	//
	// Initialize the UART.
	//
	ConfigureUART();

	//
	// Print the welcome message to the terminal.
	//
	UARTprintf("\033[2J\033[H");
	UARTprintf("SHT21 Example\n");

	//
	// The I2C7 peripheral must be enabled before use.
	//
	// Note: For BoosterPack 2 interface use I2C8.
	//
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C8);
	//ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

	//
	// Configure the pin muxing for I2C7 functions on port D0 and D1.
	// This step is not necessary if your part does not support pin muxing.
	//
	// Note: For BoosterPack 2 interface use PA2 and PA3.
	//
	ROM_GPIOPinConfigure(GPIO_PA2_I2C8SCL);
	ROM_GPIOPinConfigure(GPIO_PA3_I2C8SDA);

	//
	// Select the I2C function for these pins.  This function will also
	// configure the GPIO pins pins for I2C operation, setting them to
	// open-drain operation with weak pull-ups.  Consult the data sheet
	// to see which functions are allocated per pin.
	//
	// Note: For BoosterPack 2 interface use PA2 and PA3.
	//
	ROM_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_2);
	ROM_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_3);

	//
	// Keep only some parts of the systems running while in sleep mode.
	// GPIOE is for the ISL29023 interrupt pin.
	// UART0 is the virtual serial port.
	// I2C7 is the I2C interface to the ISL29023.
	//
	// Note: For BoosterPack 2 change this to I2C8.
	//
	ROM_SysCtlPeripheralClockGating(true);
	ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOE);
	ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0);
	ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C8);

	//
	// Enable interrupts to the processor.
	//
	ROM_IntMasterEnable();

	//
	// Initialize I2C7 peripheral.
	//
	// Note: For BoosterPack 2 use I2C8.
	//
	I2CMInit(&g_sI2CInst, I2C8_BASE, INT_I2C8, 0xff, 0xff, g_ui32SysClock);

	//
	// Turn on D2 to show we are starting an I2C transaction with the sensor.
	// This is turned off in the application callback.
	//
	LEDWrite(CLP_D1 | CLP_D2 , CLP_D2);

	//
	// Initialize the SHT21.
	//
	SHT21Init(&g_sSHT21Inst, &g_sI2CInst, SHT21_I2C_ADDRESS,
			SHT21AppCallback, &g_sSHT21Inst);

	//
	// Initialize the TMP006
	//
	TMP006Init(&g_sTMP006Inst, &g_sI2CInst, TMP006_I2C_ADDRESS,
			TMP006AppCallback, &g_sTMP006Inst);

	//
	// Wait for the I2C transactions to complete before moving forward.
	//
	SHT21AppI2CWait(__FILE__, __LINE__);

	//
	// Delay for 20 milliseconds for SHT21 reset to complete itself.
	// Datasheet says reset can take as long 15 milliseconds.
	//
	ROM_SysCtlDelay(g_ui32SysClock / (50 * 3));

	UARTprintf("Menu:\n");
	UARTprintf("h for Humidity \n");
	UARTprintf("t for Temperature \n");


	//
	// Loop Forever.
	//
	while(1)
	{
		if(UARTgets(inputBuf,bufLength)){
			if(inputBuf[0]=='h'){
				UARTprintf("Sensing Humidity Data:\n");
				printHumidityData();

			}
			else if(inputBuf[0]=='t'){
				UARTprintf("Sensing Temperature Data:\n");
				printTemperatureData();

			}

		}

	}
}