-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic_sensor.c
232 lines (195 loc) · 4.95 KB
/
ultrasonic_sensor.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
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 "ultrasonic_sensor.h"
Ultrasonic_Sensor_TypeDef ultrasonic0 = {
0,
23,
&LPC_IOCON->P0_23,
&LPC_IOCON->P5_2,
3,
3,
LPC_TIM3,
&LPC_TIM3->MR2,
&LPC_TIM3->CR0,
6,
7,
0,
1,
2,
2,
8,
2,
4,
TIMER3_IRQn
};
Ultrasonic_Sensor_TypeDef ultrasonic1 = {
1,
22,
&LPC_IOCON->P0_4,
&LPC_IOCON->P0_9,
3,
3,
LPC_TIM2,
&LPC_TIM2->MR3,
&LPC_TIM2->CR0,
9,
10,
0,
1,
2,
3,
10,
3,
4,
TIMER2_IRQn
};
Ultrasonic_Sensor_TypeDef ultrasonic2 = {
2,
22,
&LPC_IOCON->P0_5,
&LPC_IOCON->P0_9,
3,
3,
LPC_TIM2,
&LPC_TIM2->MR3,
&LPC_TIM2->CR1,
9,
10,
3,
4,
5,
3,
10,
3,
5,
TIMER2_IRQn
};
Ultrasonic_Sensor_TypeDef ultrasonic3 = {
3,
23,
&LPC_IOCON->P0_24,
&LPC_IOCON->P5_2,
3,
3,
LPC_TIM3,
&LPC_TIM3->MR2,
&LPC_TIM3->CR1,
6,
7,
3,
4,
5,
2,
8,
2,
5,
TIMER3_IRQn
};
Ultrasonic_Sensor_TypeDef* ultrasonic[] = {&ultrasonic0, &ultrasonic1, &ultrasonic2, &ultrasonic3};
const uint8_t n_sensors = sizeof(ultrasonic) / sizeof(Ultrasonic_Sensor_TypeDef*);
Ultrasonic_Sensor_TypeDef* TIM2_using_sensors[] = {&ultrasonic1, &ultrasonic2};
const uint8_t n_TIM2_using_sensors = sizeof(TIM2_using_sensors) / sizeof(Ultrasonic_Sensor_TypeDef*);
Ultrasonic_Sensor_TypeDef* TIM3_using_sensors[] = {&ultrasonic0, &ultrasonic3};
const uint8_t n_TIM3_using_sensors = sizeof(TIM3_using_sensors) / sizeof(Ultrasonic_Sensor_TypeDef*);
uint8_t is_trig_rising = 0xF; // used as a binary array
uint8_t is_echo_rising = 0xF; // used as a binary array
uint32_t last_measurement[] = {0, 0, 0, 0};
uint32_t ultrasonic_distance[] = {0, 0, 0, 0};
uint8_t ultrasonic_updated = 0; // used as a binary array
void ultrasonic_init_single(Ultrasonic_Sensor_TypeDef* sensor) {
// turn on TIM
LPC_SC->PCONP |= (1 << sensor->PCONP_bit);
// change IOCON functionalities
*sensor->IOCON_echo |= sensor->IOCON_echo_func;
*sensor->IOCON_trig |= sensor->IOCON_trig_func;
// disable and reset counter
sensor->TIM->TCR &= ~(1);
sensor->TIM->TCR |= (1 << 1);
// set PR to make TC count for every 1 microseconds
sensor->TIM->PR = 59;
// interrupt on MR3 match
sensor->TIM->MCR |= (1 << sensor->TIM_MRI_bit);
// clear external match pin when match occurs
sensor->TIM->EMR |= (1 << sensor->TIM_EMC_bit);
// interrupt on capture rising edge
sensor->TIM->CCR |= (1 << sensor->TIM_CAPRE_bit)
| (1 << sensor->TIM_CAPI_bit);
// enable interrupts
NVIC_EnableIRQ(sensor->TIM_IRQn);
// start counting
sensor->TIM->TCR |= 1;
sensor->TIM->TCR &= ~(1 << 1);
}
void start_triggering(Ultrasonic_Sensor_TypeDef* sensor) {
// set external match high
sensor->TIM->EMR |= (1 << sensor->TIM_EM_bit);
// interrupt after 10 microseconds
*sensor->TIM_MR = sensor->TIM->TC + 10;
}
void ultrasonic_init(void) {
uint8_t i;
for (i = 0; i < n_sensors; i++) {
ultrasonic_init_single(ultrasonic[i]);
}
is_trig_rising &= ~(1 << 0);
start_triggering(ultrasonic[0]);
}
void ultrasonic_handle_single(Ultrasonic_Sensor_TypeDef* sensor) {
// check which interrupt occurred
if ((sensor->TIM->IR & (1 << sensor->TIM_MRINT_bit)) != 0) {
// match occurred
// clear interrupt
sensor->TIM->IR = (1 << sensor->TIM_MRINT_bit);
if ((is_trig_rising & (1 << sensor->id)) != 0) {
// set external match pin to start triggering
sensor->TIM->EMR |= (1 << sensor->TIM_EM_bit);
// end triggering after 10 microseconds
*sensor->TIM_MR = sensor->TIM->TC + 10;
// next is falling
is_trig_rising &= ~(1 << sensor->id);
} else {
// go on to next sensor
uint8_t next_sensor = sensor->id + 1;
if (next_sensor >= n_sensors / 2) {
next_sensor = 0;
}
*ultrasonic[next_sensor]->TIM_MR = ultrasonic[next_sensor]->TIM->TC + RECOMMENDED_WAITING_MICROSECOND / (n_sensors / 2);
// next is rising
is_trig_rising |= (1 << next_sensor);
}
} else if ((sensor->TIM->IR & (1 << sensor->TIM_CRINT_bit)) != 0) {
// capture occurred
// clear interrupt
sensor->TIM->IR = (1 << sensor->TIM_CRINT_bit);
if ((is_echo_rising & (1 << sensor->id)) != 0) {
// interrupt on falling edge
sensor->TIM->CCR |= (1 << sensor->TIM_CAPFE_bit);
sensor->TIM->CCR &= ~(1 << sensor->TIM_CAPRE_bit);
last_measurement[sensor->id] = *sensor->TIM_CR;
// next is falling
is_echo_rising &= ~(1 << sensor->id);
} else {
// interrupt on rising edge
sensor->TIM->CCR &= ~(1 << sensor->TIM_CAPFE_bit);
sensor->TIM->CCR |= (1 << sensor->TIM_CAPRE_bit);
uint32_t measurement = (*sensor->TIM_CR - last_measurement[sensor->id]) / 58;
// if not noise, update distance
if (measurement < 1000) {
ultrasonic_distance[sensor->id] = measurement;
ultrasonic_updated |= (1 << sensor->id);
}
// next is rising
is_echo_rising |= (1 << sensor->id);
}
}
}
void TIMER2_IRQHandler(void) {
uint8_t i;
for (i = 0; i < n_TIM2_using_sensors; i++) {
ultrasonic_handle_single(TIM2_using_sensors[i]);
}
}
void TIMER3_IRQHandler(void) {
uint8_t i;
for (i = 0; i < n_TIM3_using_sensors; i++) {
ultrasonic_handle_single(TIM3_using_sensors[i]);
}
}