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.
I am having some trouble with I2C working consistently. I am using the msp430g2553 as a slave and having it receive messages from a master Arduino. Upon receiving a command the slave msp sends a 'D' over the i2c line.
#define NUM_BYTES 32 // How many bytes?
char *PRxData,*Start; // Pointer to RX data
volatile unsigned char TXByteCtr, RXByteCtr, RX = 0;
char RxBuffer[32]; // Allocate 128 byte of RAM
void I2C_init()
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode
UCB0I2COA = 0x48; // Own Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0I2CIE |= UCSTPIE + UCSTTIE; // Enable STT and STP interrupt
}
void main()
{
while(1)
{
RX = 1;
IE2 &= ~UCB0TXIE; // Disable TX interrupt
IE2 |= UCB0RXIE; // Enable RX interrupt
TXByteCtr=0;
RXByteCtr=0;
PRxData = (char *)RxBuffer; // Start of RX buffer
Start= RxBuffer;
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
_disable_interrupt();
//Process data obtained
_enable_interrupt();
RX=0;
IE2 |= UCB0TXIE;
IE2 &= ~UCB0RXIE;
}
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if(RX == 0)
{
UCB0TXBUF = 'D'; // Transmit data at address PTxData
TXByteCtr++; // Increment TX byte counter
}
if(RX == 1){*PRxData++ = UCB0RXBUF; // Move RX data to address PRxData
RXByteCtr++; // Increment RX byte count
if(RXByteCtr >= NUM_BYTES){ // Received enough bytes to switch
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0 if data was
}
}
}
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
if(RX == 0){ UCB0STAT &= ~(UCSTPIFG + UCSTTIFG); // Clear interrupt flags
}
if(TXByteCtr)
{
IE2 &= ~UCB0TXIE;
}
if(RX == 1){UCB0STAT &= ~(UCSTPIFG + UCSTTIFG); // Clear interrupt flags
}
}
It would be of great help if someone could tell me how to get this working consistently.
It would be great to know what kind of inconsistency you observe.
It doesn't make much sense (and likely, nobody will make the effort) to run through a program without knowing what to look for.
**Attention** This is a public forum