本帖最后由 刀鱼 于 2016-10-14 17:05 编辑
在调一个g2553板子工程,其中有用到I2C通信,写了一个I2C主机写多个字节的函数
问题是这样的,第一个字节被写入缓冲 UCB0TXBUF 之后,中断标志IFG2的 UCB0TXIFG 位被置0了。
正常情况下,不是收到STOP指令后中断标志UCB0TXIFG才被置0吗?
求大神指导!
下面是我的I2C主机写多个字节的函数:
static int8_t I2CSendBytes(uint8_t I2CSlaveAddress, uint8_t *DataBuffer, uint16_t ByteCount, uint16_t *SentByte)
{
uint16_t DelayCounter = 0;
uint16_t NumberOfBytesSent = 0;
uint8_t *DataPointer;
UCB0CTL0 |= UCMST; // set master
UCB0I2CSA = I2CSlaveAddress; // 从机地址
DataPointer = DataBuffer;
UCB0CTL1 |= UCTR; //data in transmit direction
UCB0CTL1 |= UCTXSTT; //Generate Start Condition // UCB0TXIFG was set 1 automatically
//Send Start Byte
while(!(IFG2 & UCB0TXIFG)) //if UCTXSTT != 0, wait here // wait until the slave acknowladged
{
DelayCounter ++;
if (DelayCounter >= DELAY_LIMIT)
break;
}
if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred
{
*SentByte = NumberOfBytesSent;
UCB0CTL1 |= UCTXSTP;
return -1;
}
for(NumberOfBytesSent = 0; NumberOfBytesSent < ByteCount; NumberOfBytesSent++)
{
UCB0TXBUF= *DataPointer; // 这一步执行后,中断标志UCB0TXFIG置0了
DelayCounter = 0;
while(DelayCounter < DELAY_LIMIT && (!(IFG2 & UCB0TXIFG) || (UCB0CTL1 & UCTXSTT))) // 然后程序卡在这个循环里
{
DelayCounter++;
}
if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred
{
*SentByte = NumberOfBytesSent;
UCB0CTL1 |= UCTXSTP; //send stop condition
return -1;
}
DataPointer++;
}
IFG2 &= ~UCB0TXIFG;
UCB0CTL1 |= UCTXSTP; //send stop bit
DelayCounter = 0;
while(DelayCounter < DELAY_LIMIT && ((UCB0CTL1 & UCTXSTP))) //check if the byte has been sent
{
DelayCounter++;
}
*SentByte = NumberOfBytesSent;
if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred
{
UCB0CTL1 |= UCSWRST;
return -1;
}
else
return 0;
}
|