Using the SPI interface to communicate with a sensor

Hi Everyone,

Im trying to get an SCA100T to communicate with my arduino mega with the SPI protocol. for the life of me i cant seem to get it working.

does anyone have any thoughts?

SCA100T DataSheet -->(http://www.muratamems.fi/sites/default/files/documents/sca100t_inclinometer_datasheet_8261800a.pdf)

/* 
get the SPI clock to work.
read the x and y information.

 Circuit:
 sensor attached to pins 50-53:
 
 50:Data IN
 51:Data OUT
 52:Chip select
 53:Serial Clock
  
 Version 1.0 notes:
 Dont know if this will work. the goal is to
 just display X and Y through SPI.
 
 Version 1.1 notes:
 Lots of work... ;-)
 */

// inslude the SPI library:
#include <SPI.h>

/*!!!I have pulled examples from BarometricPressureSensor Example found in File->Examples->SPI
Source: http://arduino.cc/en/Reference/SPI
On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative.
*/
// Set pins
const int dataINPin = 50;     //MISO
const int dataOUTPin = 51;    //MOSI
const int chipSelectPin = 53;//Chip Select Pin
const int serialClockPin = 52;//CSK

//!!!Sets commands according to spec sheet to access memory register addresses
//!!!Commands are 8 bits or one byte
const byte MEAS = B00000000; //Measure mode (normal operation mode after power on)
const byte RWTR = B00001000; //Read and write temperature data register, ONLY WRITE COMMAND, note currently in use
const byte RDSR = B00001010; //Read status register
const byte RLOAD = B00001011; //Reload NV data to memory output register
const byte STX = B00001110; //Activate Self test for X-channel
const byte STY = B00001111; //Activate Self test for Y-channel
const byte RDAX = B00010000; //Read X-channel acceleration through SPI
const byte RDAY = B00010001; //Read Y-channel acceleration through SPI

void setup() 
        {
        Serial.begin(9600);
       
	// initialize SPI:
	SPI.begin();
	SPI.setBitOrder(MSBFIRST);
//!!! Clock Polarity 0, Clock Phase is 1
//!!! See http://en.wikipedia.org/wiki/File:SPI_timing_diagram2.svg
//!!!SCAT datasheet page 11, figure 10
        SPI.setDataMode(1);
        
	// set the input & output:
	pinMode (dataINPin, INPUT);
	pinMode (dataOUTPin, OUTPUT);
	pinMode (chipSelectPin, OUTPUT);
	pinMode (serialClockPin, OUTPUT);

//!!!Let sensor setup
//!!!Allow self-test to initialize on start-up
          delay(500);	
}

void loop() {
		
	// Read from chip?
          
          //!!!assigns integer to store X axis value, BIN tells function that variable is in binary
          int xAxisData = readCommand(RDAX);
          //!!!outputs raw bits to serial monitor screen
          //!!! BIN tells command that variable is in binary and needs to convert to ASCII-encoded
          //!!!Leave this as raw ouput for now however library exists to easily convert this to degrees
          //!!!See sca100 datasheet Page 12, Section 2.5 and library:
          //!!!http://www.nongnu.org/avr-libc/user-manual/group__avr__math.html#ga98384ad60834911ec93ac5ae1af4cf0a
	  Serial.println("X Axis: ");
          Serial.println(xAxisData, BIN);

          //!!!assigns integer to store Y axis value
          int yAxisData = readCommand(RDAY);
          //!!!outputs raw bits to serial monitor screen
          //!!! BIN tells command that variable is in binary and needs to convert to ASCII-encoded
	  Serial.println("Y Axis: ");
	  Serial.println(yAxisData, BIN);

      }
//!!!Read from SCAT:
//Insert Command within readCommand parenthisis
//!!!Commands issued are in word formation, SCA100 page 12, section 2.5 Paragraph 1
word readCommand(byte Command) {
	word result = 0;   // result to return
        //Prints command issued
        String command = String('Command');
	Serial.println("Command: "); 
        //!!!Prints command as ascii string
        Serial.println(command);
	// take the chip select high to select the device:
	digitalWrite(chipSelectPin, LOW);
        //!!!SCAT datasheet specifies a 150 microsecond pause to get correct reading
        //!!!Page 12 of datasheet, Paragraph 5
        delay(15);
        //!!! Send command to slave(sensor):
	SPI.transfer(Command);
        //!!!Reads output by using measure command
        result = SPI.transfer(MEAS);
        //!!!Sets pin back to high
        digitalWrite(chipSelectPin, HIGH);
        

	// take the chip select high to de-select:
	digitalWrite(chipSelectPin, HIGH);
	// return the result:
	return(result);
}

If the comments on your wiring are current, your wiring is wrong. Pin 52 is not SS (chip/slave select) but SCK (clock) and vice versa pin 53 is SS.

Edit: And SPI mode is 0 according to the datasheet: "each bit is sampled on the rising edge of SCK".

oh dear lord. i spent HOURS on this and i wired it wrong. thank you SOO much for taking a look! i'll have to re-wire this up as soon as i get home and let you know if its up and running!

Well, it was a few days but i finaly got it working!! sort of…

my issue now is im getting fluctuations of +/- 8. i have even tried using a power supply connected to the arduino to get everything to level off and that doesnt seem to help. also in my circuit i have hooked up resistors and capacitors as recoomended in the chips manual. does anyone have any ideas on how i could eliminate these reading fluctuations?

/* 
get the SPI clock to work.
read the x and y information.

 Circuit:
 sensor attached to pins 50-53:
 
 50:Data IN
 51:Data OUT
 53:Chip select
 52:Serial Clock

 Version 1.0 notes:
 Dont know if this will work. the goal is to
 just display X and Y through SPI.
 
 Version 1.1 notes:
 Lots of work... ;-)
 
 
 Version 1.1
 SPI is working however results are fluctuating
*/

// inslude the SPI library:
#include <SPI.h>


// Set pins
const int dataINPin = 50;     //MISO
const int dataOUTPin = 51;    //MOSI
const int chipSelectPin = 53;//Chip Select Pin
const int serialClockPin = 52;//CSK

//!!!Sets commands according to spec sheet to access memory register addresses
//!!!Commands are 8 bits or one byte
const byte MEAS  = B00000000; //Measure mode (normal operation mode after power on)
const byte RWTR  = B00001000; //Read and write temperature data register, ONLY WRITE COMMAND, note currently in use
const byte RDSR  = B00001010; //Read status register
const byte RLOAD = B00001011; //Reload NV data to memory output register
const byte STX   = B00001110; //Activate Self test for X-channel
const byte STY   = B00001111; //Activate Self test for Y-channel
const byte RDAX  = B00010000; //Read X-channel acceleration through SPI
const byte RDAY  = B00010001; //Read Y-channel acceleration through SPI

void setup() 
        {
        Serial.begin(9600);
       
	// initialize SPI:
	SPI.begin();
	SPI.setBitOrder(MSBFIRST);
        SPI.setDataMode(0);
        
	// set the input & output:
	pinMode (dataINPin, INPUT);
	pinMode (dataOUTPin, OUTPUT);
	pinMode (chipSelectPin, OUTPUT);
	pinMode (serialClockPin, OUTPUT);

//!!!Allow self-test to initialize on start-up
          delay(500);	
}

void loop() {
		
          //!!!assigns integer to store X axis value, BIN tells function that variable is in binary
          int xAxisData = readCommand(RDAX);

	  Serial.println("X Axis: ");
          Serial.println(xAxisData);

          //!!!assigns integer to store Y axis value
          int yAxisData = readCommand(RDAY);

	  Serial.println("Y Axis: ");
	  Serial.println(yAxisData);

Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');

        delay(1000);
      }
//!!!Read from SCAT:
word readCommand(byte Command) {

        byte inByte = 0;
	word result = 0;   // result to return
	// take the chip select high to select the device:
	digitalWrite(chipSelectPin, LOW);
        //!!!SCAT datasheet specifies a 150 microsecond pause to get correct reading
        //!!!Page 12 of datasheet, Paragraph 5
        delay(20);
        //!!! Send command to slave(sensor):
	SPI.transfer(Command);
        //!!!Reads output by using measure command
        result = SPI.transfer(MEAS);
        // shift the first byte left, then get the second byte:
        result = result << 8;
        inByte = SPI.transfer(MEAS);
        // combine the byte you just got with the previous one:
        result = result | inByte;
        // Result is in 11 bit word format with MSB first to shift ouput to 11 important bits
        result = result >> 5;
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  // return the result:
  return(result);
}
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');Serial.print('\n');
Serial.print('\n');Serial.print('\n');

What is this about? Haven't you heard of Serial.println()? Haven't you heard of for loops?

your right, i was getting pretty frustrated and a little sloppy with coding 'best practices'. i have updates that to be much cleaner. do you think that would have cause such a wild reading fluctuation though?

i posted a new topic over in the project help forum as it seems like it is a better place to be talking about how to reduce reading fluctuations. thanks for the help everyone!!!!!!

http://arduino.cc/forum/index.php/topic,112738.0.html