main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2016 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f0xx_hal.h"
  35. /* USER CODE BEGIN Includes */
  36. /* USER CODE END Includes */
  37. /* Private variables ---------------------------------------------------------*/
  38. ADC_HandleTypeDef hadc;
  39. DMA_HandleTypeDef hdma_adc;
  40. UART_HandleTypeDef huart1;
  41. /* USER CODE BEGIN PV */
  42. /* Private variables ---------------------------------------------------------*/
  43. //MODE: 1 - manual, 2 - auto, DIRection 0 - left, 1 - right, POS - rotor position, STEP_ARR index
  44. uint8_t MODE=1, DIR=0, POS=0, ADCReadCount;
  45. uint32_t SPEED=0;
  46. uint32_t DELAY, SPEED_ADC;
  47. //8 positions of the rotor
  48. static uint8_t STEP_ARR[10]={
  49. 0b0000,
  50. 0b1001,
  51. 0b0001,
  52. 0b0101,
  53. 0b0100,
  54. 0b0110,
  55. 0b0010,
  56. 0b1010,
  57. 0b1000,
  58. 0b1111
  59. };
  60. /* USER CODE END PV */
  61. /* Private function prototypes -----------------------------------------------*/
  62. void SystemClock_Config(void);
  63. void Error_Handler(void);
  64. static void MX_GPIO_Init(void);
  65. static void MX_DMA_Init(void);
  66. static void MX_ADC_Init(void);
  67. static void MX_USART1_UART_Init(void);
  68. /* USER CODE BEGIN PFP */
  69. /* Private function prototypes -----------------------------------------------*/
  70. /* USER CODE END PFP */
  71. /* USER CODE BEGIN 0 */
  72. /* USER CODE END 0 */
  73. int main(void)
  74. {
  75. /* USER CODE BEGIN 1 */
  76. /* USER CODE END 1 */
  77. /* MCU Configuration----------------------------------------------------------*/
  78. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79. HAL_Init();
  80. /* Configure the system clock */
  81. SystemClock_Config();
  82. /* Initialize all configured peripherals */
  83. MX_GPIO_Init();
  84. MX_DMA_Init();
  85. MX_ADC_Init();
  86. MX_USART1_UART_Init();
  87. /* USER CODE BEGIN 2 */
  88. /* USER CODE END 2 */
  89. /* Infinite loop */
  90. /* USER CODE BEGIN WHILE */
  91. while (1)
  92. {
  93. /* USER CODE END WHILE */
  94. /* USER CODE BEGIN 3 */
  95. // while button RUN is pushed
  96. for (ADCReadCount = 0; ADCReadCount < 10; ADCReadCount++){
  97. HAL_ADC_Start_DMA(&hadc, &SPEED_ADC, 1);
  98. SPEED = SPEED + HAL_ADC_GetValue(&hadc);
  99. };
  100. SPEED = SPEED/4000;
  101. SPEED = SPEED*1000;
  102. while (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4) == 1 && HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7) == 1){
  103. DIR=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
  104. if (SPEED < 200){ SPEED = 200; };
  105. GPIOA -> BRR = STEP_ARR[POS];
  106. if (DIR == 0){
  107. POS++;
  108. };
  109. if (DIR == 1){
  110. POS--;
  111. };
  112. if (POS == 0){
  113. POS = 8;
  114. };
  115. if (POS == 9){
  116. POS = 1;
  117. };
  118. GPIOA -> BSRR = STEP_ARR[POS];
  119. for (DELAY = 0; DELAY < 10000000; DELAY = DELAY + SPEED);
  120. SPEED = 0;
  121. };
  122. GPIOA -> BRR = STEP_ARR[9];
  123. }
  124. /* USER CODE END 3 */
  125. }
  126. /** System Clock Configuration
  127. */
  128. void SystemClock_Config(void)
  129. {
  130. RCC_OscInitTypeDef RCC_OscInitStruct;
  131. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  132. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  133. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_HSE;
  134. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  135. RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  136. RCC_OscInitStruct.HSI14CalibrationValue = 16;
  137. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  138. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  139. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  140. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  141. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  146. |RCC_CLOCKTYPE_PCLK1;
  147. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  148. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  149. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  150. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  151. {
  152. Error_Handler();
  153. }
  154. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  155. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  156. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  157. {
  158. Error_Handler();
  159. }
  160. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  161. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  162. /* SysTick_IRQn interrupt configuration */
  163. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  164. }
  165. /* ADC init function */
  166. static void MX_ADC_Init(void)
  167. {
  168. ADC_ChannelConfTypeDef sConfig;
  169. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  170. */
  171. hadc.Instance = ADC1;
  172. hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  173. hadc.Init.Resolution = ADC_RESOLUTION_12B;
  174. hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  175. hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  176. hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  177. hadc.Init.LowPowerAutoWait = DISABLE;
  178. hadc.Init.LowPowerAutoPowerOff = DISABLE;
  179. hadc.Init.ContinuousConvMode = ENABLE;
  180. hadc.Init.DiscontinuousConvMode = DISABLE;
  181. hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  182. hadc.Init.DMAContinuousRequests = DISABLE;
  183. hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  184. if (HAL_ADC_Init(&hadc) != HAL_OK)
  185. {
  186. Error_Handler();
  187. }
  188. /**Configure for the selected ADC regular channel to be converted.
  189. */
  190. sConfig.Channel = ADC_CHANNEL_5;
  191. sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  192. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  193. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  194. {
  195. Error_Handler();
  196. }
  197. }
  198. /* USART1 init function */
  199. static void MX_USART1_UART_Init(void)
  200. {
  201. huart1.Instance = USART1;
  202. huart1.Init.BaudRate = 38400;
  203. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  204. huart1.Init.StopBits = UART_STOPBITS_1;
  205. huart1.Init.Parity = UART_PARITY_NONE;
  206. huart1.Init.Mode = UART_MODE_TX_RX;
  207. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  208. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  209. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  210. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  211. if (HAL_UART_Init(&huart1) != HAL_OK)
  212. {
  213. Error_Handler();
  214. }
  215. }
  216. /**
  217. * Enable DMA controller clock
  218. */
  219. static void MX_DMA_Init(void)
  220. {
  221. /* DMA controller clock enable */
  222. __HAL_RCC_DMA1_CLK_ENABLE();
  223. /* DMA interrupt init */
  224. /* DMA1_Channel1_IRQn interrupt configuration */
  225. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  226. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  227. }
  228. /** Configure pins as
  229. * Analog
  230. * Input
  231. * Output
  232. * EVENT_OUT
  233. * EXTI
  234. */
  235. static void MX_GPIO_Init(void)
  236. {
  237. GPIO_InitTypeDef GPIO_InitStruct;
  238. /* GPIO Ports Clock Enable */
  239. __HAL_RCC_GPIOF_CLK_ENABLE();
  240. __HAL_RCC_GPIOA_CLK_ENABLE();
  241. __HAL_RCC_GPIOB_CLK_ENABLE();
  242. /*Configure GPIO pin Output Level */
  243. HAL_GPIO_WritePin(GPIOA, A1_Pin|A2_Pin|B1_Pin|B2_Pin, GPIO_PIN_RESET);
  244. /*Configure GPIO pins : A1_Pin A2_Pin B1_Pin B2_Pin */
  245. GPIO_InitStruct.Pin = A1_Pin|A2_Pin|B1_Pin|B2_Pin;
  246. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  247. GPIO_InitStruct.Pull = GPIO_NOPULL;
  248. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  249. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  250. /*Configure GPIO pins : RUN_Pin ENC_Pin END_STOP_Pin */
  251. GPIO_InitStruct.Pin = RUN_Pin|ENC_Pin|END_STOP_Pin;
  252. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  253. GPIO_InitStruct.Pull = GPIO_NOPULL;
  254. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  255. /*Configure GPIO pin : L_R_Pin */
  256. GPIO_InitStruct.Pin = L_R_Pin;
  257. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  258. GPIO_InitStruct.Pull = GPIO_NOPULL;
  259. HAL_GPIO_Init(L_R_GPIO_Port, &GPIO_InitStruct);
  260. }
  261. /* USER CODE BEGIN 4 */
  262. /* USER CODE END 4 */
  263. /**
  264. * @brief This function is executed in case of error occurrence.
  265. * @param None
  266. * @retval None
  267. */
  268. void Error_Handler(void)
  269. {
  270. /* USER CODE BEGIN Error_Handler */
  271. /* User can add his own implementation to report the HAL error return state */
  272. while(1)
  273. {
  274. }
  275. /* USER CODE END Error_Handler */
  276. }
  277. #ifdef USE_FULL_ASSERT
  278. /**
  279. * @brief Reports the name of the source file and the source line number
  280. * where the assert_param error has occurred.
  281. * @param file: pointer to the source file name
  282. * @param line: assert_param error line source number
  283. * @retval None
  284. */
  285. void assert_failed(uint8_t* file, uint32_t line)
  286. {
  287. /* USER CODE BEGIN 6 */
  288. /* User can add his own implementation to report the file name and line number,
  289. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  290. /* USER CODE END 6 */
  291. }
  292. #endif
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/