TI中文支持网
TI专业的中文技术问题搜集分享网站

TM4C1294NCPDT I2C读写AT24C256的问题

typedef enum I2CChannel
{TM4C_I2C0 = 0,TM4C_I2C1,TM4C_I2C2,TM4C_I2C3,TM4C_I2C4,TM4C_I2C5,TM4C_I2C6,TM4C_I2C7,TM4C_I2C8,TM4C_I2C9,TM4C_I2C_NUM,
}tI2CChannel;

typedef struct
{uint8_t ulSCLPins;uint8_t ulSDAPins;uint32_t ulPinsBase;uint32_t ulPinPerID;uint32_t ulI2CPerID;uint32_t ulSCLPinType;uint32_t ulSDAPinType; 
}tI2CDev;

uint32_t ul32I2CBaseAddress[TM4C_I2C_NUM] = 
{I2C0_BASE,I2C1_BASE,I2C2_BASE,I2C3_BASE,I2C4_BASE,I2C5_BASE,I2C6_BASE,I2C7_BASE,I2C8_BASE,I2C9_BASE,
};

tI2CDev g_tI2CCfg = 
{//I2C0GPIO_PIN_2,//SCLGPIO_PIN_3,//SDAGPIO_PORTB_BASE,SYSCTL_PERIPH_GPIOB,SYSCTL_PERIPH_I2C0,GPIO_PB2_I2C0SCL,GPIO_PB3_I2C0SDA
},

uint8_t g_ucDevAddr = 0xA0

//I2C初始化
void BSP_I2CMasterInit(tI2CChannel Channel, tI2CDev tI2CDev, bool bFast, uint32_t ulSysClk)
{SysCtlPeripheralEnable(tI2CDev.ulPinPerID);while(!SysCtlPeripheralReady(tI2CDev.ulPinPerID));SysCtlPeripheralDisable(tI2CDev.ulI2CPerID);SysCtlPeripheralReset(tI2CDev.ulI2CPerID);SysCtlPeripheralEnable(tI2CDev.ulI2CPerID);while(!SysCtlPeripheralReady(tI2CDev.ulI2CPerID));GPIOPinConfigure(tI2CDev.ulSCLPinType);GPIOPinConfigure(tI2CDev.ulSDAPinType);//GPIOPinTypeI2C(tI2CDev.ulPinsBase,tI2CDev.ulSDAPins|tI2CDev.ulSCLPins);GPIOPinTypeI2C(tI2CDev.ulPinsBase,tI2CDev.ulSDAPins);GPIOPinTypeI2CSCL(tI2CDev.ulPinsBase,tI2CDev.ulSCLPins);I2CMasterInitExpClk(ul32I2CBaseAddress[Channel],ulSysClk,bFast);//I2CMasterIntDisable(ul32I2CBaseAddress[Channel]);
}
//  非阻塞忙判断
static bool BSP_I2CMasterBusyNoBlocking(tI2CChannel Channel)
{uint32_t usRepeat = 0u;//等待完成while(I2CMasterBusy(ul32I2CBaseAddress[Channel]) == (UINT8)true){usRepeat++;if ((usRepeat) > 8000000u){return false;}}return true;
}

//  错误检测
bool BSP_I2CErrorCheck(tI2CChannel Channel)
{uint32_tstatus;// 等待完成if (BSP_I2CMasterBusyNoBlocking(Channel) == true){// 读取错误status = I2CMasterErr(ul32I2CBaseAddress[Channel]);if (!status){return true;}}return false;
}

boolBSP_EepromWrite(tI2CChannel Channel, uint8_t ucSlaveAddr, uint16_t usChipAddr,uint8_t *pData, uint8_t ucLen)
{uint8_t ucIndex;uint8_t Epstatus = EEPROM_OP_IDLE;uint32_t ulI2CCtrl;ulI2CCtrl = I2C_MASTER_CMD_BURST_SEND_START;while(Epstatus != EEPROM_OP_STOP){switch(Epstatus){caseEEPROM_OP_IDLE:{//空闲状态,//向从机写数据I2CMasterSlaveAddrSet(I2C0_BASE, ucSlaveAddr, false);//发送存储地址高8位I2CMasterDataPut(I2C0_BASE, (usChipAddr >> 8));I2CMasterControl(I2C0_BASE, ulI2CCtrl);// 起始位if(!BSP_I2CErrorCheck(Channel)){return false;}Epstatus = EEPROM_OP_TXADDR;}break;caseEEPROM_OP_TXADDR:{ulI2CCtrl = I2C_MASTER_CMD_BURST_SEND_CONT;I2CMasterDataPut(I2C0_BASE, (usChipAddr & 0xff));//发送存储地址低8位I2CMasterControl(I2C0_BASE, ulI2CCtrl);if(!BSP_I2CErrorCheck(Channel)){return false;}Epstatus = EEPROM_OP_TXDATA;}break;case EEPROM_OP_TXDATA:{ucIndex = 0;//发送数据while(ucIndex < ucLen){if(ucIndex == (ucLen - 1)){ulI2CCtrl = I2C_MASTER_CMD_BURST_SEND_FINISH;}else{ulI2CCtrl = I2C_MASTER_CMD_BURST_SEND_CONT;}I2CMasterDataPut(I2C0_BASE, pData[ucIndex++]);I2CMasterControl(I2C0_BASE, ulI2CCtrl);if(!BSP_I2CErrorCheck(Channel)){return false;}}Epstatus = EEPROM_OP_STOP;}break;default:return false;}}return true;
}

boolBSP_EepromRead(tI2CChannel Channel, uint8_t ucSlaveAddr, uint16_t usChipAddr,uint8_t *pData, uint8_t ucLen)
{uint8_t ucIndex;uint8_t Epstatus = EEPROM_OP_IDLE;while(Epstatus != EEPROM_OP_STOP){switch(Epstatus){caseEEPROM_OP_IDLE:{I2CMasterSlaveAddrSet(I2C0_BASE, ucSlaveAddr, false);I2CMasterDataPut(I2C0_BASE, (usChipAddr >> 8));I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);if(!BSP_I2CErrorCheck(Channel)){return false;}Epstatus = EEPROM_OP_TXADDR;}break;caseEEPROM_OP_TXADDR:{I2CMasterDataPut(I2C0_BASE, (usChipAddr & 0xff));I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);if(!BSP_I2CErrorCheck(Channel)){return false;}Epstatus = EEPROM_OP_RXDATA;}break;case EEPROM_OP_RXDATA:{ucIndex = 0;// 从机读出数据I2CMasterSlaveAddrSet(I2C0_BASE, ucSlaveAddr, true);if(ucLen == 1){I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);if(!BSP_I2CErrorCheck(Channel)){return false;}pData[ucIndex++] = I2CMasterDataGet(I2C0_BASE);}else{I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);//读起始位if(!BSP_I2CErrorCheck(Channel)){return false;}pData[ucIndex++] = I2CMasterDataGet(I2C0_BASE);while(ucIndex < ucLen){if(ucIndex == (ucLen - 1)){I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);}else{I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);}if(!BSP_I2CErrorCheck(Channel)){return false;}pData[ucIndex++] = I2CMasterDataGet(I2C0_BASE);}Epstatus = EEPROM_OP_STOP;}}break;default:return false;}}return true;
}

void AT24C256Test(void)
{bool status;BSP_I2CMasterInit(g_tI2CChannel,g_tI2CCfg,false,ulSysClk);BSP_I2CMasterEnable(g_tI2CChannel,true);while(1){status = BSP_EepromWrite(g_tI2CChannel,g_ucDevAddr,0x0000,g_EpromWriteData,10);if(status){status = BSP_EepromRead(g_tI2CChannel,g_ucDevAddr,0x0000,g_EpromReadData,10);}SimpleDelay(2);}
}

现象描述:不能读/写EEPROM数据

波形上每次读写只能抓到一个字节,每帧第一个字节应该是从机地址及读写标志,但是读波形又不是从机地址0xA0,实在不清楚原因,特在此求助各位大神!

xyz549040622:

回复 user5166354:

I2C从机地址应该都是7bit的,有一位是读写位的。

赞(0)
未经允许不得转载:TI中文支持网 » TM4C1294NCPDT I2C读写AT24C256的问题
分享到: 更多 (0)