SPI pressure sensor

Hi, I am trying to program for a pressure sensor which uses the SPI protocol. However, it seems like that my code doesn't run properly. Can some one help me with this? I just want to measure the fluid pressure--Pdc. I appreciate that a lot!!!

Here is the datasheet

#include <SPI.h> // include the SPI library
const int cs = 10;
const byte sample = 10000011;
const byte resend = 01010000;

void setup(){
  pinMode(cs,OUTPUT);
  Serial.begin(9600);//initialize the serial monitor 
  Serial.println("Initializing the force monitor");
  delay(50);//give the numatac some time to setup
  SPI.begin();//begin SPI trasaction
  digitalWrite(cs,HIGH);
  }


void loop(){
  //Set up SPI bus
  //set the clock speed to be 5 Mhz, most significant byte first, and data trasfer mode 0
  SPI.beginTransaction(SPISettings(4400000,MSBFIRST,SPI_MODE0));
    uint8_t MSBbyte, LSBbyte;
    digitalWrite(cs,LOW);
    SPI.transfer(sample);
    SPI.transfer(sample);
    digitalWrite(cs,HIGH);
    delayMicroseconds(50);
    digitalWrite(cs,LOW);
    MSBbyte = SPI.transfer(resend);
    LSBbyte = SPI.transfer(resend);
    SPI.endTransaction();
    digitalWrite(cs,HIGH);

    MSBbyte = unsigned(MSBbyte)>> 1;
    LSBbyte = unsigned(LSBbyte)>> 3;
    LSBbyte <<= 3;

   unsigned int rawdata = MSBbyte*256 + LSBbyte;
   unsigned int pressure = unsigned(rawdata) >> 3;
    Serial.println(pressure);

Kevin-zheng:
Hi, I am trying to program for a pressure sensor which uses the SPI protocol. However, it seems like that my code doesn't run properly. Can some one help me with this? I just want to measure the fluid pressure--Pdc. I appreciate that a lot!!!

Here is the datasheet
SynTouch

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

const int cs = 10;
const byte sample = 10000011;
const byte resend = 01010000;

void setup(){
 pinMode(cs,OUTPUT);
 Serial.begin(9600);//initialize the serial monitor
 Serial.println("Initializing the force monitor");
 delay(50);//give the numatac some time to setup
 SPI.begin();//begin SPI trasaction
 digitalWrite(cs,HIGH);
 }

void loop(){
 //Set up SPI bus
 //set the clock speed to be 5 Mhz, most significant byte first, and data trasfer mode 0
 SPI.beginTransaction(SPISettings(4400000,MSBFIRST,SPI_MODE0));
   uint8_t MSBbyte, LSBbyte;
   digitalWrite(cs,LOW);
   SPI.transfer(sample);
   SPI.transfer(sample);
   digitalWrite(cs,HIGH);
   delayMicroseconds(50);
   digitalWrite(cs,LOW);
   MSBbyte = SPI.transfer(resend);
   LSBbyte = SPI.transfer(resend);
   SPI.endTransaction();
   digitalWrite(cs,HIGH);

MSBbyte = unsigned(MSBbyte)>> 1;
   LSBbyte = unsigned(LSBbyte)>> 3;
   LSBbyte <<= 3;

unsigned int rawdata = MSBbyte*256 + LSBbyte;
  unsigned int pressure = unsigned(rawdata) >> 3;
   Serial.println(pressure);

your byte Constants are incorrect. I assume these ones and zeros are binary values?

const byte sample = 10000011; // this is trying to put 10,000,011 into a byte 
const byte resend = 01010000;
// should actually be
const byte sample = B10000011;  // this actually sets sample to 131 decimal
const byte resend = B01010000; // 80 decimal

I would change this code also:

   LSBbyte = unsigned(LSBbyte)>> 3;
    LSBbyte <<= 3;

   unsigned int rawdata = MSBbyte*256 + LSBbyte;
  
// to this
  LSBbyte = LSBbyte & 0xF8; // mask off 3 lower bits
 
  word rawdata = word(MSBbyte,LSBbyte); // use type cast to build word from two bytes

It just makes it easier to read.

Chuck.

Hi Chuck, thanks for your reply. Yup, those zeros and ones are binary codes. I tried your code, but I got a new problem. The sensor keeps returning the commands that I sent, which means it keeps returning me
10000011 10000011. I have no clue. Do you have any idea what's wrong with this?

Reading thru that spec sheet, they claim to use odd parity, but their example on page 14 is even parity for the ReSend? Odd parity for Set/Write and Read?

Try this for the SPI sequence:

  SPI.beginTransaction(SPISettings(4400000,MSBFIRST,SPI_MODE0));
    
  uint8_t MSBbyte, LSBbyte;
    
  digitalWrite(cs,LOW);
  SPI.transfer(sample);
  SPI.transfer(sample);
  digitalWrite(cs,HIGH);
  delayMicroseconds(50);

  digitalWrite(cs,LOW);
  MSBbyte = SPI.transfer(0x01); // from the Spec sheet, the read command does not want a new cmd
  LSBbyte = SPI.transfer(0x01); // just clock translations
  digitalWrite(cs,HIGH);

  SPI.endTransaction();

  if((MSBbyte ==B10100101)&&(LSBbyte==B00101101)){ // Insuffient delay error
     Serial.println(" sampling too Fast, Increase Delay");
     }
  else if((MSBbyte ==B10100101)&&(LSBbyte==B01011000)){ // unknown channel
    Serial.println(" Unknown Channel? ");
    }
  else { 
    MSBbyte = MSBbyte >> 1;
    word w = word(MSBbyte,LSBbyte); 
    w = w >> 3;
    Serial.print(" Sample = ");
    Serial.println(w,DEC);
    }

That is one Weird Spec Sheet.
Chuck.

Updated 05/28, Instead of using 0x0 as the outbound byte for the SPI read operation use 0x01, (odd parity for zero). There is a note in the spec sheet on page 14 "While listening to the response from the NumaTrac the host should write 0x0001 to the MOSI lines to avoid errors."

Maybe this is what they mean?

send 2 byte read cmd,
wait 50micro seconds
send 0x01 twice to stimulate the data output.