406 lines
10 KiB
C
406 lines
10 KiB
C
/* 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"
|
||
#include "e22.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 Pulse_Count = 0; //脉冲次数
|
||
float32_t bat_v = 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;
|
||
float32_t max_rms_mA;
|
||
/* 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;
|
||
|
||
//存入寄存器
|
||
g_stMcs_Para.bat_v = bat_v;
|
||
}
|
||
|
||
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)
|
||
{
|
||
for(int i = 0;i < DATA_LEN;i++)
|
||
{
|
||
result_data[i] = adc_value[2*i];
|
||
}
|
||
|
||
for(int i = 0;i < DATA_LEN;i++)
|
||
{
|
||
adc_bat_data[i] = adc_value[2*i + 1];
|
||
}
|
||
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;
|
||
// }
|
||
// 最大泄漏电流
|
||
max_rms_mA = max_rms_mA > rms_mA ? max_rms_mA : rms_mA;
|
||
|
||
// 获取脉冲次数
|
||
if(rms_mA > 10.0)
|
||
{
|
||
Pulse_Count++;
|
||
}
|
||
|
||
//电池电压获取
|
||
bat_v_get();
|
||
|
||
//存入寄存器
|
||
g_stMcs_Para.current = rms_mA;
|
||
g_stMcs_Para.max_current = max_rms_mA;
|
||
g_stMcs_Para.pulse_count = Pulse_Count;
|
||
}
|
||
|
||
|
||
/* USER CODE END PFP */
|
||
|
||
/* Private user code ---------------------------------------------------------*/
|
||
/* USER CODE BEGIN 0 */
|
||
// 100ms触发一次
|
||
int read_uart_flag = 0;//控制时间,第一次检测到有字符后再等待一次中断再进行处理
|
||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||
{
|
||
UNUSED(htim);
|
||
read_and_process_uart_data(g_lora_uart_handle);
|
||
read_and_process_uart_data(g_term_uart_handle);
|
||
}
|
||
|
||
int RF_AUX_RISE = 0;
|
||
int RF_AUX_DOWN = 0;
|
||
|
||
//上升沿下降沿触发
|
||
void HAL_GPIO_EXIT_Callback(uint16_t GPIO_Pin)
|
||
{
|
||
if(GPIO_Pin == RF_AUX_Pin)
|
||
{
|
||
// 下降
|
||
if(HAL_GPIO_ReadPin(RF_AUX_GPIO_Port, RF_AUX_Pin) == GPIO_PIN_RESET)
|
||
{
|
||
RF_AUX_DOWN = 1;
|
||
}
|
||
// 上升
|
||
else if(HAL_GPIO_ReadPin(RF_AUX_GPIO_Port, RF_AUX_Pin) == GPIO_PIN_SET)
|
||
{
|
||
RF_AUX_RISE = 1;
|
||
}
|
||
}
|
||
}
|
||
/* 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 */
|
||
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_TIM7_Init();
|
||
MX_DAC1_Init();
|
||
|
||
// 串口初始化
|
||
init_lora_uart();
|
||
init_term_uart();
|
||
/* 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);
|
||
|
||
//开启定时器7中断
|
||
HAL_TIM_Base_Start_IT(&htim7);
|
||
HAL_TIM_Base_Start(&htim7);
|
||
|
||
term_printf("Current Sensor.\r\n");
|
||
term_printf("Version 1.0.0 Build: %s %s\r\n",__DATE__,__TIME__);
|
||
|
||
|
||
// 初始化E22
|
||
e22_init();
|
||
/* 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);
|
||
|
||
// 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 */
|