10 changed files with 206 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||||
|
cmake_minimum_required(VERSION 3.10) |
||||
|
|
||||
|
set(COMMON_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../library/CMakeLists.txt") |
||||
|
|
||||
|
if(EXISTS ${COMMON_CMAKE_PATH}) |
||||
|
include(${COMMON_CMAKE_PATH}) |
||||
|
else() |
||||
|
message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}") |
||||
|
endif() |
||||
|
|
||||
|
if(TARGET stm32cubemx) |
||||
|
target_link_libraries(Spoolold PUBLIC stm32cubemx) |
||||
|
else() |
||||
|
message(WARNING "[Spoolold] Dependency 'stm32cubemx' not found.") |
||||
|
endif() |
||||
@ -0,0 +1,109 @@ |
|||||
|
#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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
/*
|
||||
|
* dmk.c |
||||
|
* |
||||
|
* Created on: Jan 14, 2026 |
||||
|
* Author: akeguo |
||||
|
*/ |
||||
|
#include "dmk.h" |
||||
|
#include "main.h" |
||||
|
#include "dma.h" |
||||
|
#include "iwdg.h" |
||||
|
#include "tim.h" |
||||
|
#include "usart.h" |
||||
|
#include "gpio.h" |
||||
|
uint16_t Is_First_Run = 0; |
||||
|
|
||||
|
void handle_dmk_motor(uint16_t velocity, int state) { |
||||
|
|
||||
|
// if (Is_First_Run <= 5) {
|
||||
|
// HAL_Delay(50);
|
||||
|
// MB_WriteHoldingReg(1, 0, 4);//设置模式为0
|
||||
|
// HAL_Delay(100);
|
||||
|
// Is_First_Run++;;
|
||||
|
// return;
|
||||
|
// }
|
||||
|
|
||||
|
char ms_100_state = HAL_GetTick() / 300 % 3; |
||||
|
switch (ms_100_state) { |
||||
|
case 1: { |
||||
|
MB_WriteHoldingReg(1, 0x06a, velocity); |
||||
|
break; |
||||
|
} |
||||
|
case 2: { |
||||
|
if (state == 1) { |
||||
|
//正转
|
||||
|
MB_WriteHoldingReg(1, 0x033, 0x01); |
||||
|
|
||||
|
} else if (state == 0) { |
||||
|
//stop
|
||||
|
MB_WriteHoldingReg(1, 0x033, 0x0); |
||||
|
|
||||
|
} else if (state == 2) { |
||||
|
//reverse
|
||||
|
MB_WriteHoldingReg(1, 0x033, 0x14); |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
case 0: { |
||||
|
MB_WriteHoldingReg(1, 0, 0);//设置模式为0
|
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
//HAL_Delay(80);
|
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
/*
|
||||
|
* dmk.h |
||||
|
* |
||||
|
* Created on: Jan 14, 2026 |
||||
|
* Author: akeguo |
||||
|
*/ |
||||
|
|
||||
|
#ifndef INC_DMK_H_ |
||||
|
#define INC_DMK_H_ |
||||
|
#include "stm32g4xx_hal.h" |
||||
|
#include <stdint.h> |
||||
|
#include <stdbool.h> |
||||
|
#include "modbus.h" |
||||
|
#include "usart.h" |
||||
|
|
||||
|
|
||||
|
extern void handle_dmk_motor(uint16_t velocity, int state); |
||||
|
extern uint16_t Is_First_Run; |
||||
|
|
||||
|
#endif /* INC_DMK_H_ */ |
||||
Loading…
Reference in new issue