cancel
Showing results for 
Search instead for 
Did you mean: 

LSM303C communication to microcontroller via 3-wire SPI

pljushev
Associate
Posted on June 19, 2015 at 09:09

I am interfacing LSM303C magnetometer and accelerometer to microcontroller via 3-wire SPI, where SIMO and SOMI from uC are connected together via R 10k to allow the slave LSM303C to drive this single signal line when responding.

Clock SCK polarity is set to high and the right phase for LSM303C is selected in microcontroller USART configuration registers. I am actively pulling down CS pins on LSM303C to enable SPI communication with the particular part. Measuring with oscilloscope on the shared SDI/O line, SCK and CS it seems that LSM303C is receiving the right input according to datasheet.

However I didn't have success to get a reply from LSM303C on simple ''WHO_AM_I_A/M'' address readings - LSM303C does not pull the communication line. I have tried enabling the SPI operation, disabling I2C, turning to continuous operation and enabling reading of registers in the control registers of accelerometer and magnetometer. MSB of the register address is changed to ''1'' to enable reading of the Who_am_I_ID, but doesn't seem to have an effect.

Has anybody here in the forum had success communicating with LSM303C via 3-wire SPI and can share the initialization code (setting up 3-wire SPI operation in control registers, enabling continuous operation, reading a single data register etc)?

#3-wire-spi #lsm303c #lsm303c-spi-accelerometer
2 REPLIES 2
brent239955_stm1
Associate
Posted on July 02, 2015 at 17:48

I'm having more luck than you with my code.  I still have a bug I'd love help with.  I can read the accelerometer, magnetometer, and temperature over I2C & 'SPI'.  On the 'SPI' interface only, I eventually get garbage after dozens or hundreds of valid readings.  My µC starts getting raw values like (-1323, -1231, -1012) which don't really change when I apply accelerations.

I'd love to hear why my setup runs perfect over I2C, but only runs for 3 or 4 minutes before the accelerometer stops giving valid data.

Here are some code snippets that might help you...

    // Setup pins for SPI

    // CS & CLK must be outputs DDRxn = 1

    bitSet(DIR_REG, CSBIT_MAG);

    bitSet(DIR_REG, CSBIT_XL);

    bitSet(DIR_REG, CLKBIT);

    // Deselect SPI chips

    bitSet(CSPORT_MAG, CSBIT_MAG);

    bitSet(CSPORT_XL, CSBIT_XL);

    // Clock polarity (CPOL) = 1

    bitSet(CLKPORT, CLKBIT);

    // SPI Serial Interface Mode (SIM) bits must be set

    SPI_WriteByte(ACC, ACC_CTRL4, 0b111);

    SPI_WriteByte(MAG, MAG_CTRL_REG3, _BV(2));

  ////////// Initialize Magnetometer //////////

  // Initialize magnetometer output data rate

  MAG_SetODR(MAG_DO_40_Hz);

  // Initialize magnetic field full scale

  MAG_SetFullScale(MAG_FS_16_Ga);

  // Enabling block data updating

  MAG_BlockDataUpdate(MAG_BDU_ENABLE);

  // Initialize magnetometer X/Y axes ouput data rate

  MAG_XY_AxOperativeMode(MAG_OMXY_HIGH_PERFORMANCE);

  // Initialize magnetometer Z axis performance mode

  MAG_Z_AxOperativeMode(MAG_OMZ_HIGH_PERFORMANCE);

  // Initialize magnetometer run mode.

  MAG_SetMode(MAG_MD_CONTINUOUS);

  ////////// Initialize Accelerometer //////////

  // Initialize acceleration full scale

  ACC_SetFullScale(ACC_FS_2g);

  // Enable block data updating

  ACC_BlockDataUpdate(ACC_BDU_ENABLE);

  // Enable X, Y, and Z accelerometer axes

  ACC_EnableAxis(ACC_X_ENABLE|ACC_Y_ENABLE|ACC_Z_ENABLE);

  // Initialize accelerometer output data rate

  ACC_SetODR(ACC_ODR_100_Hz);

- Brent

Posted on March 30, 2018 at 10:00

Hi Brent,

Did you manage to solve the garbage data issue? I face the same issue, any fixes?