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.

TIVA TM4C129 的I2C读取问题

IIC的初始化如下

Myi2cMaster_Init( I2C0_BASE, GPIO_PORTB_BASE, 100000 );

void Myi2cMaster_Init(uint32_t ui32Base, uint32_t PortBase, 
						uint32_t ui32SCLFreq)
{
    ASSERT(_I2CBaseValid(ui32Base));
	uint32_t SCLGPIO_Pin, SDAGPIO_Pin, SCLPinConfig, SDAPinConfig;
	switch( ui32Base )
	{
		case I2C0_BASE:		
			SCLPinConfig		= GPIO_PB2_I2C0SCL;
			SDAPinConfig		= GPIO_PB3_I2C0SDA;
			SCLGPIO_Pin			= GPIO_PIN_2;
			SDAGPIO_Pin			= GPIO_PIN_3;
			SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
			SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
			break;
		case I2C1_BASE:		
			SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
			if( PortBase == GPIO_PORTG_BASE )
			{
				SCLPinConfig		= GPIO_PG0_I2C1SCL;
				SDAPinConfig		= GPIO_PG1_I2C1SDA;
				SCLGPIO_Pin			= GPIO_PIN_0;
				SDAGPIO_Pin			= GPIO_PIN_1;
				SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
			}
			else
			{
				SCLPinConfig		= GPIO_PR0_I2C1SCL;
				SDAPinConfig		= GPIO_PR1_I2C1SDA;
				SCLGPIO_Pin			= GPIO_PIN_0;
				SDAGPIO_Pin			= GPIO_PIN_1;
				SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR);
			}
				break;
		case I2C2_BASE:
			SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
			SCLPinConfig		= GPIO_PG2_I2C2SCL;
			SDAPinConfig		= GPIO_PG3_I2C2SDA;
			SCLGPIO_Pin			= GPIO_PIN_2;
			SDAGPIO_Pin			= GPIO_PIN_3;
			SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
			break;
		case I2C3_BASE:		
			SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
			if( PortBase == GPIO_PORTG_BASE )
			{
				SCLPinConfig		= GPIO_PG4_I2C3SCL;
				SDAPinConfig		= GPIO_PG5_I2C3SDA;
				SCLGPIO_Pin			= GPIO_PIN_4;
				SDAGPIO_Pin			= GPIO_PIN_5;
				SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
			}
			else
			{
				SCLPinConfig		= GPIO_PR4_I2C3SCL;
				SDAPinConfig		= GPIO_PR5_I2C3SDA;
				SCLGPIO_Pin			= GPIO_PIN_4;
				SDAGPIO_Pin			= GPIO_PIN_5;
				SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR);
			}
			break;
		case I2C4_BASE:
			SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C4);
			SCLPinConfig		= GPIO_PG6_I2C4SCL;
			SDAPinConfig		= GPIO_PG7_I2C4SDA;
			SCLGPIO_Pin			= GPIO_PIN_6;
			SDAGPIO_Pin			= GPIO_PIN_7;
			SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
			break;
		case I2C5_BASE:
			SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C5);
			SCLPinConfig		= GPIO_PB0_I2C5SCL;
			SDAPinConfig		= GPIO_PB1_I2C5SDA;
			SCLGPIO_Pin			= GPIO_PIN_0;
			SDAGPIO_Pin			= GPIO_PIN_1;
			SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
			break;
	}
	
	GPIOPinConfigure( SCLPinConfig );
	GPIOPinConfigure( SDAPinConfig );
	GPIOPinTypeI2CSCL( PortBase, SCLGPIO_Pin );
	GPIOPinTypeI2C( PortBase, SDAGPIO_Pin );
	
	I2CMasterPrivateInit( ui32Base, ui32SysClock, ui32SCLFreq);
	I2CMasterEnable( ui32Base );
}

读取程序如下:

I2CMasterSlaveAddrSet( I2C0_BASE, 0x50, 0);	
		I2CMasterDataPut( I2C0_BASE, 0x01 );														
while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
		I2CMasterControl( I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START );				
		I2CMasterDataPut( I2C0_BASE, 0x06  );//write data									SysCtlDelay( 5 );
		while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );
		I2CMasterControl( I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH );					//主机数据+stop传输
		while( I2CMasterErr(I2C0_BASE) | I2CMasterBusy(I2C0_BASE) );


程序移植停在了些寄存器地址后的While语句中了。为什么会这样呢?

  • 你初始化的是I2C6,读取的是I2C0,不出问题才怪了。

  • 初始化是另写的程序

  • 原来这样,我说么,这么低级的错误。

  • IntEnable(INT_I2C0);

    //
    // Configure and turn on the I2C0 slave interrupt. The I2CSlaveIntEnableEx()
    // gives you the ability to only enable specific interrupts. For this case
    // we are only interrupting when the slave device receives data.
    //
    I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA);

    //
    // Enable and initialize the I2C0 master module. Use the system clock for
    // the I2C0 module. The last parameter sets the I2C data transfer rate.
    // If false the data rate is set to 100kbps and if true the data rate will
    // be set to 400kbps. For this example we will use a data rate of 100kbps.
    //
    I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);

    //
    // Enable the I2C0 slave module.
    //
    I2CSlaveEnable(I2C0_BASE);

    //
    // Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an
    // arbitrary 7-bit number (set in a macro above) that is sent to the
    // I2CMasterSlaveAddrSet function.
    //
    I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);

    //
    // Tell the master module what address it will place on the bus when
    // communicating with the slave. Set the address to SLAVE_ADDRESS
    // (as set in the slave module). The receive parameter is set to false
    // which indicates the I2C Master is initiating a writes to the slave. If
    // true, that would indicate that the I2C Master is initiating reads from
    // the slave.
    //
    I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);

    //
    // Set up the serial console to use for displaying messages. This is just
    // for this example program and is not needed for proper I2C operation.
    //
    InitConsole();

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

    官方给的例程的初始化顺序如下,TIVA对时序有些严格,或许存在你有的参数没写进去的情况。要么按照官方的顺序来,参考官方的例程,要么看看哪个寄存器没配置好吧。