172 lines
5.7 KiB
C
172 lines
5.7 KiB
C
/* USER CODE BEGIN Header */
|
||
/**
|
||
******************************************************************************
|
||
* @file : power_meter.h
|
||
* @brief : 电量计驱动程序
|
||
* @version : 1.0
|
||
* @changelog : version 1.0 初始版本 2025.12.31
|
||
******************************************************************************
|
||
* @attention
|
||
*
|
||
* Copyright (c) 2025 Yuwell Software Danyang.Jiangsu.China
|
||
* THIS SOFTWARE is licensed under the Mulan PSL v1.
|
||
* can use this software according to the terms and conditions of the Mulan PSL v1.
|
||
* You may obtain a copy of Mulan PSL v1 at:
|
||
* http://license.coscl.org.cn/MulanPSL
|
||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||
* PURPOSE.
|
||
* See the Mulan PSL v1 for more details.
|
||
*
|
||
******************************************************************************
|
||
*/
|
||
/* USER CODE END Header */
|
||
|
||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||
#ifndef __COULOMBMETER_H
|
||
#define __COULOMBMETER_H
|
||
|
||
#ifdef __cplusplus
|
||
extern "C" {
|
||
#endif
|
||
|
||
#include <stdint.h>
|
||
#include "mf_config.h"
|
||
#include "string.h"
|
||
|
||
|
||
/************************** 硬件参数重映射 **************************/
|
||
/* 对硬件使用进行配置 */
|
||
#define COULOMBMETER_USED_UART_NUM UART5
|
||
#define COULOMBMETER_UART_TX_GPIO_PORT GPIOC
|
||
#define COULOMBMETER_UART_TX_GPIO_PIN FL_GPIO_PIN_5
|
||
#define COULOMBMETER_UART_RX_GPIO_PORT GPIOC
|
||
#define COULOMBMETER_UART_RX_GPIO_PIN FL_GPIO_PIN_4
|
||
|
||
|
||
#define COULOMBMETER_IRQn UART5_IRQn
|
||
|
||
|
||
// CPU阻塞式发送
|
||
#define CHECK_TRANS_DONE while(FL_UART_IsActiveFlag_TXShiftBuffEmpty(COULOMBMETER_USED_UART_NUM) != FL_SET){};
|
||
#define COULOMBMETER_UART_TRANS_BYTE(x) FL_UART_WriteTXBuff(COULOMBMETER_USED_UART_NUM, x);CHECK_TRANS_DONE;
|
||
|
||
|
||
|
||
/************************** 芯片内部寄存器地址定义 **************************/
|
||
// 电参数寄存器(只读)
|
||
#define Addr_I_WAVE 0x01 // 电流通道波形
|
||
#define Addr_V_WAVE 0x02 // 电压通道波形
|
||
#define Addr_I_RMS 0x03 // 电流有效值
|
||
#define Addr_V_RMS 0x04 // 电压有效值
|
||
#define Addr_I_FAST_RMS 0x05 // 电流快速有效值
|
||
#define Addr_WATT 0x06 // 有功功率
|
||
#define Addr_CF_CNT 0x07 // 有功电能脉冲计数
|
||
#define Addr_FREQ 0x08 // 工频频率
|
||
#define Addr_STATUS 0x09 // 状态
|
||
#define Addr_VERSION 0x0F // 版本
|
||
|
||
// 用户操作寄存器(读写)
|
||
#define Addr_I_CHOS 0x11 // 电流通道直流偏置校正
|
||
#define Addr_I_RMSOS 0x12 // 电流通道有效值小信号校正
|
||
#define Addr_WA_CREEP 0x14 // 有功功率防潜阈值
|
||
#define Addr_FAST_RMS_TH 0x15 // 电流快速有效值阈值寄存器
|
||
#define Addr_FAST_RMS_CYC 0x16 // 电流快速有效值刷新周期寄存器
|
||
#define Addr_FREQ_CYC 0x17 // 线电压频率刷新周期寄存器
|
||
#define Addr_MASK 0x18 // 输出配置寄存器
|
||
#define Addr_MODE 0x19 // 用户模式选择寄存器
|
||
#define Addr_GAIN_CR 0x1A // 电流通道增益控制寄存器
|
||
#define Addr_SOFT_RESET 0x1C // 软复位
|
||
#define Addr_WRPROT 0x1D // 用户写保护设置
|
||
|
||
|
||
|
||
//注意 BL0940的读命令字节固定为0x58+ICAddr,写命令字节固定为0xA8+ICAddr;SOP10封装芯片的IC_Addr地址固定为0
|
||
// BL0942 TSSOP14封装带地址选择管脚,需根据A1~A2地址选择管脚的电平配置命令字节,可以进行多机并联通信
|
||
// 8F-5AW 制氧机采用的是SOP-10封装芯片 地址为ICAddr = 0
|
||
#define BL0942_ADDR_R 0x58
|
||
#define BL0942_ADDR_W 0xA8
|
||
|
||
|
||
|
||
//出厂校准芯片,增益控制1%以内,外围器件精度控制1%以内,采用同一系数,不用 EEPROM保存参数
|
||
// 功率转换系数=3537*1毫欧*0.51K*1000/(1.218*1.218)/(390K*5+0.51K)=623.39 ,整数运算考虑,放大10倍,*10=6234
|
||
// 电流转换系数=305978*1毫欧/1.218=251213,整数运算考虑,防止运算溢出,缩小10倍,/10=25121
|
||
// 电流转换系数=305978*2毫欧/1.218=502426,整数运算考虑,防止运算溢出,缩小10倍,/10=50243
|
||
// 电压转换系数=73989*0.51K*1000/1.218/(390K*5+0.51K)=15883 0
|
||
// 电能系数=3600000*Power_K/1638.4/256=5350.6,对应于1度电的脉冲计数
|
||
// 电流采用1毫欧电阻采样,电压采用390K*5+0.51K进行分压,实际测试发现电阻存在偏差,进行微调
|
||
// BL0942评估版,立创直接贴片合金电阻(台湾厚声MS121WF100NT4E ),实际测量比1毫欧偏小,约0.93毫欧
|
||
|
||
#define Power_K 5798; //理论值*0.93
|
||
#define Current_K 50243; //理论值*0.93
|
||
#define Voltage_K 15883; //
|
||
#define Energy_K 4976; //理论值*0.93
|
||
|
||
//采用美隆1毫欧贴片合金电阻,实际比1毫欧偏大,约1.023毫欧
|
||
/*
|
||
#define Power_K 6378; //理论值*1.023
|
||
#define Current_K 25699; //理论值*1.023
|
||
#define Voltage_K 15883; //
|
||
#define Energy_K 5474; //理论值*1.023
|
||
*/
|
||
|
||
|
||
|
||
|
||
/************************** 存储结构体定义 **************************/
|
||
typedef enum
|
||
{
|
||
BUS_COM_STATE_IDLE,
|
||
BUS_COM_STATE_WAITE_CURRENT,
|
||
BUS_COM_STATE_WAITE_VOLTAGE,
|
||
BUS_CHECK_ERROR,
|
||
BUS_COM_STATE_WAITE_CHECK
|
||
|
||
}Bus_Com_State;
|
||
|
||
|
||
typedef enum
|
||
{
|
||
MOD_CURRENT_METER,
|
||
MOD_VOLTAGE_METER
|
||
}Bus_Mod;
|
||
|
||
|
||
typedef struct
|
||
{
|
||
|
||
uint16_t bus_rms_current_mA; // 总线供电电路有效值 单位为mA
|
||
uint16_t bus_rms_voltage_V; // 总线供电电压有效值 单位为V
|
||
|
||
uint32_t voltage_raw;
|
||
uint8_t voltage_array[4];
|
||
|
||
uint32_t current_raw;
|
||
uint8_t current_array[4];
|
||
|
||
Bus_Com_State bus_com_stm;
|
||
|
||
uint8_t current_index;
|
||
|
||
}EleC_V_data;
|
||
|
||
extern EleC_V_data elec_v_data;
|
||
|
||
|
||
|
||
|
||
/************************** 外部API函数导出 **************************/
|
||
void User_PowerMeter_Init(void);
|
||
void UART5_IRQHandler(void);
|
||
|
||
void User_Coulombmeter_Data_refresh(void);
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif /* __COULOMBMETER_H */
|
||
|
||
/************************ (C) COPYRIGHT Yuwell *****END OF FILE****/
|