This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhsc_ssc_i2c.cpp
100 lines (81 loc) · 3.48 KB
/
hsc_ssc_i2c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
TruStability HSC and SSC pressure sensor library for the Arduino.
This library implements the following features:
- read raw pressure and temperature count values
- compute absolute pressure and temperature
Author: Petre Rodan <petre.rodan@simplex.ro>
Available from: https://github.com/rodan/honeywell_hsc_ssc_i2c
License: GNU GPLv3
Honeywell High Accuracy Ceramic (HSC) and Standard Accuracy Ceramic
(SSC) Series are piezoresistive silicon pressure sensors.
GNU GPLv3 license:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Wire.h>
#include "hsc_ssc_i2c.h"
/* you must define the slave address. you can find it based on the part number:
_SC_________XA_
where X can be one of:
S - spi (this is not the right library for spi opperation)
2 - i2c slave address 0x28
3 - i2c slave address 0x38
4 - i2c slave address 0x48
5 - i2c slave address 0x58
6 - i2c slave address 0x68
7 - i2c slave address 0x78
*/
/// function that requests raw data from the sensor via i2c
///
/// input
/// slave_addr - i2c slave addr of the sensor chip
/// output
/// raw - struct containing 4 bytes of read data
/// returns
/// 0 if all is fine
/// 1 if chip is in command mode
/// 2 if old data is being read
/// 3 if a diagnostic fault is triggered in the chip
/// 4 if the sensor is not hooked up
uint8_t ps_get_raw(const uint8_t slave_addr, struct cs_raw *raw)
{
uint8_t i, val[4] = { 0, 0, 0, 0 };
Wire.requestFrom(slave_addr, (uint8_t) 4);
for (i = 0; i <= 3; i++) {
delay(4); // sensor might be missing, do not block
val[i] = Wire.read(); // by using Wire.available()
}
raw->status = (val[0] & 0xc0) >> 6; // first 2 bits from first byte
raw->bridge_data = ((val[0] & 0x3f) << 8) + val[1];
raw->temperature_data = ((val[2] << 8) + (val[3] & 0xe0)) >> 5;
if ( raw->temperature_data == 65535 ) return 4;
return raw->status;
}
/// function that converts raw data read from the sensor into temperature and pressure values
///
/// input:
/// raw - struct containing all 4 bytes read from the sensor
/// output_min - output at minimal calibrated pressure (counts)
/// output_max - output at maximum calibrated pressure (counts)
/// pressure_min - minimal value of pressure range
/// pressure_max - maxium value of pressure range
///
/// output:
/// pressure
/// temperature
uint8_t ps_convert(const struct cs_raw raw, float *pressure, float *temperature,
const uint16_t output_min, const uint16_t output_max, const float pressure_min,
const float pressure_max)
{
*pressure = 1.0 * (raw.bridge_data - output_min) * (pressure_max - pressure_min) / (output_max - output_min) + pressure_min;
*temperature = (raw.temperature_data * 0.0977) - 50;
return 0;
}