LSM303D SPI Communication for MSP430G2553

Hi,
I am a student at university and I have a project for my lesson.
I would like to get the acceleration data in the X, Y and Z axes from the LMS303D accelerometer sensor using SPI Communication. I am using the MSP430G2553 microcontroller for this. But I have a problem, I can not get the acceleration data from the LSM303D.Can you help me with how to do it? The data I get from the LSM303D is only 255 :frowning:

My code for MSPG2553:

#include "io430.h"
unsigned int write_reg(unsigned char );
void konfigurasyon( unsigned char ,unsigned char );
void spi_init(void);

void spi_init(void)
{
    P1OUT |= BIT4;
    P1DIR |= BIT4;
    P1SEL = BIT7 | BIT6 | BIT5;
    P1SEL2 = BIT7 | BIT6 | BIT5;

    UCB0CTL1 = UCSWRST;
    UCB0CTL0 |= UCCKPH + UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
    UCB0CTL1 |= UCSSEL1;// + ID_1; // SMCLK
    UCB0BR0 |= 0x02; // /2
    UCB0BR1 = 0; //
//    UCB0CTL0 = 0; // No modulation
    UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
   // IE2 |= UCA0RXIE;     // Enable USCI0 RX interrupt
}  

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;  
  unsigned int in[6]={0x00,0x00,0x00,0x00,0x00,0x00}; 
  
  spi_init();
  konfigurasyon(0x21,0x00); //CTRL2
  konfigurasyon(0x20,0x57); //CTRl 1
  while(1)
  {  
     in[0]=write_reg(0x28);
     in[1]=write_reg(0x29);
     in[2]=write_reg(0x2A);
     in[3]=write_reg(0x2B);
     in[4]=write_reg(0x2C);
     in[5]=write_reg(0x2D);
  
}
  return 0;
}

void konfigurasyon( unsigned char adress,unsigned char data)
{ 
  unsigned int temp=0;
   P1OUT &= (~BIT4); // Select Device
   while (!(IFG2 & UCB0TXIFG));
   UCB0TXBUF=(adress);  
   while (!(IFG2 & UCB0RXIFG)); // USCI_B0 RX Received?
   temp = (unsigned int) UCB0RXBUF;
  while (!(IFG2 & UCB0TXIFG));
   UCB0TXBUF=(data);  
   while (!(IFG2 & UCB0RXIFG)); // USCI_B0 RX Received?
   temp = (unsigned int) UCB0RXBUF;
 P1OUT |= (BIT4); // Unselect Device
}

unsigned int write_reg(unsigned char adress)
{
  unsigned int read_reg=0x00;
  P1OUT &= (~BIT4); // Select Device 
   while (!(IFG2 & UCB0TXIFG));
   UCB0TXBUF=(0x80 | adress);
   while (!(IFG2 & UCB0RXIFG)); // USCI_B0 RX Received?
   read_reg = (unsigned int) UCB0RXBUF;
 P1OUT |= (BIT4); // Unselect Device  
 return read_reg;
}

Hello.

We are not too familiar with the MSP430 MCU, so we cannot offer much help with your code. It looks like you are using the correct registers and values to initialize, enable, and read from the LSM303D’s accelerometer, however, your SPI mode might be configured wrong. Can you try replacing UCCKPH with UCCKPL for UCB0CTL0 and see if that works? If it does not, try adding both bits or neither.

In your main while loop, after storing the accelerometer readings into the array, how are you outputting the raw readings? Can you post a screenshot showing your program output and pictures showing all your connections or a wiring diagram? If you have an oscilloscope or logic analyzer, can you check what the SPI pins are doing?

- Amanda