-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomaticGate.c
More file actions
211 lines (183 loc) · 5.16 KB
/
automaticGate.c
File metadata and controls
211 lines (183 loc) · 5.16 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
/**
* @file automaticGate.c
*
* @author Alessandro Ferrante (github@alessandroferrante.net)
* @brief This file contains the main implementation for the Automated Gate system.
*
* This module is responsible for initializing the system, handling the main control loop,
* and managing the interactions between different components of the Automated Gate system.
*
* The system operates in different states: IDLE, OPENING, WAIT, CLOSING, and STOPPED.
* - IDLE: The gate is closed and waiting for an open command.
* - OPENING: The gate is in the process of opening.
* - WAIT: The gate is fully open and waiting for a close command after a delay.
* - CLOSING: The gate is in the process of closing.
* - STOPPED: The gate operation is halted.
*
* The system uses timers (TIM2 and TIM3) to manage the timing of gate operations and external interrupts (EXTI)
* to handle button presses for controlling the gate state transitions.
*
* The display shows the gate's opening percentage using a simple visual representation.
*
* This implementation is designed to run on an STM32 microcontroller.
* @version 0.1
* @date 2024-10-
*
* @copyright Copyright (c) 2025
*
*/
#include "stm32_unict_lib.h"
#include <string.h>
#include <stdbool.h>
typedef enum {
IDLE,
OPENING,
WAIT,
CLOSING,
STOPPED
}t_state;
t_state state = IDLE;
int percentage = 0;
int t_wait = 0;
void setup(void)
{
ClockConfig();
// configure the clock @ 84 MHz
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, 5000);//500ms
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);
}
void TIM2_IRQHandler(void){
if (TIM_update_check(TIM2) && (state==OPENING || state== CLOSING)) {
GPIO_write(GPIOC, 2, 0);
GPIO_write(GPIOC, 3, 0);
GPIO_toggle(GPIOB, 0);
percentage++;
TIM_update_clear(TIM2);
}
else if(state == WAIT)
GPIO_write(GPIOC, 2, 1);
else
GPIO_write(GPIOC, 3, 1);
}
void TIM3_IRQHandler(void){
if (TIM_update_check(TIM3)) {
switch(state){
case OPENING:
percentage++;
if (percentage >= 100)
state = WAIT;
break;
case CLOSING:
percentage--;
if(percentage == 0)
state = IDLE;
break;
case WAIT:
t_wait++;
if (t_wait == 40){
t_wait = 0;
state = CLOSING;
}
break;
case STOPPED:
GPIO_write(GPIOB, 0, 1);
GPIO_write(GPIOC, 2, 1);
GPIO_write(GPIOC, 3, 1);
break;
default:
GPIO_write(GPIOB, 0, 0);
break;
}
TIM_update_clear(TIM3);
}
}
void EXTI15_10_IRQHandler(void)
{
if (EXTI_isset(EXTI10)) {
// button X
if(state == IDLE || state == CLOSING || state == STOPPED)
state = OPENING;
EXTI_clear(EXTI10);
}
}
void EXTI4_IRQHandler(void)
{
if (EXTI_isset(EXTI4)) {
// button Y
if(state == WAIT || state == OPENING || state == STOPPED)
state = CLOSING;
EXTI_clear(EXTI4);
}
}
void EXTI9_5_IRQHandler(void)
{
if (EXTI_isset(EXTI5)) {
// button Z
switch(state){
case OPENING:
break;
case WAIT:
t_wait = 0;
break;
case CLOSING:
state = OPENING;
break;
}
EXTI_clear(EXTI5);
}
if (EXTI_isset(EXTI6)) {
// button T
if(state == OPENING || state == CLOSING || state == WAIT)
state = STOPPED;
EXTI_clear(EXTI6);
}
}
void loop(void)
{
char s[5];
if(percentage < 25)
strcpy(s, "----");
else if(percentage < 50)
strcpy(s, "--- ");
else if(percentage < 75)
strcpy(s, "-- ");
else if(percentage < 100)
strcpy(s, "- ");
else
strcpy(s, " ");
//sprintf(s, "%4d", percentage);
DISPLAY_puts(0, s);
}
int main()
{
setup();
for (;;) {
loop();
}
}