31
31
#include "stm32f3xx_hal_rcc_ex.h"
32
32
#include "vl53lx_api.h"
33
33
#include "stdbool.h"
34
+ #include <stdio.h>
35
+ #include <string.h>
34
36
/* USER CODE END Includes */
35
37
36
38
/* Private typedef -----------------------------------------------------------*/
50
52
/* Private variables ---------------------------------------------------------*/
51
53
52
54
/* USER CODE BEGIN PV */
53
- VL53L3CX_Object_t sensor ;
54
- long distance = 0 ;
55
- volatile bool isReady = false;
56
- volatile bool hasRead = false;
55
+
56
+
57
+ long distance [5 ] = {0 ,0 ,0 ,0 ,0 };
58
+ volatile bool isReady [5 ] = {false, false, false, false, false};
59
+ volatile bool hasRead [5 ] = {false, false, false, false, false};
57
60
VL53L3CX_Result_t results ;
58
61
/* USER CODE END PV */
59
62
@@ -65,7 +68,7 @@ void SystemClock_Config(void);
65
68
66
69
/* Private user code ---------------------------------------------------------*/
67
70
/* USER CODE BEGIN 0 */
68
-
71
+ VL53L3CX_Object_t sensor [];
69
72
/* USER CODE END 0 */
70
73
71
74
/**
@@ -75,21 +78,20 @@ void SystemClock_Config(void);
75
78
int main (void )
76
79
{
77
80
/* USER CODE BEGIN 1 */
78
-
79
81
/* USER CODE END 1 */
80
82
81
83
/* MCU Configuration--------------------------------------------------------*/
82
84
83
85
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
84
86
HAL_Init ();
85
-
87
+ HAL_Delay ( 20 );
86
88
/* USER CODE BEGIN Init */
87
89
88
90
/* USER CODE END Init */
89
91
90
92
/* Configure the system clock */
91
93
SystemClock_Config ();
92
-
94
+ HAL_Delay ( 20 );
93
95
/* USER CODE BEGIN SysInit */
94
96
95
97
/* USER CODE END SysInit */
@@ -101,27 +103,12 @@ int main(void)
101
103
MX_USART1_UART_Init ();
102
104
/* USER CODE BEGIN 2 */
103
105
104
- sensor .IO .DeInit = CUSTOM_VL53L3CX_I2C_DeInit ;
105
- sensor .IO .Init = CUSTOM_VL53L3CX_I2C_Init ;
106
- sensor .IO .ReadReg = CUSTOM_VL53L3CX_I2C_ReadReg ;
107
- sensor .IO .WriteReg = CUSTOM_VL53L3CX_I2C_WriteReg ;
108
- sensor .IO .GetTick = CUSTOM_VL53L3CX_I2C_GetTick ;
109
- sensor .IO .Address = 0x52 ;
110
-
111
- HAL_GPIO_WritePin (XSHUT_0_GPIO_Port , XSHUT_0_Pin , 0 );
112
- HAL_GPIO_WritePin (XSHUT_1_GPIO_Port , XSHUT_1_Pin , 0 );
113
- HAL_GPIO_WritePin (XSHUT_2_GPIO_Port , XSHUT_2_Pin , 0 );
114
- HAL_GPIO_WritePin (XSHUT_3_GPIO_Port , XSHUT_3_Pin , 0 );
115
- HAL_GPIO_WritePin (XSHUT_4_GPIO_Port , XSHUT_4_Pin , 0 );
116
-
117
- HAL_Delay (20 );
118
- sensor .IsInitialized = 0 ;
119
-
120
- HAL_GPIO_WritePin (XSHUT_4_GPIO_Port , XSHUT_4_Pin , 1 );
121
- CUSTOM_VL53L3CX_I2C_Init ();
122
- VL53L3CX_Init (& sensor );
123
- VL53L3CX_SetAddress (& sensor , 0x53 );
124
- sensor .IO .Address = 0x53 ;
106
+ sensor -> IO .DeInit = CUSTOM_VL53L3CX_I2C_DeInit ;
107
+ sensor -> IO .Init = CUSTOM_VL53L3CX_I2C_Init ;
108
+ sensor -> IO .ReadReg = CUSTOM_VL53L3CX_I2C_ReadReg ;
109
+ sensor -> IO .WriteReg = CUSTOM_VL53L3CX_I2C_WriteReg ;
110
+ sensor -> IO .GetTick = CUSTOM_VL53L3CX_I2C_GetTick ;
111
+ sensor -> IO .Address = VL53L3CX_DEVICE_ADDRESS ; // Default adres
125
112
126
113
// Config profile
127
114
VL53L3CX_ProfileConfig_t Profile ;
@@ -132,57 +119,101 @@ int main(void)
132
119
Profile .EnableAmbient = 1 ; /* Enable: 1, Disable: 0 */
133
120
Profile .EnableSignal = 1 ; /* Enable: 1, Disable: 0 */
134
121
135
- VL53L3CX_ConfigProfile (& sensor , & Profile );
122
+ HAL_GPIO_WritePin (XSHUT_0_GPIO_Port , XSHUT_0_Pin , 0 );
123
+ HAL_GPIO_WritePin (XSHUT_1_GPIO_Port , XSHUT_1_Pin , 0 );
124
+ HAL_GPIO_WritePin (XSHUT_2_GPIO_Port , XSHUT_2_Pin , 0 );
125
+ HAL_GPIO_WritePin (XSHUT_3_GPIO_Port , XSHUT_3_Pin , 0 );
126
+ HAL_GPIO_WritePin (XSHUT_4_GPIO_Port , XSHUT_4_Pin , 0 );
136
127
137
- // Starten
138
- VL53L3CX_Start (& sensor , VL53L3CX_MODE_ASYNC_CONTINUOUS );
128
+ CUSTOM_VL53L3CX_I2C_Init ();
139
129
140
- VL53L3CX_GetDistance (& sensor , & results );
141
- // HAL_Delay(1000);
142
- // VL53L3CX_GetDistance(&sensor, &results);
130
+ HAL_Delay (20 );
131
+ HAL_GPIO_WritePin (XSHUT_0_GPIO_Port , XSHUT_0_Pin , 1 );
132
+ HAL_Delay (20 );
133
+ VL53L3CX_Init (& sensor [0 ]);
134
+ HAL_Delay (20 );
135
+ VL53L3CX_SetAddress (& sensor [0 ], 0x53 );
136
+ VL53L3CX_ConfigProfile (& sensor [0 ], & Profile );
143
137
144
- /* USER CODE END 2 */
138
+ // Starten sensor 0
139
+ VL53L3CX_Start (& sensor [0 ], VL53L3CX_MODE_ASYNC_CONTINUOUS );
140
+ HAL_Delay (20 );
141
+ VL53L3CX_GetDistance (& sensor [0 ], & results );
142
+ HAL_Delay (20 );
145
143
146
- /* Infinite loop */
147
- /* USER CODE BEGIN WHILE */
148
- // VL53LX_ClearInterruptAndStartMeasurement(&sensor);
149
- while (1 )
144
+ // HAL_GPIO_WritePin(XSHUT_1_GPIO_Port, XSHUT_1_Pin, 1);
145
+ // HAL_Delay(20);
146
+ // VL53L3CX_Init(&sensor[1]);
147
+ // HAL_Delay(20);
148
+ // VL53L3CX_SetAddress(&sensor[1], 0x54);
149
+
150
+ // HAL_Delay(20);
151
+
152
+ // sensor.IO.Address = address[1];
153
+ // VL53L3CX_ConfigProfile(&sensor, &Profile);
154
+
155
+ // // Starten sensor 1
156
+ // sensor.IO.Address = address[1];
157
+ // sensor.IsRanging = (uint8_t)isRanging[1];
158
+ // VL53L3CX_Start(&sensor, VL53L3CX_MODE_ASYNC_CONTINUOUS);
159
+ // isRanging[1] = true;
160
+ // HAL_Delay(20);
161
+ // VL53L3CX_GetDistance(&sensor, &results);
162
+ // HAL_Delay(1000);
163
+ // VL53L3CX_GetDistance(&sensor, &results);
164
+
165
+ /* USER CODE END 2 */
166
+
167
+ /* Infinite loop */
168
+ /* USER CODE BEGIN WHILE */
169
+ // VL53LX_ClearInterruptAndStartMeasurement(&sensor);
170
+ while (1 )
150
171
{
151
172
/* USER CODE END WHILE */
152
173
// VL53L3CX_GetDistance(&sensor, &results);
153
- // printf("distance: %4d\r\n", (long)results.ZoneResult[0].Distance[0] );
154
- if (isReady || (!isReady && !HAL_GPIO_ReadPin (GPIOI_4_Pin , GPIOI_4_GPIO_Port )))
174
+ HAL_Delay ( 5 );
175
+ if (isReady [ 0 ] || (!isReady [ 0 ] && !HAL_GPIO_ReadPin (GPIOI_0_GPIO_Port , GPIOI_0_Pin )))
155
176
{
156
- isReady = false;
157
- VL53L3CX_GetDistance (& sensor , & results );
158
177
HAL_Delay (1 );
159
- distance = (long )results .ZoneResult [0 ].Distance [0 ];
178
+ isReady [0 ] = false;
179
+
180
+ VL53L3CX_GetDistance (& sensor [0 ], & results );
181
+ HAL_Delay (1 );
182
+ distance [0 ] = (long )results .ZoneResult [0 ].Distance [0 ];
183
+ }
184
+ if (isReady [1 ] || (!isReady [1 ] && !HAL_GPIO_ReadPin (GPIOI_1_GPIO_Port , GPIOI_1_Pin )))
185
+ {
186
+ HAL_Delay (1 );
187
+ isReady [1 ] = false;
188
+ VL53L3CX_GetDistance (& sensor [1 ], & results );
189
+ HAL_Delay (1 );
190
+ distance [1 ] = (long )results .ZoneResult [0 ].Distance [0 ];
160
191
}
161
192
162
- if (distance >= 50 && distance <= 300 )
193
+ if (distance [ 0 ] >= 50 && distance [ 0 ] <= 300 )
163
194
{
164
195
HAL_GPIO_WritePin (LED_0_GPIO_Port , LED_0_Pin , 1 );
165
196
HAL_Delay (200 );
166
197
}
167
198
168
- if (distance > 300 && distance <= 600 )
199
+ if (distance [ 0 ] > 300 && distance [ 0 ] <= 600 )
169
200
{
170
201
HAL_GPIO_WritePin (LED_1_GPIO_Port , LED_1_Pin , 1 );
171
202
HAL_Delay (200 );
172
203
}
173
204
174
- if (distance > 600 && distance <= 800 )
205
+ if (distance [ 0 ] > 600 && distance [ 0 ] <= 800 )
175
206
{
176
207
HAL_GPIO_WritePin (LED_2_GPIO_Port , LED_2_Pin , 1 );
177
208
HAL_Delay (200 );
178
209
}
179
210
180
- if (distance > 800 && distance <= 1200 )
211
+ if (distance [ 0 ] > 800 && distance [ 0 ] <= 1200 )
181
212
{
182
213
HAL_GPIO_WritePin (LED_3_GPIO_Port , LED_3_Pin , 1 );
183
214
HAL_Delay (200 );
184
215
}
185
- if (distance > 1200 )
216
+ if (distance [ 0 ] > 1200 )
186
217
{
187
218
HAL_GPIO_WritePin (LED_4_GPIO_Port , LED_4_Pin , 1 );
188
219
HAL_Delay (200 );
@@ -194,6 +225,8 @@ int main(void)
194
225
HAL_GPIO_WritePin (LED_2_GPIO_Port , LED_2_Pin , 0 );
195
226
HAL_GPIO_WritePin (LED_3_GPIO_Port , LED_3_Pin , 0 );
196
227
HAL_GPIO_WritePin (LED_4_GPIO_Port , LED_4_Pin , 0 );
228
+
229
+ printf ("distance 0: %4d\tdistance 1: %4d\r\n" , distance [0 ], distance [1 ]);
197
230
/* USER CODE BEGIN 3 */
198
231
}
199
232
/* USER CODE END 3 */
@@ -254,8 +287,7 @@ int _write(int file, char *data, int len)
254
287
}
255
288
256
289
// arbitrary timeout 1000
257
- HAL_StatusTypeDef status =
258
- HAL_UART_Transmit (& huart2 , (uint8_t * )data , len , 1000 );
290
+ HAL_StatusTypeDef status = HAL_UART_Transmit (& huart1 , (uint8_t * )data , len , 1000 );
259
291
260
292
// return # of bytes written - as best we can tell
261
293
return (status == HAL_OK ? len : 0 );
@@ -266,53 +298,58 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
266
298
switch (GPIO_Pin )
267
299
{
268
300
case GPIOI_0_Pin :
269
- if (!hasRead )
301
+ if (!hasRead [ 0 ] )
270
302
{
271
- hasRead = true;
272
- VL53L3CX_GetDistance (& sensor , & results );
303
+ hasRead [0 ] = true;
304
+ // sensor.IO.Address = address[0];
305
+ // VL53L3CX_GetDistance(&sensor, &results);
273
306
}
274
307
else
275
- isReady = true;
308
+ isReady [ 0 ] = true;
276
309
break ;
277
310
278
311
case GPIOI_1_Pin :
279
- if (!hasRead )
312
+ if (!hasRead [ 1 ] )
280
313
{
281
- hasRead = true;
282
- VL53L3CX_GetDistance (& sensor , & results );
314
+ hasRead [1 ] = true;
315
+ // sensor.IO.Address = address[1];
316
+ // VL53L3CX_GetDistance(&sensor, &results);
283
317
}
284
318
else
285
- isReady = true;
319
+ isReady [ 1 ] = true;
286
320
break ;
287
321
288
322
case GPIOI_2_Pin :
289
- if (!hasRead )
323
+ if (!hasRead [ 2 ] )
290
324
{
291
- hasRead = true;
292
- VL53L3CX_GetDistance (& sensor , & results );
325
+ hasRead [2 ] = true;
326
+ // sensor.IO.Address = address[2];
327
+ // VL53L3CX_GetDistance(&sensor, &results);
293
328
}
294
329
else
295
- isReady = true;
330
+ isReady [ 2 ] = true;
296
331
break ;
297
332
298
333
case GPIOI_3_Pin :
299
- if (!hasRead )
334
+ if (!hasRead [ 3 ] )
300
335
{
301
- hasRead = true;
302
- VL53L3CX_GetDistance (& sensor , & results );
336
+ hasRead [3 ] = true;
337
+ // sensor.IO.Address = address[3];
338
+ // VL53L3CX_GetDistance(&sensor, &results);
303
339
}
304
340
else
305
- isReady = true;
341
+ isReady [ 3 ] = true;
306
342
break ;
307
343
308
344
case GPIOI_4_Pin :
309
- if (!hasRead )
345
+ if (!hasRead [ 4 ] )
310
346
{
311
- hasRead = true;
312
- VL53L3CX_GetDistance (& sensor , & results );
347
+ hasRead [4 ] = true;
348
+ // sensor.IO.Address = address[4];
349
+ // VL53L3CX_GetDistance(&sensor, &results);
313
350
}
314
351
else
315
- isReady = true;
352
+ isReady [ 4 ] = true;
316
353
break ;
317
354
default :
318
355
break ;
0 commit comments