Hi,
I have a simple code for I2C master receiver but it do not work, not even able to break on ISR break points.
I have another MSP430 as Master TX with Arduino, that one works fine,mean the hardware connection is good and Arduino board is good.
I connected this with Arduino board where Arduino board as I2C slave TX.
use P4.1 and P4.2 as I2C pins.
Any idea on what I missed? Thanks a lot!
Here is my code.
#include <msp430.h>
//#include "debug_uart.h"
unsigned char *PTxData; // Pointer to TX data
unsigned char TXByteCtr;
void I2C_send(unsigned char Data);
void I2C_receive();
int RXByteCtr, RPT_Flag = 0; // enables repeated start when 1
volatile unsigned char RxBuffer[128]; // Allocate 128 byte of RAM
unsigned char *PTxData; // Pointer to TX data
unsigned char *PRxData; // Pointer to RX data
unsigned char SLAVE_ADDRESS = 0x8;
unsigned int ByteToGet = 1;
int main(void)
{
int i;
P4SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB1CTL1 |= UCSWRST; // Enable SW reset
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB1BR0 = 0x12; // 12; // fSCL = SMCLK/12 = ~100kHz
UCB1BR1 = 0;
UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB1I2CSA = SLAVE_ADDRESS; //
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ByteToGet = 2;
RXByteCtr = 0;
UCB1I2CSA = SLAVE_ADDRESS;
PRxData = (unsigned char *)RxBuffer; // Start of RX buffer
while (UCB1CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB1IE &= ~UCRXIE;
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
// while (!(UCB1IFG & UCTXIFG)) ; // wait until data can be written
UCB1IE |= UCSTPIE + UCRXIE + UCNACKIE; // Enable RX interrupt
// UCB1IE |= UCTXIE + UCRXIE + UCNACKIE; // Enable RX interrupt
for(i=0;i<10;i++); // Delay
UCB1CTL1 &= ~UCTR;
UCB1CTL1 |= UCTXSTT;
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts
while (UCB1CTL1 & UCTXSTT); // Ensure START condition got sent
__no_operation(); // Remain in LPM0 until all data
// read out the RxData buffer
}
//------------------------------------------------------------------------------
// The USCIAB0TX_ISR is structured such that it can be used to transmit any
// number of bytes by pre-loading TXByteCtr with the byte count. Also, TXData
// points to the next byte to transmit.
//------------------------------------------------------------------------------
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_B1_VECTOR
__interrupt void USCI_B1_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_B1_VECTOR))) USCI_B1_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(UCB1IV,12))
{
case 0: break; // Vector 0: No interrupts
case 2: break; // Vector 2: ALIFG
case 4: // Vector 4: NACKIFG
UCB1CTL1 |= UCTXSTP; // I2C stop condition
UCB1IFG &= ~UCRXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(LPM0_bits);
break;
case 6: break; // Vector 6: STTIFG
case 8: // Vector 8: STPIFG
UCB1IFG &= ~UCSTPIFG;
if (RXByteCtr >= ByteToGet) // Check RX byte counter
{
UCB1CTL1 |= UCTXSTP; // I2C stop condition
UCB1IFG &= ~UCRXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(LPM0_bits);
}
break;
case 10: // Vector 10: RXIFG
//==================================== rx
*PRxData++ = UCB0RXBUF; // Get RX'd byte into buffer
RXByteCtr++;
break;
//==================================== tx
case 12: break; // Vector 12: TXIFG
default: break;
}
}
// Arduino code.
#include <Wire.h>
byte x;
void setup() {
Serial.begin(9600);
Wire.begin(0x8); // join i2c bus with address #8
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
// Wire.write("123456"); //
Serial.print("Request from Master. Sending: ");
Serial.print("\n");
Wire.write("1234");
}
void receiveEvent(int bytes)
{
if(Wire.available() != 0)
{
for(int i = 0; i< bytes; i++)
{
x = Wire.read();
Serial.print("Received: ");
Serial.print(x, HEX);
Serial.print("\n");
}
}
}