Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2023
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.27 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2023 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. #define PORT_LED GPIOB
  35. #define PORT_Bouton GPIOC
  36.  
  37. #define PIN_LED GPIO_PIN_13
  38. #define PIN_Bouton GPIO_PIN_9
  39. /* USER CODE END PD */
  40.  
  41. /* Private macro -------------------------------------------------------------*/
  42. /* USER CODE BEGIN PM */
  43.  
  44. /* USER CODE END PM */
  45.  
  46. /* Private variables ---------------------------------------------------------*/
  47. UART_HandleTypeDef huart2;
  48.  
  49. /* USER CODE BEGIN PV */
  50.  
  51. /* USER CODE END PV */
  52.  
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. static void MX_GPIO_Init(void);
  56. static void MX_USART2_UART_Init(void);
  57. /* USER CODE BEGIN PFP */
  58.  
  59. /* USER CODE END PFP */
  60.  
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63.  
  64. /* USER CODE END 0 */
  65.  
  66. /**
  67.   * @brief  The application entry point.
  68.   * @retval int
  69.   */
  70. int main(void)
  71. {
  72.   /* USER CODE BEGIN 1 */
  73.  
  74.   /* USER CODE END 1 */
  75.  
  76.   /* MCU Configuration--------------------------------------------------------*/
  77.  
  78.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79.   HAL_Init();
  80.  
  81.   /* USER CODE BEGIN Init */
  82.  
  83.   /* USER CODE END Init */
  84.  
  85.   /* Configure the system clock */
  86.   SystemClock_Config();
  87.  
  88.   /* USER CODE BEGIN SysInit */
  89.  
  90.   /* USER CODE END SysInit */
  91.  
  92.   /* Initialize all configured peripherals */
  93.   MX_GPIO_Init();
  94.   MX_USART2_UART_Init();
  95.   /* USER CODE BEGIN 2 */
  96.  
  97.   /* USER CODE END 2 */
  98.  
  99.   /* Infinite loop */
  100.   /* USER CODE BEGIN WHILE */
  101.   while (1)
  102.   {
  103.     /* USER CODE END WHILE */
  104.  
  105.     /* USER CODE BEGIN 3 */
  106.   }
  107.   /* USER CODE END 3 */
  108. }
  109.  
  110. /**
  111.   * @brief System Clock Configuration
  112.   * @retval None
  113.   */
  114. void SystemClock_Config(void)
  115. {
  116.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  117.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  118.  
  119.   /** Initializes the RCC Oscillators according to the specified parameters
  120.   * in the RCC_OscInitTypeDef structure.
  121.   */
  122.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  123.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  124.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  125.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  126.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  127.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  128.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  129.   {
  130.     Error_Handler();
  131.   }
  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.  
  142.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  143.   {
  144.     Error_Handler();
  145.   }
  146. }
  147.  
  148. /**
  149.   * @brief USART2 Initialization Function
  150.   * @param None
  151.   * @retval None
  152.   */
  153. static void MX_USART2_UART_Init(void)
  154. {
  155.  
  156.   /* USER CODE BEGIN USART2_Init 0 */
  157.  
  158.   /* USER CODE END USART2_Init 0 */
  159.  
  160.   /* USER CODE BEGIN USART2_Init 1 */
  161.  
  162.   /* USER CODE END USART2_Init 1 */
  163.   huart2.Instance = USART2;
  164.   huart2.Init.BaudRate = 38400;
  165.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  166.   huart2.Init.StopBits = UART_STOPBITS_1;
  167.   huart2.Init.Parity = UART_PARITY_NONE;
  168.   huart2.Init.Mode = UART_MODE_TX_RX;
  169.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  170.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  171.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  172.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  173.   if (HAL_UART_Init(&huart2) != HAL_OK)
  174.   {
  175.     Error_Handler();
  176.   }
  177.   /* USER CODE BEGIN USART2_Init 2 */
  178.  
  179.   /* USER CODE END USART2_Init 2 */
  180.  
  181. }
  182.  
  183. /**
  184.   * @brief GPIO Initialization Function
  185.   * @param None
  186.   * @retval None
  187.   */
  188. static void MX_GPIO_Init(void)
  189. {
  190.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  191. /* USER CODE BEGIN MX_GPIO_Init_1 */
  192. /* USER CODE END MX_GPIO_Init_1 */
  193.  
  194.   /* GPIO Ports Clock Enable */
  195.   __HAL_RCC_GPIOC_CLK_ENABLE();
  196.   __HAL_RCC_GPIOF_CLK_ENABLE();
  197.   __HAL_RCC_GPIOA_CLK_ENABLE();
  198.   __HAL_RCC_GPIOB_CLK_ENABLE();
  199.  
  200.   /*Configure GPIO pin Output Level */
  201.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  202.  
  203.   /*Configure GPIO pins : B1_Pin monBouton_Pin */
  204.   GPIO_InitStruct.Pin = B1_Pin|monBouton_Pin;
  205.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  206.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  207.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  208.  
  209.   /*Configure GPIO pin : LD2_Pin */
  210.   GPIO_InitStruct.Pin = LD2_Pin;
  211.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  212.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  213.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  214.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  215.  
  216.   /* EXTI interrupt init*/
  217.   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  218.   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  219.  
  220. /* USER CODE BEGIN MX_GPIO_Init_2 */
  221. /* USER CODE END MX_GPIO_Init_2 */
  222. }
  223.  
  224. /* USER CODE BEGIN 4 */
  225. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
  226.     if(GPIO_Pin == PIN_Bouton){
  227.         HAL_GPIO_WritePin(PORT_LED, PIN_LED, GPIO_PIN_SET);
  228.         HAL_Delay(2000);
  229.         HAL_GPIO_WritePin(PORT_LED, PIN_LED, GPIO_PIN_RESET);
  230.     }
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement