main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include "main.h"
  2. ADC_HandleTypeDef hadc;
  3. IWDG_HandleTypeDef hiwdg;
  4. UART_HandleTypeDef huart1;
  5. uint32_t voltage_value = 0;
  6. #define VOLTAGE_CMD 0xA5A5
  7. #define ADC_CONVERT_COUNT (30)
  8. uint8_t RxBuff[4] = {0};
  9. uint8_t RxData = 0;
  10. uint8_t RxCnt = 0;
  11. void SystemClock_Config(void);
  12. static void MX_GPIO_Init(void);
  13. static void MX_ADC_Init(void);
  14. static void MX_USART1_UART_Init(void);
  15. static void MX_IWDG_Init(void);
  16. uint16_t adc_buff[30] = {0};
  17. static uint32_t get_adc_val()
  18. {
  19. uint32_t value = 0;
  20. for(int i = 0 ;i < ADC_CONVERT_COUNT;i++)
  21. {
  22. HAL_ADC_Start(&hadc);
  23. HAL_ADC_PollForConversion(&hadc,50);
  24. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc),HAL_ADC_STATE_REG_EOC))
  25. {
  26. value += HAL_ADC_GetValue(&hadc);
  27. }
  28. }
  29. value /= ADC_CONVERT_COUNT;
  30. //calculate voltage
  31. }
  32. int main(void)
  33. {
  34. HAL_Init();
  35. SystemClock_Config();
  36. MX_GPIO_Init();
  37. MX_ADC_Init();
  38. HAL_ADCEx_Calibration_Start(&hadc);
  39. MX_USART1_UART_Init();
  40. MX_IWDG_Init();
  41. HAL_UART_Receive_IT(&huart1,&RxData,1);
  42. while (1)
  43. {
  44. HAL_IWDG_Refresh(&hiwdg); // 看门狗喂狗
  45. HAL_Delay(1000);
  46. }
  47. }
  48. void HAL_UART_RxCpltCallBack(UART_HandleTypeDef *huart)
  49. {
  50. //UNUSED(huart1);
  51. if(RxCnt > 2)
  52. {
  53. RxCnt = 0;
  54. }else
  55. {
  56. RxBuff[RxCnt++] = RxData;
  57. if(RxCnt == 2)
  58. {
  59. RxCnt = 0;
  60. uint16_t *ad = (uint16_t*)(&RxBuff);
  61. if(*ad == VOLTAGE_CMD)
  62. {
  63. HAL_UART_Transmit(&huart1,(uint8_t*)(&voltage_value),4,0xffff);
  64. while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);
  65. }
  66. }
  67. }
  68. HAL_UART_Receive_IT(&huart1,&RxData,1);
  69. }
  70. /**
  71. * @brief System Clock Configuration
  72. * @retval None
  73. */
  74. void SystemClock_Config(void)
  75. {
  76. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  77. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  78. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  79. /** Initializes the RCC Oscillators according to the specified parameters
  80. * in the RCC_OscInitTypeDef structure.
  81. */
  82. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14
  83. |RCC_OSCILLATORTYPE_LSI;
  84. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  85. RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  86. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  87. RCC_OscInitStruct.HSI14CalibrationValue = 16;
  88. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  89. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  90. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  91. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  92. RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  93. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  94. {
  95. Error_Handler();
  96. }
  97. /** Initializes the CPU, AHB and APB buses clocks
  98. */
  99. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  100. |RCC_CLOCKTYPE_PCLK1;
  101. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  102. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  103. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  104. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  105. {
  106. Error_Handler();
  107. }
  108. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  109. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  110. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  111. {
  112. Error_Handler();
  113. }
  114. }
  115. /**
  116. * @brief ADC Initialization Function
  117. * @param None
  118. * @retval None
  119. */
  120. static void MX_ADC_Init(void)
  121. {
  122. /* USER CODE BEGIN ADC_Init 0 */
  123. /* USER CODE END ADC_Init 0 */
  124. ADC_ChannelConfTypeDef sConfig = {0};
  125. /* USER CODE BEGIN ADC_Init 1 */
  126. /* USER CODE END ADC_Init 1 */
  127. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  128. */
  129. hadc.Instance = ADC1;
  130. hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  131. hadc.Init.Resolution = ADC_RESOLUTION_12B;
  132. hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  133. hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  134. hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  135. hadc.Init.LowPowerAutoWait = DISABLE;
  136. hadc.Init.LowPowerAutoPowerOff = DISABLE;
  137. hadc.Init.ContinuousConvMode = ENABLE;
  138. hadc.Init.DiscontinuousConvMode = DISABLE;
  139. hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  140. hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  141. hadc.Init.DMAContinuousRequests = DISABLE;
  142. hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  143. if (HAL_ADC_Init(&hadc) != HAL_OK)
  144. {
  145. Error_Handler();
  146. }
  147. /** Configure for the selected ADC regular channel to be converted.
  148. */
  149. sConfig.Channel = ADC_CHANNEL_4;
  150. sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  151. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  152. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  153. {
  154. Error_Handler();
  155. }
  156. /* USER CODE BEGIN ADC_Init 2 */
  157. /* USER CODE END ADC_Init 2 */
  158. }
  159. /**
  160. * @brief IWDG Initialization Function
  161. * @param None
  162. * @retval None
  163. */
  164. static void MX_IWDG_Init(void)
  165. {
  166. hiwdg.Instance = IWDG;
  167. hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
  168. hiwdg.Init.Window = 2500;
  169. hiwdg.Init.Reload = 2500;
  170. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  171. {
  172. Error_Handler();
  173. }
  174. }
  175. /**
  176. * @brief USART1 Initialization Function
  177. * @param None
  178. * @retval None
  179. */
  180. static void MX_USART1_UART_Init(void)
  181. {
  182. huart1.Instance = USART1;
  183. huart1.Init.BaudRate = 115200;
  184. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  185. huart1.Init.StopBits = UART_STOPBITS_1;
  186. huart1.Init.Parity = UART_PARITY_NONE;
  187. huart1.Init.Mode = UART_MODE_TX_RX;
  188. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  189. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  190. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  191. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  192. if (HAL_UART_Init(&huart1) != HAL_OK)
  193. {
  194. Error_Handler();
  195. }
  196. }
  197. /**
  198. * @brief GPIO Initialization Function
  199. * @param None
  200. * @retval None
  201. */
  202. static void MX_GPIO_Init(void)
  203. {
  204. __HAL_RCC_GPIOA_CLK_ENABLE();
  205. }
  206. /* USER CODE BEGIN 4 */
  207. /* USER CODE END 4 */
  208. /**
  209. * @brief This function is executed in case of error occurrence.
  210. * @retval None
  211. */
  212. void Error_Handler(void)
  213. {
  214. /* USER CODE BEGIN Error_Handler_Debug */
  215. /* User can add his own implementation to report the HAL error return state */
  216. __disable_irq();
  217. while (1)
  218. {
  219. }
  220. /* USER CODE END Error_Handler_Debug */
  221. }
  222. #ifdef USE_FULL_ASSERT
  223. /**
  224. * @brief Reports the name of the source file and the source line number
  225. * where the assert_param error has occurred.
  226. * @param file: pointer to the source file name
  227. * @param line: assert_param error line source number
  228. * @retval None
  229. */
  230. void assert_failed(uint8_t *file, uint32_t line)
  231. {
  232. /* USER CODE BEGIN 6 */
  233. /* User can add his own implementation to report the file name and line number,
  234. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  235. /* USER CODE END 6 */
  236. }
  237. #endif /* USE_FULL_ASSERT */