我把StellarisWare\examples\peripherals\i2c中的第一个例子移植到\StellarisWare\boards\ek-lm3s9b92\hello工程中,用I2C来访问另外一颗芯片的ID寄存器(slave地址0X68,ID寄存器地址0X02,此寄存器的值是芯片ID,是固定的),以下是main函数
int
main(void)
{
//
// Set the clocking to run directly from the crystal.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Initialize the UART.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioInit(0);
//
// Hello!
//
UARTprintf("\033[2JHello World!\n");
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);// The I2C0 peripheral must be enabled before use.
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);// GPIO port B needs to be enabled so these pins can be used.
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// Configure the pin muxing for I2C0 functions on port B2 and B3.
// This step is not necessary if your part does not support pin muxing.
ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);
// 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.
ROM_I2CMasterEnable(I2C0_MASTER_BASE);
ROM_I2CMasterInitExpClk(I2C0_MASTER_BASE, ROM_SysCtlClockGet(), false);//the data rate is set to 100kbps
// 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.
ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x68, false);//I2C Master is initiating a writes to the slave
// 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.
ROM_I2CMasterDataPut(I2C0_MASTER_BASE, 0x02); // Place the data to be sent in the data register
ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); // Initiate send of data from the master.
while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_RREQ))// Wait until the slave has received and acknowledged the data.
{
}
ROM_I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x68, true);//I2C Master is initiating reads from the slave.
// Modifiy the data direction to true, so that seeing the address will
// indicate that the I2C Master is initiating a read from the slave.
ROM_I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ))
{
}
// Dummy acknowledge and wait for the receive request from the master.
// This is done to clear any flags that should not be set.
UARTprintf("Receive:%d\n",ROM_I2CMasterDataGet(I2C0_MASTER_BASE));
//
// Finished.
//
while(1)
{
}
}
编译烧录后只有串口打印“[2JHello World!”
如果注释掉 while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_RREQ))// Wait until the slave has received and acknowledged the data.
{
}和
while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ))
{
}
串口打印
[2JHello World!
Receive:0
麻烦各位高手帮我分析,谢谢!