| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- #include "main.h"
- ADC_HandleTypeDef hadc;
- IWDG_HandleTypeDef hiwdg;
- UART_HandleTypeDef huart1;
- uint32_t voltage_value = 0;
- #define VOLTAGE_CMD 0xA5A5
- #define ADC_CONVERT_COUNT (30)
- uint8_t RxBuff[4] = {0};
- uint8_t RxData = 0;
- uint8_t RxCnt = 0;
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_ADC_Init(void);
- static void MX_USART1_UART_Init(void);
- static void MX_IWDG_Init(void);
- uint16_t adc_buff[30] = {0};
- static uint32_t get_adc_val()
- {
- uint32_t value = 0;
- for(int i = 0 ;i < ADC_CONVERT_COUNT;i++)
- {
- HAL_ADC_Start(&hadc);
- HAL_ADC_PollForConversion(&hadc,50);
- if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc),HAL_ADC_STATE_REG_EOC))
- {
- value += HAL_ADC_GetValue(&hadc);
- }
- }
- value /= ADC_CONVERT_COUNT;
-
- //calculate voltage
-
-
- }
-
-
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- MX_ADC_Init();
- HAL_ADCEx_Calibration_Start(&hadc);
- MX_USART1_UART_Init();
-
- MX_IWDG_Init();
-
-
- HAL_UART_Receive_IT(&huart1,&RxData,1);
-
- while (1)
- {
- HAL_IWDG_Refresh(&hiwdg); // 看门狗喂狗
- HAL_Delay(1000);
- }
- }
- void HAL_UART_RxCpltCallBack(UART_HandleTypeDef *huart)
- {
- //UNUSED(huart1);
- if(RxCnt > 2)
- {
- RxCnt = 0;
- }else
- {
- RxBuff[RxCnt++] = RxData;
- if(RxCnt == 2)
- {
-
- RxCnt = 0;
- uint16_t *ad = (uint16_t*)(&RxBuff);
- if(*ad == VOLTAGE_CMD)
- {
- HAL_UART_Transmit(&huart1,(uint8_t*)(&voltage_value),4,0xffff);
- while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);
- }
- }
- }
- HAL_UART_Receive_IT(&huart1,&RxData,1);
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14
- |RCC_OSCILLATORTYPE_LSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.HSI14CalibrationValue = 16;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
- RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief ADC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_ADC_Init(void)
- {
- /* USER CODE BEGIN ADC_Init 0 */
- /* USER CODE END ADC_Init 0 */
- ADC_ChannelConfTypeDef sConfig = {0};
- /* USER CODE BEGIN ADC_Init 1 */
- /* USER CODE END ADC_Init 1 */
- /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc.Instance = ADC1;
- hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
- hadc.Init.Resolution = ADC_RESOLUTION_12B;
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
- hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc.Init.LowPowerAutoWait = DISABLE;
- hadc.Init.LowPowerAutoPowerOff = DISABLE;
- hadc.Init.ContinuousConvMode = ENABLE;
- hadc.Init.DiscontinuousConvMode = DISABLE;
- hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc.Init.DMAContinuousRequests = DISABLE;
- hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
- if (HAL_ADC_Init(&hadc) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_4;
- sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN ADC_Init 2 */
- /* USER CODE END ADC_Init 2 */
- }
- /**
- * @brief IWDG Initialization Function
- * @param None
- * @retval None
- */
- static void MX_IWDG_Init(void)
- {
- hiwdg.Instance = IWDG;
- hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
- hiwdg.Init.Window = 2500;
- hiwdg.Init.Reload = 2500;
- if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief USART1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART1_UART_Init(void)
- {
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- __HAL_RCC_GPIOA_CLK_ENABLE();
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
|