This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi
i want to interface Msp430f5438 and mcp4161 by i2c communication.After
sending the start bit i am getting NACK whats the problem i don't
understand if anyone find any bugs in my code please told me.
void i2c_init(void)
{
P10SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB3CTL1 |= UCSWRST;
UCB3CTL1 = UCSSEL_2 + UCSWRST; // Enable SW reset
UCB3CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB3BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB3BR1 = 0;
__delay_cycles(10);
UCB3CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}
void write_i2c(unsigned char reg_add,unsigned char data)
{
while(UCB3CTL1 & UCTXSTP);
UCB3I2CSA = (0x5c);
UCB3CTL1 |= UCTR; // I2C transmitter
UCB3CTL1 |= UCTXSTT; // I2C start condition
while(UCB3CTL1 & UCTXSTT); // Wait for START to finish
// I2C start condition
if(UCB3IFG & UCNACKIFG)
{
UCB3IFG &=~ UCNACKIFG;
// UCB3CTL1 |= UCTXSTP; // I2C stop condition
// while(UCB3CTL1 & UCTXSTP);
}
UCB3TXBUF =reg_add; // Move reg to TX
while(UCB3STAT&UCBUSY);
UCB3IFG &=~ UCNACKIFG;
__delay_cycles(10);
UCB3TXBUF = data ; // Move reg to TX
while ((UCB3STAT & UCBUSY));
__delay_cycles(10);
UCB3CTL1 |= UCTXSTP; // I2C stop condition
while(UCB3CTL1 & UCTXSTP);
return;
}
void main(void)
{
i2c_init();
write_i2c(0xC0,0x0f)
}
**Attention** This is a public forum