Jump to content
Sign in to follow this  
trungkiena6

[Help] How to test SPI Communication

Recommended Posts

Hello everybody. I read an example code MSP430G2553 about SPI, But i can't know how it run, and how i can test SPI in it. Please help me.

My code: 

#include <msp430.h>

int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
  while (!(P1IN & BIT4));                   // If clock sig from mstr stays low,
                                            // it is not yet in SPI mode
  
  P1SEL = BIT1 + BIT2 + BIT4;
  P1SEL2 = BIT1 + BIT2 + BIT4;
  UCA0CTL1 = UCSWRST;                       // **Put state machine in reset**
  UCA0CTL0 |= UCCKPL + UCMSB + UCSYNC;      // 3-pin, 8-bit SPI master
  UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  IE2 |= UCA0RXIE;                          // Enable USCI0 RX interrupt

  __bis_SR_register(LPM4_bits + GIE);       // Enter LPM4, enable interrupts
}

// Echo character
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR (void)
{
  while (!(IFG2 & UCA0TXIFG));              // USCI_A0 TX buffer ready?
  UCA0TXBUF = UCA0RXBUF;
}

Thank you and gold bless you

Share this post


Link to post
Share on other sites

The easy & visual way is to build a circuit using a 74HC595 shift register and 8 LEDs.  Hook that into the SPI bus and drive the chip select line low and you can try sending different values.

 

The other way (my preference) is to buy a Logic Analyzer and hook it up to the SPI pins to capture what's actually happening & analyze it :D

Share this post


Link to post
Share on other sites

or get another launchpad or equivalent to talk to and take data from one serial port and pipe it out on the other. 

 

if the other chip doesnt have hardware serial, you can always bit bang it out. lots of examples on the web. 

 

crazier yet, loop it back to the same micro using the bit bang SPI

 

code snippet for bit bang spi: (CPOL and CPHA = 0 )

uint8_t spi_send(const uint8_t _data)
{
  const uint8_t MOSI = BIT7;
  const uint8_t MISO = BIT6;
  const uint8_t SCK = BIT5;

  uint8_t data = 0;
  if (_data & 0x80)
    P1OUT |= BIT7;
  else
    P1OUT &= BIT7;

  __delay_cycles(10);
  for (int i = 0 ; i < 8 ; i++)
  {
    if (_data & (1 << (7-i)))
      P1OUT |= BIT7;
    else
      P1OUT &= BIT7;
    
    data |= (P1IN & MISO) << (7-i);
    __delay_cycles( 4);
    P1OUT |= SCK;
    __delay_cycles( 8);
    P1OUT &= SCK;
  }
 return data; 
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×