2024-07-15 01:27:36 +00:00
|
|
|
|
/* 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"
|
2024-07-15 09:07:44 +00:00
|
|
|
|
|
|
|
|
|
#include "uart_dev.h"
|
|
|
|
|
#include "frt_protocol.h"
|
2024-07-23 07:38:51 +00:00
|
|
|
|
#include "e22.h"
|
2024-07-15 01:27:36 +00:00
|
|
|
|
/* USER CODE END Includes */
|
|
|
|
|
|
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
|
#define DATA_LEN 2048
|
2024-07-15 09:07:44 +00:00
|
|
|
|
uint32_t adc_value[DATA_LEN*2];
|
2024-07-15 01:27:36 +00:00
|
|
|
|
uint32_t result_data[DATA_LEN];
|
2024-07-15 09:07:44 +00:00
|
|
|
|
uint32_t adc_bat_data[DATA_LEN];
|
2024-07-15 01:27:36 +00:00
|
|
|
|
//float32_t rms_buf[DATA_LEN];
|
|
|
|
|
|
2024-07-15 09:07:44 +00:00
|
|
|
|
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; //千倍电池电压
|
2024-07-15 01:27:36 +00:00
|
|
|
|
/* 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 */
|
|
|
|
|
|
2024-07-15 09:07:44 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2024-07-15 01:27:36 +00:00
|
|
|
|
|
|
|
|
|
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<blockSize;i++)
|
|
|
|
|
{
|
|
|
|
|
w = PI * i * k;
|
|
|
|
|
w = 0.5f * (1.0f - cosf (w));
|
|
|
|
|
pDst[i] = w;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Cur_sensor_pwr_on(void)
|
|
|
|
|
{
|
|
|
|
|
HAL_GPIO_WritePin(GPIOC, SENSOR_PWR_CTRL_Pin, GPIO_PIN_SET);
|
|
|
|
|
HAL_Delay(1);
|
|
|
|
|
// init dac
|
|
|
|
|
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048);
|
|
|
|
|
// 延时等待模拟部分启动稳定。具体延时等待测试待测试。
|
|
|
|
|
HAL_Delay(800);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Cur_sensor_pwr_off(void)
|
|
|
|
|
{
|
|
|
|
|
// init dac
|
|
|
|
|
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0);
|
|
|
|
|
//HAL_DAC_Stop(&hdac1,DAC_CHANNEL_1);
|
|
|
|
|
//HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048);
|
|
|
|
|
//HAL_Delay(1);
|
|
|
|
|
HAL_GPIO_WritePin(GPIOC, SENSOR_PWR_CTRL_Pin, GPIO_PIN_RESET);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
|
|
|
|
{
|
2024-07-15 09:07:44 +00:00
|
|
|
|
for(int i = 0;i < DATA_LEN;i++)
|
|
|
|
|
{
|
|
|
|
|
result_data[i] = adc_value[2*i];
|
|
|
|
|
}
|
2024-07-15 01:27:36 +00:00
|
|
|
|
|
2024-07-15 09:07:44 +00:00
|
|
|
|
for(int i = 0;i < DATA_LEN;i++)
|
|
|
|
|
{
|
|
|
|
|
adc_bat_data[i] = adc_value[2*i + 1];
|
|
|
|
|
}
|
2024-07-15 01:27:36 +00:00
|
|
|
|
uint32_t i;
|
|
|
|
|
// 关闭模拟电路电源
|
|
|
|
|
//Cur_sensor_pwr_off();
|
|
|
|
|
|
|
|
|
|
//float32_t max_val,main_freq;
|
|
|
|
|
uint32_t max_val_freq;
|
|
|
|
|
for(i = 0;i<DATA_LEN;i++)
|
|
|
|
|
{
|
|
|
|
|
// 加窗
|
|
|
|
|
fft_rawdata[i]= (float32_t)result_data[i]*window_data[i];
|
|
|
|
|
}
|
|
|
|
|
//arm_rms_f32(fft_rawdata,DATA_LEN,&rms_mV);
|
|
|
|
|
//arm_q15_to_float(result_data,fft_rawdata,DATA_LEN);
|
|
|
|
|
arm_rfft_fast_f32(&S,fft_rawdata,fft_outdata,0);
|
|
|
|
|
|
|
|
|
|
arm_cmplx_mag_f32(fft_outdata,fft_mag,DATA_LEN>>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<DATA_LEN;i++)
|
|
|
|
|
{
|
|
|
|
|
fft_rawdata[i]= result_data[i]-Vdc;
|
|
|
|
|
}
|
|
|
|
|
// 去除直流信号后,标准方式计算有效值,此数据容易受噪声影响,有底噪RMS?
|
|
|
|
|
arm_rms_f32(fft_rawdata,DATA_LEN,&rms_mV);
|
|
|
|
|
rms_mV = rms_mV/4096.0*3300.0f;
|
|
|
|
|
// 300匝 交流互感 0.0380
|
|
|
|
|
// 350匝 交流互感 0.04412
|
|
|
|
|
rms_mA = rms_mV*0.0380;
|
|
|
|
|
// }else
|
|
|
|
|
// {
|
|
|
|
|
// //rms_mV = 0;
|
|
|
|
|
// /// rms_mA = 0;
|
|
|
|
|
// }
|
2024-07-15 09:07:44 +00:00
|
|
|
|
rms_10uA = (uint16_t)(100.0*rms_mA); //扩大100倍取uA值
|
|
|
|
|
// 获取最大泄漏电流值
|
|
|
|
|
if(Max_rms_10uA < rms_10uA)
|
|
|
|
|
{
|
|
|
|
|
Max_rms_10uA = rms_10uA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取脉冲次数
|
|
|
|
|
if(rms_mA > 10.0)
|
|
|
|
|
{
|
|
|
|
|
Pulse_Count++;
|
|
|
|
|
}
|
|
|
|
|
bat_v_get();
|
2024-07-15 01:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
2024-07-23 02:00:15 +00:00
|
|
|
|
HAL_GPIO_WritePin(RF_PWR_CTRL_GPIO_Port, RF_PWR_CTRL_Pin, GPIO_PIN_SET);//使能e22
|
2024-07-15 01:27:36 +00:00
|
|
|
|
MX_GPIO_Init();
|
|
|
|
|
MX_DMA_Init();
|
|
|
|
|
MX_ADC1_Init();
|
|
|
|
|
MX_TIM6_Init();
|
|
|
|
|
MX_DAC1_Init();
|
2024-07-23 07:38:51 +00:00
|
|
|
|
|
|
|
|
|
// 串口初始化
|
|
|
|
|
init_lora_uart();
|
|
|
|
|
init_term_uart();
|
2024-07-15 01:27:36 +00:00
|
|
|
|
/* 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();
|
2024-07-15 09:07:44 +00:00
|
|
|
|
HAL_ADC_Start_DMA(&hadc1,(uint32_t *)adc_value,DATA_LEN * 2);
|
2024-07-15 01:27:36 +00:00
|
|
|
|
HAL_TIM_Base_Start(&htim6);
|
|
|
|
|
|
2024-07-23 07:38:51 +00:00
|
|
|
|
// 初始化E22
|
|
|
|
|
e22_init();
|
2024-07-15 01:27:36 +00:00
|
|
|
|
//HAL_Delay(500);
|
|
|
|
|
|
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
|
/* Infinite loop */
|
|
|
|
|
/* USER CODE BEGIN WHILE */
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* USER CODE END WHILE */
|
2024-07-23 07:38:51 +00:00
|
|
|
|
|
2024-07-15 01:27:36 +00:00
|
|
|
|
/* 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);
|
2024-07-15 09:07:44 +00:00
|
|
|
|
read_and_process_uart_data(g_lora_uart_handle);
|
2024-07-23 02:00:15 +00:00
|
|
|
|
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);
|
|
|
|
|
|
2024-07-15 01:27:36 +00:00
|
|
|
|
// 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 */
|