-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHP20x_dev.cpp
250 lines (217 loc) · 5.96 KB
/
HP20x_dev.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
File name : HP20x_dev.cpp
Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C]
Author : Oliver Wang from Seeed studio
Version : V0.1
Create Time: 2014/04
Change Log :
*/
/****************************************************************************/
/*** Include files ***/
/****************************************************************************/
#include "HP20x_dev.h"
#include <Wire.h>
#include <Arduino.h>
#include "KalmanFilter.h"
/****************************************************************************/
/*** Local Variable ***/
/****************************************************************************/
HP20x_dev HP20x;
/****************************************************************************/
/*** Class member Functions ***/
/****************************************************************************/
/*
**@ Function name: HP20x_dev
**@ Description: Constructor
**@ Input: none
**@ OutPut: none
**@ Retval: none
*/
HP20x_dev::HP20x_dev() {
OSR_CFG = HP20X_CONVERT_OSR1024;
OSR_ConvertTime = 25;
}
/*
**@ Function name: begin
**@ Description: Initialize HP20x_dev
**@ Input: none
**@ OutPut: none
**@ Retval: none
*/
void HP20x_dev::begin() {
Wire.begin();
/* Reset HP20x_dev */
HP20x.HP20X_IIC_WriteCmd(HP20X_SOFT_RST);
HP20x.HP20X_EnableCompensate();
}
/*
**@ Function name: ReadTemperature
**@ Description: Read Temperature from HP20x_dev
**@ Input:
**@ OutPut:
**@ Retval:
*/
ulong HP20x_dev::ReadTemperature(void) {
HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD | OSR_CFG); //ADC convert
delay(OSR_ConvertTime); //difference OSR_CFG will be difference OSR_ConvertTime
HP20X_IIC_WriteCmd(HP20X_READ_T);
ulong Temperature = HP20X_IIC_ReadData();
return Temperature;
}
/*
**@ Function name: ReadPressure
**@ Description: Read Pressure value
**@ Input:
**@ OutPut:
**@ Retval: value
*/
ulong HP20x_dev::ReadPressure(void) {
HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD | OSR_CFG);
delay(OSR_ConvertTime);
HP20X_IIC_WriteCmd(HP20X_READ_P);
ulong Pressure = HP20X_IIC_ReadData();
return Pressure;
}
/*
**@ Function name: ReadAltitude
**@ Description: Read Pressure value
**@ Input:
**@ OutPut:
**@ Retval: value
*/
ulong HP20x_dev::ReadAltitude(void) {
HP20X_IIC_WriteCmd(HP20X_READ_A);
ulong Altitude = HP20X_IIC_ReadData();
return Altitude;
}
/*
void ReadPressureAndTemperature(void)
{
HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG);
Timer_Delayxms(OSR_ConvertTime*2);
HP20X_IIC_WriteCmd(HP20X_READ_PT);
Temperature=HP20X_IIC_ReadData();
Pressure=HP20X_IIC_ReadData3byte();
}
void IIC_ReadAltitudeAndTemperature(void)
{
HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG);
Timer_Delayxms(OSR_ConvertTime*2);
HP20X_IIC_WriteCmd(HP20X_READ_AT);
Temperature=HP20X_IIC_ReadData();
IIC_ACK();
Altitude=HP20X_IIC_ReadData3byte();
IIC_NoAck();
IIC_Stop();
}*/
/****************************************************************************/
/*** Local Functions ***/
/****************************************************************************/
/*
**@ Function name: HP20X_IIC_WriteCmd
**@ Description:
**@ Input:
**@ OutPut:
**@ Retval:
*/
void HP20x_dev::HP20X_IIC_WriteCmd(uchar uCmd) {
/* Port to arduino */
Wire.beginTransmission(HP20X_I2C_DEV_ID);
Wire.write(uCmd);
Wire.endTransmission();
}
/*
**@ Function name: HP20X_IIC_ReadReg
**@ Description:
**@ Input:
**@ OutPut:
**@ Retval:
*/
uchar HP20x_dev::HP20X_IIC_ReadReg(uchar bReg) {
/* Port to arduino */
uchar Temp = 0;
/* Send a register reading command */
HP20X_IIC_WriteCmd(bReg | HP20X_RD_REG_MODE);
Wire.requestFrom(HP20X_I2C_DEV_ID, 1);
// while(Wire.available())
// {
// Temp = Wire.read();
// }
Temp = Wire.read();
return Temp;
}
/*
**@ Function name: HP20X_IIC_WriteReg
**@ Description:
**@ Input:
**@ OutPut:
**@ Retval:
*/
void HP20x_dev::HP20X_IIC_WriteReg(uchar bReg, uchar bData) {
Wire.beginTransmission(HP20X_I2C_DEV_ID);
Wire.write(bReg | HP20X_WR_REG_MODE);
Wire.write(bData);
Wire.endTransmission();
}
/*
**@ Function name: HP20X_IIC_ReadData
**@ Description:
**@ Input:
**@ OutPut:
**@ Retval:
*/
ulong HP20x_dev::HP20X_IIC_ReadData(void) {
/* Port to arduino */
ulong Temp = HP20X_IIC_ReadData3byte();
return Temp;
}
/*
**@ Function name: HP20X_IIC_ReadData3byte
**@ Description:
**@ Input:
**@ OutPut:
**@ Retval:
*/
ulong HP20x_dev::HP20X_IIC_ReadData3byte(void) {
ulong TempData = 0;
ulong tmpArray[3] = {0};
int cnt = 0;
/* Require three bytes from slave */
Wire.requestFrom(HP20X_I2C_DEV_ID, 3);
while (Wire.available()) { // slave may send less than requested
uchar c = Wire.read(); // receive a byte as character
tmpArray[cnt] = (ulong)c;
cnt++;
}
/* MSB */
TempData = tmpArray[0] << 16 | tmpArray[1] << 8 | tmpArray[2];
if (TempData & 0x800000) {
TempData |= 0xff000000;
}
/* // 24 bit to 32 bit
if(TempData&0x800000)
{
// 1:minus
TempData |= 0x80000000;
TempData &= 0xff7fffff;
}
else
{
// 0:plus
//do noting
} */
return TempData;
}
/**
@brief Enable Compensation by set CMPS_EN bit on 0x0F PARA register
*/
void HP20x_dev::HP20X_EnableCompensate(void) {
HP20X_IIC_WriteReg(REG_PARA, OK_HP20X_DEV);
}
/**
@brief Disable Compensation by clear CMPS_EN bit on 0x0F PARA register
*/
void HP20x_dev::HP20X_DisableCompensate(void) {
HP20X_IIC_WriteReg(REG_PARA, 0);
}
/**************************************END OF FILE**************************************/