-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2.c
116 lines (89 loc) · 3.34 KB
/
P2.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
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "delay.h"
GPIO_InitTypeDef GPIO_InitStructure;
void offLEDS(void); // those functions defined
void onLEDS(void); // at the bottom of the page
int main(void)
{
// Enabling clock for PortA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Configuring A2,A3 and A4 as output for LEDS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configuring PA7 as input for Button
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
delayInit();
int state; //variable for "switch case"
int a; // creating variables for "for loops" inside "switch case"
int b;
int c;
int d;
while(1){
switch(state){ // switch case can be used for "Finite State Machines"
case 1: //All LEDS are ON
onLEDS();
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
offLEDS(); state=2; } // if button is released, it will switch to case 2
break;
case 2: //LEDS blink as "RED->YELLOW->GREEN" order
for (a=0;a<=500;a+=10) { // Red LED becomes ON
GPIO_SetBits(GPIOA,GPIO_Pin_2);
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
state=3; break;} // if button is pressed, it will switch to case 3
delayMs(10);
}
for (b=0;b<=500;b+=10) {
GPIO_ResetBits(GPIOA,GPIO_Pin_2); // Red LED becomes OFF
GPIO_SetBits(GPIOA,GPIO_Pin_3); // Yellow LED becomes ON
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
state=3; break; } // if button is pressed, it will switch to case 3
delayMs(10);
}
for (c=0;c<=500;c+=10) {
GPIO_ResetBits(GPIOA,GPIO_Pin_3); // Yellow LED becomes OFF
GPIO_SetBits(GPIOA,GPIO_Pin_4); // Green LED becomes ON
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
state=3; break; } // if button is pressed, it will switch to case 3
delayMs(10);
}
for(d=0;d<=100;d+=10){
GPIO_ResetBits(GPIOA,GPIO_Pin_4); // Green LED becomes OFF
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
state=3; break;} // if button is pressed, it will switch to case 3
delayMs(10);
}
break;
case 3: // All LEDS are ON
onLEDS();
delayMs(10);
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
state=0; break; } //if button is released, it will switch to default case
break;
default:
offLEDS(); // As default, all LEDS are OFF
delayMs(10);
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) {
state=1; } //if button 1 pressed, it will switch to case 1
delayMs(10);
break;
}
}
}
void onLEDS(void) //function for making all LEDS ON
{ GPIO_SetBits(GPIOA,GPIO_Pin_2);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
void offLEDS(void) //function for making all LEDS OFF
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
}