-
Notifications
You must be signed in to change notification settings - Fork 0
/
P4.c
163 lines (131 loc) · 5.03 KB
/
P4.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
#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;
NVIC_InitTypeDef NVIC_InitStructure;
void GPIO_Config(void); // those functions defined
void EXTI_Config(void);
void TIM2_Config(void); // at the bottom part
static uint8_t state=0; // variable for "switch case"
int time=0; // variable for delay and timing purposes
void EXTI1_IRQHandler(void) //external interrupt handler function
{
if(state==2) // checking if interrupt occured while we are in case 2
{if((100-time)>20) //checking if the time is bigger than 20
{ //because we don't want the pedestrians to wait more.
time=80; // setting the time as "80" for waiting 2seconds (100-80=20=2seconds)
}
}
EXTI_ClearITPendingBit(EXTI_Line1); // clearing the interrupt flag
}
void TIM2_IRQHandler(void)
{if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{ time++; //increasing the time one more for every 0.1 seconds
TIM_ClearITPendingBit(TIM2,TIM_IT_Update); // clearing the interrupt flag
}
}
int main(void)
{
GPIO_Config();
EXTI_Config();
TIM2_Config();
while(1){
switch(state){ // switch case can be used for "Finite State Machines"
case 0: //For Cars: Red Led ON, Yellow and Green Leds OFF ; For Pedestrians:Green Led ON, Red Led OFF
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
GPIO_SetBits(GPIOA,GPIO_Pin_7);
if(time==100) // after 10 seconds
{ time=0;
state=1; //Going to state 1
}
break;
case 1: //For Cars: Yellow Led ON, Red Led OFF ; For Pedestrians: Red Led ON, Green Led OFF
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
if(time==20) // after 2 seconds
{
time=0;
state=2; //Going to state 2
}
break;
case 2: //For Cars: Green Led ON, Yellow Led OFF ; For Pedestrians: Red Led ON
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
if(time==100) // after 10 seconds
{ time=0;
state=3; //Going to state 3
}
break;
case 3: //For Cars: Yellow Led ON, Green Led OFF ; For Pedestrians: Red Led ON
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
if(time==20) // after 2 seconds
{ time=0;
state=0; //Going to state 0
}
break;
default:
state=0; // Going to state 0 as default
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 2 as Red, Pin 3 as Yellow, Pin 4 as Green
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configuring PA1 as input for Button
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
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_PinSource1);
EXTI_DeInit();
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
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
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_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
TIM_TimeBaseStructure.TIM_Period = 49999;
TIM_TimeBaseStructure.TIM_Prescaler = 143;
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_Cmd(TIM2, ENABLE);
// Configuring nested vector interrupt controller for timer interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}