-
Notifications
You must be signed in to change notification settings - Fork 0
/
altimeter.c
196 lines (145 loc) · 2.97 KB
/
altimeter.c
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
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "i2c-dev.h"
int file;
int adapter_nr= 2;
char filename[20];
uint8_t accelAddr = 0x19;
uint8_t magAddr = 0x1E;
uint8_t gyroAddr = 0x6B;
uint8_t altAddr = 0x77;
uint16_t altCalVals[11];
uint8_t acceleration[6];
uint8_t gyroscope[6];
uint8_t magnetic[6];
//read from 0xF6 and 0xF7 to get values
uint8_t readAddr = 0xF6; // MSB
void initAccel();
void readAccel(uint8_t* data);
void initMag();
void readMag(uint8_t* data);
void initGyro();
void readGyro(uint8_t* data);
void initAltimeter();
int main()
{
printf(filename, 19, "/dev/i2c-1", adapter_nr);
file = open(filename, O_RDWR);
if(file <0)
{
exit(1);
}
initAccel();
}
void initAccel()
{
if(ioctl(file, I2C_SLAVE, accelAddr)<0)
{
exit(1);
}
//set register 0x20 to 0x77
i2c_smbus_write_word_data(file, 0x20, 0x77); //enable accelerometer reading
}
void readAccel(uint8_t* data)
{
if(ioctl(file, I2C_SLAVE, accelAddr)<0)
{
exit(1);
}
uint8_t readAddr = 0x28;
int i = 0;
for(i = 0; i < 6; i++)
{
data[i] = i2c_smbus_read_byte_data(file, (readAddr+i));
}
}
void initMag()
{
if(ioctl(file, I2C_SLAVE, magAddr)<0)
{
exit(1);
}
//set register 0x2 to 0x00
//enable magnetometer to continuous conversion mode
i2c_smbus_write_word_data(file, 0x02, 0x00);
}
void readMag(uint8_t* data)
{
if(ioctl(file, I2C_SLAVE, magAddr)<0)
{
exit(1);
}
uint8_t readAddr = 0x03;
int i = 0;
for(i = 0; i < 6; i++)
{
data[i] = i2c_smbus_read_byte_data(file, (readAddr+i));
}
}
void initGyro()
{
if(ioctl(file, I2C_SLAVE, gyroAddr)<0)
{
exit(1);
}
//set register 0x20 to 0xFF
//enable gyro and selects max bandwidth
i2c_smbus_write_word_data(file, 0x20, 0xFF);
}
void readGyro(uint8_t* data)
{
if(ioctl(file, I2C_SLAVE, gyroAddr)<0)
{
exit(1);
}
uint8_t readAddr = 0x28;
int i = 0;
for(i = 0; i < 6; i++)
{
data[i] = i2c_smbus_read_byte_data(file, (readAddr+i));
}
}
void initAltimeter(uint16_t* data)
{
if(ioctl(file, I2C_SLAVE, altAddr)<0)
{
exit(1);
}
//get calibration values (0xAA- 0xBF)
for(int i = 0; i < 11; i++)
{
data[i] = i2c_smbus_read_byte_data(file, (readAddr+2i));
data[i] |= (i2c_smbus_read_byte_data(file, (readAddr+2i+1)) << 8) ;
}
}
void readTemp(uint8_t* data)
{
if(ioctl(file, I2C_SLAVE, gyroAddr)<0)
{
exit(1);
}
//send conversion command to control register 0xF4
//temperature 0x2E
i2c_smbus_write_word_data(file, 0xF4, 0x2E);
data[0] = i2c_smbus_read_byte_data(file, readAddr);
readAddr = 0xF7; //LSB
data[1] = i2c_smbus_read_byte_data(file, readAddr);
}
void readPres(uint8_t* data)
{
if(ioctl(file, I2C_SLAVE, gyroAddr)<0)
{
exit(1);
}
//send conversion command to control register 0xF4
//pressure 0x34
i2c_smbus_write_word_data(file, 0xF4, 0x34);
uint8_t readAddr = 0xF6; // MSB
data[0] = i2c_smbus_read_byte_data(file, readAddr);
readAddr = 0xF7; // LSB
data[1] = i2c_smbus_read_byte_data(file, readAddr);
}