This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SPI Communication with an LIS331 Accelerometer

Other Parts Discussed in Thread: MSP430F2618

Hi TI Community,

I have been trying for the past few weeks to simply communicate my MSP430f2618 with an accelerometer found on Sparkfun (https://www.sparkfun.com/products/10345). I tested the breakout board with an Arduino and the example code where it worked, however using it on the TI chip is proving to be more difficult.

Currently I can see it sending the correct bits on the oscilloscope, but the response I get from the accelerometer  is most of the time just 0x00 and if it does respond it doesn't make sense. Code I have been using

unsigned char test;
unsigned int i;
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
DCOCTL = CALDCO_1MHZ;
BCSCTL1 = CALBC1_1MHZ;
BCSCTL1 |= XT2OFF;
// master initialise
initSPI();
for (;;) {
// get accelerometer readings
P3OUT &= ~0x01;
IFG2 &= ~UCB0TXIFG;
UCB0TXBUF = 0xA0; //read bit | non consecutive measure bit | 0x28 = 0xA8
while (!UCB0TXIFG) ;
for(i=0;i<5;i++);
IFG2 &= ~UCB0RXIFG;
UCB0TXBUF = 0x00; //read bit
while (!UCB0RXIFG)
;
test = UCB0RXBUF;
P3OUT |= 0x01;
printf("%x\n", test);
}
}
void initSPI(void) {
P3SEL |= 0x0e;
P3DIR |= 0x01;
UCB0CTL0 |= UCMST + UCSYNC + UCMSB + UCCKPL;
UCB0CTL1 |= UCSSEL_2;
UCB0CTL1 &= ~UCSWRST;
}

The code initialises the SPI ports etc, then sends a byte and should print the one it receives... Is there something major I have missed ? Using CCS Version: 5.2.1.00018. 

  • Daniel Tomicek said:
    I can see it sending the correct bits on the oscilloscope

    Is the timing correct?

    Are the voltage levels correct?

    Have you compared it to the Arduino?

    Have you compared it to the accelerometer datasheet?

    Are all other connections correct?

    but the response I get from the accelerometer  is most of the time just 0x00

    Is that as seen on the oscilloscope?

     and if it does respond it doesn't make sense.

    And is that as seen on the oscilloscope?

    Remember that the accelerometer neither knows nor cares what it's connected to - so long as what it receives is correct, it will respond as specified.

    So either what you're sending is not correct, or the accelerometer is broken...

  • Daniel Tomicek said:
    an accelerometer found on Sparkfun (https://www.sparkfun.com/products/10345).

    https://www.sparkfun.com/products/10345

    :

    Sparkfun said:
     

    Triple Axis Accelerometer Breakout - LIS331
    SEN-10345 RoHS Compliant

    Description: This is a breakout board for the ultra low-power LIS331HH three axis linear accelerometer. The device has a digital I2C/SPI serial interface which makes it ideal for using in embedded applications. All major pins are broken out to 0.1" spaced headers. It also includes two mounting holes as well.

    .

    Small:

    Are you sure it's correctly configured for SPI mode?

     

  • Daniel Tomicek said:

    while (!UCB0TXIFG) ;

    while (!UCB0RXIFG)

    These two tests are no-ops, since they both test (non-zero) constants. You probably want something resembling:

       while (!(IFG2&UCB0TXIFG));

    More generally, the way the loop is constructed you may or may not get Rx overruns, depending on how the code

    compiles. You may want to explicitly drain the SPI before entering the loop.

  • Thanks! This (along with adjusting the clock polarity and putting delays between bytes) seemed to have fixed the problem :)
    Cheers for the help everyone.  

**Attention** This is a public forum