AnsweredAssumed Answered

Help reading ADXL312

Question asked by scotty2541 on Dec 18, 2012
Latest reply on Jan 14, 2013 by MattHazel

I am trying to read an ADXL312, and get values of 0x01ff for all the axis.

An am setting register value, and reading them back, and the match, I know the communication is working.

I also don't get anything if the measure bit is turned off.  So there is no problem reading the chip.  Just getting actual values from the measurements.

 

My C code is shown below.  The CS line is tied to RA6. 

It shows how I initialize the chip, and read 0x31-0x37 in a loop.

Any advice is appreciated.

 

PORTAbits.RA6 = 0;

WriteSPI(0x2d);                    // Turn off measure

WriteSPI(0x00);

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

PORTAbits.RA6 = 0;

WriteSPI(0x2c);

WriteSPI(0x06);          // Set slow sampling

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

PORTAbits.RA6 = 0;

WriteSPI(0x2e);          // Sett defaults

WriteSPI(0x00);

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

PORTAbits.RA6 = 0;

WriteSPI(0x31);

WriteSPI(0x00);

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

PORTAbits.RA6 = 0;

WriteSPI(0x1E | MULTIBYTE);

WriteSPI(0x00);

WriteSPI(0x00);

WriteSPI(0x00);

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

 

PORTAbits.RA6 = 0;

WriteSPI(0x24 | MULTIBYTE);

WriteSPI(0x00);

WriteSPI(0x00);

WriteSPI(0x00);

WriteSPI(0x00);

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

PORTAbits.RA6 = 0;

WriteSPI(0x38 | MULTIBYTE);

WriteSPI(0x00);

WriteSPI(0x00);

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

PORTAbits.RA6 = 0;

WriteSPI(0x2d);

WriteSPI(0x08);                    // turn on measure

PORTAbits.RA6 = 1;

Delay1KTCYx(100);

 

do {

     PORTAbits.RA6 = 0;

     WriteSPI(0x2c | READ | MULTIBYTE);

     r2c = ReadSPI( );

     r2d = ReadSPI( );

     r2e = ReadSPI( );

     r2f = ReadSPI( );

     r30 = ReadSPI( );

     r31 = ReadSPI( );          // Read 0x31

     PORTAbits.RA6 = 1;

     if ((r30 & 0x80) == 0) {

               Delay1KTCYx(50);

               continue;

     }

     PORTAbits.RA6 = 0;

     WriteSPI(0x32 | READ | MULTIBYTE);

     xlow = ReadSPI( );          //  Read 0x32

     xhi = ReadSPI( );          //  Read 0x33

     ylow = ReadSPI( );          //  Read 0x34

     yhi = ReadSPI( );          //  Read 0x35

     zlow = ReadSPI( );          //  Read 0x36

     zhi = ReadSPI( );          //  Read 0x37

     PORTAbits.RA6 = 1;

 

etc....

Outcomes