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.
109 lines
2.9 KiB
109 lines
2.9 KiB
|
1 day ago
|
#include "dmk.h"
|
||
|
|
#include "flash_operation.h"
|
||
|
|
#include "modbus.h"
|
||
|
|
#include "iwdg.h"
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
uint32_t Rx_Pre_time = 0;
|
||
|
|
|
||
|
|
void handle_power_on_off() {
|
||
|
|
// holdingRegisters[5] 决定是否手动关闭电源
|
||
|
|
if (holdingRegisters[5] != 0) // 手动关闭电源
|
||
|
|
{
|
||
|
|
|
||
|
|
if (holdingRegisters[6] == 0) // 关闭电源
|
||
|
|
{
|
||
|
|
if (holdingRegisters[4] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin,
|
||
|
|
GPIO_PIN_RESET); // k5设为1 低电平亮灯
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if (holdingRegisters[4] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// 自动关闭电源 决定是否手动关闭电源
|
||
|
|
if (HAL_GetTick() >= Rx_Pre_time &&
|
||
|
|
HAL_GetTick() - Rx_Pre_time > holdingRegisters[7]) {
|
||
|
|
if (holdingRegisters[4] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if (holdingRegisters[4] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_RESET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K5_GPIO_Port, K5_Pin, GPIO_PIN_SET);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void SpoolendInit(void) {
|
||
|
|
Modbus_Init();
|
||
|
|
ReadParametersFromFlash((int16_t *)holdingRegisters);
|
||
|
|
holdingRegisters[8] = 2000;
|
||
|
|
Rx_Pre_time = HAL_GetTick();
|
||
|
|
}
|
||
|
|
|
||
|
|
void SpoolendTask(void) {
|
||
|
|
HAL_IWDG_Refresh(&hiwdg);
|
||
|
|
Modbus_Process();
|
||
|
|
|
||
|
|
if (holdingRegisters[0] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_RESET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K1_GPIO_Port, K1_Pin, GPIO_PIN_SET);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (holdingRegisters[1] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_RESET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K2_GPIO_Port, K2_Pin, GPIO_PIN_SET);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (holdingRegisters[2] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K3_GPIO_Port, K3_Pin, GPIO_PIN_RESET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K3_GPIO_Port, K3_Pin, GPIO_PIN_SET);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (holdingRegisters[3] == 0) {
|
||
|
|
HAL_GPIO_WritePin(K4_GPIO_Port, K4_Pin, GPIO_PIN_RESET);
|
||
|
|
} else {
|
||
|
|
HAL_GPIO_WritePin(K4_GPIO_Port, K4_Pin, GPIO_PIN_SET);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (RS485_1_RX) {
|
||
|
|
RS485_1_RX = 0;
|
||
|
|
Rx_Pre_time = HAL_GetTick();
|
||
|
|
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (RS485_1_Host_Existed) {
|
||
|
|
RS485_1_Host_Existed = 0;
|
||
|
|
Rx_Pre_time = HAL_GetTick();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (HAL_GetTick() >= Rx_Pre_time &&
|
||
|
|
HAL_GetTick() - Rx_Pre_time > holdingRegisters[7]) // 超时了
|
||
|
|
{
|
||
|
|
MB_WriteHoldingReg(1, 0x033, 0x0); // 停止运行
|
||
|
|
} else {
|
||
|
|
handle_dmk_motor(holdingRegisters[8], holdingRegisters[9]);
|
||
|
|
}
|
||
|
|
handle_power_on_off();
|
||
|
|
|
||
|
|
// only when holdingRegisters[9] == 55 time can be changed
|
||
|
|
if (holdingRegisters[10] == 55) {
|
||
|
|
WriteParametersToFlash((int16_t *)holdingRegisters);
|
||
|
|
holdingRegisters[10] = 1;
|
||
|
|
}
|
||
|
|
}
|