Dear @jaski.tx,
thank you very much for your support.
On the Arduino the result are:
byte[0] 10 byte[1] 200 the pressure value is 1,01325 atm
On the VF61 I obtain
byte[0] 8 byte[1] 17 the pressure is about 0,2368616 atm
The Arduino code is
MySensor sensor();
void setup() {
Serial.begin(115200); // start Serial communication
SPI.begin(); // start SPI communication
sensor.begin(); // run sensor initialization
}
void loop() {
// the sensor returns 0 when new data is ready
sensor.readSensor();
delay( 100 ); // Slow down sampling to 10 Hz. This is just a test.
}
where
void init()
{
_spi_settings = SPISettings(800000, MSBFIRST, SPI_MODE0);
pinMode(_SS_PIN, OUTPUT); // set PIN --> output
digitalWrite(_SS_PIN, HIGH); //set PIN --> 1
}
uint8_t readSensor()
{
uint8_t count = 4; // transfer 4 bytes (the last two are only used by some sensors)
memset(_buf, 0x00, count); // probably not necessary, sensor is half-duplex
SPI.beginTransaction(_spi_settings);
digitalWrite(_SS_PIN, LOW); // set PIN 0
SPI.transfer(_buf, count);
digitalWrite(_SS_PIN, HIGH); // set PIN 1
SPI.endTransaction();
}
My Code is:
int main(int argc, char *argv[])
{
int ret =0;
PressureSensor absolutePressure = PressureSensor();
sleep(0.5);
absolutePressure.Init();
sleep(0.5);
for(int i=0; i< 100; i++)
{
absolutePressure.ReadSensor();
sleep(0.5);
}
absolutePressure.Close();
return ret;
}
/* Constructor*/
PressureSensor::PressureSensor() :
_fd(-1), // int _fd;
_mode(SPI_MODE_0),
_speed(800000),
_device("/dev/spidev1.1"),
{
std::cout<<" fd "<< _fd<< " mode "<<_mode<<"\n"
<<" speed "<< _speed<< "_device"<<"\n"
<< _device <<"\n";
}
/*Init */
void PressureSensor::Init()
{
int ret = 0;
int msb =0;
int bits =8;
_fd = open(_device, O_RDONLY);
if (_fd < 0)
pabort("can't open device");
ret = ioctl(_fd, SPI_IOC_RD_MODE32, &_mode);
if (ret == -1)
pabort("can't get spi mode");
ret = ioctl(_fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
ret = ioctl(_fd, SPI_IOC_RD_LSB_FIRST, &msb);
if (ret == -1)
pabort("can't set MSB per word");
ret = ioctl(_fd, SPI_IOC_RD_MAX_SPEED_HZ, &_speed);
if (ret == -1)
pabort("can't get max speed hz");
printf("spi mode: 0x%x\n", _mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n", _speed, _speed/1000);
}
/* ReadSensor */
uint8_t PressureSensor::ReadSensor()
{
uint8_t count = 4; // transfer 4 bytes (the last two are only used by some sensors)
memset(_buf, 0x00, count); // probably not necessary, sensor is half-duplex
struct spi_ioc_transfer tr;
tr.rx_buf =(unsigned long)_buf; // where _ uint8_t _buf[4];
tr.len = 4;
tr.delay_usecs = 200;
tr.speed_hz = _speed;
tr.bits_per_word = 8;
int ret = ioctl(_fd, SPI_IOC_MESSAGE(1), &tr);
if (ret == -1)
pabort("can't send spi message");
else {
printf("Device Value: %d %d - %d - %d\n",_buf[0], _buf[1], _buf[2], _buf[3]);
// if device is normal and there is new data, bitmask and save the raw data
}
return _status;
}
I think that the 2 codes are equivalent, or am I wrong?
Best regards