在做I2C读取9250模块的时候 数据总是0 设备ID有时能读出来 有时读不出来 我进入到我的I2C写字节函数里面 发现程序卡在了while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));里面 如果改成ARB_LOST就通过了 可以肯定I2C失去总线控制了 这个怎么解决啊 很蛋疼
这是读一个字节 写一个字节 连续读的代码
void I2C_Init(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinConfigure(GPIO_PE4_I2C2SCL);
GPIOPinConfigure(GPIO_PE5_I2C2SDA);
GPIOPinTypeI2C(GPIO_PORTE_BASE,GPIO_PIN_4);
GPIOPinTypeI2C(GPIO_PORTE_BASE,GPIO_PIN_5);
GPIOPinTypeI2CSCL(GPIO_PORTE_BASE, GPIO_PIN_4);
I2CMasterInitExpClk(I2C2_BASE, SysCtlClockGet(), true);
I2CMasterEnable(I2C2_BASE);
}
void MPU_Write_Byte(uint8_t addr,uint8_t reg,uint8_t data)
{
I2CMasterSlaveAddrSet(I2C2_BASE,addr,false);
I2CMasterDataPut(I2C2_BASE,reg);
I2CMasterControl(I2C2_BASE,I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C2_BASE));
while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));
I2CMasterDataPut(I2C2_BASE,data);
I2CMasterControl(I2C2_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C2_BASE));
while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));
I2CMasterControl(I2C2_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
}
uint8_t MPU_Read_Byte(uint8_t addr,uint8_t reg)
{
uint8_t res;
I2CMasterSlaveAddrSet(I2C2_BASE,addr,false);
I2CMasterDataPut(I2C2_BASE,reg);
I2CMasterControl(I2C2_BASE,I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C2_BASE));
while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));
I2CMasterSlaveAddrSet(I2C2_BASE,addr,true);
I2CMasterControl(I2C2_BASE,I2C_MASTER_CMD_SINGLE_RECEIVE);
while(I2CMasterBusy(I2C2_BASE));
while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));
res=I2CMasterDataGet(I2C2_BASE);
return res;
}
void MPU_Read_Len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf)
{
uint32_t DataNumber;
uint32_t MsterCMD;
I2CMasterSlaveAddrSet(I2C2_BASE,addr,false);
I2CMasterDataPut(I2C2_BASE,reg);
I2CMasterControl(I2C2_BASE,I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C2_BASE));
while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));
I2CMasterSlaveAddrSet(I2C2_BASE,addr,true);
for(DataNumber=0;DataNumber<len;DataNumber++)
{
if(DataNumber==0)
{
MsterCMD=I2C_MASTER_CMD_BURST_RECEIVE_START;
}
else if(DataNumber==(len-1))
{
MsterCMD=I2C_MASTER_CMD_FIFO_BURST_RECEIVE_FINISH;
}
else
{
MsterCMD=I2C_MASTER_CMD_BURST_RECEIVE_CONT;
}
I2CMasterControl(I2C2_BASE,MsterCMD);
while(I2CMasterBusy(I2C2_BASE));
while(!(I2CMasterErr(I2C2_BASE)==I2C_MASTER_ERR_NONE));
*buf=I2CMasterDataGet(I2C2_BASE);
buf++;
}
}
此帖出自
小平头技术问答
如果是用的STM32 劝你不要用了,直接换端口模拟
一周热门 更多>