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