新增校验功能

This commit is contained in:
ipason 2026-01-06 16:00:44 +08:00
parent 8cafcfd9db
commit 8a32570868
7 changed files with 3788 additions and 19243 deletions

View File

@ -1,6 +1,6 @@
/** /**
****************************************************************************** ******************************************************************************
* @file coulombmeter.c * @file power_meter.c
* @author Motor Control SDK Team, Yuwell Software XiangenWang * @author Motor Control SDK Team, Yuwell Software XiangenWang
* @brief Voice Recognition Module Initialization Section, * @brief Voice Recognition Module Initialization Section,
including peripheral initialization and message node insertion, etc. including peripheral initialization and message node insertion, etc.
@ -47,7 +47,7 @@
*/ */
/* USER CODE END Header */ /* USER CODE END Header */
#include "coulombmeter.h" #include "power_meter.h"
// 提供给外部的采样结果数据 // 提供给外部的采样结果数据
@ -92,11 +92,9 @@ static void coulombmeter_read_reg(uint8_t addr)
COULOMBMETER_UART_TRANS_BYTE(addr); COULOMBMETER_UART_TRANS_BYTE(addr);
} }
//
uint8_t data[32]; void User_PowerMeter_Init(void)
void User_Coulombmeter_Init(void)
{ {
FL_GPIO_InitTypeDef GPIO_InitStruct; FL_GPIO_InitTypeDef GPIO_InitStruct;
@ -131,28 +129,22 @@ void User_Coulombmeter_Init(void)
FL_UART_ClearFlag_RXBuffFull(COULOMBMETER_USED_UART_NUM); FL_UART_ClearFlag_RXBuffFull(COULOMBMETER_USED_UART_NUM);
FL_UART_EnableIT_RXBuffFull(COULOMBMETER_USED_UART_NUM); FL_UART_EnableIT_RXBuffFull(COULOMBMETER_USED_UART_NUM);
// 开启发送完毕中断(由于需要发送的字节较少 此处不使用)
// FL_UART_ClearFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM);
// FL_UART_EnableIT_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM);
// 清空结果 // 清空结果
memset(&elec_v_data, 0, sizeof(elec_v_data)); memset(&elec_v_data, 0, sizeof(elec_v_data));
memset(data, 0, sizeof(data));
} }
//
// 推荐1s刷新一次 最终数据将存储在全局变量elec_v_data中 // 推荐1s刷新一次 最终数据将存储在全局变量elec_v_data中
void User_Coulombmeter_Data_refresh(void) void User_Coulombmeter_Data_refresh(void)
{ {
static uint8_t mod = 0; static Bus_Mod mod = MOD_CURRENT_METER;
if(mod == 0) if(mod == MOD_VOLTAGE_METER)
{ {
mod = 1; mod = MOD_CURRENT_METER;
coulombmeter_read_reg(Addr_V_RMS); coulombmeter_read_reg(Addr_V_RMS);
elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_VOLTAGE; // 转入等待电压数据状态 elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_VOLTAGE; // 转入等待电压数据状态
@ -160,7 +152,7 @@ void User_Coulombmeter_Data_refresh(void)
elec_v_data.voltage_raw = 0; elec_v_data.voltage_raw = 0;
}else{ }else{
mod = 0; mod = MOD_VOLTAGE_METER;
coulombmeter_read_reg(Addr_I_RMS); coulombmeter_read_reg(Addr_I_RMS);
elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_CURRENT; // 转入等待电压数据状态 elec_v_data.bus_com_stm = BUS_COM_STATE_WAITE_CURRENT; // 转入等待电压数据状态
@ -172,7 +164,6 @@ void User_Coulombmeter_Data_refresh(void)
uint8_t i = 0;
void UART5_IRQHandler(void) void UART5_IRQHandler(void)
{ {
@ -189,11 +180,20 @@ void UART5_IRQHandler(void)
uint8_t tmp_data = 0; uint8_t tmp_data = 0;
tmp_data = FL_UART_ReadRXBuff(COULOMBMETER_USED_UART_NUM); tmp_data = FL_UART_ReadRXBuff(COULOMBMETER_USED_UART_NUM);
elec_v_data.voltage_raw |= (tmp_data<<(elec_v_data.current_index*8)); if(elec_v_data.current_index < 3)
elec_v_data.current_index++;
if(elec_v_data.current_index == 3)
{ {
elec_v_data.voltage_raw |= (tmp_data<<(elec_v_data.current_index*8));
}
elec_v_data.voltage_array[elec_v_data.current_index++] = tmp_data;
if(elec_v_data.current_index == 4)
{
uint8_t checkSum = ~((elec_v_data.voltage_array[0] + elec_v_data.voltage_array[1] + elec_v_data.voltage_array[2] + BL0942_ADDR_R + Addr_V_RMS)&0xFF);
if(elec_v_data.voltage_array[3] == checkSum)
{
// 校验正确对数据进行赋值
elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE; elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE;
elec_v_data.bus_rms_voltage_V =elec_v_data.voltage_raw / Voltage_K; elec_v_data.bus_rms_voltage_V =elec_v_data.voltage_raw / Voltage_K;
@ -203,16 +203,29 @@ void UART5_IRQHandler(void)
elec_v_data.current_index = 0; elec_v_data.current_index = 0;
} }
}
}else if(elec_v_data.bus_com_stm == BUS_COM_STATE_WAITE_CURRENT) }else if(elec_v_data.bus_com_stm == BUS_COM_STATE_WAITE_CURRENT)
{ {
uint8_t tmp_data = 0; uint8_t tmp_data = 0;
tmp_data = FL_UART_ReadRXBuff(COULOMBMETER_USED_UART_NUM); tmp_data = FL_UART_ReadRXBuff(COULOMBMETER_USED_UART_NUM);
elec_v_data.current_raw |= (tmp_data<<(elec_v_data.current_index*8)); if(elec_v_data.current_index < 3)
elec_v_data.current_index++;
if(elec_v_data.current_index == 3)
{ {
elec_v_data.current_raw |= (tmp_data<<(elec_v_data.current_index*8));
}
elec_v_data.current_array[elec_v_data.current_index++] = tmp_data;
if(elec_v_data.current_index == 4)
{
uint8_t checkSum = ~((elec_v_data.current_array[0] + elec_v_data.current_array[1] + elec_v_data.current_array[2] + BL0942_ADDR_R + Addr_I_RMS)&0xFF);
if(elec_v_data.current_array[3] == checkSum)
{
// 校验正确对数据进行赋值
elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE; elec_v_data.bus_com_stm = BUS_COM_STATE_IDLE;
elec_v_data.bus_rms_current_mA =elec_v_data.current_raw*100 / Current_K; elec_v_data.bus_rms_current_mA =elec_v_data.current_raw*100 / Current_K;
@ -223,18 +236,14 @@ void UART5_IRQHandler(void)
} }
} }
}
FL_UART_ClearFlag_RXBuffFull(COULOMBMETER_USED_UART_NUM); FL_UART_ClearFlag_RXBuffFull(COULOMBMETER_USED_UART_NUM);
} }
// 发送中断处理
if(FL_UART_IsEnabledIT_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM) &&
FL_UART_IsActiveFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM))
{
FL_UART_ClearFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM); //清除发送中断标志
}
} }

View File

@ -1,7 +1,7 @@
/* USER CODE BEGIN Header */ /* USER CODE BEGIN Header */
/** /**
****************************************************************************** ******************************************************************************
* @file : coulombmeter.h * @file : power_meter.h
* @brief : * @brief :
* @version : 1.0 * @version : 1.0
* @changelog : version 1.0 2025.12.31 * @changelog : version 1.0 2025.12.31
@ -113,56 +113,8 @@ extern "C" {
typedef union
{
uint8_t uByte[4];
uint16_t uInt[2];
uint32_t uLongs;
}FourBytes_Type;
typedef union
{
uint8_t uByte[2];
uint16_t uInt;
}TwoByte_Type;
//全电参数数据包格式
typedef struct
{
uint8_t head;
uint8_t I_RMS[3];
uint8_t V_RMS[3];
uint8_t I_FAST_RMS[3];
uint8_t WATT[3];
uint8_t CF_CNT[3];
uint8_t FREQ[3];
uint8_t STATUS[3];
uint8_t CHKSUM;
}AllFlame_StructDef;
typedef union
{
uint8_t B8[23];
AllFlame_StructDef Flame;
}AllFlame_TypeDef;
//BL0942/0940的电参数定义
typedef struct
{
uint16_t Energy_kwh; // 实际用电量,千瓦小时 XXXXXX,1度电/LSB最大65535度电
uint32_t Fir_CF_CNT; // 上一次的CF_CNT寄存器读数
uint32_t Mid_CF_CNT; // 小于1度电的脉冲底数
uint16_t Power_Value; // unit: 0.1W XXXX.X整型变量最低位代表0.1W
uint16_t Current_Value; // unit: 0.001A XX.XXX整型变量最低位代表0.001A
uint16_t Voltage_Value; // unit:0.01V XXX.XX整型变量最低位代表0.01V
uint16_t Freq; // unit:0.01Hz XX.XX整型变量最低位代表0.01Hz
uint16_t F_Current_Value;
}Elect_StructDef;
/************************** ´æ´¢½á¹¹Ì嶨Òå **************************/
typedef enum typedef enum
{ {
BUS_COM_STATE_IDLE, BUS_COM_STATE_IDLE,
@ -174,14 +126,11 @@ typedef enum
}Bus_Com_State; }Bus_Com_State;
typedef enum
typedef union
{ {
uint32_t raw_long; MOD_CURRENT_METER,
uint8_t raw_b8[4]; MOD_VOLTAGE_METER
}Bus_Mod;
}Raw_Data;
typedef struct typedef struct
@ -191,7 +140,10 @@ typedef struct
uint16_t bus_rms_voltage_V; // 总线供电电压有效值 单位为V uint16_t bus_rms_voltage_V; // 总线供电电压有效值 单位为V
uint32_t voltage_raw; uint32_t voltage_raw;
uint8_t voltage_array[4];
uint32_t current_raw; uint32_t current_raw;
uint8_t current_array[4];
Bus_Com_State bus_com_stm; Bus_Com_State bus_com_stm;
@ -204,27 +156,8 @@ extern EleC_V_data elec_v_data;
extern Elect_StructDef BL0942_Elect;
extern uint16_t u16_TmrCnt; //定时计数
extern bool IsGetBL0942EData; //定时到1秒与BL0942通信获取电参数
extern bool flg_Comm; //读数据是否成功状态
void Delay_MS(uint16_t Count);
void Uart_Init(void);
void Delay_100uS(uint16_t Count);
void BL0942_Uart_Init(void);
bool BL0942_Uart1_R(uint8_t ICAddr,uint8_t cmd);
void BL0942_Uart1_W(uint8_t ICAddr,uint8_t cmd);
void BL0942_Uart_ReadEData(void);
void BL0942_Uart_ConfigInit(void);
bool BL0942_Uart1_ReadAll(uint8_t ICAddr);
void BL0942_Uart_ReadEDataAll(void);
/************************** 外部API函数导出 **************************/ /************************** 外部API函数导出 **************************/
void User_Coulombmeter_Init(void); void User_PowerMeter_Init(void);
void UART5_IRQHandler(void); void UART5_IRQHandler(void);
void User_Coulombmeter_Data_refresh(void); void User_Coulombmeter_Data_refresh(void);

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -120,7 +120,6 @@
<SetRegEntry> <SetRegEntry>
<Number>0</Number> <Number>0</Number>
<Key>DLGUARM</Key> <Key>DLGUARM</Key>
<Name>?</Name>
</SetRegEntry> </SetRegEntry>
<SetRegEntry> <SetRegEntry>
<Number>0</Number> <Number>0</Number>
@ -174,7 +173,7 @@
<DebugFlag> <DebugFlag>
<trace>0</trace> <trace>0</trace>
<periodic>1</periodic> <periodic>1</periodic>
<aLwin>1</aLwin> <aLwin>0</aLwin>
<aCover>0</aCover> <aCover>0</aCover>
<aSer1>0</aSer1> <aSer1>0</aSer1>
<aSer2>0</aSer2> <aSer2>0</aSer2>
@ -306,11 +305,11 @@
<GroupNumber>3</GroupNumber> <GroupNumber>3</GroupNumber>
<FileNumber>6</FileNumber> <FileNumber>6</FileNumber>
<FileType>1</FileType> <FileType>1</FileType>
<tvExp>1</tvExp> <tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg> <tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2> <bDave2>0</bDave2>
<PathWithFileName>..\Src\coulombmeter.c</PathWithFileName> <PathWithFileName>..\8F-3AW-PowerMeter\power_meter.c</PathWithFileName>
<FilenameWithoutPath>coulombmeter.c</FilenameWithoutPath> <FilenameWithoutPath>power_meter.c</FilenameWithoutPath>
<RteFlg>0</RteFlg> <RteFlg>0</RteFlg>
<bShared>0</bShared> <bShared>0</bShared>
</File> </File>

View File

@ -10,7 +10,7 @@
<TargetName>Coulombmeter</TargetName> <TargetName>Coulombmeter</TargetName>
<ToolsetNumber>0x4</ToolsetNumber> <ToolsetNumber>0x4</ToolsetNumber>
<ToolsetName>ARM-ADS</ToolsetName> <ToolsetName>ARM-ADS</ToolsetName>
<pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARMCLANG506</pCCUsed> <pCCUsed>5060960::V5.06 update 7 (build 960)::.\ARM_Compiler_5.06u7</pCCUsed>
<uAC6>0</uAC6> <uAC6>0</uAC6>
<TargetOption> <TargetOption>
<TargetCommonOption> <TargetCommonOption>
@ -340,7 +340,7 @@
<MiscControls></MiscControls> <MiscControls></MiscControls>
<Define>FM33LG0XX,USE_FULL_ASSERT</Define> <Define>FM33LG0XX,USE_FULL_ASSERT</Define>
<Undefine></Undefine> <Undefine></Undefine>
<IncludePath>..\Drivers\CMSIS;..\Drivers\FM33LG0xx_FL_Driver\Inc;..\Inc;..\MF-config\Inc</IncludePath> <IncludePath>..\Drivers\CMSIS;..\Drivers\FM33LG0xx_FL_Driver\Inc;..\Inc;..\MF-config\Inc;..\8F-3AW-PowerMeter</IncludePath>
</VariousControls> </VariousControls>
</Cads> </Cads>
<Aads> <Aads>
@ -420,9 +420,9 @@
<FilePath>..\Src\fm33lg0xx_fl_common.c</FilePath> <FilePath>..\Src\fm33lg0xx_fl_common.c</FilePath>
</File> </File>
<File> <File>
<FileName>coulombmeter.c</FileName> <FileName>power_meter.c</FileName>
<FileType>1</FileType> <FileType>1</FileType>
<FilePath>..\Src\coulombmeter.c</FilePath> <FilePath>..\8F-3AW-PowerMeter\power_meter.c</FilePath>
</File> </File>
</Files> </Files>
</Group> </Group>

View File

@ -51,7 +51,7 @@
#include "main.h" #include "main.h"
#include "fm33_assert.h" #include "fm33_assert.h"
#include "coulombmeter.h" #include "power_meter.h"
#define FREQ_CHECK_ENABLE (1) // 选择是否使能频率检查 仅在debug阶段使用 #define FREQ_CHECK_ENABLE (1) // 选择是否使能频率检查 仅在debug阶段使用
@ -76,7 +76,7 @@ int main(void)
FL_GPIO_InitTypeDef GPIO_InitStruct; FL_GPIO_InitTypeDef GPIO_InitStruct;
// 初始化GPIO // 开启LED闪烁
GPIO_InitStruct.pin = FL_GPIO_PIN_5; GPIO_InitStruct.pin = FL_GPIO_PIN_5;
GPIO_InitStruct.mode = FL_GPIO_MODE_OUTPUT; GPIO_InitStruct.mode = FL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
@ -86,10 +86,15 @@ int main(void)
FL_GPIO_Init(GPIOD, &GPIO_InitStruct); FL_GPIO_Init(GPIOD, &GPIO_InitStruct);
// 开启压缩机继电器
GPIO_InitStruct.pin = FL_GPIO_PIN_12; GPIO_InitStruct.pin = FL_GPIO_PIN_12;
FL_GPIO_Init(GPIOC, &GPIO_InitStruct); FL_GPIO_Init(GPIOC, &GPIO_InitStruct);
FL_GPIO_SetOutputPin(GPIOC, FL_GPIO_PIN_12); FL_GPIO_SetOutputPin(GPIOC, FL_GPIO_PIN_12);
// 初始化定时器任务 // 初始化定时器任务
#if FREQ_CHECK #if FREQ_CHECK
/* 系统始终频率检测 */ /* 系统始终频率检测 */
@ -99,13 +104,15 @@ int main(void)
#endif #endif
User_Coulombmeter_Init(); // 初始化电量计芯片 User_PowerMeter_Init(); // 初始化电量计芯片
while(1) while(1)
{ {
FL_DelayMs(1000); FL_DelayMs(1000);
User_Coulombmeter_Data_refresh(); User_Coulombmeter_Data_refresh();
// LED灯闪烁
FL_GPIO_ToggleOutputPin(GPIOD, FL_GPIO_PIN_5); FL_GPIO_ToggleOutputPin(GPIOD, FL_GPIO_PIN_5);
} }