-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegment_Functions.c
More file actions
341 lines (295 loc) · 9.66 KB
/
Segment_Functions.c
File metadata and controls
341 lines (295 loc) · 9.66 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
The functions for setting up and interfacing with the 7 segment display
All functions here are prototyped in the supporting_functions.h file
*/
#include "stm32f4xx_hal.h"
#include "supporting_functions.h"
#include "lis3dsh.h"
/*
PIN MAPPING FOR 7-SEGMENT
PE4 => pin 1 / Digit1
PE5 => pin 3 / section D
PE6 => pin 5 / section E
PE7 => pin 7 / decimal
PE8 => pin 11 / section F
PE9 => pin 13 / section C
PE10 => pin 14 / section A
PE11 => pin 15 / section G
PE12 => pin 16 / section B
PE13 => pin 2 /Digit2
PE14 => pin 6 / Digit3
*/
//first, set variables for convenience
uint16_t segment_A = GPIO_PIN_10;
uint16_t segment_B = GPIO_PIN_12;
uint16_t segment_C = GPIO_PIN_9;
uint16_t segment_D = GPIO_PIN_5;
uint16_t segment_E = GPIO_PIN_6;
uint16_t segment_F = GPIO_PIN_8;
uint16_t segment_G = GPIO_PIN_11;
uint16_t segment_decimal = GPIO_PIN_4;
uint16_t digit_1 = GPIO_PIN_4;
uint16_t digit_2 = GPIO_PIN_13;
uint16_t digit_3 = GPIO_PIN_14;
uint16_t decimal = GPIO_PIN_7;
GPIO_InitTypeDef GPIO_Init_Seg;
//the configuration function for the GPIOS for the 7-seg
//all GPIOS are on E and are set here
void Display_Config (void)
{
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_Init_Seg.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6| GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14;
GPIO_Init_Seg.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Init_Seg.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_Init_Seg.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOE, &GPIO_Init_Seg);
}
void Set_Off(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_decimal, GPIO_PIN_RESET);
}
//Set all segments to on
//usually for testing
void Set_All(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_decimal, GPIO_PIN_SET);
}
/*Sets a negative symbol on the 7-seg
no inputs or outputs*/
void Set_Neg(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
}
//sets a 0 digit to the 7 segment display
//no inputs or outputs required
void Set_0(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_RESET);
}
/*
the function that outputs a 9 at a digit on the 7-seg display
the digit needs to be set elsewhere (or this won't work)
The segments set are A,B,C,D, F and G
E is NOT set
*/
void Set_9(void)
{
//turn E off
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
// turn the rest on
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
}
/*
the function that outputs an 8 at a digit on the 7-seg display
the digit needs to be set elsewhere (or this won't work)
The segments set are A,B,C,D,E F and G
All are set, its identical to Set_All
*/
void Set_8(void)
{
Set_All();
}
//sets a 7 digit to the 7 segment display
//no inputs or outputs required
void Set_7(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_RESET);
}
//sets a 6 digit to the 7 segment display
//no inputs or outputs required
void Set_6(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_RESET);
}
//sets a 5 digit to the 7 segment display
//no inputs or outputs required
void Set_5(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
}
//sets a 4 digit to the 7 segment display
//no inputs or outputs required
void Set_4(void)
{
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
}
//sets a 3 digit to the 7 segment display
//no inputs or outputs required
void Set_3(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_RESET);
}
//sets a 2 digit to the 7 segment display
//no inputs or outputs required
void Set_2(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_SET);
}
//sets a 1 digit to the 7 segment display
//no inputs or outputs required
void Set_1(void)
{
HAL_GPIO_WritePin(GPIOE, segment_A ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_B, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_C ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, segment_D, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_E, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_F, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, segment_G, GPIO_PIN_RESET);
}
/*functions for controlling the decimal point
same style as the others
*/
void Set_Decimal(int decimal_flag)
{
if(decimal_flag)
{HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7 ,GPIO_PIN_SET);}
else
{HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7 ,GPIO_PIN_RESET);}
}
void Reset_Decimal(void)
{
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7 ,GPIO_PIN_RESET);
}
//determines what digit to write for the input digit
//input is an integer number between 0-9
//no output, directly sets the 7 seg
//the digit GPIO needs to be set externally, this only decides WHICH digit to write, from input
void Set_Number(int number)
{
switch(number)
{
case 0:
Set_0();
break;
case 1:
Set_1();
break;
case 2:
Set_2();
break;
case 3:
Set_3();
break;
case 4:
Set_4();
break;
case 5:
Set_5();
break;
case 6:
Set_6();
break;
case 7:
Set_7();
break;
case 8:
Set_8();
break;
case 9:
Set_9();
break;
case 10: //this is for the case to turn off. display nothing.
Set_Off();
break;
default:
Set_0();
break;
}
}
//turns on ALL digits simultaneously
//usually used for testing, thus called
void All_Digits(void)
{
HAL_GPIO_WritePin(GPIOE, digit_1 ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, digit_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, digit_3 ,GPIO_PIN_SET);
}
//turns on the most significant digit only (usually hundreds digit)
void First_Digit_Set(void)
{
HAL_GPIO_WritePin(GPIOE, digit_1 ,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, digit_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, digit_3 ,GPIO_PIN_RESET);
}
//turns on the second digit only (tens digit)
void Second_Digit_Set(void)
{
HAL_GPIO_WritePin(GPIOE, digit_1 ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, digit_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, digit_3 ,GPIO_PIN_RESET);
}
//turns on the third digit only, (ones digit)
void Third_Digit_Set(void)
{
HAL_GPIO_WritePin(GPIOE, digit_1 ,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, digit_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, digit_3 ,GPIO_PIN_SET);
}