ddeexxiikk

LAB3

Jun 5th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /* USER CODE END Header */
  3. /* Includes ------------------------------------------------------------------*/
  4. #include "main.h"
  5.  
  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */
  8.  
  9. /* USER CODE END Includes */
  10.  
  11. /* Private typedef -----------------------------------------------------------*/
  12. /* USER CODE BEGIN PTD */
  13.  
  14. /* USER CODE END PTD */
  15.  
  16. /* Private define ------------------------------------------------------------*/
  17. /* USER CODE BEGIN PD */
  18. /* USER CODE END PD */
  19.  
  20. /* Private macro -------------------------------------------------------------*/
  21. /* USER CODE BEGIN PM */
  22.  
  23. /* USER CODE END PM */
  24.  
  25. /* Private variables ---------------------------------------------------------*/
  26.  
  27. /* USER CODE BEGIN PV */
  28.  
  29. /* USER CODE END PV */
  30.  
  31. /* Private function prototypes -----------------------------------------------*/
  32. void SystemClock_Config(void);
  33. static void MX_GPIO_Init(void);
  34. /* USER CODE BEGIN PFP */
  35.  
  36. /* USER CODE END PFP */
  37.  
  38. /* Private user code ---------------------------------------------------------*/
  39. /* USER CODE BEGIN 0 */
  40.  
  41. /*
  42. //Przykład 4
  43. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_PIN)
  44. {
  45.     HAL_GPIO_WritePin(LED_Green_GPIO_Port, LED_Green_Pin,
  46.     HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin));
  47. }
  48. */
  49.  
  50. //Zadanie 2
  51. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_PIN)
  52. {
  53.     if(GPIO_PIN==GPIO_PIN_13)
  54.     {
  55.         if(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)==GPIO_PIN_SET)
  56.         {
  57.             HAL_GPIO_TogglePin(LED_Green_GPIO_Port, LED_Green_Pin);
  58.             while(HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)==GPIO_PIN_SET){}
  59.         }
  60.     }
  61. }
  62.  
  63. /* USER CODE END 0 */
  64.  
  65. /**
  66.   * @brief  The application entry point.
  67.   * @retval int
  68.   */
  69. int main(void)
  70. {
  71.   /* USER CODE BEGIN 1 */
  72.  
  73.   /* USER CODE END 1 */
  74.  
  75.   /* MCU Configuration--------------------------------------------------------*/
  76.  
  77.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78.   HAL_Init();
  79.  
  80.   /* USER CODE BEGIN Init */
  81.  
  82.   /* USER CODE END Init */
  83.  
  84.   /* Configure the system clock */
  85.   SystemClock_Config();
  86.  
  87.   /* USER CODE BEGIN SysInit */
  88.  
  89.   /* USER CODE END SysInit */
  90.  
  91.   /* Initialize all configured peripherals */
  92.   MX_GPIO_Init();
  93.   /* USER CODE BEGIN 2 */
  94.  
  95.   //Przykład 1
  96.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
  97.   HAL_Delay(1000);
  98.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
  99.  
  100.   /*
  101.   //Zadanie 1
  102.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET);
  103.   HAL_Delay(5000);
  104.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
  105.   */
  106.  
  107.   HAL_GPIO_EXTI_Callback(Button_Pin);
  108.  
  109.   /* USER CODE END 2 */
  110.  
  111.   /* Infinite loop */
  112.   /* USER CODE BEGIN WHILE */
  113.   while (1)
  114.   {
  115.     /* USER CODE END WHILE */
  116.  
  117.     /* USER CODE BEGIN 3 */
  118.       /*Miganie co pół sekundy
  119.        * HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13);
  120.       HAL_Delay(500);*/
  121.  
  122.       /* Gaszenie na przycisk
  123.       if( HAL_GPIO_ReadPin(Button_GPIO_Port, Button_Pin)==GPIO_PIN_SET)
  124.       {
  125.           HAL_GPIO_WritePin(LED_Green_GPIO_Port, LED_Green_Pin, GPIO_PIN_SET);
  126.       }
  127.       else
  128.       {
  129.           HAL_GPIO_WritePin(LED_Green_GPIO_Port, LED_Green_Pin, GPIO_PIN_RESET);
  130.       }
  131.       */
  132.  
  133.       //Zadanie 1
  134.       //Miganie 10Hz
  135.       HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13);
  136.       HAL_Delay(50);
  137.  
  138.   }
  139.   /* USER CODE END 3 */
  140. }
  141.  
  142. /**
  143.   * @brief System Clock Configuration
  144.   * @retval None
  145.   */
  146. void SystemClock_Config(void)
  147. {
  148.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  149.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  150.  
  151.   /** Initializes the RCC Oscillators according to the specified parameters
  152.   * in the RCC_OscInitTypeDef structure.
  153.   */
  154.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  155.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  156.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  157.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  158.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  159.   {
  160.     Error_Handler();
  161.   }
  162.  
  163.   /** Initializes the CPU, AHB and APB buses clocks
  164.   */
  165.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  166.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  167.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  168.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  169.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  170.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  171.  
  172.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  173.   {
  174.     Error_Handler();
  175.   }
  176. }
  177.  
  178. /**
  179.   * @brief GPIO Initialization Function
  180.   * @param None
  181.   * @retval None
  182.   */
  183. static void MX_GPIO_Init(void)
  184. {
  185.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  186.  
  187.   /* GPIO Ports Clock Enable */
  188.   __HAL_RCC_GPIOC_CLK_ENABLE();
  189.   __HAL_RCC_GPIOB_CLK_ENABLE();
  190.  
  191.   /*Configure GPIO pin Output Level */
  192.   HAL_GPIO_WritePin(LED_Green_GPIO_Port, LED_Green_Pin, GPIO_PIN_RESET);
  193.  
  194.   /*Configure GPIO pin : Button_Pin */
  195.   GPIO_InitStruct.Pin = Button_Pin;
  196.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  197.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  198.   HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
  199.  
  200.   /*Configure GPIO pin : LED_Green_Pin */
  201.   GPIO_InitStruct.Pin = LED_Green_Pin;
  202.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  203.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  204.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  205.   HAL_GPIO_Init(LED_Green_GPIO_Port, &GPIO_InitStruct);
  206.  
  207.   /* EXTI interrupt init*/
  208.   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  209.   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  210.  
  211. }
  212.  
  213. /* USER CODE BEGIN 4 */
  214.  
  215. /* USER CODE END 4 */
  216.  
  217. /**
  218.   * @brief  This function is executed in case of error occurrence.
  219.   * @retval None
  220.   */
  221. void Error_Handler(void)
  222. {
  223.   /* USER CODE BEGIN Error_Handler_Debug */
  224.   /* User can add his own implementation to report the HAL error return state */
  225.   __disable_irq();
  226.   while (1)
  227.   {
  228.   }
  229.   /* USER CODE END Error_Handler_Debug */
  230. }
  231.  
  232. #ifdef  USE_FULL_ASSERT
  233. /**
  234.   * @brief  Reports the name of the source file and the source line number
  235.   *         where the assert_param error has occurred.
  236.   * @param  file: pointer to the source file name
  237.   * @param  line: assert_param error line source number
  238.   * @retval None
  239.   */
  240. void assert_failed(uint8_t *file, uint32_t line)
  241. {
  242.   /* USER CODE BEGIN 6 */
  243.   /* User can add his own implementation to report the file name and line number,
  244.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  245.   /* USER CODE END 6 */
  246. }
  247. #endif /* USE_FULL_ASSERT */
  248.  
Add Comment
Please, Sign In to add comment