-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDFRobot_LIS2DH12.cpp
More file actions
135 lines (120 loc) · 3.69 KB
/
DFRobot_LIS2DH12.cpp
File metadata and controls
135 lines (120 loc) · 3.69 KB
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
/*!
* @file DFRobot_LIS2DH12.cpp
* @brief DFRobot's Read LIS2DH12 data
* @n This example is in order to achieve the serial port to receive LIS2DH12 back to the data
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
* @author [Wuxiao](xiao.wu@dfrobot.com)
* @version V1.0
* @date 2016-10-13
* @https://github.com/DFRobot/DFRobot_LIS2DH12
*/
#include <DFRobot_LIS2DH12.h>
#include <Wire.h>
int8_t DFRobot_LIS2DH12::init(uint8_t range, uint8_t sensorAddress)
{
int8_t ret = 0;
this->sensorAddress = sensorAddress;
setRange(range);
Wire.beginTransmission(sensorAddress);
ret = Wire.endTransmission();
if(ret != 0){
ret = -1;
}else{
uint8_t ctrl_reg_values[] = {0x2F, 0x00, 0x00, range, 0x00, 0x00};
ret = (int8_t)writeReg(0xA0, ctrl_reg_values, sizeof(ctrl_reg_values));
}
return ret;
}
void DFRobot_LIS2DH12::readXYZ(int16_t &x, int16_t &y, int16_t &z) //read x, y, z data
{
uint8_t sensorData[6];
readReg(0xA8, sensorData, 6);
x = ((int8_t)sensorData[1])*256+sensorData[0]; //return values
y = ((int8_t)sensorData[3])*256+sensorData[2];
z = ((int8_t)sensorData[5])*256+sensorData[4];
}
void DFRobot_LIS2DH12::mgScale(int16_t &x, int16_t &y, int16_t &z)
{
x = (int32_t)x*1000/(1024*mgScaleVel); //transform data to millig, for 2g scale axis*1000/(1024*16),
y = (int32_t)y*1000/(1024*mgScaleVel); //for 4g scale axis*1000/(1024*8),
z = (int32_t)z*1000/(1024*mgScaleVel); //for 8g scale axis*1000/(1024*4)
}
uint8_t DFRobot_LIS2DH12::readReg(uint8_t regAddress)
{
uint8_t regValue;
Wire.beginTransmission(sensorAddress);
Wire.write(regAddress);
Wire.endTransmission();
Wire.requestFrom(sensorAddress, (uint8_t)1);
regValue = Wire.read();
return regValue;
}
void DFRobot_LIS2DH12::readReg(uint8_t regAddress, uint8_t *regValue, uint8_t quanity, bool autoIncrement)
{
regAddress = 0x80 | regAddress;
if(autoIncrement){
Wire.beginTransmission(sensorAddress);
Wire.write(regAddress);
Wire.endTransmission();
Wire.requestFrom(sensorAddress, quanity);
for(uint8_t i=0; i < quanity; i++)
regValue[i] = Wire.read();
}else{
for(uint8_t i = 0; i < quanity; i++){
Wire.beginTransmission(sensorAddress);
Wire.write(regAddress+i);
Wire.endTransmission();
Wire.requestFrom(sensorAddress,(uint8_t)1);
regValue[i] = Wire.read();
}
}
}
uint8_t DFRobot_LIS2DH12::writeReg(uint8_t regAddress, uint8_t regValue)
{
Wire.beginTransmission(sensorAddress);
Wire.write(regAddress);
Wire.write(regValue);
return Wire.endTransmission(true);
}
uint8_t DFRobot_LIS2DH12::writeReg(uint8_t regAddress, uint8_t *regValue, size_t quanity, bool autoIncrement)
{
Wire.beginTransmission(sensorAddress);
if(autoIncrement) {
Wire.write(regAddress);
Wire.write(regValue, quanity);
}
else {
for(size_t i = 0; i < quanity; i++){
Wire.write(regAddress+i);
Wire.write(regValue[i]);
if( i<(quanity-1) ){
Wire.endTransmission(false);
Wire.beginTransmission(sensorAddress);
}
}
}
return Wire.endTransmission(true);
}
void DFRobot_LIS2DH12::setRange(uint8_t range)
{
switch(range)
{
case LIS2DH12_RANGE_2GA:
mgScaleVel = 16;
break;
case LIS2DH12_RANGE_4GA:
mgScaleVel = 8;
break;
case LIS2DH12_RANGE_8GA:
mgScaleVel = 4;
break;
case LIS2DH12_RANGE_16GA:
mgScaleVel = 2;
break;
default:
mgScaleVel = 16;
break;
}
}