Check my I2C code for interfacing MSP430f5438 and MCP4161

Status
Not open for further replies.

nileshthakare2

Member level 1
Joined
Jul 16, 2011
Messages
34
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,618
Hi
i want to interface Msp430f5438 and mcp4161 by i2c communication.After sending the start bit i am getting NACK whats the problem i dont 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)


}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…