Part Number:MSP430G2553
Tool/software: Code Composer Studio
Hi implemeting i2c communication between master and slave (Master will be transmitting and slave will be receiving)...Master part works fine but slave is not at all raecieving.Enclsoed the slave part code here..Some one please pointout the mistake.
Any help will be highly helpful.
#ifdef MASTER
//----------------------------------------------------------------------------
// I2C Initialization (function only for MASTER)
//----------------------------------------------------------------------------
void Init_I2C(void)
{
P1SEL |= SCL + SDA; //Assign I2C pins to USCI_B0
P1SEL2|= SCL + SDA; //Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; //Enable SW reset
//----------------------------------------------------------------------------
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //Single Master-Env, Master, I2C mode, synchronous mode
//----------------------------------------------------------------------------
UCB0CTL1 = UCSSEL_2 + UCSWRST; //Use SMCLK, keep SW reset
UCB0BR0 = 12; //fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
//----------------------------------------------------------------------------
UCB0I2CSA = SLAVE_ADDR; //Slave Address
//----------------------------------------------------------------------------
UCB0CTL1 |= UCTR; //I2C (as transmitter initially)
//----------------------------------------------------------------------------
IFG2 &= ~(UCB0TXIFG + UCB0RXIFG); //Clear TX and RX interrupt flags
IE2 |= UCB0TXIE + UCB0RXIE; //Enable TX and RX interrupts
//----------------------------------------------------------------------------
UCB0CTL1 &= ~UCSWRST; //I2C Clear SW reset, resume operation
}
//----------------------------------------------------------------------------
#endif
#ifdef SLAVE
//----------------------------------------------------------------------------
// I2C Initialization (function only for SLAVE)
//----------------------------------------------------------------------------
void Init_I2C(void)
{
P1SEL |= SCL + SDA; //Assign I2C pins to USCI_B0
P1SEL2|= SCL + SDA; //Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; //Enable SW reset
//----------------------------------------------------------------------------
UCB0CTL0 = UCMODE_3 + UCSYNC; //Slave, I2C mode, synchronous mode
//----------------------------------------------------------------------------
UCB0I2COA = SLAVE_ADDR; //Own Address
//----------------------------------------------------------------------------
UCB0CTL1 &= ~UCTR; //I2C (as receiver initially)
//----------------------------------------------------------------------------
IFG2 &= ~(UCB0TXIFG + UCB0RXIFG); //Clear TX and RX interrupt flags
//UCB0I2CIE |= UCSTPIE + UCSTTIE; // Enable STT and STP interrupt
IE2 &= ~UCB0TXIE; //Disable TX interrupt
IE2 |= UCB0RXIE; //Enable RX interrupt
//----------------------------------------------------------------------------
UCB0CTL1 &= ~UCSWRST; //I2C Clear SW reset, resume operation
}
#endif
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// I2C Transmit the Byte Master (function only for MASTER)
//----------------------------------------------------------------------------
#ifdef MASTER
void Master_Tx(void)
{
while(UCB0STAT & UCBBUSY); //BUS busy
UCB0CTL1 |= UCTR; //I2C TX
UCB0CTL1 |= UCTXSTT; //I2C start condition
}
//----------------------------------------------------------------------------
#endif
#ifdef MASTER
void Master_Rx(void)
{
while(UCB0STAT & UCBBUSY); //BUS busy
UCB0CTL1 &= ~UCTR ; //I2C RX
UCB0CTL1 |= UCTXSTT; //I2C start condition
}
//----------------------------------------------------------------------------
#endif
#ifdef MASTER
//----------------------------------------------------------------------------
// I2C Transmit-Interrupt service routine (function only for MASTER)
//----------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if (IFG2 & UCB0TXIFG)
{
while (UCB0CTL1 & UCTXSTT); //Start condition sent?
serialPrintln("sending..");
// writing to the buffer
UCB0TXBUF = TXData; //Send TX data
UCB0CTL1 |= UCTXSTP; //I2C stop condition
IFG2 &= ~UCB0TXIFG; //Clear USCI_B0 TX int flag
}
if (IFG2 & UCB0RXIFG)
{
while (UCB0CTL1 & UCTXSTT); //Start condition sent?
serialPrintln("receiving..");
// reading from the buffer
RXData = UCB0RXBUF; //Get RX data
UCB0CTL1 |= UCTXSTP; //I2C stop condition
IFG2 &= ~UCB0RXIFG; //Clear USCI_B0 RX int flag
}
}
//----------------------------------------------------------------------------
#endif
#ifdef SLAVE
//----------------------------------------------------------------------------
// I2C Transmit-Interrupt service routine (function only for SLAVE)
//----------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
serialPrintln("ISR..");
if (IFG2 & UCB0TXIFG)
{
serialPrintln("sending..");
// writing to the buffer
UCB0TXBUF = TXData; //Send TX data
}
if (IFG2 & UCB0RXIFG)
{
serialPrintln("receiving..");
// reading from the buffer
RXData = UCB0RXBUF; //Get RX data
serialPrintln(RXData);
}
}
#endif
#endif /* COMM_H_ */
Thanks