-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bai1.c
182 lines (154 loc) · 5.17 KB
/
Bai1.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
/*******************************************************************************
*
* Copyright (c) 2020
* Lumi, JSC.
* All Rights Reserved
*
* Description: Lab2.1
*
* Author: Developer embedded team
*
* Last Changed By: $Author: HoangNH $
* Revision: $Revision: 1.0 $
* Last Changed: $Date: 10/7/2020 $
*
******************************************************************************/
#include "stm32f401re_rcc.h"
#include "stm32f401re_gpio.h"
//function declaration---------------------------------------------------------
static void LedControl_SetStatus(GPIO_TypeDef * GPIOx, uint8_t GPIO_PIN, uint8_t Status);
static void Delay(uint32_t ms);
static uint8_t ButtonRead_Status(GPIO_TypeDef * GPIOx, uint32_t GPIO_PIN);
static void Button_Init(void);
static void Led_Init(void);
static void AppInitCommon(void);
//-----------------------------------------------------------------------------
int main(void)
{
AppInitCommon();
while(1)
{
//Turn on the Led when pressing the button----------------------------
if (ButtonRead_Status(BUTTON_GPIO_PORT, BUTTON_PIN13) == GPIO_PIN_LOW)
{
Delay(5);
LedControl_SetStatus(LED_GPIO_PORT, LED_PIN5, GPIO_PIN_SET);
}
//Turn off the led when pressing the button---------------------------
else
{
LedControl_SetStatus(LED_GPIO_PORT, LED_PIN5, GPIO_PIN_RESET);
}
}
}
/**
* @func Initializes
* @brief Initializes All Peripheral
* @param None
* @retval None
*/
static void AppInitCommon(void)
{
//System Init--------------------------------------------------------------
SystemCoreClockUpdate();
//Button Init--------------------------------------------------------------
Button_Init();
//Led Init-----------------------------------------------------------------
Led_Init();
}
/**
* @func LedControl_SetStatus
* @brief Control Turn on or Turn off Led
* @param None
* @retval None
*/
static void LedControl_SetStatus(GPIO_TypeDef * GPIOx, uint8_t GPIO_PIN, uint8_t Status)
{
//Set bit in BSRR Registers------------------------------------------------
if (Status == GPIO_PIN_SET)
{
GPIOx->BSRR |= 1 << GPIO_PIN;
}
//Reset bit in BSRR Registers----------------------------------------------
else if (Status == GPIO_PIN_RESET)
{
GPIOx->BSRR |= 1 << (GPIO_PIN + 16);
}
}
/**
* @func ButtonRead_Status
* @brief Read Status Button
* @param None
* @retval None
*/
static uint8_t ButtonRead_Status(GPIO_TypeDef * GPIOx, uint32_t GPIO_PIN)
{
uint32_t Read_Pin;
//Read bit in IDR Registers------------------------------------------------
Read_Pin = (GPIOx->IDR) >> GPIO_PIN;
Read_Pin = Read_Pin & 0x01;
return Read_Pin;
}
/**
* @func Delay
* @brief Delay Time
* @param None
* @retval None
*/
static void Delay(uint32_t ms)
{
uint32_t i, j;
for (i = 0 ; i < ms ; i ++)
{
for (j = 0; j < 5000; j++) {;}
}
}
/**
* @func Led_Init
* @brief Initializes GPIO Use Led
* @param None
* @retval None
*/
static void Led_Init(void)
{
//Declare type variable GPIO Struct----------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
//Enable Clock GPIOA-------------------------------------------------------
RCC_AHB1PeriphClockCmd(LEDControl_SetClock, ENABLE);
//Choose Pin Led-----------------------------------------------------------
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
//Choose Pin Led as Out----------------------------------------------------
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
//Choose Speed Pin---------------------------------------------------------
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//Select type--------------------------------------------------------------
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//Select status------------------------------------------------------------
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
//The function initializes all of the above values-------------------------
GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
/**
* @func ButtonInit
* @brief Initializes GPIO Use Button
* @param None
* @retval None
*/
static void Button_Init(void)
{
//Declare type variable GPIO Struc-----------------------------------------
GPIO_InitTypeDef GPIO_InitStructure ;
//Enable Clock GPIOC-------------------------------------------------------
RCC_AHB1PeriphClockCmd(BUTTONControl_SetClock, ENABLE);
//Choose Pin BUTTON--------------------------------------------------------
GPIO_InitStructure.GPIO_Pin = BUTTON_GPIO_PIN;
//Choose Pin Led as Input--------------------------------------------------
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
//Choose Speed Pin---------------------------------------------------------
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//Select status------------------------------------------------------------
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
//The function initializes all of the above values-------------------------
GPIO_Init(BUTTON_GPIO_PORT, &GPIO_InitStructure);
}
/* END_FILE */