/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "dac.h" #include "dma.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "arm_math.h" #include "uart_dev.h" #include "frt_protocol.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ #define DATA_LEN 2048 uint32_t adc_value[DATA_LEN*2]; uint32_t result_data[DATA_LEN]; uint32_t adc_bat_data[DATA_LEN]; //float32_t rms_buf[DATA_LEN]; uint16_t rms_10uA = 0; //扩大100倍的泄漏电流值 uint16_t Max_rms_10uA = 0; //最大泄漏电流值 uint16_t Pulse_Count = 0; //脉冲次数 float32_t bat_v = 0; //电池电压 uint16_t batteryVoltage_mV = 0; //千倍电池电压 /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ arm_rfft_fast_instance_f32 S; float32_t fft_rawdata[DATA_LEN]; float32_t fft_outdata[DATA_LEN]; float32_t fft_mag[DATA_LEN>>1]; float32_t window_data[DATA_LEN]; float32_t Vdc= 0; float32_t V50hz_mA=0; //float32_t offset= 0; float32_t max_val,main_freq; float32_t rms_mV,rms_mA,Vpp; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ void bat_v_get(void) { float32_t temp_sum = 0; float32_t temp_f = 0; for(int i = 0; i < DATA_LEN; i++) { temp_sum += adc_bat_data[i]; } temp_f = temp_sum/DATA_LEN; bat_v = ((temp_f/4096.0)*3.3)*570.0/100.0; batteryVoltage_mV = (uint16_t)(bat_v * 1000); } void arm_hanning_f32(float32_t * pDst,uint32_t blockSize) { float32_t k = 2.0f / ((float32_t) blockSize); float32_t w; for(uint32_t i=0;i>1); arm_scale_f32(fft_mag,4.0f/DATA_LEN,fft_mag,DATA_LEN>>1); Vdc = fft_mag[0]/2.0f; arm_max_f32(&fft_mag[5],(DATA_LEN>>2)-1,&max_val,&max_val_freq); main_freq = ((float32_t)max_val_freq+5.0)/DATA_LEN*2000.0; // 单独50Hz频率信号的有效值 无需归零校准 V50hz_mA = fft_mag[51]/4096.0*3300.0f*0.0380/1.414; // 信号频率为50Hz左右 // if(main_freq<75.0f && main_freq>35.0f) // { for(i = 0;i 10.0) { Pulse_Count++; } bat_v_get(); } /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ arm_rfft_fast_init_f32(&S, DATA_LEN); arm_hanning_f32(window_data,DATA_LEN); /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_USART1_UART_Init(9600); MX_USART3_UART_Init(9600); HAL_GPIO_WritePin(RF_PWR_CTRL_GPIO_Port, RF_PWR_CTRL_Pin, GPIO_PIN_SET);//使能e22 MX_GPIO_Init(); MX_DMA_Init(); MX_ADC1_Init(); MX_TIM6_Init(); MX_DAC1_Init(); /* USER CODE BEGIN 2 */ // HAL_PWREx_EnterSTOP0Mode(PWR_STOPENTRY_WFI); HAL_PWREx_EnableBatteryCharging(PWR_BATTERY_CHARGING_RESISTOR_1_5); HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED); // 模拟电路电源没开之前 dac输出0 保证不会串电流 HAL_DAC_Start(&hdac1,DAC_CHANNEL_1); // 开启缓冲后 设置为0 实测有40mV左右电压 猜测和缓冲器Vos有关 HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0); // 开启模拟电路电源,抬参考源 Cur_sensor_pwr_on(); HAL_ADC_Start_DMA(&hadc1,(uint32_t *)adc_value,DATA_LEN * 2); HAL_TIM_Base_Start(&htim6); //HAL_Delay(500); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048); //HAL_ADC_Start_DMA(&hadc1,(uint32_t *)result_data,DATA_LEN); // HAL_GPIO_TogglePin(GPIO_LED_GPIO_Port, GPIO_LED_Pin); read_and_process_uart_data(g_term_uart_handle); read_and_process_uart_data(g_lora_uart_handle); HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI); // HAL_TIM_Base_Start(&htim6); } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2) != HAL_OK) { Error_Handler(); } /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; 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_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } } /* 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 */