//****************************************************************************** // MSP430G2xx2 - I2C Slave Receiver / Slave Transmitter, multiple bytes // // Description: I2C Master communicates with I2C Slave using // the USI Master data should increment from 0x55 with each transmitted byte // ACLK = n/a, MCLK = SMCLK = Calibrated 1MHz // // ***THIS IS THE SLAVE CODE*** // // Slave Master // (MSP430G2xx2_usi_12.c) // MSP430G2xx2MSP430G2xx2 // // /|\| XIN|/|\| XIN|// | | | | | | // |RST XOUT| |RST XOUT|// | | | | // LED LED // | SDA/P1.7| ->|P1.6/SDA | // | SCL/P1.6|< -|P1.7/SCL | // // Note: internal pull-ups are used in this example for SDA & SCL // // D Dang // Texas Instruments Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include #define Number_of_Bytes // **** How many bytes?? **** void Setup_USI_Slave(void); char MST_Data = 0; char SLV_Data = 0x55; char SLV_Addr = 0x90; int I2C_State, Bytecount, transmit = 0; // Variable for received data // Address is 0x486->8 // Tx bytes to Master: State 2->4->10->12->14 //****************************************************************************** #pragma vector = USI_VECTOR interrupt void USI_TXRX (void) { if (USICTL1 & USISTTIFG) // Start entry? { P1OUT |= 0x01; // LED on: sequence start I2C_State = 2; // Enter 1st state on start } switch( even_in_range(I2C_State,14)) { case 0: break; // Idle, should not get here case 2: // RX Address USICNT = (USICNT & 0xE0) + 0x08; // Bit counter = 8, RX address USICTL1 &= ~USISTTIFG; // Clear start flag I2C_State = 4; // Go to next state: check address break; case 4: // Process Address and send (N)Ack if (USISRL & 0x01){ // If master read SLV_Addr = 0x91; // Save R/W bit transmit = 1;} else{transmit = 0; SLV_Addr = 0x90;} USICTL0 |= USIOE; // SDA = output if (USISRL == SLV_Addr) // Address match? { USISRL = 0x00; // Send Ack P1OUT &= ~0x01; // LED off if (transmit == 0){ I2C_State = 6;} // Go to next state: RX data if (transmit == 1){ I2C_State = 10;} // Else go to next state: TX data } else { USISRL = 0xFF; // Send NAck P1OUT |= 0x01; // LED on: error I2C_State = 8; // next state: prep for next Start } USICNT |= 0x01; // Bit counter = 1, send (N)Ack bit break; case 6: // Receive data byte Data_RX(); break; case 8:// Check Data & TX (N)Ack USICTL0 |= USIOE; // SDA = output if (Bytecount