TI的工程师您好:
目前我在调试CC430F5137与HDC1000的I2C通讯遇到如下问题,I2C的通讯有时候能够正常有时候就不正常了。通过软件跟踪代码是停在I2C写代码上。
代码如下:
char UCB0_I2C_write(unsigned char slave_addr, unsigned char reg, unsigned char *data, unsigned char byteCount)
{
unsigned char comres = 0;
//ASSIGN DATA TO LOCAL POINTER
sUCB0_I2C.Data = data;
//ASSIGN BYTECOUNT TOO
sUCB0_I2C.ByteCount = byteCount;
//WAIT FOR PREVIOUS TRAFFIC TO CLEAR
while(UCB0STAT & UCBBUSY); 无法正常通讯的时候就是代码就是一直死在这里无法继续走下去。不知道具体什么原因。
// LOAD THE DEVICE SLAVE ADDRESS
UCB0I2CSA = slave_addr;
// ENABLE TRANSMIT, GENERATE START BIT UCB0CTL1 |= UCTR + UCTXSTT;
// WAIT FOR FIRST INT while(!(UCB0IFG & UCTXIFG));
// LOAD THE REGISTER ADDRESS
UCB0TXBUF = reg;
// NOW WAIT FOR START BIT TO COMPLETE
while(UCB0CTL1 & UCTXSTT);
// CHECK IF SLAVE ACK/NACK
if(UCB0IFG & UCNACKIFG)
{
// IF NACK, SET STOP BIT AND EXIT
SET_BITS(UCB0CTL1, UCTXSTP);
return(USCI_I2C_STATUS_SLAVE_NACK);
}
// SHOW WE ARE BUSY
sUCB0_I2C.Busy = true;
// ENABLE TX INTERRUPT
UCB0IE |= UCTXIE + UCSTPIE + UCSTTIE; // Enable STT and STP interrupt
// Enable TX interrupt
// NOW WRITE ONE OR MORE DATA BYTES
while(sUCB0_I2C.Busy)
{
LPM0;
}
// DISABLE TX INTERRUPT
CLR_BITS(UCB0IE, UCTXIE);
return(comres);
}
请帮忙看能否解决这个问题,谢谢!
gaoyang9992006:
楼主看看例程怎么做的,我给你发到附件里了。
dory.du:
回复 gaoyang9992006:
谢谢,这个例程我看过了,无法解决我的问题。