M4与外接器件进行I2C通信,硬件IIC。程序卡死。

2019-07-21 02:47发布

[mw_shl_code=c,true]各位,本来实现通信了,但是波形有错,改来改去,现在在产生起始条件的地方就卡死了,怎么改都无效,郁闷死啊。[/mw_shl_code] [mw_shl_code=c,true]M4C129 与外接DAC5571.大家畅所欲言啊,多多提醒多多问我啊。说不定就帮我解决了!!!谢谢啊。[/mw_shl_code] [mw_shl_code=c,true]// 100110x,通过程序时x=0,故1001 100 +R/W .因库函数回将写入地址做移动一位和读写位凑成8位,所以定义地址为0100 1100,就是0x4c. #define SLAVE_ADDRESS 0x4c #define WRITE_ADDRESS 0x01 //***************************************************************************** //***************************************************************************** #define STATE_IDLE 0 #define STATE_WRITE_NEXT 1 #define STATE_WRITE_FINAL 2 #define STATE_WAIT_ACK 3 #define STATE_SEND_ACK 4 #define STATE_READ_ONE 5 #define STATE_READ_FIRST 6 #define STATE_READ_NEXT 7 #define STATE_READ_FINAL 8 #define STATE_READ_WAIT 9 static unsigned char *g_pucData = 0; static unsigned long g_ulCount = 0; static volatile unsigned long g_ulState = STATE_IDLE; //***************************************************************************** void I2CIntHander(void) { // I2CMasterIntClear(I2C1_BASE); switch(g_ulState) { case STATE_IDLE: //空闲状态 { break; } case STATE_WRITE_NEXT: //发送下一个,主机继续在传输状态 { I2CMasterDataPut(I2C1_BASE, *g_pucData++); g_ulCount--; I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); if(g_ulCount == 1) { g_ulState = STATE_WRITE_FINAL; } break; } case STATE_WRITE_FINAL: //最后一字节 { I2CMasterDataPut(I2C1_BASE, *g_pucData++); g_ulCount--; I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); g_ulState = STATE_IDLE; //不需要读取从机数据。所以发完直接IDLE //g_ulState = STATE_SEND_ACK;//原来步骤 break; } // // Wait for an ACK on the read after a write. // case STATE_WAIT_ACK: { // // See if there was an error on the previously issued read. // if(I2CMasterErr(I2C1_BASE) == I2C_MASTER_ERR_NONE) { I2CMasterDataGet(I2C1_BASE); g_ulState = STATE_IDLE; break; } } case STATE_SEND_ACK: { // Put the I2C master into receive mode. I2CMasterSlaveAddrSet(I2C1_BASE, SLAVE_ADDRESS, true); I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); g_ulState = STATE_WAIT_ACK; break; } case STATE_READ_ONE: { // Put the SoftI2C module into receive mode. I2CMasterSlaveAddrSet(I2C1_BASE, SLAVE_ADDRESS, true); I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); g_ulState = STATE_READ_WAIT; break; } case STATE_READ_FIRST: { I2CMasterSlaveAddrSet(I2C1_BASE, SLAVE_ADDRESS, true); I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); g_ulState = STATE_READ_NEXT; break; } case STATE_READ_NEXT: { *g_pucData++ = I2CMasterDataGet(I2C1_BASE); g_ulCount--; I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); if(g_ulCount == 2) { g_ulState = STATE_READ_FINAL; } break; } case STATE_READ_FINAL: { *g_pucData++ = I2CMasterDataGet(I2C1_BASE); g_ulCount--; I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); g_ulState = STATE_READ_WAIT; break; } case STATE_READ_WAIT: { *g_pucData++ = I2CMasterDataGet(I2C1_BASE); g_ulCount--; g_ulState = STATE_IDLE; break; } } } void I2CPinConfg() { //所用I2C器件的A0为设备地址选择端。我把它拉低。则完整地址为1001 1000 GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_5 );//A0 GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5,0); //这样可以吧? SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); //配置管脚 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG); GPIOPinConfigure(GPIO_PG0_I2C1SCL); GPIOPinConfigure(GPIO_PG1_I2C1SDA); GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_1); GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_0); } //***************************************************************************** //***************************************************************************** void WriteToDAC(unsigned char *pucData, unsigned long ulOffset, unsigned long ulCount) { g_pucData = pucData; g_ulCount = ulCount; if(ulCount != 1) { g_ulState = STATE_WRITE_NEXT; } else { g_ulState = STATE_WRITE_FINAL; } //设置从机地址 I2CMasterSlaveAddrSet(I2C1_BASE, SLAVE_ADDRESS , false); //DAC器件是无子地址的吧? I2CMasterDataPut(I2C1_BASE, SLAVE_ADDRESS); //产生开始条件,采用突发方式 I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START); while(g_ulState != STATE_IDLE) //程序卡死在这里,用示波器观察SCL一直为低了。 { I2CIntHander(); } } //***************************************************************************** int main(void) { unsigned char pucData[16]; unsigned long ulIdx; I2CPinConfg(); // 系统时钟40MHz,I2C传输速率100Kbps. I2CMasterInitExpClk(I2C1_BASE, 4000000, false); for(ulIdx = 0; ulIdx < 16; ulIdx++) { pucData[ulIdx] = ulIdx; } WriteToDAC(pucData, 0, 16); return(0); } [/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。