Hi everybody, I spent a lot of time trying to figure out what I did wrong with the SPI connection on the Arduino DUE. Its to measure pressure from a SSC sensor from Honeywell. When I read data it only gives me 255 (first_byte and second_byte). Attached is a picture of the connection and below is the code. I hope you can help me understand and I thank you in advance.
#include <SPI.h>
#define SPI_MOSI 11
#define SPI_MISO 12
#define SPI_SCK 13
#define BAUD 115200
// changing constants
//unsigned long previousMillis = 0; // will store last time pressure was updated
int P; // will store sensor value
byte sys; // initial 2 bits from byte 1 sending pass/stale info
int rest; // resting bit from byte 1 during reading and reconstructing bits
byte bytes; // will store both bytes
uint8_t first_byte; // will store byte string 1
uint8_t second_byte; // will store byte string 2
float pressure_final; // pressure measured after calculations
float auto_zero; // store Auto Zero value of known reference pressure
// unchanging constants
//const long interval = 1000; // interval at which to measure (milliseconds)
const long clock_speed = 800000; // SPI clock speed
const int SPI_SS = 10;
int pressure_reference = 0; // Reference pressure for auto zero
int pressure_min = 0; // Minimum pressure in mbar
int pressure_max = 100; // Maximum pressure in mbar
int raw_min = 1638; // Minimum digital count
int raw_max = 14745; // Maximum digital count
SPISettings mySetting(clock_speed, MSBFIRST, SPI_MODE1);
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD); // opens serial port, sets data rate to 115200 bps
delay(100);
pinMode(SPI_SS, OUTPUT); // set SS output
//pinMode(SPI_MISO, INPUT); // set miso input
//pinMode(SPI_SCK, OUTPUT); // set sck output
digitalWrite(SPI_SS, HIGH); // close SPI connection
// start the SPI library:
SPI.begin();
delay(500);
// Auto Zero calibration
Serial.print("Calibrating ... ");
auto_zero_calibration(100, pressure_reference);
Serial.println(" ... Done calibrating succesfully.");
}
void loop() {
// put your main code here, to run repeatedly:
//unsigned long currentMillis = millis();
//if (currentMillis - previousMillis >= interval) {
// save the last time you measured
// previousMillis = currentMillis;
// Read sensor
read_pressure();
pressure_final = pressure_final - auto_zero;
/*
// Print bits value
Serial.print("received 1st byte: ");
Serial.print(first_byte);
Serial.print(" received 2nd byte: ");
Serial.print(second_byte);
Serial.print(" modified 1st byte: ");
Serial.print(rest);
Serial.print(" total in bits: ");
Serial.print(P);
Serial.print(" System error: ");
Serial.println(sys);
// Print pressure value
Serial.print("Calculated pressure in Pa: ");
Serial.println(pressure_final);
*/
delay(500);
}
void read_pressure()
{
/*
// Read and reconstruct bits
bytes = SPI.transfer(0x00); //0x00 is a dummy;
sys = (bytes >> 6) & 3 ;
P = first_byte & 63; //16383 ;
*/
//start the communication with SPI
SPI.beginTransaction(mySetting);
// initalize the data ready and chip select pins:
digitalWrite(SPI_SS, LOW);
// Read and reconstruct bits
first_byte = SPI.transfer(0); //0x00 is a dummy;
second_byte = SPI.transfer(0);
Serial.print("received 1st byte: ");
Serial.print(first_byte);
Serial.print(" received 2nd byte: ");
Serial.println(second_byte);
sys = (first_byte >> 6) & 3 ;
rest = first_byte & 63 ;
rest = rest << 8;
P = rest | second_byte;
// initalize the data ready and chip select pins:
digitalWrite(SPI_SS, HIGH);
SPI.endTransaction();
// Calculate pressure
pressure_final = (float(P - raw_min) * (pressure_max - pressure_min) / (raw_max - raw_min) + pressure_min) * 100;
}
void auto_zero_calibration(int sample_size, int reference){
float average=0.0;
float total=0.0;
for (int i = 0; i < sample_size; i++){ // loop to the number of samples to average
read_pressure(); // measure current pressure
total = total + pressure_final; // keep a total of the pressure readings
delay(50);
}
average = total / sample_size; // calculate the average pressure reading
auto_zero = average - reference; // calculate the AutoZero value as the average value - reference value
}
SPI Comms Digital Ouptu Pressure Sensors_TN_008202-3-EN_Final_30May12.pdf (210 KB)