-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainSensor.c
More file actions
278 lines (242 loc) · 6.89 KB
/
Copy pathrainSensor.c
File metadata and controls
278 lines (242 loc) · 6.89 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @file RainsSensor.c
* @author Alessandro Ferrante (github@alessandroferrante.net)
* @brief This file contains the implementation of a rain sensor to manage the windshield wipers.
*
* The trimmer simulates the variation of the intensity of the rain.
* The rain sensor is then responsible for reading the analog data
* and converting it into a digital value using the ADC (Analog-to-Digital Converter).
* The converted digital value is used to determine the presence and intensity of rain.
* The intensity of the rain determines the speed of the windshield wipers
*
* @version 0.1
* @date 2024-10-30
*
* @copyright Copyright (c) 2025
*
*/
#include <stdio.h>
#include <string.h>
#include "stm32_unict_lib.h"
typedef enum {
OFF,
TO_OFF,
RAIN,
TIMED_ON,
TIMED_OFF,
CONTINUE
} states;
#define TS1 200
#define TS2 50
char * wipers_display[] = {
"- ",
" - ",
" - ",
" -"
" - ",
" - ",
"- "};
states state = OFF;
int wipers_pos = 0;
int wipers_duration, wipers_step_duration; // redundant but functional
int wipers_step_counter = 0;
int wait_time = 0;
int wait_time_counter = 0;
int rain_speed = 0;
int drop_time = 0, drop_time_counter = 0, drops = 0;
void setup(){
ClockConfig();
DISPLAY_init();
GPIO_init(GPIOB); // initialize port B
GPIO_init(GPIOC); // initialize port C
GPIO_config_output(GPIOB, 0); // configure pin PB0 as output
GPIO_config_output(GPIOC, 2); // configure pin PC2 as output
GPIO_config_output(GPIOC, 3); // configure pin PC2 as output
GPIO_config_input(GPIOB, 10); // configure pin PB10 as input
GPIO_config_input(GPIOB, 4); // configure pin PB4 as input
GPIO_config_input(GPIOB, 5); // configure pin PB5 as input
GPIO_config_input(GPIOB, 6); // configure pin PB6 as input
GPIO_config_EXTI(GPIOB, EXTI10); // attach PB10 to EXTI10
GPIO_config_EXTI(GPIOB, EXTI4); // attach PB4 to EXTI4
GPIO_config_EXTI(GPIOB, EXTI5); // attach PB5 to EXTI5
GPIO_config_EXTI(GPIOB, EXTI6); // attach PB6 to EXTI6
EXTI_enable(EXTI10, FALLING_EDGE);
EXTI_enable(EXTI4, FALLING_EDGE);
EXTI_enable(EXTI5, BOTH_EDGES);
EXTI_enable(EXTI6, FALLING_EDGE);
TIM_init(TIM2);
TIM_config_timebase(TIM2, 8400, 10000);//10ms
TIM_enable_irq(TIM2, IRQ_UPDATE);
TIM_set(TIM2, 0);
TIM_on(TIM2);
TIM_init(TIM3);
TIM_config_timebase(TIM3, 8400, 1000);//100ms
TIM_enable_irq(TIM3, IRQ_UPDATE);
TIM_set(TIM3, 0);
TIM_on(TIM3);
// ADC
ADC_init(ADC1, ADC_RES_10, ADC_ALIGN_RIGHT); // ADC1, 8bit,
ADC_channel_config(ADC1, GPIOC, 0, 10);
ADC_on(ADC1);
ADC_sample_channel(ADC1, 10);
/*
ADC_init(ADC2, ADC_RES_8, ADC_ALIGN_RIGHT); // ADC2, 8bit,
ADC_channel_config(ADC2, GPIOC, 0, 10);
ADC_on(ADC2);
ADC_sample_channel(ADC2, 10);
*/
}
void TIM2_IRQHandler(void){
if (TIM_update_check(TIM2)){
if(state != OFF){
if (state == RAIN){
if (rain_speed > 0){
++drop_time_counter;
if(drop_time_counter >= drop_time){
drop_time_counter = 0;
GPIO_toggle(GPIOB, 9);
++drops;
}
}
}
int do_wipe = 0;
switch (state){
case TO_OFF:
case TIMED_ON:
case CONTINUE:
do_wipe = 1;
break;
case RAIN:
do_wipe = 1;
drops = 0;
default:
break;
}
//if (state == CONTINUE || state == TIMED_ON || state == RAIN ){
if(do_wipe){
++wipers_step_counter;
if(wipers_step_counter >= wipers_step_duration){
wipers_step_counter = 0;
++wipers_pos;
if(wipers_pos == 8){
wipers_pos = 0;
switch (state){
case TO_OFF:
state = OFF;
case TIMED_ON:
state = TIMED_OFF;
break;
default:
break;
}
}
}
}
else if (state == CONTINUE || state == TIMED_OFF){
++wait_time_counter;
if(wait_time_counter >= wait_time){
wait_time_counter = 0;
state = TIMED_ON;
}
}
}
}
}
void TIM3_IRQHandler(void){
if (TIM_update_check(TIM3)) {
TIM_update_clear(TIM3);
}
}
void EXTI15_10_IRQHandler(void){
if (EXTI_isset(EXTI10)) {
// button X
if (state != RAIN){
state = RAIN;
}
else
state = OFF;
EXTI_clear(EXTI10);
}
}
void EXTI4_IRQHandler(void)
{
if (EXTI_isset(EXTI4)) {
// button Y
if (state != TIMED_ON){
wipers_duration = TS1;
wipers_step_duration = wipers_duration / 8;
}
else
state = OFF;
EXTI_clear(EXTI4);
}
}
void EXTI9_5_IRQHandler(void){
if (EXTI_isset(EXTI5)) {
// button Z
if (state != CONTINUE){
wipers_duration = TS2;
wipers_step_duration = wipers_duration / 8;
}
else{
state = OFF;
GPIO_write(GPIOC, 2, 0);
}
EXTI_clear(EXTI5);
}
if (EXTI_isset(EXTI6)) {
// button T
EXTI_clear(EXTI6);
}
}
void loop(){
if(state != OFF)
DISPLAY_puts(0, wipers_display[0]);
else
DISPLAY_puts(0, wipers_display[wipers_pos]);
switch (state){
case OFF:
GPIO_write(GPIOB, 0, 0);
GPIO_write(GPIOC, 2, 0);
GPIO_write(GPIOC, 3, 0);
break;
case RAIN:
GPIO_write(GPIOB, 0, 1);
GPIO_write(GPIOC, 2, 0);
GPIO_write(GPIOC, 3, 0);
break;
case TIMED_ON:
GPIO_write(GPIOB, 0, 0);
GPIO_write(GPIOC, 2, 1);
GPIO_write(GPIOC, 3, 0);
break;
case CONTINUE:
GPIO_write(GPIOB, 0, 0);
GPIO_write(GPIOC, 2, 0);
GPIO_write(GPIOC, 3, 1);
break;
default:
break;
}
if(state == TIMED_ON || state == CONTINUE){
ADC_sample_channel(ADC1, 10);
ADC_start(ADC1);
while (!ADC_completed(ADC1)){ }
int value = ADC_read(ADC1);
wait_time = (value * 400) / 1023 + 100;
}
if (state == RAIN){
ADC_sample_channel(ADC1, 11);
ADC_start(ADC1);
while (!ADC_completed(ADC1)){ }
int value = ADC_read(ADC1);
rain_speed = (value * 500) / 1023 + 100;
drop_time = 1/ rain_speed;
}
}
void main(void){
setup();
for (;;){
loop();
}
}