Hi, I wrote a Class for my Honeywell HSS Pressure transducer.
Everything works fine when I read only one. If I want to use this class with a second Sensor, all my values are rubbish. Is there a problem with my SPI Port? Am I not terminating it correctly?
#include "HSS.h"
#include "SPI.h"
HSS absolutePress = HSS(2);
//HSS relativePress = HSS(3);
void setup() {
Serial.begin(9600);
}
void loop()
{
delay(1000);
float val1 = (1600 * ((float)absolutePress.getPressure() - 1638) / 13107);
Serial.print("PRESS1 = "); Serial.println(val1);
Serial.print("Temp1 = "); Serial.println(absolutePress.getTemperature());
//float val2 = (40 * ((float)relativePress.getPressure() - 1638) / 4369) - 60;
//Serial.print("PRESS2 = "); Serial.println(val2);
//Serial.print("Temp2 = "); Serial.println(relativePress.getTemperature());
}
#include "HSS.h"
#include "Arduino.h"
#include "SPI.h"
//Define Communication Settings
#define baudrate 500000
#define BitFirst MSBFIRST
#define SPIMode SPI_MODE3
#define DEBUG 0
uint8_t ChipselectPin = 0;
float temperature = 0;
int16_t pressure = 0;
HSS::HSS(uint8_t ChipSelect) {
ChipselectPin = ChipSelect;
pinMode(ChipselectPin, OUTPUT);
digitalWrite(ChipselectPin, HIGH);
}
float HSS::getTemperature() {
readSensor();
return temperature;
}
uint16_t HSS::getPressure() {
readSensor();
return pressure;
}
void HSS::readSensor() {
SPI.begin();
digitalWrite(ChipselectPin, LOW); //pull Chipselect Pin to Low
SPI.beginTransaction(SPISettings(baudrate, BitFirst, SPIMode)); // Set to 800kHz, MSB and MODE1
int inByte_1 = SPI.transfer(0x00); // Read first Byte of Pressure
int inByte_2 = SPI.transfer(0x00); // Read second Byte of Pressure
int inByte_3 = SPI.transfer(0x00); // Read first Byte of Temperature
int inByte_4 = SPI.transfer(0x00); // Read second Byte of Temperature
if (DEBUG) {
Serial.print("Chipselect = "); Serial.print(ChipselectPin, DEC); Serial.print(" ");
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(" ");
}
SPI.endTransaction(); //end SPI Transaction
SPI.end();
digitalWrite(ChipselectPin, HIGH); //pull Chipselect Pin to High
pressure = inByte_1 << 8 | inByte_2;
inByte_3 = inByte_3 << 3 | inByte_4; //Shift first Temperature byte 3 left
temperature = ((float)inByte_3 * 200 / 2047) - 50; //Convert Digital value to °C
if (DEBUG) {
Serial.print("Temp[C]= "); Serial.print(temperature); Serial.print(" ");
}
}
#include "Arduino.h"
class HSS
{
public:
HSS(uint8_t ChipSelect);
float getTemperature();
uint16_t getPressure();
void readSensor();
};
Also, how can I implement the SPI library without importing it to the main sketch? Is this possible?
Thanks!