main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. #include<stdio.h>
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. /* USER CODE END PTD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */
  33. /* USER CODE END PM */
  34. /* Private variables ---------------------------------------------------------*/
  35. ADC_HandleTypeDef hadc1;
  36. UART_HandleTypeDef huart2;
  37. /* USER CODE BEGIN PV */
  38. uint32_t adcCounter = 0;
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. static void MX_USART2_UART_Init(void);
  44. static void MX_ADC1_Init(void);
  45. static void MX_NVIC_Init(void);
  46. /* USER CODE BEGIN PFP */
  47. /* USER CODE END PFP */
  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. /*void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  51. {
  52. // Conversion Complete & DMA Transfer Complete As Well
  53. // So The AD_RES Is Now Updated & Let's Move IT To The PWM CCR1
  54. // Update The PWM Duty Cycle With Latest ADC Conversion Result
  55. //TIM2->CCR1 = (AD_RES<<4);
  56. //HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);
  57. adcCounter++;
  58. }*/
  59. /* USER CODE END 0 */
  60. /**
  61. * @brief The application entry point.
  62. * @retval int
  63. */
  64. int main(void)
  65. {
  66. /* USER CODE BEGIN 1 */
  67. /* USER CODE END 1 */
  68. /* MCU Configuration--------------------------------------------------------*/
  69. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  70. HAL_Init();
  71. /* USER CODE BEGIN Init */
  72. /* USER CODE END Init */
  73. /* Configure the system clock */
  74. SystemClock_Config();
  75. /* USER CODE BEGIN SysInit */
  76. /* USER CODE END SysInit */
  77. /* Initialize all configured peripherals */
  78. MX_GPIO_Init();
  79. MX_USART2_UART_Init();
  80. MX_ADC1_Init();
  81. /* Initialize interrupts */
  82. MX_NVIC_Init();
  83. /* USER CODE BEGIN 2 */
  84. // Enable USART1 Interrupts
  85. USART2->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
  86. // Start continous ADC-conversion
  87. HAL_Delay(10);
  88. ADC1->SR = 0;
  89. ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_CONT;
  90. ADC1->CR1 = ADC_IT_EOC;
  91. ADC1->CR2 |= ADC_CR2_ADON;
  92. /* USER CODE END 2 */
  93. /* Infinite loop */
  94. /* USER CODE BEGIN WHILE */
  95. printf("VVB Energy Sensor\n");
  96. while (1)
  97. {
  98. /* USER CODE END WHILE */
  99. /* USER CODE BEGIN 3 */
  100. HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);
  101. HAL_Delay (1000);
  102. }
  103. /* USER CODE END 3 */
  104. }
  105. /**
  106. * @brief System Clock Configuration
  107. * @retval None
  108. */
  109. void SystemClock_Config(void)
  110. {
  111. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  112. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  113. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  114. /** Initializes the RCC Oscillators according to the specified parameters
  115. * in the RCC_OscInitTypeDef structure.
  116. */
  117. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  118. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  119. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  120. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  121. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  122. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  123. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL7;
  124. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  125. {
  126. Error_Handler();
  127. }
  128. /** Initializes the CPU, AHB and APB buses clocks
  129. */
  130. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  132. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  133. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  135. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  136. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  141. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  142. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  143. {
  144. Error_Handler();
  145. }
  146. }
  147. /**
  148. * @brief NVIC Configuration.
  149. * @retval None
  150. */
  151. static void MX_NVIC_Init(void)
  152. {
  153. /* ADC1_2_IRQn interrupt configuration */
  154. HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
  155. HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
  156. /* USART2_IRQn interrupt configuration */
  157. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  158. HAL_NVIC_EnableIRQ(USART2_IRQn);
  159. }
  160. /**
  161. * @brief ADC1 Initialization Function
  162. * @param None
  163. * @retval None
  164. */
  165. static void MX_ADC1_Init(void)
  166. {
  167. /* USER CODE BEGIN ADC1_Init 0 */
  168. /* USER CODE END ADC1_Init 0 */
  169. ADC_ChannelConfTypeDef sConfig = {0};
  170. /* USER CODE BEGIN ADC1_Init 1 */
  171. /* USER CODE END ADC1_Init 1 */
  172. /** Common config
  173. */
  174. hadc1.Instance = ADC1;
  175. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  176. hadc1.Init.ContinuousConvMode = ENABLE;
  177. hadc1.Init.DiscontinuousConvMode = DISABLE;
  178. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  179. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  180. hadc1.Init.NbrOfConversion = 1;
  181. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  182. {
  183. Error_Handler();
  184. }
  185. /** Configure Regular Channel
  186. */
  187. sConfig.Channel = ADC_CHANNEL_5;
  188. sConfig.Rank = ADC_REGULAR_RANK_1;
  189. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  190. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  191. {
  192. Error_Handler();
  193. }
  194. /* USER CODE BEGIN ADC1_Init 2 */
  195. HAL_ADCEx_Calibration_Start(&hadc1);
  196. // Set sampling frequency
  197. sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
  198. HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  199. /*
  200. * Tconv = Sampling time + 12.5 cycles
  201. *
  202. * Example:
  203. * With an ADCCLK = 14 MHz and a sampling time of 1.5 cycles: Tconv = 1.5 + 12.5 = 14 cycles = 1 μs
  204. *
  205. * In this application we want to sample a 50Hz signal = 20mS
  206. *
  207. * Calcs here: https://docs.google.com/spreadsheets/d/1an5f3Aog4bdwpe-rDquWTxlXBpsXEK-1DVhAsiCqlq0/edit#gid=304302332
  208. *
  209. * Select: 56MHz /4 55,5 = 205 882,35Hz = 4 117,6 samples / 50Hz wave
  210. *
  211. *
  212. *
  213. */
  214. /* USER CODE END ADC1_Init 2 */
  215. }
  216. /**
  217. * @brief USART2 Initialization Function
  218. * @param None
  219. * @retval None
  220. */
  221. static void MX_USART2_UART_Init(void)
  222. {
  223. /* USER CODE BEGIN USART2_Init 0 */
  224. /* USER CODE END USART2_Init 0 */
  225. /* USER CODE BEGIN USART2_Init 1 */
  226. /* USER CODE END USART2_Init 1 */
  227. huart2.Instance = USART2;
  228. huart2.Init.BaudRate = 19200;
  229. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  230. huart2.Init.StopBits = UART_STOPBITS_1;
  231. huart2.Init.Parity = UART_PARITY_NONE;
  232. huart2.Init.Mode = UART_MODE_TX_RX;
  233. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  234. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  235. if (HAL_UART_Init(&huart2) != HAL_OK)
  236. {
  237. Error_Handler();
  238. }
  239. /* USER CODE BEGIN USART2_Init 2 */
  240. /* USER CODE END USART2_Init 2 */
  241. }
  242. /**
  243. * @brief GPIO Initialization Function
  244. * @param None
  245. * @retval None
  246. */
  247. static void MX_GPIO_Init(void)
  248. {
  249. GPIO_InitTypeDef GPIO_InitStruct = {0};
  250. /* GPIO Ports Clock Enable */
  251. __HAL_RCC_GPIOC_CLK_ENABLE();
  252. __HAL_RCC_GPIOD_CLK_ENABLE();
  253. __HAL_RCC_GPIOA_CLK_ENABLE();
  254. /*Configure GPIO pin Output Level */
  255. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  256. /*Configure GPIO pin : PC13 */
  257. GPIO_InitStruct.Pin = GPIO_PIN_13;
  258. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  259. GPIO_InitStruct.Pull = GPIO_NOPULL;
  260. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  261. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  262. }
  263. /* USER CODE BEGIN 4 */
  264. /* USER CODE END 4 */
  265. /**
  266. * @brief This function is executed in case of error occurrence.
  267. * @retval None
  268. */
  269. void Error_Handler(void)
  270. {
  271. /* USER CODE BEGIN Error_Handler_Debug */
  272. /* User can add his own implementation to report the HAL error return state */
  273. __disable_irq();
  274. while (1)
  275. {
  276. }
  277. /* USER CODE END Error_Handler_Debug */
  278. }
  279. #ifdef USE_FULL_ASSERT
  280. /**
  281. * @brief Reports the name of the source file and the source line number
  282. * where the assert_param error has occurred.
  283. * @param file: pointer to the source file name
  284. * @param line: assert_param error line source number
  285. * @retval None
  286. */
  287. void assert_failed(uint8_t *file, uint32_t line)
  288. {
  289. /* USER CODE BEGIN 6 */
  290. /* User can add his own implementation to report the file name and line number,
  291. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  292. /* USER CODE END 6 */
  293. }
  294. #endif /* USE_FULL_ASSERT */