sensors.c
6.13 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
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
#include "sensors.h"
sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux",NULL};
sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm",NULL};
sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "kPa",NULL};
uint32_t get_light_data(void)
{
uint32_t data = 0;
data = get_light_ch1();
data = data << 16;
data = data | get_light_ch0();
return data;
}
uint16_t get_light_ch0(void)
{
uint16_t data = 0;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0D);
I2C_stop(I2C1);
I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
data = data << 8;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0C);
I2C_stop(I2C1);
I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = data | I2C_read_ack(I2C1);
I2C_stop(I2C1);
return data;
}
uint16_t get_light_ch1(void)
{
uint16_t data = 0;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0F);
I2C_stop(I2C1);
I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
data = data << 8;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0E);
I2C_stop(I2C1);
I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = data | I2C_read_ack(I2C1);
I2C_stop(I2C1);
return data;
}
uint16_t get_distance_data(void)
{
init_ultrasound();
uint16_t data = get_distance_high();
data = data << 8;
data = data | get_distance_low();
return data;
}
void init_ultrasound(void)
{
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x00);
I2C_stop(I2C1);
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x50);
I2C_stop(I2C1);
chThdSleepMilliseconds(70);
}
uint8_t get_distance_low(void)
{
uint8_t data;
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x03);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
I2C_stop(I2C1);
return data;
}
uint8_t get_distance_high(void)
{
uint8_t data;
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x02);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
I2C_stop(I2C1);
return data;
}
uint16_t get_pressure_data(void)
{
uint16_t data;
init_pressure();
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF6);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
data = data << 8;
data = data | I2C_read_nack(I2C1);
return data;
}
bmp085_callibration get_pressure_callibration_data(void)
{
bmp085_callibration calib_data;
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAA);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC1 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAC);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC2 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAE);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC3 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB0);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC4 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB2);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC5 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB4);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC6 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB6);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.B1 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB8);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.B2 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBA);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.MB = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBC);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.MC = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBE);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.MD = I2C_read_ack(I2C1);
I2C_stop(I2C1);
return calib_data;
}
char* callibration_pressure_data_csv(bmp085_callibration parameters)
{
char *str = chHeapAlloc(NULL,sizeof(char)*(4*11-1));
memset(str,0x00,sizeof(char)*(4*11-1));
sprintf(str + strlen(str),"%d,",parameters.AC1);
sprintf(str + strlen(str),"%d,",parameters.AC2);
sprintf(str + strlen(str),"%d,",parameters.AC3);
sprintf(str + strlen(str),"%d,",parameters.AC4);
sprintf(str + strlen(str),"%d,",parameters.AC5);
sprintf(str + strlen(str),"%d,",parameters.AC6);
sprintf(str + strlen(str),"%d,",parameters.B1);
sprintf(str + strlen(str),"%d,",parameters.B2);
sprintf(str + strlen(str),"%d,",parameters.MB);
sprintf(str + strlen(str),"%d,",parameters.MC);
sprintf(str + strlen(str),"%d",parameters.MD);
return str;
}
void init_pressure(void)
{
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF4);
I2C_write(I2C1, 0x34);
I2C_stop(I2C1);
chThdSleepMilliseconds(5);
}