-
Notifications
You must be signed in to change notification settings - Fork 0
/
P5.c
312 lines (249 loc) · 9.28 KB
/
P5.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
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
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "delay.h"
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void GPIO_Config(void); // those functions defined
void EXTI_Config(void);
void TIM2_Config(void); // at the bottom part
int state=0; // variables for "switch case"
int count=0;
int count2=0; // counting,debounce
int a=0;
int b=0; //and "brightness" purposes
void EXTI4_IRQHandler(void) //external interrupt handler function for button1(pin4)
{ if(EXTI_GetITStatus(EXTI_Line4)!=RESET)
{
a++;
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
void EXTI9_5_IRQHandler(void) //external interrupt handler function for button2(pin5)
{
if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
{
b++;
}
EXTI_ClearITPendingBit(EXTI_Line5);
}
void TIM2_IRQHandler(void) //timer interrupt handler (empty)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) // Periodic IRQ
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
void LEDSwitch(void) //LED Switch Cases Control
{
//looks a little bit complex, because of bounce issue.
//i debounced it and handled that issue. Short explanation:
//i used a counter and if the state is stable more than 100 times,
//it is going inside this function. After that, we are checking if there is no more signal coming from button.
if(a>=1)
{count++;
if(count>=100)
{if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4))
{
count=0;a=0;
if((state==3)||(state==6)||(state==9)) //if red led is on, when the button pressed;
//all leds will be off(it switches to default state)
{state=0;}
else
{state=(state%3)+1;} //else, it will switch to next LED.
}
else
{count=0;a=0;}
}
}
}
void Brightness(void) //Brightness Switch Cases Control
{
//looks a little bit complex, because of bounce issue.
//i debounced it and handled that issue. Short explanation:
//i used a counter and if the state is stable more than 100 times,
//it is going inside this function. After that, we are checking if there is no more signal coming from button.
if(b>=1)
{count2++;
if(count2>=100)
{if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5))
{count2=0;b=0;
if(state==0) //if it is default state, "brightness" control is not used, because all LEDS are off.
{count2=0;b=0;state=0;}
else if(state>=7) //if it is already in the brightest state, it will switch to the least bright state.
{state=(state%3);
if(state==0) // Since state=9 and 9%3=0, it was not working properly.
{state=3;} //So i solved this issue by using this line of code.
}
else
{state=state+3;} //Else, it will switch to state+3, state. which is 1 more brighter state.
}
else
{count2=0;b=0;}
}
}
}
int main(void)
{
GPIO_Config();
EXTI_Config();
TIM2_Config();
delayInit();
LEDSwitch();
Brightness();
while(1){
switch(state){
case 1: //RED LED is ON, Lowest Brightness
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 2: //YELLOW LED is ON, Lowest Brightness
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 3: //GREEN LED is ON, Lowest Brightness
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 4: //RED LED is ON, Medium Brightness
TIM_OCInitStructure.TIM_Pulse = 1500;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 5: //YELLOW LED is ON, Medium Brightness
TIM_OCInitStructure.TIM_Pulse = 1500;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 6: //GREEN LED is ON, Medium Brightness
TIM_OCInitStructure.TIM_Pulse = 1500;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 7: //RED LED is ON, Brightest
TIM_OCInitStructure.TIM_Pulse = 3000;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 8: //YELLOW LED is ON, Brightest
TIM_OCInitStructure.TIM_Pulse = 3000;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
case 9: //GREEN LED is ON, Brightest
TIM_OCInitStructure.TIM_Pulse = 3000;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
default: //DEFAULT STATE, ALL LEDS are OFF.
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
LEDSwitch();
Brightness();
break;
}
}
}
void GPIO_Config(void)
{ // Enabling clock for PortA, External Interrupt and Timer Interrupt
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
// Configuring A2,A3 and A4 as output for LEDS; // I used Pin 0 as Red, Pin 1 as Yellow, Pin 2 as Green
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configuring PA1 as input for Button
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void EXTI_Config(void)
{
// Configuring external interrupt
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4 | GPIO_PinSource5);
EXTI_DeInit();
EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// Configuring nested vector interrupt controller for external interrupt 4
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Configuring nested vector interrupt controller for external interrupt 5
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM2_Config(void)
{
// Configuring timer interrupt for Pin1
TIM_TimeBaseStructure.TIM_Period = 3499;
TIM_TimeBaseStructure.TIM_Prescaler = 200;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
// Configuring timer interrupt for Pin2
TIM_TimeBaseStructure.TIM_Period = 3499;
TIM_TimeBaseStructure.TIM_Prescaler = 200;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
// Configuring timer interrupt for Pin3
TIM_TimeBaseStructure.TIM_Period = 3499;
TIM_TimeBaseStructure.TIM_Prescaler = 200;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
}