This repository has been archived on 2025-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
torsion/User/board/flowmeter.h

99 lines
3.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __FLOWMETER_H
#define __FLOWMETER_H
#include "board.h"
#include "agile_modbus.h"
#define FLOWMETER_RS485_PORT_1 UART_NUM_2
#define FLOWMETER_RS485_PORT_2 UART_NUM_4
#define FLOWMETER_MAX 2U // 流量计最大数量
#define FLOWMETER_MODBUS_RECV_LENGTH 30U // 接收缓冲区大小
#define FLOWMETER_MODBUS_SEND_LENGTH 30U // modbus最大数据长度
#define FLOWMETER_SLAVER_ADDR 1U // 从机地址
#define FLOWMETER1_START_REGISTERS_ADDR 0x02 // 起始寄存器地址
#define FLOWMETER1_REGISTERS_LEN 2U // 寄存器长度,一次性读取0x34个寄存器FLOWMETER_MODBUS_RECV_LENGTH、FLOWMETER_MODBUS_SEND_LENGTH根据这个来设置
#define FLOWMETER2_START_REGISTERS_ADDR 0x3A // 起始寄存器地址
#define FLOWMETER2_REGISTERS_LEN 2U // 寄存器长度
typedef struct
{
uint16_t data1; // 04寄存器值
uint16_t data2; // 05寄存器值
uint16_t data3; // 06寄存器值
} total_flow_t; // 累计总量=04寄存器值*65535+05寄存器值+06寄存器值/1000
typedef union
{
uint8_t data;
struct
{
uint8_t
total_flow : 1,
gas_correct : 1,
response_time : 1,
auto_zero : 1,
lower_limit : 1,
upper_limit : 1;
} bits;
} registers_u; // 寄存器
typedef struct
{
registers_u registers_write_enable; // 写使能寄存器
total_flow_t total_flow; // 累计总量 0x04、0x05、0x06寄存器
uint16_t gas_correct; // 气体修正系数 0x16寄存器
uint16_t response_time; // 响应时间 0x17寄存器 10、20、50、100、200、500、1000 毫秒
uint16_t auto_zero; // 自动教零 0x27寄存器 0xAA55
uint16_t lower_limit; // 下限报警 0x31寄存器 0-110
uint16_t upper_limit; // 上限报警 0x33寄存器 0-110
} flowmeter_calibrate_t;
typedef enum
{
PROCESS_IDEL, // 空闲状态,等待初始化,执行一次初始化
PROCESS_WRITE_REGISTERS, // 写寄存器
PROCESS_READ_REGISTERS, // 读取寄存器
PROCESS_RECEIVE_REGISTERS_DATA, // 接收数据
PROCESS_DESERIALIZE_DATA, // 反序列化数据
PROCESS_WAIT,
PROCESS_END,
} flowmeter_process_status_e;
typedef enum
{
FLOWMETER_PROCESS_NO_SLEEP,
FLOWMETER_PROCESS_SLEEP_RECVTM,
FLOWMETER_PROCESS_SLEEP_100ms,
FLOWMETER_PROCESS_SLEEP_1s,
FLOWMETER_PROCESS_SLEEP_3s,
} flowmeter_process_sleep_e;
typedef struct
{
uart_num_e id;
uint8_t send_buf[FLOWMETER_MODBUS_SEND_LENGTH];
uint16_t send_len;
} flowmeter_write_t; // 需要写寄存器的数据
typedef struct
{
BOOL idel_flag; // 通过这个标识位来判断是否需要初始化modbus
uint8_t calibration_flag;
uart_t *huart[FLOWMETER_MAX];
float32_t flow[FLOWMETER_MAX]; // 顺时流量
agile_modbus_rtu_t ctx_rtu;
uint8_t ctx_send_buf[FLOWMETER_MODBUS_SEND_LENGTH];
uint8_t ctx_recv_buf[FLOWMETER_MODBUS_RECV_LENGTH]; // 公用一个接收缓冲区
send_data_cb_t send_data_cb; // 发送数据回调函数,外部传入
flowmeter_write_t flowmeter_write; // 需要写寄存器的数据
} flowmeter_t;
extern void flowmeter_init(send_data_cb_t cb);
extern void flowmeter_process(void);
extern flowmeter_process_sleep_e flowmeter_process_need_sleep(void);
extern float32 flowmeter_get_flow(uint8_t index);
extern void flowmeter_calibrate_simulate(uint8_t index);
extern void flowmeter_set_calibration_flag(uint8_t bits);
#endif // __FLOWMETER_H