#include "mbed.h"
#define OUT_MAX 16384
#define OUT_MIN 0
#define PRES_MAX 5
#define PRES_MIN 0
SPI spi(p11, p12, p13); // mosi - no connection, miso - connected to pin 3 of sensor, sclk connected to pin 4 of sensor
DigitalOut cs(p8); // chip Select line connected to pin 5 of sensor
Serial pc(p9,p10); // tx, rx
int main() {
// Setup the spi for 8 bit data with a 800KHz clock rate
spi.format(8,0);
spi.frequency(500000);
while(1)
{
// Select the device by seting chip select low
cs = 0;
wait_us(8);
// Send a dummy byte to receive the contents
int byte_1 = spi.write(0x00);
int byte_2 = spi.write(0x00);
int byte_3 = spi.write(0x00);
int byte_4 = spi.write(0x00);
float temp = byte_3<<3;
temp = (temp*200/2047)-50;
float psi = byte_1<<8|byte_2;
psi = ((psi-OUT_MIN)*PRES_MAX)/(OUT_MAX-OUT_MIN);
pc.printf("Byte 1 = %X; Byte 2 = %X; Byte 3 = %X; Byte 4 = %X; PSI = %.4f; TEMP = %.2f:\r",byte_1,byte_2,byte_3,byte_4,psi,temp);
// Deselect the device
cs = 1;
wait(1);
}
}
#include <SPI.h>
// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
const int dataReadyPin = 6;
const int chipSelectPin = 7;
void setup() {
Serial.begin(9600);
// start the SPI library:
SPI.begin();
SPI.setClockDivider(128);
// initalize the data ready and chip select pins:
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin,HIGH);
delay(100);
}
void loop()
{
digitalWrite(chipSelectPin,LOW);
delay(10);
int inByte_1 = SPI.transfer(0x00);
int inByte_2 = SPI.transfer(0x00);
int inByte_3 = SPI.transfer(0x00);
int inByte_4 = SPI.transfer(0x00);
Serial.print("Byte_1 = ");Serial.print(inByte_1,DEC);Serial.print(" ");
Serial.print("Byte_2 = ");Serial.print(inByte_2,DEC);Serial.print(" ");
Serial.print("Byte_3 = ");Serial.print(inByte_3,DEC);Serial.print(" ");
Serial.print("Byte_4 = ");Serial.print(inByte_4,DEC);Serial.print(" ");
float psi = inByte_1<<8|inByte_2;
psi = (psi*5)/16384;
Serial.print("PRES = ");Serial.print(psi);Serial.print(" ");
inByte_3 = inByte_3<<3;
float realTemp = ((float)inByte_3*200/2047)-50;
Serial.print("Temp[C]= ");Serial.print(realTemp);Serial.print("\n");
digitalWrite(chipSelectPin,HIGH);
delay(1000);
}
Note: all numbers are in Decimal notation.