Advertisement
Guest User

STM32 Compare mode

a guest
Mar 17th, 2022
17
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24.  
  25. /* USER CODE END Includes */
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29.  
  30. /* USER CODE END PTD */
  31.  
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35.  
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38.  
  39. /* USER CODE END PM */
  40.  
  41. /* Private variables ---------------------------------------------------------*/
  42.  TIM_HandleTypeDef htim3;
  43.  
  44. /* USER CODE BEGIN PV */
  45. volatile uint16_t CH1_FREQ = 0; // ўрёЄюЄр ърэрыр 1
  46. volatile uint16_t CH2_FREQ = 0; // ўрёЄюЄр ърэрыр 2
  47.  
  48. /* USER CODE END PV */
  49.  
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. static void MX_GPIO_Init(void);
  53. static void MX_TIM3_Init(void);
  54. /* USER CODE BEGIN PFP */
  55. uint16_t computePulse(TIM_HandleTypeDef *htim, uint32_t chFrequency);
  56. void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim);
  57. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
  58. /* USER CODE END PFP */
  59.  
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62.  
  63. /* USER CODE END 0 */
  64.  
  65. /**
  66.   * @brief  The application entry point.
  67.   * @retval int
  68.   */
  69. int main(void)
  70. {
  71.   /* USER CODE BEGIN 1 */
  72.  
  73.   /* USER CODE END 1 */
  74.  
  75.   /* MCU Configuration--------------------------------------------------------*/
  76.  
  77.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78.   HAL_Init();
  79.  
  80.   /* USER CODE BEGIN Init */
  81.  
  82.   /* USER CODE END Init */
  83.  
  84.   /* Configure the system clock */
  85.   SystemClock_Config();
  86.  
  87.   /* USER CODE BEGIN SysInit */
  88.  
  89.   /* USER CODE END SysInit */
  90.  
  91.   /* Initialize all configured peripherals */
  92.   MX_GPIO_Init();
  93.   MX_TIM3_Init();
  94.   /* USER CODE BEGIN 2 */
  95.   HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_1); //╟ряєёъ ърэрыр 1
  96.   HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_2); //╟ряєёъ ърэрыр 2
  97.   /* USER CODE END 2 */
  98.  
  99.   /* Infinite loop */
  100.   /* USER CODE BEGIN WHILE */
  101.   while (1)
  102.   {
  103.     /* USER CODE END WHILE */
  104.  
  105.     /* USER CODE BEGIN 3 */
  106.   }
  107.   /* USER CODE END 3 */
  108. }
  109.  
  110. /**
  111.   * @brief System Clock Configuration
  112.   * @retval None
  113.   */
  114. void SystemClock_Config(void)
  115. {
  116.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  117.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  118.  
  119.   /** Initializes the RCC Oscillators according to the specified parameters
  120.   * in the RCC_OscInitTypeDef structure.
  121.   */
  122.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  123.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  124.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  125.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  126.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  127.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  128.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  129.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  130.   {
  131.     Error_Handler();
  132.   }
  133.  
  134.   /** Initializes the CPU, AHB and APB buses clocks
  135.   */
  136.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  137.                               |RCC_CLOCKTYPE_PCLK1;
  138.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  139.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  140.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  141.  
  142.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  143.   {
  144.     Error_Handler();
  145.   }
  146. }
  147.  
  148. /**
  149.   * @brief TIM3 Initialization Function
  150.   * @param None
  151.   * @retval None
  152.   */
  153. static void MX_TIM3_Init(void)
  154. {
  155.  
  156.   /* USER CODE BEGIN TIM3_Init 0 */
  157.  
  158.   /* USER CODE END TIM3_Init 0 */
  159.  
  160.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  161.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  162.   TIM_OC_InitTypeDef sConfigOC = {0};
  163.  
  164.   /* USER CODE BEGIN TIM3_Init 1 */
  165.  
  166.   /* USER CODE END TIM3_Init 1 */
  167.   htim3.Instance = TIM3;
  168.   htim3.Init.Prescaler = 2;
  169.   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  170.   htim3.Init.Period = 65535;
  171.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  172.   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  173.   if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  174.   {
  175.     Error_Handler();
  176.   }
  177.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  178.   if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  179.   {
  180.     Error_Handler();
  181.   }
  182.   if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
  183.   {
  184.     Error_Handler();
  185.   }
  186.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  187.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  188.   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  189.   {
  190.     Error_Handler();
  191.   }
  192.   sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
  193.   sConfigOC.Pulse = 320;
  194.   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  195.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  196.   if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  197.   {
  198.     Error_Handler();
  199.   }
  200.   sConfigOC.Pulse = 200;
  201.   if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  202.   {
  203.     Error_Handler();
  204.   }
  205.   /* USER CODE BEGIN TIM3_Init 2 */
  206.   CH1_FREQ = computePulse(&htim3,50000);
  207.   CH2_FREQ = computePulse(&htim3,80000);
  208.   /* USER CODE END TIM3_Init 2 */
  209.   HAL_TIM_MspPostInit(&htim3);
  210.  
  211. }
  212.  
  213. /**
  214.   * @brief GPIO Initialization Function
  215.   * @param None
  216.   * @retval None
  217.   */
  218. static void MX_GPIO_Init(void)
  219. {
  220.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  221.  
  222.   /* GPIO Ports Clock Enable */
  223.   __HAL_RCC_GPIOA_CLK_ENABLE();
  224.  
  225.   /*Configure GPIO pin Output Level */
  226.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  227.  
  228.   /*Configure GPIO pin : PA5 */
  229.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  230.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  231.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  232.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  233.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  234.  
  235. }
  236.  
  237. /* USER CODE BEGIN 4 */
  238. uint16_t computePulse(TIM_HandleTypeDef *htim, uint32_t chFrequency) {
  239.     //╘єэъЎш  Ёрёў╕Єр чэрўхэш  Pulse ърэрыр.
  240.     uint32_t timFrequency= HAL_RCC_GetPCLK1Freq() / (htim->Instance->PSC + 1);
  241.   return (uint16_t)(timFrequency / chFrequency);}
  242.  
  243. void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)        {
  244. //єтхышўє Pulse эр 320 фы  ърэрыр 1 (200 фы  2). ╥юуфр ърэры сєфхЄ яхЁхъы■ўрЄ№ё  эр цхырхьющ ўрёЄюЄх
  245.   uint32_t pulse;
  246.   /* TIMx_CH1 toggling with frequency = 50KHz */
  247.   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)     {
  248.     pulse = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
  249.     /* Set the Capture Compare Register value */
  250.     __HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_1, (pulse + CH1_FREQ));     }
  251.   /* TIMx_CH2 toggling with frequency = 80KHz */
  252.   if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)      {
  253.     pulse = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
  254.     /* Set the Capture Compare Register value */
  255.    __HAL_TIM_SET_COMPARE(htim, TIM_CHANNEL_2, (pulse + CH2_FREQ));    }
  256.   }
  257.  
  258. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)     {
  259.     //▌Єр ЇєэъЎш  т√ч√трхЄё  яюёых ёЁрсрЄ√трэш  ёюс√Єш  UEV, ЁрёўхЄэюх ўрёЄюЄр ёЁрсрЄ√трэш  244,140625 Hz. ┬эхщ   ш сєфє ьхэ Є№ яюыюцхэшх LED
  260.   if(htim->Instance == TIM3)     HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); }
  261. /* USER CODE END 4 */
  262.  
  263. /**
  264.   * @brief  This function is executed in case of error occurrence.
  265.   * @retval None
  266.   */
  267. void Error_Handler(void)
  268. {
  269.   /* USER CODE BEGIN Error_Handler_Debug */
  270.   /* User can add his own implementation to report the HAL error return state */
  271.   __disable_irq();
  272.   while (1)
  273.   {
  274.   }
  275.   /* USER CODE END Error_Handler_Debug */
  276. }
  277.  
  278. #ifdef  USE_FULL_ASSERT
  279. /**
  280.   * @brief  Reports the name of the source file and the source line number
  281.   *         where the assert_param error has occurred.
  282.   * @param  file: pointer to the source file name
  283.   * @param  line: assert_param error line source number
  284.   * @retval None
  285.   */
  286. void assert_failed(uint8_t *file, uint32_t line)
  287. {
  288.   /* USER CODE BEGIN 6 */
  289.   /* User can add his own implementation to report the file name and line number,
  290.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  291.   /* USER CODE END 6 */
  292. }
  293. #endif /* USE_FULL_ASSERT */
  294.  
Advertisement
RAW Paste Data Copied
Advertisement