0% found this document useful (0 votes)
31 views

#Include "Main.H": Systemclock - Config Periphcommonclock - Config MX - Gpio - Init

This C code defines the main functions for initializing an MCU. It includes functions for configuring the system clock, initializing GPIO pins including LEDs and buttons, and implementing an infinite while loop to toggle the LEDs when a button is pressed. The main function calls the initialization functions and contains the infinite loop that checks the button state and controls the LEDs accordingly.

Uploaded by

allexims
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

#Include "Main.H": Systemclock - Config Periphcommonclock - Config MX - Gpio - Init

This C code defines the main functions for initializing an MCU. It includes functions for configuring the system clock, initializing GPIO pins including LEDs and buttons, and implementing an infinite while loop to toggle the LEDs when a button is pressed. The main function calls the initialization functions and contains the infinite loop that checks the button state and controls the LEDs accordingly.

Uploaded by

allexims
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1 /* USER CODE BEGIN Header */

2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2024 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 /* USER CODE END Header */
19 /* Includes ------------------------------------------------------------------*/
20 #include "main.h"
21
22 /* Private includes ----------------------------------------------------------*/
23 /* USER CODE BEGIN Includes */
24
25 /* USER CODE END Includes */
26
27 /* Private typedef -----------------------------------------------------------*/
28 /* USER CODE BEGIN PTD */
29
30 /* USER CODE END PTD */
31
32 /* Private define ------------------------------------------------------------*/
33 /* USER CODE BEGIN PD */
34
35 /* USER CODE END PD */
36
37 /* Private macro -------------------------------------------------------------*/
38 /* USER CODE BEGIN PM */
39
40 /* USER CODE END PM */
41
42 /* Private variables ---------------------------------------------------------*/
43
44 /* USER CODE BEGIN PV */
45
46 /* USER CODE END PV */
47
48 /* Private function prototypes -----------------------------------------------*/
49 void SystemClock_Config(void);
50 void PeriphCommonClock_Config(void);
51 static void MX_GPIO_Init(void);
52 /* USER CODE BEGIN PFP */
53
54 /* USER CODE END PFP */
55
56 /* Private user code ---------------------------------------------------------*/
57 /* USER CODE BEGIN 0 */
58
59 /* USER CODE END 0 */
60
61 /**
62 * @brief The application entry point.
63 * @retval int
64 */
65 int main(void)
66 {
67 /* USER CODE BEGIN 1 */
68
69 /* USER CODE END 1 */
70
71 /* MCU Configuration--------------------------------------------------------*/
72
73 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
74 HAL_Init();
75
76 /* USER CODE BEGIN Init */
77
78 /* USER CODE END Init */
79
80 /* Configure the system clock */
81 SystemClock_Config();
82
83 /* Configure the peripherals common clocks */
84 PeriphCommonClock_Config();
85
86 /* USER CODE BEGIN SysInit */
87
88 /* USER CODE END SysInit */
89
90 /* Initialize all configured peripherals */
91 MX_GPIO_Init();
92 /* USER CODE BEGIN 2 */
93 int status=0;
94 /* USER CODE END 2 */
95
96 /* Infinite loop */
97 /* USER CODE BEGIN WHILE */
98 while (1)
99 {
100 /*HAL_GPIO_WritePin(GPIOB, LED1_Pin, GPIO_PIN_SET);
101 HAL_GPIO_WritePin(GPIOB, LED3_Pin, GPIO_PIN_RESET);
102 HAL_Delay(400);
103 HAL_GPIO_WritePin(GPIOB, LED1_Pin, GPIO_PIN_RESET);
104 HAL_GPIO_WritePin(GPIOB, LED3_Pin, GPIO_PIN_SET);
105 HAL_Delay(400);*/
106
107 if(!HAL_GPIO_ReadPin(GPIOC, SW1_Pin) && status==0)
108 {
109 HAL_GPIO_WritePin(GPIOB, LED2_Pin, GPIO_PIN_SET);
110 status=1;
111 }
112 else if(!HAL_GPIO_ReadPin(GPIOC, SW1_Pin) && status==1)
113 {
114
115 HAL_GPIO_WritePin(GPIOB, LED2_Pin, GPIO_PIN_RESET);
116 status=0;
117 }
118 HAL_Delay(200);
119 /* USER CODE END WHILE */
120
121 /* USER CODE BEGIN 3 */
122 }
123 /* USER CODE END 3 */
124 }
125
126 /**
127 * @brief System Clock Configuration
128 * @retval None
129 */
130 void SystemClock_Config(void)
131 {
132 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
133 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
134
135 /** Configure the main internal regulator output voltage
136 */
137 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
138
139 /** Initializes the RCC Oscillators according to the specified parameters
140 * in the RCC_OscInitTypeDef structure.
141 */
142 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
143 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
144 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
145 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
146 RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
147 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
148 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
149 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
150 {
151 Error_Handler();
152 }
153
154 /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
155 */
156 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
157 |RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
158 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
159 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
160 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
161 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
162 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
163 RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
164 RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
165
166 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
167 {
168 Error_Handler();
169 }
170 }
171
172 /**
173 * @brief Peripherals Common Clock Configuration
174 * @retval None
175 */
176 void PeriphCommonClock_Config(void)
177 {
178 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
179
180 /** Initializes the peripherals clock
181 */
182 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS;
183 PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSI;
184 PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;
185
186 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
187 {
188 Error_Handler();
189 }
190 /* USER CODE BEGIN Smps */
191
192 /* USER CODE END Smps */
193 }
194
195 /**
196 * @brief GPIO Initialization Function
197 * @param None
198 * @retval None
199 */
200 static void MX_GPIO_Init(void)
201 {
202 GPIO_InitTypeDef GPIO_InitStruct = {0};
203 /* USER CODE BEGIN MX_GPIO_Init_1 */
204 /* USER CODE END MX_GPIO_Init_1 */
205
206 /* GPIO Ports Clock Enable */
207 __HAL_RCC_GPIOC_CLK_ENABLE();
208 __HAL_RCC_GPIOB_CLK_ENABLE();
209
210 /*Configure GPIO pin Output Level */
211 HAL_GPIO_WritePin(GPIOB, LED2_Pin|LED3_Pin|LED1_Pin, GPIO_PIN_RESET);
212
213 /*Configure GPIO pin : SW1_Pin */
214 GPIO_InitStruct.Pin = SW1_Pin;
215 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
216 GPIO_InitStruct.Pull = GPIO_PULLUP;
217 HAL_GPIO_Init(SW1_GPIO_Port, &GPIO_InitStruct);
218
219 /*Configure GPIO pins : LED2_Pin LED3_Pin LED1_Pin */
220 GPIO_InitStruct.Pin = LED2_Pin|LED3_Pin|LED1_Pin;
221 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
222 GPIO_InitStruct.Pull = GPIO_NOPULL;
223 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
224 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
225
226 /* USER CODE BEGIN MX_GPIO_Init_2 */
227 /* USER CODE END MX_GPIO_Init_2 */
228 }
229
230 /* USER CODE BEGIN 4 */
231
232 /* USER CODE END 4 */
233
234 /**
235 * @brief This function is executed in case of error occurrence.
236 * @retval None
237 */
238 void Error_Handler(void)
239 {
240 /* USER CODE BEGIN Error_Handler_Debug */
241 /* User can add his own implementation to report the HAL error return state */
242 __disable_irq();
243 while (1)
244 {
245 }
246 /* USER CODE END Error_Handler_Debug */
247 }
248
249 #ifdef USE_FULL_ASSERT
250 /**
251 * @brief Reports the name of the source file and the source line number
252 * where the assert_param error has occurred.
253 * @param file: pointer to the source file name
254 * @param line: assert_param error line source number
255 * @retval None
256 */
257 void assert_failed(uint8_t *file, uint32_t line)
258 {
259 /* USER CODE BEGIN 6 */
260 /* User can add his own implementation to report the file name and line number,
261 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
262 /* USER CODE END 6 */
263 }
264 #endif /* USE_FULL_ASSERT */
265

You might also like