Advertisement
2andnot

Untitled

Aug 22nd, 2020
2,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.90 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.   * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "stm32f1xx_hal.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25.  
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  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. TIM_HandleTypeDef htim2;
  44.  
  45. /* USER CODE BEGIN PV */
  46.  
  47. /* USER CODE END PV */
  48.  
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. static void MX_GPIO_Init(void);
  52. static void MX_TIM2_Init(void);
  53. /* USER CODE BEGIN PFP */
  54.  
  55. /* USER CODE END PFP */
  56.  
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59.  
  60. /* USER CODE END 0 */
  61.  
  62. /**
  63.   * @brief  The application entry point.
  64.   * @retval int
  65.   */
  66. int main(void)
  67. {
  68.   /* USER CODE BEGIN 1 */
  69.  
  70.   /* USER CODE END 1 */
  71.  
  72.   /* MCU Configuration--------------------------------------------------------*/
  73.  
  74.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  75.   HAL_Init();
  76.  
  77.   /* USER CODE BEGIN Init */
  78.  
  79.   /* USER CODE END Init */
  80.  
  81.   /* Configure the system clock */
  82.   SystemClock_Config();
  83.  
  84.   /* USER CODE BEGIN SysInit */
  85.  
  86.   /* USER CODE END SysInit */
  87.  
  88.   /* Initialize all configured peripherals */
  89.   MX_GPIO_Init();
  90.   MX_TIM2_Init();
  91.   /* USER CODE BEGIN 2 */
  92.  
  93.   /* USER CODE END 2 */
  94.  
  95.   /* Infinite loop */
  96.   /* USER CODE BEGIN WHILE */
  97.   while (1)
  98.   {
  99.     /* USER CODE END WHILE */
  100.  
  101.     /* USER CODE BEGIN 3 */
  102.   }
  103.   /* USER CODE END 3 */
  104. }
  105.  
  106. /**
  107.   * @brief System Clock Configuration
  108.   * @retval None
  109.   */
  110. void SystemClock_Config(void)
  111. {
  112.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  113.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  114.  
  115.   /** Initializes the RCC Oscillators according to the specified parameters
  116.   * in the RCC_OscInitTypeDef structure.
  117.   */
  118.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  119.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  120.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  121.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  122.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  123.   {
  124.     Error_Handler();
  125.   }
  126.   /** Initializes the CPU, AHB and APB buses clocks
  127.   */
  128.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  129.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  130.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  131.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  132.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  133.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  134.  
  135.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  136.   {
  137.     Error_Handler();
  138.   }
  139. }
  140.  
  141. /**
  142.   * @brief TIM2 Initialization Function
  143.   * @param None
  144.   * @retval None
  145.   */
  146. static void MX_TIM2_Init(void)
  147. {
  148.  
  149.   /* USER CODE BEGIN TIM2_Init 0 */
  150.  
  151.   /* USER CODE END TIM2_Init 0 */
  152.  
  153.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  154.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  155.  
  156.   /* USER CODE BEGIN TIM2_Init 1 */
  157.  
  158.   /* USER CODE END TIM2_Init 1 */
  159.   htim2.Instance = TIM2;
  160.   htim2.Init.Prescaler = 8000;
  161.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  162.   htim2.Init.Period = 1000;
  163.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  164.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  165.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  166.   {
  167.     Error_Handler();
  168.   }
  169.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  170.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  171.   {
  172.     Error_Handler();
  173.   }
  174.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  175.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  176.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  177.   {
  178.     Error_Handler();
  179.   }
  180.   /* USER CODE BEGIN TIM2_Init 2 */
  181.  
  182.   /* USER CODE END TIM2_Init 2 */
  183. }
  184.  
  185. /**
  186.   * @brief GPIO Initialization Function
  187.   * @param None
  188.   * @retval None
  189.   */
  190. static void MX_GPIO_Init(void)
  191. {
  192.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  193.  
  194.   /* GPIO Ports Clock Enable */
  195.   __HAL_RCC_GPIOA_CLK_ENABLE();
  196.  
  197.   /*Configure GPIO pin Output Level */
  198.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  199.  
  200.   /*Configure GPIO pin : PA5 */
  201.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  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(GPIOA, &GPIO_InitStruct);
  206.  
  207. }
  208. int trig = 0;
  209. void TIM2_IRQHandler(){
  210. HAL_TIM_IRQHandler(&htim2);
  211.  
  212.     if(trig){
  213.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  214.         trig = 0;
  215.     }else{
  216.         trig = 1;
  217.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  218.     }
  219.  
  220. }
  221. /* USER CODE BEGIN 4 */
  222.  
  223. /* USER CODE END 4 */
  224.  
  225. /**
  226.   * @brief  This function is executed in case of error occurrence.
  227.   * @retval None
  228.   */
  229. void Error_Handler(void)
  230. {
  231.   /* USER CODE BEGIN Error_Handler_Debug */
  232.   /* User can add his own implementation to report the HAL error return state */
  233.  
  234.   /* USER CODE END Error_Handler_Debug */
  235. }
  236.  
  237. #ifdef  USE_FULL_ASSERT
  238. /**
  239.   * @brief  Reports the name of the source file and the source line number
  240.   *         where the assert_param error has occurred.
  241.   * @param  file: pointer to the source file name
  242.   * @param  line: assert_param error line source number
  243.   * @retval None
  244.   */
  245. void assert_failed(uint8_t *file, uint32_t line)
  246. {
  247.   /* USER CODE BEGIN 6 */
  248.   /* User can add his own implementation to report the file name and line number,
  249.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  250.   /* USER CODE END 6 */
  251. }
  252. #endif /* USE_FULL_ASSERT */
  253.  
  254. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement