Facebook
From Eratic Motmot, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 171
  1.  
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   ** This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * COPYRIGHT(c) 2019 STMicroelectronics
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f3xx_hal.h"
  42. #include "tim.h"
  43. #include "gpio.h"
  44.  
  45. /* USER CODE BEGIN Includes */
  46.  
  47. /* USER CODE END Includes */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50.  
  51. /* USER CODE BEGIN PV */
  52. /* Private variables ---------------------------------------------------------*/
  53.  
  54. /* USER CODE END PV */
  55.  
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58.  
  59. /* USER CODE BEGIN PFP */
  60. /* Private function prototypes -----------------------------------------------*/
  61.  
  62. /* USER CODE END PFP */
  63.  
  64. /* USER CODE BEGIN 0 */
  65.  
  66. /* USER CODE END 0 */
  67.  
  68. /**
  69.   * @brief  The application entry point.
  70.   *
  71.   * @retval None
  72.   */
  73. int main(void)
  74. {
  75.   /* USER CODE BEGIN 1 */
  76.  
  77.   /* USER CODE END 1 */
  78.  
  79.   /* MCU Configuration----------------------------------------------------------*/
  80.  
  81.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  82.   HAL_Init();
  83.  
  84.   /* USER CODE BEGIN Init */
  85.  
  86.   /* USER CODE END Init */
  87.  
  88.   /* Configure the system clock */
  89.   SystemClock_Config();
  90.  
  91.   /* USER CODE BEGIN SysInit */
  92.  
  93.   /* USER CODE END SysInit */
  94.  
  95.   /* Initialize all configured peripherals */
  96.   MX_GPIO_Init();
  97.   MX_TIM2_Init();
  98.   /* USER CODE BEGIN 2 */
  99.         HAL_TIM_Base_Start(&htim2);
  100.         HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_3);
  101.   HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_1);
  102.         /* USER CODE END 2 */
  103.  
  104.   /* Infinite loop */
  105.   /* USER CODE BEGIN WHILE */
  106.   while (1)
  107.   {
  108.  
  109.   /* USER CODE END WHILE */
  110.  
  111.   /* USER CODE BEGIN 3 */
  112.  
  113.   }
  114.   /* USER CODE END 3 */
  115.  
  116. }
  117.  
  118. /**
  119.   * @brief System Clock Configuration
  120.   * @retval None
  121.   */
  122. void SystemClock_Config(void)
  123. {
  124.  
  125.   RCC_OscInitTypeDef RCC_OscInitStruct;
  126.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  127.  
  128.     /**Initializes the CPU, AHB and APB busses clocks
  129.     */
  130.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  131.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  132.   RCC_OscInitStruct.HSICalibrationValue = 16;
  133.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  134.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  135.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  136.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  137.   {
  138.     _Error_Handler(__FILE__, __LINE__);
  139.   }
  140.  
  141.     /**Initializes the CPU, AHB and APB busses clocks
  142.     */
  143.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  144.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  145.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  146.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  147.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  148.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  149.  
  150.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  151.   {
  152.     _Error_Handler(__FILE__, __LINE__);
  153.   }
  154.  
  155.     /**Configure the Systick interrupt time
  156.     */
  157.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  158.  
  159.     /**Configure the Systick
  160.     */
  161.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  162.  
  163.   /* SysTick_IRQn interrupt configuration */
  164.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  165. }
  166.  
  167. /* USER CODE BEGIN 4 */
  168. unsigned int period, lastTime;
  169. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
  170.        
  171.         unsigned int time;
  172.         time =  HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_3);
  173.   period = time-lastTime;
  174.         lastTime = time;
  175.         TIM2->CCR2=time+100;
  176. }
  177.        
  178. void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim){
  179.  
  180.        
  181. }
  182.        
  183. /* USER CODE END 4 */
  184.  
  185. /**
  186.   * @brief  This function is executed in case of error occurrence.
  187.   * @param  file: The file name as string.
  188.   * @param  line: The line in file as a number.
  189.   * @retval None
  190.   */
  191. void _Error_Handler(char *file, int line)
  192. {
  193.   /* USER CODE BEGIN Error_Handler_Debug */
  194.   /* User can add his own implementation to report the HAL error return state */
  195.   while(1)
  196.   {
  197.   }
  198.   /* USER CODE END Error_Handler_Debug */
  199. }
  200.  
  201. #ifdef  USE_FULL_ASSERT
  202. /**
  203.   * @brief  Reports the name of the source file and the source line number
  204.   *         where the assert_param error has occurred.
  205.   * @param  file: pointer to the source file name
  206.   * @param  line: assert_param error line source number
  207.   * @retval None
  208.   */
  209. void assert_failed(uint8_t* file, uint32_t line)
  210. {
  211.   /* USER CODE BEGIN 6 */
  212.   /* User can add his own implementation to report the file name and line number,
  213.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  214.   /* USER CODE END 6 */
  215. }
  216. #endif /* USE_FULL_ASSERT */
  217.  
  218. /**
  219.   * @}
  220.   */
  221.  
  222. /**
  223.   * @}
  224.   */
  225.  
  226. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  227.