我使用的单片机型号为LM4F231H5QRIG。
目前问题如下:在程序运行中 加载 IntEnable(INT_I2C0)/ I2CMasterIntEnable(I2C0_MASTER_BASE) 中断初始化后,程序发送一个字节后到while(true== I2CMasterBusy(I2C0_MASTER_BASE));就无法发送数据了。 如果去掉上面的中断初始化,程序可以正常发送数据,但是无法接受数据。
代码如下,请帮忙确认是否有问题。
#include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_sysctl.h" #include "inc/hw_gpio.h" #include "inc/hw_uart.h" #include "inc/hw_types.h" #include "inc/hw_ssi.h" #include "driverlib/debug.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/sysctl.h" #include "driverlib/systick.h" #include "driverlib/uart.h" #include "driverlib/pin_map.h" #include "driverlib/ssi.h" #include "driverlib/i2c.h" #include "utils/uartstdio.h" #include "driverlib/ssiflash.h" unsigned char I2C[255];/*I2C data*/ int main(void) { /** system clock set **/ SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); /*设置引脚功能为I2C*/ GPIOPinConfigure(GPIO_PB3_I2C0SDA); GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_DIR_MODE_HW); GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD); // Set the pad(s) for push-pull operation. GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_DIR_MODE_HW); // Set the pad(s) for open-drain operation with a weak pull-up. GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD); //GPIOPinTypeI2C(GPIO_PORTB_BASE,GPIO_PIN_2 | GPIO_PIN_3); /*I2C初始化:400K快速模式,工作在主模式,开启I2C中断*/ I2CMasterInitExpClk(I2C_MASTER_BASE, SysCtlClockGet()/4, 0); IntEnable(INT_I2C0); I2CMasterIntEnable(I2C0_MASTER_BASE); I2CMasterEnable(I2C0_MASTER_BASE); while(1) { I2C[0]=0x00;//0x08 I2C[1]=0x01;//0X01 I2C[2]=0x1d; I2C[3]=0x00; I2C[4]=0x02; I2C[5]=0x03; I2CMasterSlaveAddrSet(I2C0_MASTER_BASE,0x68,false); //Indicates whether or not the I2C bus is busy, transimitted byte is finished I2CMasterDataPut(I2C0_MASTER_BASE,I2C[0]); while(true ==I2CMasterBusBusy(I2C0_MASTER_BASE)); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START); while(true== I2CMasterBusy(I2C0_MASTER_BASE));// fail I2CMasterDataPut(I2C0_MASTER_BASE,I2C[1]); //Indicates whether or not the I2C bus is busy, transimitted byte is finished I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT); while(true ==I2CMasterBusy(I2C0_MASTER_BASE)); I2CMasterDataPut(I2C0_MASTER_BASE,I2C[2]); //Indicates whether or not the I2C bus is busy, transimitted byte is finished I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT); while(true ==I2CMasterBusy(I2C0_MASTER_BASE)); I2CMasterDataPut(I2C0_MASTER_BASE,I2C[3]); //Indicates whether or not the I2C bus is busy, transimitted byte is finished I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT); while(true ==I2CMasterBusy(I2C0_MASTER_BASE)); I2CMasterDataPut(I2C0_MASTER_BASE,I2C[4]); //Indicates whether or not the I2C bus is busy, transimitted byte is finished I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_CONT); while(true ==I2CMasterBusy(I2C0_MASTER_BASE)); I2CMasterDataPut(I2C0_MASTER_BASE,I2C[5]); //Indicates whether or not the I2C bus is busy, transimitted byte is finished I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH); while(true ==I2CMasterBusy(I2C0_MASTER_BASE)); SysCtlDelay(SysCtlClockGet()); } while(1) { I2CMasterSlaveAddrSet(I2C0_MASTER_BASE,0x68,true); //true--the I2C Master is initiating a RECEIVE from the slave //Indicates whether or not the I2C bus is busy, transimitted byte is finished while(true ==I2CMasterBusBusy(I2C0_MASTER_BASE)); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START); while(true ==I2CMasterBusy(I2C0_MASTER_BASE)); I2CtoUart[0] =(unsigned char)I2CMasterDataGet(I2C0_MASTER_BASE); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT); I2CtoUart[1] =(unsigned char)I2CMasterDataGet(I2C0_MASTER_BASE); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT); I2CtoUart[2] =(unsigned char)I2CMasterDataGet(I2C0_MASTER_BASE); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT); I2CtoUart[3] =(unsigned char)I2CMasterDataGet(I2C0_MASTER_BASE); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT); I2CtoUart[4] =(unsigned char)I2CMasterDataGet(I2C0_MASTER_BASE); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT); I2CtoUart[5] =(unsigned char)I2CMasterDataGet(I2C0_MASTER_BASE); I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } }
另外,此款芯片是否支持查询读数据,手册中未提到。
谢谢!