tim.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file tim.c
  5. * @brief This file provides code for the configuration
  6. * of the TIM instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "tim.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. TIM_HandleTypeDef htim2;
  25. TIM_HandleTypeDef htim4;
  26. /* TIM2 init function */
  27. void MX_TIM2_Init(void)
  28. {
  29. /* USER CODE BEGIN TIM2_Init 0 */
  30. /* USER CODE END TIM2_Init 0 */
  31. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  32. TIM_MasterConfigTypeDef sMasterConfig = {0};
  33. /* USER CODE BEGIN TIM2_Init 1 */
  34. /* USER CODE END TIM2_Init 1 */
  35. htim2.Instance = TIM2;
  36. htim2.Init.Prescaler = 7200-1;
  37. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  38. htim2.Init.Period = 10000-1;
  39. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  40. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  41. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  42. {
  43. Error_Handler();
  44. }
  45. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  46. if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  47. {
  48. Error_Handler();
  49. }
  50. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  51. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  52. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  53. {
  54. Error_Handler();
  55. }
  56. /* USER CODE BEGIN TIM2_Init 2 */
  57. /* USER CODE END TIM2_Init 2 */
  58. }
  59. /* TIM4 init function */
  60. void MX_TIM4_Init(void)
  61. {
  62. /* USER CODE BEGIN TIM4_Init 0 */
  63. /* USER CODE END TIM4_Init 0 */
  64. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  65. TIM_MasterConfigTypeDef sMasterConfig = {0};
  66. /* USER CODE BEGIN TIM4_Init 1 */
  67. /* USER CODE END TIM4_Init 1 */
  68. htim4.Instance = TIM4;
  69. htim4.Init.Prescaler = 72-1;
  70. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  71. htim4.Init.Period = 10-1;
  72. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  73. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  74. if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  75. {
  76. Error_Handler();
  77. }
  78. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  79. if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  80. {
  81. Error_Handler();
  82. }
  83. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  84. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  85. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  86. {
  87. Error_Handler();
  88. }
  89. /* USER CODE BEGIN TIM4_Init 2 */
  90. /* USER CODE END TIM4_Init 2 */
  91. }
  92. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
  93. {
  94. if(tim_baseHandle->Instance==TIM2)
  95. {
  96. /* USER CODE BEGIN TIM2_MspInit 0 */
  97. /* USER CODE END TIM2_MspInit 0 */
  98. /* TIM2 clock enable */
  99. __HAL_RCC_TIM2_CLK_ENABLE();
  100. /* TIM2 interrupt Init */
  101. HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0);
  102. HAL_NVIC_EnableIRQ(TIM2_IRQn);
  103. /* USER CODE BEGIN TIM2_MspInit 1 */
  104. /* USER CODE END TIM2_MspInit 1 */
  105. }
  106. else if(tim_baseHandle->Instance==TIM4)
  107. {
  108. /* USER CODE BEGIN TIM4_MspInit 0 */
  109. /* USER CODE END TIM4_MspInit 0 */
  110. /* TIM4 clock enable */
  111. __HAL_RCC_TIM4_CLK_ENABLE();
  112. /* TIM4 interrupt Init */
  113. HAL_NVIC_SetPriority(TIM4_IRQn, 5, 0);
  114. HAL_NVIC_EnableIRQ(TIM4_IRQn);
  115. /* USER CODE BEGIN TIM4_MspInit 1 */
  116. /* USER CODE END TIM4_MspInit 1 */
  117. }
  118. }
  119. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
  120. {
  121. if(tim_baseHandle->Instance==TIM2)
  122. {
  123. /* USER CODE BEGIN TIM2_MspDeInit 0 */
  124. /* USER CODE END TIM2_MspDeInit 0 */
  125. /* Peripheral clock disable */
  126. __HAL_RCC_TIM2_CLK_DISABLE();
  127. /* TIM2 interrupt Deinit */
  128. HAL_NVIC_DisableIRQ(TIM2_IRQn);
  129. /* USER CODE BEGIN TIM2_MspDeInit 1 */
  130. /* USER CODE END TIM2_MspDeInit 1 */
  131. }
  132. else if(tim_baseHandle->Instance==TIM4)
  133. {
  134. /* USER CODE BEGIN TIM4_MspDeInit 0 */
  135. /* USER CODE END TIM4_MspDeInit 0 */
  136. /* Peripheral clock disable */
  137. __HAL_RCC_TIM4_CLK_DISABLE();
  138. /* TIM4 interrupt Deinit */
  139. HAL_NVIC_DisableIRQ(TIM4_IRQn);
  140. /* USER CODE BEGIN TIM4_MspDeInit 1 */
  141. /* USER CODE END TIM4_MspDeInit 1 */
  142. }
  143. }
  144. /* USER CODE BEGIN 1 */
  145. /* USER CODE END 1 */