急,MSP430F5438硬件I2C不能工作
我正在弄msp430f5438硬件i2c,可是为什么初始化以后设置UCB0CTL1 |= UCTXSTT+UCTR 都没有起始信号呢,scl和sda线都没任何反应,看了文档觉得初始化都没问题呀,就是找不到原因,贴上代码,还请大家帮我看看哪里有问题。急死我了!
void init_iic(void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 20; // fSCL = SMCLK/20 = 400kHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}
BOOL iic_tx_byte_rx_ack(uint8_t byte)
{
uint8_t retry = 50;
while (--retry)
{
if (UCB0IFG & UCTXIFG)
{
break;
}
else
{
delay_us(2);
}
}
if (retry == 0)
{
return FALSE;
}
UCB0TXBUF = byte; // Write data to TXBUF
while (!UCTXIFG)//1空闲,0繁忙
{
if(UCNACKIFG == 1)
{
return FALSE;
}
}
return TRUE;
}
static BOOL iicBusy = NO;
BOOL iic_write(uint8_t seven_bit_addr, uint8_t *txdata, uint16_t txlen)
{
uint8_t *temp = txdata;
if (iicBusy == YES)
{
return FALSE;
}
iicBusy = YES;
#if 1
while (UCB0CTL1 & UCTXSTP);
UCB0I2CSA =seven_bit_addr;
UCB0CTL1 |= UCTXSTT+UCTR ;
// while(UCB1CTL1 & UCTXSTT) ; uctxstt·ÅÔÚÕâÀï»áËÀÑ»·¡£
while(! (UCB0IFG & UCTXIFG) ) ;
//UCB0TXBUF = seven_bit_addr;
//while(UCB0CTL1 & UCTXSTT) ; //·ÅÔÚÕâÀï¾Íͨ¹ýÁË
#else
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//while (UCTXSTP);
// I2C TX, start condition
UCB0CTL1 |= UCTR + UCTXSTT;
UCB0I2CSA = (seven_bit_addr);// & 0x7f);
#endif
uint8_t index = 0;
while (txlen--)
{
while (!(UCB0IFG & UCTXIFG)); // Block until byte is transferred from TXBUF to shift reg.
if(txlen == 0)
{
UCB0CTL1 |= UCTXSTP;
UCB0IFG &= ~UCTXIFG;
}
//UCB0TXBUF = *temp; // Write data to TXBUF
if(iic_tx_byte_rx_ack(*temp) == TRUE)
{
temp++;
}
else
{
return FALSE;
}
}
while (!(UCB0IFG & UCTXIFG)); // Block until byte is transferred from TXBUF to shift reg.
// I2C stop condition
//UCB0CTL1 |= UCTXSTP;
// Clear USCI_B0 TX int flag
// UCB0IFG &= ~UCTXIFG;
iicBusy = NO;
return TRUE;
}
设备地址我已经右移了一位了,再说即使我设备地址弄错,最多是写不进设备,但是读芯片的管脚上应该还是有相应的起始信号的吧。
[解决办法]
咋弄的?我STT位置1后,一直等待从设备应答把STT清0,可是一直都没有变化。