Facebook
From Baby Hog, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 169
  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. #include "Engine.h"
  45.  
  46. /* USER CODE BEGIN Includes */
  47.  
  48. /* USER CODE END Includes */
  49.  
  50. /* Private variables ---------------------------------------------------------*/
  51.  
  52. /* USER CODE BEGIN PV */
  53. /* Private variables ---------------------------------------------------------*/
  54.  
  55. /* USER CODE END PV */
  56.  
  57. /* Private function prototypes -----------------------------------------------*/
  58. void SystemClock_Config(void);
  59.  
  60. /* USER CODE BEGIN PFP */
  61. /* Private function prototypes -----------------------------------------------*/
  62.  
  63. /* USER CODE END PFP */
  64.  
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70.   * @brief  The application entry point.
  71.   *
  72.   * @retval None
  73.   */
  74. int main(void)
  75. {
  76.   /* USER CODE BEGIN 1 */
  77.  
  78.   /* USER CODE END 1 */
  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_TIM2_Init();
  99.   /* USER CODE BEGIN 2 */
  100.         HAL_TIM_Base_Start(&htim2);
  101.         HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_3);
  102.   HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_1);
  103.         /* USER CODE END 2 */
  104.  
  105.   /* Infinite loop */
  106.   /* USER CODE BEGIN WHILE */
  107.   while (1)
  108.   {
  109.  
  110.   /* USER CODE END WHILE */
  111.  
  112.   /* USER CODE BEGIN 3 */
  113.  
  114.   }
  115.   /* USER CODE END 3 */
  116.  
  117. }
  118.  
  119. /**
  120.   * @brief System Clock Configuration
  121.   * @retval None
  122.   */
  123. void SystemClock_Config(void)
  124. {
  125.  
  126.   RCC_OscInitTypeDef RCC_OscInitStruct;
  127.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  128.  
  129.     /**Initializes the CPU, AHB and APB busses clocks
  130.     */
  131.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  132.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  133.   RCC_OscInitStruct.HSICalibrationValue = 16;
  134.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  136.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  137.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  138.   {
  139.     _Error_Handler(__FILE__, __LINE__);
  140.   }
  141.  
  142.     /**Initializes the CPU, AHB and APB busses clocks
  143.     */
  144.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  145.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  146.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  147.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  148.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  149.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  150.  
  151.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  152.   {
  153.     _Error_Handler(__FILE__, __LINE__);
  154.   }
  155.  
  156.     /**Configure the Systick interrupt time
  157.     */
  158.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  159.  
  160.     /**Configure the Systick
  161.     */
  162.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  163.  
  164.   /* SysTick_IRQn interrupt configuration */
  165.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  166. }
  167.  
  168. /* USER CODE BEGIN 4 */
  169. unsigned int period=0, lastTime=0, counter=0, lastperiod=0, state=0;
  170. float angle, slope_size;
  171. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
  172.        
  173.         unsigned int time;
  174.         time =  HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_3);
  175.   period = time-lastTime;
  176.         lastTime = time;
  177.        
  178.                 if(period>(15*lastperiod/10))
  179.                 {
  180.                         counter=0;
  181.                         angle=-60;
  182.                 }
  183.                 else
  184.                 {
  185.                         counter++;
  186.                         angle+=slope_size;
  187.                
  188.                 }
  189.                
  190.                 lastperiod = period;
  191.         TIM2->CCR2=time+100;
  192. }
  193.  
  194.  
  195. int MyStateMachine (int mState)
  196.         {int nextState;
  197.                 switch(mState){
  198.                         case 0:
  199.                                 //State Machine First State
  200.                                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_RESET);
  201.                                 // Next function calls for 0.1 seconds
  202.                                 htim2.Instance->CCR1+=100000;
  203.                                 // Next state No. 1.
  204.                                 nextState = 1;
  205.                                 break;
  206.                         case 1:
  207.                                 //State machine Second State
  208.                                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_SET);
  209.                                 // Next function calls for 0.1 seconds
  210.                                 htim2.Instance->CCR1+=100000;
  211.                                 // Next state No. 2.
  212.                                 nextState = 2;
  213.                                 break;
  214.                         case 2:
  215.                                 // State machine Third State
  216.                                 HAL_GPIO_WritePin(GPIOE,GPIO_PIN_8,GPIO_PIN_RESET);
  217.                                 // Next state No. 2.
  218.                                 nextState = 2;
  219.                                 break;
  220.                 }
  221.                 return nextState;
  222. }
  223.        
  224. void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim){
  225.  
  226.         state = MyStateMachine(state);
  227.        
  228. }
  229.        
  230. /* USER CODE END 4 */
  231.  
  232. /**
  233.   * @brief  This function is executed in case of error occurrence.
  234.   * @param  file: The file name as string.
  235.   * @param  line: The line in file as a number.
  236.   * @retval None
  237.   */
  238. void _Error_Handler(char *file, int line)
  239. {
  240.   /* USER CODE BEGIN Error_Handler_Debug */
  241.   /* User can add his own implementation to report the HAL error return state */
  242.   while(1)
  243.   {
  244.   }
  245.   /* USER CODE END Error_Handler_Debug */
  246. }
  247.  
  248. #ifdef  USE_FULL_ASSERT
  249. /**
  250.   * @brief  Reports the name of the source file and the source line number
  251.   *         where the assert_param error has occurred.
  252.   * @param  file: pointer to the source file name
  253.   * @param  line: assert_param error line source number
  254.   * @retval None
  255.   */
  256. void assert_failed(uint8_t* file, uint32_t line)
  257. {
  258.   /* USER CODE BEGIN 6 */
  259.   /* User can add his own implementation to report the file name and line number,
  260.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  261.   /* USER CODE END 6 */
  262. }
  263. #endif /* USE_FULL_ASSERT */
  264.  
  265. /**
  266.   * @}
  267.   */
  268.  
  269. /**
  270.   * @}
  271.   */
  272.  
  273. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  274.