Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

MSPG2553 I2C communication problems

$
0
0

I am using two MSPG2553.  I have connected them as shown below.

 

MSP430G2xx3                  MSP430G2xx3

slave                                   master

P1.7/UCB0SDA|<-|---+->|P1.7/UCB0SDA 

P1.6/UCB0SCL|<-+----->|P1.6/UCB0SCL    

 

I am trying to send a byte from the master to the slave.  On the Master, red led on P1.0 is suppose to blink while transmitting.  On the Slave, red led on P1.0 is suppose to blink while receiving. 

 

I am using example code msp430g2xx3_uscib0_i2c_06.c and msp430g2xx3_uscib0_i2c_07.c from the MSP430G2xx3 Code Examples.  I added the blinking red led to see if the code worked.  

 

I can not get the red led to blink on the slave, which means no data is being received.

 

How do I get i2c communication to work?

 

I modified the example code to get the red led blinking on the Master code.  I modified the code by commenting out  //__bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts.

Master Code: 

 #include "msp430g2553.h"

 

#define RED_LED               BIT0

 

unsigned char TXData;

unsigned char TXByteCtr;

 

void main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

 

  P1DIR |= RED_LED;

  P1OUT |= RED_LED;                             // Initialize all GPIO

 

  P1SEL |= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0

  P1SEL2|= BIT6 + BIT7;                     // Assign I2C pins to USCI_B0

  UCB0CTL1 |= UCSWRST;                      // Enable SW reset

  UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // I2C Master, synchronous mode

  UCB0CTL1 = UCSSEL_2 + UCSWRST;            // Use SMCLK, keep SW reset

  UCB0BR0 = 12;                             // fSCL = SMCLK/12 = ~100kHz

  UCB0BR1 = 0;

  UCB0I2CSA = 0x48;                         // Slave Address is 048h

  UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation

  IE2 |= UCB0TXIE;                          // Enable TX interrupt

 

  TXData = 0x00;                            // Holds TX data

 

  while (1)

  {

    TXByteCtr = 1;                          // Load TX byte counter

    while (UCB0CTL1 & UCTXSTP);             // Ensure stop condition got sent

    UCB0CTL1 |= UCTR + UCTXSTT;             // I2C TX, start condition

    //__bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts

                                            // Remain in LPM0 until all data

                                            // is TX'd

   if(TXData>0xFE)                         // Reset so no overflow

      TXData=0x0D;

    else

      TXData++;                             // Increment data byte

   

    __delay_cycles(500000);                 // 0.5s Delay so the data is readable

    P1OUT ^= RED_LED;

  }

}

 

//------------------------------------------------------------------------------

// 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.

//------------------------------------------------------------------------------

#pragma vector = USCIAB0TX_VECTOR

__interrupt void USCIAB0TX_ISR(void)

{

  if (TXByteCtr)                            // Check TX byte counter

  {

    UCB0TXBUF = TXData;                     // Load TX buffer

    TXByteCtr--;                            // Decrement TX byte counter

  }

  else

  {

    UCB0CTL1 |= UCTXSTP;                    // I2C stop condition

    IFG2 &= ~UCB0TXIFG;                     // Clear USCI_B0 TX int flag

    __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0

  }

}

 

Slave Code:

#include "msp430g2553.h"

 

#define RED_LED               BIT0

 

volatile unsigned char RXData;

 

void main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

 

  P1DIR |= RED_LED;

  P1OUT |= RED_LED;                             // Initialize all GPIO

 

  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

  IE2 |= UCB0RXIE;                          // Enable RX interrupt

 

  while (1)

  {

    __bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts

    __no_operation();                       // Set breakpoint >>here<< and read

  }                                         // RXData

}

 

// USCI_B0 Data ISR

#pragma vector = USCIAB0RX_VECTOR

__interrupt void USCIAB0RX_ISR(void)

{

  RXData = UCB0RXBUF;                       // Get RX data

  P1OUT ^= RED_LED;

  __bic_SR_register_on_exit(CPUOFF);        // Exit LPM0

}


Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>