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