main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2015 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 "stm32f4xx_hal.h"
  35. /* USER CODE BEGIN Includes */
  36. /* USER CODE END Includes */
  37. /* Private variables ---------------------------------------------------------*/
  38. ADC_HandleTypeDef hadc1;
  39. DMA_HandleTypeDef hdma_adc1;
  40. /* USER CODE BEGIN PV */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. static void MX_GPIO_Init(void);
  46. static void MX_DMA_Init(void);
  47. static void MX_ADC1_Init(void);
  48. /* USER CODE BEGIN PFP */
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* USER CODE END PFP */
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. int main(void)
  54. {
  55. /* USER CODE BEGIN 1 */
  56. /* USER CODE END 1 */
  57. /* MCU Configuration----------------------------------------------------------*/
  58. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  59. HAL_Init();
  60. /* Configure the system clock */
  61. SystemClock_Config();
  62. /* Initialize all configured peripherals */
  63. MX_GPIO_Init();
  64. MX_DMA_Init();
  65. MX_ADC1_Init();
  66. /* USER CODE BEGIN 2 */
  67. /* USER CODE END 2 */
  68. /* Infinite loop */
  69. /* USER CODE BEGIN WHILE */
  70. while (1)
  71. {
  72. /* USER CODE END WHILE */
  73. /* USER CODE BEGIN 3 */
  74. }
  75. /* USER CODE END 3 */
  76. }
  77. /** System Clock Configuration
  78. */
  79. void SystemClock_Config(void)
  80. {
  81. RCC_OscInitTypeDef RCC_OscInitStruct;
  82. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  83. __PWR_CLK_ENABLE();
  84. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  85. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  86. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  87. RCC_OscInitStruct.HSICalibrationValue = 16;
  88. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  89. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  90. RCC_OscInitStruct.PLL.PLLM = 16;
  91. RCC_OscInitStruct.PLL.PLLN = 336;
  92. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  93. RCC_OscInitStruct.PLL.PLLQ = 7;
  94. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  95. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  96. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  97. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  98. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  99. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  100. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  101. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  102. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  103. }
  104. /* ADC1 init function */
  105. void MX_ADC1_Init(void)
  106. {
  107. ADC_ChannelConfTypeDef sConfig;
  108. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  109. */
  110. hadc1.Instance = ADC1;
  111. hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
  112. hadc1.Init.Resolution = ADC_RESOLUTION8b;
  113. hadc1.Init.ScanConvMode = ENABLE;
  114. hadc1.Init.ContinuousConvMode = ENABLE;
  115. hadc1.Init.DiscontinuousConvMode = DISABLE;
  116. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  117. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  118. hadc1.Init.NbrOfConversion = 1;
  119. hadc1.Init.DMAContinuousRequests = ENABLE;
  120. hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
  121. HAL_ADC_Init(&hadc1);
  122. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  123. */
  124. sConfig.Channel = ADC_CHANNEL_0;
  125. sConfig.Rank = 1;
  126. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  127. HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  128. }
  129. /**
  130. * Enable DMA controller clock
  131. */
  132. void MX_DMA_Init(void)
  133. {
  134. /* DMA controller clock enable */
  135. __DMA2_CLK_ENABLE();
  136. /* DMA interrupt init */
  137. HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  138. HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
  139. }
  140. /** Configure pins as
  141. * Analog
  142. * Input
  143. * Output
  144. * EVENT_OUT
  145. * EXTI
  146. PA2 ------> USART2_TX
  147. PA3 ------> USART2_RX
  148. */
  149. void MX_GPIO_Init(void)
  150. {
  151. GPIO_InitTypeDef GPIO_InitStruct;
  152. /* GPIO Ports Clock Enable */
  153. __GPIOC_CLK_ENABLE();
  154. __GPIOH_CLK_ENABLE();
  155. __GPIOA_CLK_ENABLE();
  156. __GPIOB_CLK_ENABLE();
  157. /*Configure GPIO pin : PC13 */
  158. GPIO_InitStruct.Pin = GPIO_PIN_13;
  159. GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  160. GPIO_InitStruct.Pull = GPIO_NOPULL;
  161. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  162. /*Configure GPIO pins : PA2 PA3 */
  163. GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
  164. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  165. GPIO_InitStruct.Pull = GPIO_NOPULL;
  166. GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  167. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  168. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  169. /*Configure GPIO pin : PA5 */
  170. GPIO_InitStruct.Pin = GPIO_PIN_5;
  171. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  172. GPIO_InitStruct.Pull = GPIO_NOPULL;
  173. GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  174. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  175. }
  176. /* USER CODE BEGIN 4 */
  177. /* USER CODE END 4 */
  178. #ifdef USE_FULL_ASSERT
  179. /**
  180. * @brief Reports the name of the source file and the source line number
  181. * where the assert_param error has occurred.
  182. * @param file: pointer to the source file name
  183. * @param line: assert_param error line source number
  184. * @retval None
  185. */
  186. void assert_failed(uint8_t* file, uint32_t line)
  187. {
  188. /* USER CODE BEGIN 6 */
  189. /* User can add his own implementation to report the file name and line number,
  190. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  191. /* USER CODE END 6 */
  192. }
  193. #endif
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @}
  199. */
  200. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/