Other Parts Discussed in Thread: MSP430F2013
Dear all,
I'm working with the micro MSP430F2013 like Master and Slave, so I want to communicate there devices by I2C, I've the code for Master but I can't configure like Slave, I think that is with the register USIMST=0 rigth? but there any register like USISLV or something there?
//#include "io430.h"
#include "msp430x20x3.h"
char SLV_data = 0x00;
char SLV_Addr = 0x39;
int I2C_State = 0;
void main(void)
{
volatile unsigned int i; // Use volatile to prevent removal
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
//if (CALBC1_8MHZ==0xFD || CALDCO_8MHZ ==0xFC )
{
while(1);
}
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1OUT = 0xC0;
P1REN |= 0xC0;
P1DIR = 0x13;
P1DIR |= 0x07;
USICTL0 = USIPE6+USIPE7+USIMST+USISWRST;
USICTL1 = USII2C+USIIE;
USICKCTL = USIDIV_3+USISSEL_2+USICKPL;
USICNT |= USIIFGCC;
USICTL0 &= ~USISWRST;
USICTL1 &= ~USIIFG;
_EINT();
while(1)
{
USICTL1 |= USIIFG;
LPM0;
_NOP();
for (i = 0; i < 5000; i++);
}
}
/******************************************************
// USI interrupt service routine
******************************************************/
#pragma vector = USI_VECTOR
__interrupt void USI_TXRX (void)
{
switch(I2C_State)
{
case 0: // Generate Start Condition & send address to slave
USISRL = 0x00; // Generate Start Condition...
USICTL0 |= USIGE+USIOE; // Set SDA = Output and set the Latch to Transparent
USICTL0 &= ~USIGE; // Set the Latch to Gated by Clock
USISRL = SLV_Addr ; // Load the shift register to the transmit address, R/W = 0
USISRL = SLV_Addr; // transmit slv address, R/W bit = 1
USICNT = (USICNT & 0xE0) + 0x08; // Bit counter = 8, TX Address
I2C_State = 2; // Go to next state: receive address (N)Ack
break;
case 2: // Receive Address Ack/Nack bit
USICTL0 &= ~(USIOE + USIGE); // SDA = input
USICNT |= 0x01; // Bit counter = 1, receive (N)Ack bit
I2C_State = 4; // Go to next state: check (N)Ack
break;
case 4: // Process Address Ack/Nack & handle data RX
if (USISRL & 0x01) // If Nack received...
{ // Prep Stop Condition
USICTL0 |= USIOE;
USISRL = 0x00;
USICNT |= 0x01; // Bit counter = 1, SCL high, SDA low
I2C_State = 10; // Go to next state: generate Stop
P1OUT |= 0x07; //Turn high pin 1.2(relay1)
P1OUT |= 0x13; // Turn high pin 1.4(relay2)
}
else // Ack received
{ // Receive Data from slave
USICNT |= 0x08; // Bit counter = 8, RX data
I2C_State = 6; // Go to next state: Test data and (N)Ack
P1OUT &= ~0x07; // pin 1.2 is low
P1OUT &= ~0x13; // pin 1.4 is low
}
break;
case 6: // Send Data Ack/Nack bit
USICTL0 |= USIOE; // SDA = output
if (USISRL == SLV_data) // If data valid...
{
USISRL = 0x00; // Send Ack
SLV_data++; // Increment Slave data
P1OUT &= ~0x07; // pin 1.2 is low
P1OUT &= ~0x13; // pin 1.4 is low
// P1OUT &= ~0x01; // LED off
}
else
{
USISRL = 0xFF; // Send NAck
P1OUT |= 0x07; //Turn high pin 1.2(relay1)
P1OUT |= 0x13; // Turn high pin 1.4(relay2)
}
USICNT |= 0x01; // Bit counter = 1, send (N)Ack bit
I2C_State = 8; // Go to next state: prep stop
break;
case 8: // Prep Stop Condition
USICTL0 |= USIOE; // SDA = output
USISRL = 0x00;
USICNT |= 0x01; // Bit counter = 1, SCL high, SDA low
I2C_State = 10; // Go to next state: generate Stop
break;
case 10: // Generate Stop Condition
USISRL = 0x0FF; // USISRL = 1 to release SDA
USICTL0 |= USIGE; // Transparent latch enabled
USICTL0 &= ~(USIGE+USIOE);// Latch/SDA output disabled
I2C_State = 0; // Reset state machine for next transmission
LPM0_EXIT; // Exit active for next transfer
break;
}
USICTL1 &= ~USIIFG; // Clear pending flag
}