Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.91 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.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26.  
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31.  
  32. /* USER CODE END PTD */
  33.  
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40.  
  41. /* USER CODE END PM */
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. I2C_HandleTypeDef hi2c1;
  45.  
  46. TIM_HandleTypeDef htim3;
  47.  
  48. UART_HandleTypeDef huart2;
  49.  
  50. /* USER CODE BEGIN PV */
  51.  
  52. /* USER CODE END PV */
  53.  
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. static void MX_GPIO_Init(void);
  57. static void MX_USART2_UART_Init(void);
  58. static void MX_I2C1_Init(void);
  59. static void MX_TIM3_Init(void);
  60. /* USER CODE BEGIN PFP */
  61.  
  62. /* USER CODE END PFP */
  63.  
  64. /* Private user code ---------------------------------------------------------*/
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70.   * @brief  The application entry point.
  71.   * @retval int
  72.   */
  73. int main(void)
  74. {
  75.   /* USER CODE BEGIN 1 */
  76.  
  77.   /* USER CODE END 1 */
  78.  
  79.  
  80.   /* MCU Configuration--------------------------------------------------------*/
  81.  
  82.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83.   HAL_Init();
  84.  
  85.   /* USER CODE BEGIN Init */
  86.  
  87.   /* USER CODE END Init */
  88.  
  89.   /* Configure the system clock */
  90.   SystemClock_Config();
  91.  
  92.   /* USER CODE BEGIN SysInit */
  93.  
  94.   /* USER CODE END SysInit */
  95.  
  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init();
  98.   MX_USART2_UART_Init();
  99.   MX_I2C1_Init();
  100.   MX_TIM3_Init();
  101.   /* USER CODE BEGIN 2 */
  102.  
  103.   /* USER CODE END 2 */
  104.  
  105.  
  106.   HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
  107.   /* Infinite loop */
  108.   /* USER CODE BEGIN WHILE */
  109.  
  110.   while (1)
  111.   {
  112.     __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, TIM3->CCR2 + 1);
  113.     HAL_Delay(10);
  114.     /* USER CODE END WHILE */
  115.     /* USER CODE BEGIN 3 */
  116.   }
  117.   /* USER CODE END 3 */
  118. }
  119.  
  120. /**
  121.   * @brief System Clock Configuration
  122.   * @retval None
  123.   */
  124. void SystemClock_Config(void)
  125. {
  126.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  127.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  128.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  129.  
  130.   /** Initializes the CPU, AHB and APB busses clocks
  131.   */
  132.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  133.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  134.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  135.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  136.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  137.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  138.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  139.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  140.   {
  141.     Error_Handler();
  142.   }
  143.   /** Initializes the CPU, AHB and APB busses clocks
  144.   */
  145.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  146.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  147.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  148.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  149.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  150.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  151.  
  152.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  153.   {
  154.     Error_Handler();
  155.   }
  156.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1
  157.                               |RCC_PERIPHCLK_TIM34;
  158.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  159.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  160.   PeriphClkInit.Tim34ClockSelection = RCC_TIM34CLK_HCLK;
  161.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  162.   {
  163.     Error_Handler();
  164.   }
  165. }
  166.  
  167. /**
  168.   * @brief I2C1 Initialization Function
  169.   * @param None
  170.   * @retval None
  171.   */
  172. static void MX_I2C1_Init(void)
  173. {
  174.  
  175.   /* USER CODE BEGIN I2C1_Init 0 */
  176.  
  177.   /* USER CODE END I2C1_Init 0 */
  178.  
  179.   /* USER CODE BEGIN I2C1_Init 1 */
  180.  
  181.   /* USER CODE END I2C1_Init 1 */
  182.   hi2c1.Instance = I2C1;
  183.   hi2c1.Init.Timing = 0x2000090E;
  184.   hi2c1.Init.OwnAddress1 = 0;
  185.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  186.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  187.   hi2c1.Init.OwnAddress2 = 0;
  188.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  189.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  190.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  191.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  192.   {
  193.     Error_Handler();
  194.   }
  195.   /** Configure Analogue filter
  196.   */
  197.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  198.   {
  199.     Error_Handler();
  200.   }
  201.   /** Configure Digital filter
  202.   */
  203.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  204.   {
  205.     Error_Handler();
  206.   }
  207.   /* USER CODE BEGIN I2C1_Init 2 */
  208.  
  209.   /* USER CODE END I2C1_Init 2 */
  210.  
  211. }
  212.  
  213. /**
  214.   * @brief TIM3 Initialization Function
  215.   * @param None
  216.   * @retval None
  217.   */
  218. static void MX_TIM3_Init(void)
  219. {
  220.  
  221.   /* USER CODE BEGIN TIM3_Init 0 */
  222.  
  223.   /* USER CODE END TIM3_Init 0 */
  224.  
  225.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  226.   TIM_OC_InitTypeDef sConfigOC = {0};
  227.  
  228.   /* USER CODE BEGIN TIM3_Init 1 */
  229.  
  230.   /* USER CODE END TIM3_Init 1 */
  231.   htim3.Instance = TIM3;
  232.   htim3.Init.Prescaler = 200;
  233.   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  234.   htim3.Init.Period = 200;
  235.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  236.   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  237.   if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  238.   {
  239.     Error_Handler();
  240.   }
  241.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  242.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  243.   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  244.   {
  245.     Error_Handler();
  246.   }
  247.   sConfigOC.OCMode = TIM_OCMODE_PWM1;
  248.   sConfigOC.Pulse = 100;
  249.   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  250.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  251.   if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  252.   {
  253.     Error_Handler();
  254.   }
  255.   /* USER CODE BEGIN TIM3_Init 2 */
  256.  
  257.   /* USER CODE END TIM3_Init 2 */
  258.   HAL_TIM_MspPostInit(&htim3);
  259.   HAL_TIM_Base_Start(&htim3);
  260. }
  261.  
  262. /**
  263.   * @brief USART2 Initialization Function
  264.   * @param None
  265.   * @retval None
  266.   */
  267. static void MX_USART2_UART_Init(void)
  268. {
  269.  
  270.   /* USER CODE BEGIN USART2_Init 0 */
  271.  
  272.   /* USER CODE END USART2_Init 0 */
  273.  
  274.   /* USER CODE BEGIN USART2_Init 1 */
  275.  
  276.   /* USER CODE END USART2_Init 1 */
  277.   huart2.Instance = USART2;
  278.   huart2.Init.BaudRate = 38400;
  279.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  280.   huart2.Init.StopBits = UART_STOPBITS_1;
  281.   huart2.Init.Parity = UART_PARITY_NONE;
  282.   huart2.Init.Mode = UART_MODE_TX_RX;
  283.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  284.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  285.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  286.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  287.   if (HAL_UART_Init(&huart2) != HAL_OK)
  288.   {
  289.     Error_Handler();
  290.   }
  291.   /* USER CODE BEGIN USART2_Init 2 */
  292.  
  293.   /* USER CODE END USART2_Init 2 */
  294.  
  295. }
  296.  
  297. /**
  298.   * @brief GPIO Initialization Function
  299.   * @param None
  300.   * @retval None
  301.   */
  302. static void MX_GPIO_Init(void)
  303. {
  304.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  305.  
  306.   /* GPIO Ports Clock Enable */
  307.   __HAL_RCC_GPIOC_CLK_ENABLE();
  308.   __HAL_RCC_GPIOF_CLK_ENABLE();
  309.   __HAL_RCC_GPIOA_CLK_ENABLE();
  310.   __HAL_RCC_GPIOB_CLK_ENABLE();
  311.  
  312.   /*Configure GPIO pin Output Level */
  313.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  314.  
  315.   /*Configure GPIO pin : B1_Pin */
  316.   GPIO_InitStruct.Pin = B1_Pin;
  317.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  318.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  319.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  320.  
  321.   /*Configure GPIO pin : LD2_Pin */
  322.   GPIO_InitStruct.Pin = LD2_Pin;
  323.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  324.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  325.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  326.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  327.  
  328. }
  329.  
  330. /* USER CODE BEGIN 4 */
  331.  
  332. /* USER CODE END 4 */
  333.  
  334. /**
  335.   * @brief  This function is executed in case of error occurrence.
  336.   * @retval None
  337.   */
  338. void Error_Handler(void)
  339. {
  340.   /* USER CODE BEGIN Error_Handler_Debug */
  341.   /* User can add his own implementation to report the HAL error return state */
  342.  
  343.   /* USER CODE END Error_Handler_Debug */
  344. }
  345.  
  346. #ifdef  USE_FULL_ASSERT
  347. /**
  348.   * @brief  Reports the name of the source file and the source line number
  349.   *         where the assert_param error has occurred.
  350.   * @param  file: pointer to the source file name
  351.   * @param  line: assert_param error line source number
  352.   * @retval None
  353.   */
  354. void assert_failed(char *file, uint32_t line)
  355. {
  356.   /* USER CODE BEGIN 6 */
  357.   /* User can add his own implementation to report the file name and line number,
  358.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  359.   /* USER CODE END 6 */
  360. }
  361. #endif /* USE_FULL_ASSERT */
  362.  
  363. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement