Hello,
I wrote a sketch for a pressure sensor using SPI protocol, and I want to read the pressure from it. However it keeps sending me back the command code(e.g. 10000011). I have no clue what's wrong with my code. Does some one know whats going on? Thanks.
The datasheet is here
#include <SPI.h> // include the SPI library
const int cs = 10;
const byte sample = 0b10000011;
const byte resend = 0b01010000;
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(5000000,MSBFIRST,SPI_MODE0));
uint8_t MSBbyte, LSBbyte;
digitalWrite(cs,LOW);
SPI.transfer(sample);
SPI.transfer(sample);
digitalWrite(cs,HIGH);
delayMicroseconds(50);
SPI.transfer(0x0001);
digitalWrite(cs,LOW);
MSBbyte = SPI.transfer(resend);
LSBbyte = SPI.transfer(resend);
digitalWrite(cs,HIGH);
MSBbyte = MSBbyte>> 1;
LSBbyte = LSBbyte & 0xF8; // mask off 3 lower bits
word rawdata = word(MSBbyte,LSBbyte); // use type cast to build word from two bytes
unsigned int pressure =(rawdata) >> 3;
Serial.println(MSBbyte,BIN);
Serial.println(LSBbyte,BIN);
Serial.println(pressure);
Serial.println(" ");
delay(300);
}