main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 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. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. /* USER CODE END Includes */
  24. /* Private typedef -----------------------------------------------------------*/
  25. /* USER CODE BEGIN PTD */
  26. /* USER CODE END PTD */
  27. /* Private define ------------------------------------------------------------*/
  28. /* USER CODE BEGIN PD */
  29. /* USER CODE END PD */
  30. /* Private macro -------------------------------------------------------------*/
  31. /* USER CODE BEGIN PM */
  32. /* USER CODE END PM */
  33. /* Private variables ---------------------------------------------------------*/
  34. UART_HandleTypeDef huart1;
  35. /* USER CODE BEGIN PV */
  36. /* USER CODE END PV */
  37. /* Private function prototypes -----------------------------------------------*/
  38. void SystemClock_Config(void);
  39. static void MX_GPIO_Init(void);
  40. static void MX_USART1_UART_Init(void);
  41. /* USER CODE BEGIN PFP */
  42. /* USER CODE END PFP */
  43. /* Private user code ---------------------------------------------------------*/
  44. /* USER CODE BEGIN 0 */
  45. #include <stdio.h>
  46. // 重定向fputc函数到USART1
  47. // int _write(int file, char *ptr, int len)
  48. // {
  49. // HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, HAL_MAX_DELAY);
  50. // return len;
  51. // }
  52. //
  53. // int fputc(int ch, FILE *f)
  54. // {
  55. // uint8_t c = ch;
  56. // HAL_UART_Transmit(&huart1, &c, 1, HAL_MAX_DELAY);
  57. // return ch;
  58. // }
  59. int __io_putchar(int ch)
  60. {
  61. uint8_t c = ch;
  62. HAL_UART_Transmit(&huart1, &c, 1, HAL_MAX_DELAY);
  63. return ch;
  64. }
  65. /* USER CODE END 0 */
  66. /**
  67. * @brief The application entry point.
  68. * @retval int
  69. */
  70. int main(void)
  71. {
  72. /* USER CODE BEGIN 1 */
  73. /* USER CODE END 1 */
  74. /* MCU Configuration--------------------------------------------------------*/
  75. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  76. HAL_Init();
  77. /* USER CODE BEGIN Init */
  78. /* USER CODE END Init */
  79. /* Configure the system clock */
  80. SystemClock_Config();
  81. /* USER CODE BEGIN SysInit */
  82. /* USER CODE END SysInit */
  83. /* Initialize all configured peripherals */
  84. MX_GPIO_Init();
  85. MX_USART1_UART_Init();
  86. /* USER CODE BEGIN 2 */
  87. /* USER CODE END 2 */
  88. /* Infinite loop */
  89. /* USER CODE BEGIN WHILE */
  90. int i = 0;
  91. while (1)
  92. {
  93. /* USER CODE END WHILE */
  94. // HAL_GPIO_TogglePin(GPIOA, LED_Pin);
  95. // printf("i = %d\r\n", i);
  96. // i++;
  97. GPIO_PinState keyState = HAL_GPIO_ReadPin(GPIOC, KEY1_Pin);
  98. if (keyState == GPIO_PIN_RESET) { // 假设按键按下时为低电平
  99. // 按键按下时的处理逻辑
  100. printf("我被按下了\r\n");
  101. HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_RESET);
  102. }else {
  103. printf("我被松开了\r\n");
  104. HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_SET);
  105. }
  106. HAL_Delay(500);
  107. /* USER CODE BEGIN 3 */
  108. }
  109. /* USER CODE END 3 */
  110. }
  111. /**
  112. * @brief System Clock Configuration
  113. * @retval None
  114. */
  115. void SystemClock_Config(void)
  116. {
  117. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  118. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  119. /** Initializes the RCC Oscillators according to the specified parameters
  120. * in the RCC_OscInitTypeDef structure.
  121. */
  122. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  123. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  124. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  125. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  126. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  127. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  128. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  129. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  130. {
  131. Error_Handler();
  132. }
  133. /** Initializes the CPU, AHB and APB buses clocks
  134. */
  135. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  136. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  137. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  138. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  139. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  140. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  141. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. }
  146. /**
  147. * @brief USART1 Initialization Function
  148. * @param None
  149. * @retval None
  150. */
  151. static void MX_USART1_UART_Init(void)
  152. {
  153. /* USER CODE BEGIN USART1_Init 0 */
  154. /* USER CODE END USART1_Init 0 */
  155. /* USER CODE BEGIN USART1_Init 1 */
  156. /* USER CODE END USART1_Init 1 */
  157. huart1.Instance = USART1;
  158. huart1.Init.BaudRate = 115200;
  159. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  160. huart1.Init.StopBits = UART_STOPBITS_1;
  161. huart1.Init.Parity = UART_PARITY_NONE;
  162. huart1.Init.Mode = UART_MODE_TX_RX;
  163. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  164. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  165. if (HAL_UART_Init(&huart1) != HAL_OK)
  166. {
  167. Error_Handler();
  168. }
  169. /* USER CODE BEGIN USART1_Init 2 */
  170. /* USER CODE END USART1_Init 2 */
  171. }
  172. /**
  173. * @brief GPIO Initialization Function
  174. * @param None
  175. * @retval None
  176. */
  177. static void MX_GPIO_Init(void)
  178. {
  179. GPIO_InitTypeDef GPIO_InitStruct = {0};
  180. /* USER CODE BEGIN MX_GPIO_Init_1 */
  181. /* USER CODE END MX_GPIO_Init_1 */
  182. /* GPIO Ports Clock Enable */
  183. __HAL_RCC_GPIOD_CLK_ENABLE();
  184. __HAL_RCC_GPIOC_CLK_ENABLE();
  185. __HAL_RCC_GPIOA_CLK_ENABLE();
  186. /*Configure GPIO pin Output Level */
  187. HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
  188. /*Configure GPIO pin : KEY1_Pin */
  189. GPIO_InitStruct.Pin = KEY1_Pin;
  190. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  191. GPIO_InitStruct.Pull = GPIO_NOPULL;
  192. HAL_GPIO_Init(KEY1_GPIO_Port, &GPIO_InitStruct);
  193. /*Configure GPIO pin : LED_Pin */
  194. GPIO_InitStruct.Pin = LED_Pin;
  195. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  196. GPIO_InitStruct.Pull = GPIO_NOPULL;
  197. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  198. HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
  199. /* USER CODE BEGIN MX_GPIO_Init_2 */
  200. /* USER CODE END MX_GPIO_Init_2 */
  201. }
  202. /* USER CODE BEGIN 4 */
  203. /* USER CODE END 4 */
  204. /**
  205. * @brief This function is executed in case of error occurrence.
  206. * @retval None
  207. */
  208. void Error_Handler(void)
  209. {
  210. /* USER CODE BEGIN Error_Handler_Debug */
  211. /* User can add his own implementation to report the HAL error return state */
  212. __disable_irq();
  213. while (1)
  214. {
  215. }
  216. /* USER CODE END Error_Handler_Debug */
  217. }
  218. #ifdef USE_FULL_ASSERT
  219. /**
  220. * @brief Reports the name of the source file and the source line number
  221. * where the assert_param error has occurred.
  222. * @param file: pointer to the source file name
  223. * @param line: assert_param error line source number
  224. * @retval None
  225. */
  226. void assert_failed(uint8_t *file, uint32_t line)
  227. {
  228. /* USER CODE BEGIN 6 */
  229. /* User can add his own implementation to report the file name and line number,
  230. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  231. /* USER CODE END 6 */
  232. }
  233. #endif /* USE_FULL_ASSERT */