You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.3 KiB
128 lines
4.3 KiB
/*
|
|
* msp_K1061DC.c
|
|
*
|
|
* Created on: 2026年4月27日
|
|
* Author: SHI
|
|
*
|
|
* 压强传感器:量程-1~1Bar,测量精度:0.5%FS(1Kpa)
|
|
* 采集压强传感器输出(4~20mA,对应-1~1Bar)
|
|
* 电流采集模块K10-61DC
|
|
* 输出数据: 4000~20000μA
|
|
* 换算单位:(μA - 12000) / 80; //0.01 Bar (范围 -100 ~ 100 Kpa)
|
|
* 波特率:
|
|
* 地址:默认01
|
|
*/
|
|
|
|
#include "msp_K1061DC.h"
|
|
|
|
//压强 (单位: 0.01 Bar)
|
|
float *pressure;
|
|
|
|
// 模块 Modbus 地址 (默认 1,可根据实际修改)
|
|
#define addr_K1061DC 0x01
|
|
// 工程量A寄存器地址 (40027 = 0x001A)
|
|
#define REG_CURRENT_A 0x001A
|
|
|
|
struct UARTHandler *K1061DC_UART_Handler;
|
|
DispacherController *K1061DC_DispacherController;
|
|
|
|
static void decode_K1061DC(uint8_t *buffer, uint16_t length);
|
|
static void reading_K1061DC_Task(void);
|
|
|
|
void K1061DC_intialize(struct UARTHandler *Handler)
|
|
{
|
|
K1061DC_UART_Handler = Handler;
|
|
K1061DC_UART_Handler->Wait_time = 6;
|
|
// K1061DC_UART_Handler->UART_Decode = decode_K1061DC;
|
|
|
|
K1061DC_DispacherController = Handler->dispacherController;
|
|
K1061DC_DispacherController->Dispacher_Enable = 1;//不周期性发送
|
|
K1061DC_DispacherController->DispacherCallTime = 20;
|
|
K1061DC_DispacherController->Add_Dispatcher_List(K1061DC_DispacherController, reading_K1061DC_Task);
|
|
|
|
HardWareErrorController->Add_PCOMHardWare(HardWareErrorController,"K1061DC", 0, ComError_K1061DC);//注册硬件错误处理
|
|
LOG("K1061DC initialized success");//记录初始化日志
|
|
}
|
|
|
|
|
|
static void decode_K1061DC(uint8_t *buffer, uint16_t length)
|
|
{
|
|
if (buffer == NULL || length < 7) {
|
|
LOGFF(DL_ERROR, "K1061DC decode: invalid length %d", length);
|
|
return;
|
|
}
|
|
|
|
// // K1061DC: 帧长0x(字节), 地址0x
|
|
if (buffer[0] == addr_K1061DC)
|
|
{
|
|
uint16_t current_uA = 0;
|
|
// 使用 Modbus 库"bsp_MB_host.c"的解码函数
|
|
if (MB_Decode_HoldingRegs(buffer, length, 1, ¤t_uA) == 1)
|
|
{
|
|
if (current_uA < 4000) current_uA = 4000;
|
|
if (current_uA > 20000) current_uA = 20000;
|
|
// 线性映射
|
|
// float pressure_value = ((int)((current_uA - 12000) / 8))/10.0;//输出范围是 -100 ~ 100 Kpa(对应 -1.00 ~ 1.00 Bar)
|
|
float pressure_value = (current_uA - 12000) / 80.0;
|
|
if (pressure != NULL) {
|
|
*pressure = pressure_value;
|
|
}
|
|
// 可选: 更新硬件错误状态(通信成功)
|
|
HardWareErrorController->Set_PCOMHardWare(HardWareErrorController, "K1061DC", 1);
|
|
LOG("K1061DC decoding succeeded");
|
|
} else {
|
|
LOGFF(DL_ERROR,"K1061DC decoding failed");
|
|
}
|
|
}
|
|
}
|
|
//发送:01 03 00 1A 00 01 A5 CD
|
|
//接收:01 03 02 高位 地位 CRC(4000~20000对应4~20mA)
|
|
static void reading_K1061DC_Task(void)
|
|
{
|
|
// 使用 Modbus 库"bsp_MB_host.c"的读保持寄存器函数
|
|
MB_ReadHoldingReg(K1061DC_UART_Handler->Tx_Buf, &K1061DC_UART_Handler->TxCount,
|
|
addr_K1061DC, REG_CURRENT_A, 1);//addr reg num
|
|
|
|
K1061DC_UART_Handler->AddSendList(
|
|
K1061DC_UART_Handler,
|
|
K1061DC_UART_Handler->Tx_Buf,
|
|
K1061DC_UART_Handler->TxCount,
|
|
20, //总发送周期= 20 + (DispacherCallTime = 100) = 120
|
|
decode_K1061DC);
|
|
// K1061DC_UART_Handler->UART_Tx(K1061DC_UART_Handler);
|
|
LOG("read data from K1061DC");
|
|
}
|
|
|
|
|
|
//void T_F4_Remote_Controller_intialize(struct UARTHandler *Handler)
|
|
//{
|
|
// T_F4_Remote_Controller_UART_Handler = Handler;
|
|
// T_F4_Remote_Controller_UART_Handler->Wait_time = 6; //等待10ms 最低不要低于4;
|
|
//
|
|
// T_F4_dispacherController = Handler->dispacherController;
|
|
// T_F4_dispacherController->DispacherCallTime = 100;
|
|
// T_F4_dispacherController->Dispacher_Enable = 1;
|
|
// T_F4_dispacherController->Add_Dispatcher_List(T_F4_dispacherController,
|
|
// read_T_F4_Data);
|
|
// T_F4_dispacherController->Add_Dispatcher_List(T_F4_dispacherController,
|
|
// upload_state);
|
|
// HardWareErrorController->Add_PCOMHardWare(HardWareErrorController,
|
|
// "T_F4_Remote", 0, 12);
|
|
// LOG(DL_ERROR, "angle encoder decoding failed");
|
|
//}
|
|
//void read_T_F4_Data(void)
|
|
//{
|
|
// MB_ReadHoldingReg(&T_F4_Remote_Controller_UART_Handler->Tx_Buf,
|
|
// &T_F4_Remote_Controller_UART_Handler->TxCount, 2, 0, 6); //addr 3 reg 0 读取6 个
|
|
//
|
|
// T_F4_Remote_Controller_UART_Handler->AddSendList(
|
|
// T_F4_Remote_Controller_UART_Handler,
|
|
// T_F4_Remote_Controller_UART_Handler->Tx_Buf,
|
|
// T_F4_Remote_Controller_UART_Handler->TxCount,
|
|
// 20,
|
|
// decode_T_F4);
|
|
//// T_F4_Remote_Controller_UART_Handler->UART_Tx(
|
|
//// T_F4_Remote_Controller_UART_Handler);
|
|
//
|
|
//}
|
|
|
|
|