main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 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. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. #define IN1_PIN GPIO_PIN_6
  24. #define IN1_PORT GPIOA
  25. #define IN2_PIN GPIO_PIN_5
  26. #define IN2_PORT GPIOA
  27. #define IN3_PIN GPIO_PIN_4
  28. #define IN3_PORT GPIOA
  29. #define IN4_PIN GPIO_PIN_3
  30. #define IN4_PORT GPIOA
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40. /* USER CODE END PM */
  41. /* Private variables ---------------------------------------------------------*/
  42. TIM_HandleTypeDef htim1;
  43. /* USER CODE BEGIN PV */
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. static void MX_GPIO_Init(void);
  48. static void MX_TIM1_Init(void);
  49. /* USER CODE BEGIN PFP */
  50. /* USER CODE END PFP */
  51. /* Private user code ---------------------------------------------------------*/
  52. /* USER CODE BEGIN 0 */
  53. void microDelay (uint16_t delay)
  54. {
  55. __HAL_TIM_SET_COUNTER(&htim1, 0);
  56. while (__HAL_TIM_GET_COUNTER(&htim1) < delay);
  57. }
  58. void stepCCV (int steps, uint16_t delay) // CCV - Counter Clockwise
  59. {
  60. for(int x=0; x<steps; x=x+1)
  61. {
  62. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_SET); // IN1
  63. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  64. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  65. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  66. microDelay(delay);
  67. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_SET); // IN1
  68. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_SET); // IN2
  69. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  70. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  71. microDelay(delay);
  72. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  73. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_SET); // IN2
  74. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  75. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  76. microDelay(delay);
  77. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  78. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_SET); // IN2
  79. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_SET); // IN3
  80. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  81. microDelay(delay);
  82. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  83. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  84. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_SET); // IN3
  85. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  86. microDelay(delay);
  87. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  88. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  89. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_SET); // IN3
  90. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_SET); // IN4
  91. microDelay(delay);
  92. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  93. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  94. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  95. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_SET); // IN4
  96. microDelay(delay);
  97. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_SET); // IN1
  98. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  99. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  100. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_SET); // IN4
  101. microDelay(delay);
  102. }
  103. }
  104. void stepCV (int steps, uint16_t delay) // CV - Clockwise
  105. {
  106. for(int x=0; x<steps; x=x+1)
  107. {
  108. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_SET); // IN1
  109. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  110. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  111. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_SET); // IN4
  112. microDelay(delay);
  113. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  114. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  115. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  116. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_SET); // IN4
  117. microDelay(delay);
  118. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  119. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  120. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_SET); // IN3
  121. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_SET); // IN4
  122. microDelay(delay);
  123. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  124. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  125. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_SET); // IN3
  126. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  127. microDelay(delay);
  128. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  129. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_SET); // IN2
  130. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_SET); // IN3
  131. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  132. microDelay(delay);
  133. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_RESET); // IN1
  134. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_SET); // IN2
  135. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  136. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  137. microDelay(delay);
  138. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_SET); // IN1
  139. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_SET); // IN2
  140. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  141. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  142. microDelay(delay);
  143. HAL_GPIO_WritePin(IN1_PORT, IN1_PIN, GPIO_PIN_SET); // IN1
  144. HAL_GPIO_WritePin(IN2_PORT, IN2_PIN, GPIO_PIN_RESET); // IN2
  145. HAL_GPIO_WritePin(IN3_PORT, IN3_PIN, GPIO_PIN_RESET); // IN3
  146. HAL_GPIO_WritePin(IN4_PORT, IN4_PIN, GPIO_PIN_RESET); // IN4
  147. microDelay(delay);
  148. }
  149. }
  150. /* USER CODE END 0 */
  151. /**
  152. * @brief The application entry point.
  153. * @retval int
  154. */
  155. int main(void)
  156. {
  157. /* USER CODE BEGIN 1 */
  158. /* USER CODE END 1 */
  159. /* MCU Configuration--------------------------------------------------------*/
  160. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  161. HAL_Init();
  162. /* USER CODE BEGIN Init */
  163. /* USER CODE END Init */
  164. /* Configure the system clock */
  165. SystemClock_Config();
  166. /* USER CODE BEGIN SysInit */
  167. /* USER CODE END SysInit */
  168. /* Initialize all configured peripherals */
  169. MX_GPIO_Init();
  170. MX_TIM1_Init();
  171. /* USER CODE BEGIN 2 */
  172. HAL_TIM_Base_Start(&htim1); // Start Timer 1
  173. /* use like delay */
  174. /* USER CODE END 2 */
  175. /* Infinite loop */
  176. /* USER CODE BEGIN WHILE */
  177. while (1)
  178. {
  179. /* USER CODE END WHILE */
  180. /* USER CODE BEGIN 3 */
  181. stepCCV(80, 1000); // 256 half revolution
  182. HAL_Delay(10000);
  183. stepCV(80, 1000); // 128 quarter revolution
  184. HAL_Delay(1000);
  185. stepCV(80, 1000); // 128 quarter revolution
  186. HAL_Delay(10000);
  187. stepCCV(80, 1000); // 256 half revolution
  188. HAL_Delay(1000);
  189. }
  190. /* USER CODE END 3 */
  191. }
  192. /**
  193. * @brief System Clock Configuration
  194. * @retval None
  195. */
  196. void SystemClock_Config(void)
  197. {
  198. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  199. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  200. /** Configure the main internal regulator output voltage
  201. */
  202. __HAL_RCC_PWR_CLK_ENABLE();
  203. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  204. /** Initializes the RCC Oscillators according to the specified parameters
  205. * in the RCC_OscInitTypeDef structure.
  206. */
  207. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  208. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  209. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  210. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  211. RCC_OscInitStruct.PLL.PLLM = 4;
  212. RCC_OscInitStruct.PLL.PLLN = 72;
  213. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  214. RCC_OscInitStruct.PLL.PLLQ = 4;
  215. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  216. {
  217. Error_Handler();
  218. }
  219. /** Initializes the CPU, AHB and APB buses clocks
  220. */
  221. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  222. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  223. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  224. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  225. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  226. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  227. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  228. {
  229. Error_Handler();
  230. }
  231. }
  232. /**
  233. * @brief TIM1 Initialization Function
  234. * @param None
  235. * @retval None
  236. */
  237. static void MX_TIM1_Init(void)
  238. {
  239. /* USER CODE BEGIN TIM1_Init 0 */
  240. /* USER CODE END TIM1_Init 0 */
  241. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  242. TIM_MasterConfigTypeDef sMasterConfig = {0};
  243. /* USER CODE BEGIN TIM1_Init 1 */
  244. /* USER CODE END TIM1_Init 1 */
  245. htim1.Instance = TIM1;
  246. htim1.Init.Prescaler = 71;
  247. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  248. htim1.Init.Period = 65535;
  249. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  250. htim1.Init.RepetitionCounter = 0;
  251. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  252. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  253. {
  254. Error_Handler();
  255. }
  256. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  257. if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  258. {
  259. Error_Handler();
  260. }
  261. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  262. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  263. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  264. {
  265. Error_Handler();
  266. }
  267. /* USER CODE BEGIN TIM1_Init 2 */
  268. /* USER CODE END TIM1_Init 2 */
  269. }
  270. /**
  271. * @brief GPIO Initialization Function
  272. * @param None
  273. * @retval None
  274. */
  275. static void MX_GPIO_Init(void)
  276. {
  277. GPIO_InitTypeDef GPIO_InitStruct = {0};
  278. /* USER CODE BEGIN MX_GPIO_Init_1 */
  279. /* USER CODE END MX_GPIO_Init_1 */
  280. /* GPIO Ports Clock Enable */
  281. __HAL_RCC_GPIOC_CLK_ENABLE();
  282. __HAL_RCC_GPIOH_CLK_ENABLE();
  283. __HAL_RCC_GPIOA_CLK_ENABLE();
  284. __HAL_RCC_GPIOB_CLK_ENABLE();
  285. /*Configure GPIO pin Output Level */
  286. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_RESET);
  287. /*Configure GPIO pin : B1_Pin */
  288. GPIO_InitStruct.Pin = B1_Pin;
  289. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  290. GPIO_InitStruct.Pull = GPIO_NOPULL;
  291. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  292. /*Configure GPIO pins : PA3 PA4 PA5 PA6 */
  293. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
  294. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  295. GPIO_InitStruct.Pull = GPIO_NOPULL;
  296. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  297. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  298. /* USER CODE BEGIN MX_GPIO_Init_2 */
  299. /* USER CODE END MX_GPIO_Init_2 */
  300. }
  301. /* USER CODE BEGIN 4 */
  302. /* USER CODE END 4 */
  303. /**
  304. * @brief This function is executed in case of error occurrence.
  305. * @retval None
  306. */
  307. void Error_Handler(void)
  308. {
  309. /* USER CODE BEGIN Error_Handler_Debug */
  310. /* User can add his own implementation to report the HAL error return state */
  311. __disable_irq();
  312. while (1)
  313. {
  314. }
  315. /* USER CODE END Error_Handler_Debug */
  316. }
  317. #ifdef USE_FULL_ASSERT
  318. /**
  319. * @brief Reports the name of the source file and the source line number
  320. * where the assert_param error has occurred.
  321. * @param file: pointer to the source file name
  322. * @param line: assert_param error line source number
  323. * @retval None
  324. */
  325. void assert_failed(uint8_t *file, uint32_t line)
  326. {
  327. /* USER CODE BEGIN 6 */
  328. /* User can add his own implementation to report the file name and line number,
  329. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  330. /* USER CODE END 6 */
  331. }
  332. #endif /* USE_FULL_ASSERT */