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.

Best I2C data sampling routine

Hello everyone, 

this should be a fairly easy question for you guys, just need advice on the best way for I2C thermal sensor read with TM4CAE6PM. The system clock is set to 80MHz, but the main loop is spinning at freq of 1kHz, it is more than enough for this system (Timer 0 is used to regulate this). I want to read this I2C value 8 samples per second that means, if I am correct 8Hz cycle to read this data, I used Timer 2 for this on Timer2_ISR I read thermal sensor value and convert it to degrees. Below is the I2C configuration as well as this interrupt of Timer2 which is used to read the data. Is there some other way to do this that would be more acceptable or less processor consuming? 

void I2C_Configuration()
{
	SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
	GPIOPinConfigure(GPIO_PB2_I2C0SCL);
	GPIOPinConfigure(GPIO_PB3_I2C0SDA);
	GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
	GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

	I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);

	I2CMasterSlaveAddrSet(I2C0_BASE, TC74_ADDRESS, false);

	I2CMasterDataPut(I2C0_BASE, READ_TEMP_CMD);
	I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
	while(I2CMasterBusy(I2C0_BASE))
	{
	}
}

void Timer2_ISR(void)
{
	TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
	I2CMasterSlaveAddrSet(I2C0_BASE, TC74_ADDRESS, true);
	I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
	ui32_I2C_DataRx = I2CMasterDataGet(I2C0_BASE);

	ConvertToValidTempValue();
	flag_thermal_sens_read=true;
}

  • Djedjica said:
    this should be a fairly easy question for you guys

    Beware fellow forum visitors when: Boss, Wife, Vendor (or posters) describe task as, "EASY!"   Rarely does that prove true!

    I fly now - but shall return w/comments (should) Amit, Robert, f.m. not arrive - and fully satisfy...  (they always do!)

    Other than the "snuck in" (fairly easy) yours is a thoughtful question - and far exceeds the norm, "Does not Work!"

    As a "quickie stab" have you another, regular task - occurring at about this same (or near) rate?   Bundling the two together may absorb less MCU resource.   (note the "hedge" (may))

  • Hello Djedjica

    Performing the slow I2C transaction in an interrupt handler is not a good idea. Rather I would move it to the main function where a flag from the interrupt handler causes it to start the i2c transaction.

    Regards
    Amit