增加读取寄存器状态和控制步进电机功能
This commit is contained in:
136
Core/Inc/machine.h
Normal file
136
Core/Inc/machine.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : machine.h
|
||||
* @brief : LK540 Machine Stepper Motor Control Header
|
||||
* 步进电机控制模块头文件
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2026 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#ifndef __MACHINE_H
|
||||
#define __MACHINE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 步进电机方向枚举
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MOTOR_DIR_CWW = 0, /* 向左移动 (Counter Clockwise) */
|
||||
MOTOR_DIR_CW = 1 /* 向右移动 (Clockwise) */
|
||||
} Motor_DirectionTypeDef;
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* 电机参数定义 */
|
||||
#define MOTOR_PULSE_PER_MM 5.33f /* 每毫米脉冲数 */
|
||||
#define MOTOR_ORIGIN_DISTANCE 215.0f /* 原点距离 (mm) */
|
||||
#define MOTOR_MATERIAL1_DISTANCE 192.4f /* 1#料距离 (mm) */
|
||||
#define MOTOR_MATERIAL2_DISTANCE 65.4f /* 2#料距离 (mm) */
|
||||
#define MOTOR_MATERIAL3_DISTANCE 61.6f /* 3#料距离 (mm) */
|
||||
#define MOTOR_MATERIAL4_DISTANCE 188.6f /* 4#料距离 (mm) */
|
||||
|
||||
/* GPIO引脚定义 */
|
||||
#define MOTOR_ORIGIN_PIN_PORT GPIOB
|
||||
#define MOTOR_ORIGIN_PIN GPIO_PIN_3
|
||||
|
||||
#define MOTOR_LIMIT_PIN_PORT GPIOG
|
||||
#define MOTOR_LIMIT_PIN GPIO_PIN_15
|
||||
|
||||
#define MOTOR_DIR_PIN_PORT GPIOD
|
||||
#define MOTOR_DIR_PIN GPIO_PIN_7
|
||||
|
||||
#define MOTOR_STEP_PIN_PORT GPIOG
|
||||
#define MOTOR_STEP_PIN GPIO_PIN_9
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
/**
|
||||
* @brief 检测步进电机是否在原点
|
||||
* @retval 1: 在原点, 0: 不在原点
|
||||
*/
|
||||
uint8_t Machine_IsAtOrigin(void);
|
||||
|
||||
/**
|
||||
* @brief 检测步进电机是否在临界点(限位传感器)
|
||||
* @retval 1: 在临界点, 0: 不在临界点
|
||||
*/
|
||||
uint8_t Machine_IsAtLimit(void);
|
||||
|
||||
/**
|
||||
* @brief 设置步进电机方向
|
||||
* @param dir: 方向 (MOTOR_DIR_CWW: 向左, MOTOR_DIR_CW: 向右)
|
||||
* @retval None
|
||||
*/
|
||||
void Machine_SetDirection(Motor_DirectionTypeDef dir);
|
||||
|
||||
/**
|
||||
* @brief 步进电机移动一步
|
||||
* @note 产生一个脉冲信号,电机移动一步(1/5.33 mm)
|
||||
* @retval None
|
||||
*/
|
||||
void Machine_Step(void);
|
||||
|
||||
/**
|
||||
* @brief 步进电机移动到原点
|
||||
* @note 一直向左移动直到检测到原点传感器(PB3为0)
|
||||
* 如果检测到限位传感器(PG15为0),则停止并返回错误
|
||||
* @retval 0: 成功到达原点, 1: 到达限位点(错误)
|
||||
*/
|
||||
uint8_t Machine_MoveToOrigin(void);
|
||||
|
||||
/**
|
||||
* @brief 步进电机从原点移动到中心点
|
||||
* @note 中心点就是原点位置(215.0mm处)
|
||||
* 如果已经在原点,则不需要移动
|
||||
* @retval 0: 成功, 1: 失败
|
||||
*/
|
||||
uint8_t Machine_MoveToCenter(void);
|
||||
|
||||
/**
|
||||
* @brief 步进电机从中心点移动到1#料位置
|
||||
* @note 从原点(215.0mm)向左移动22.6mm到达1#料(192.4mm)
|
||||
* @retval 0: 成功, 1: 失败(到达限位点)
|
||||
*/
|
||||
uint8_t Machine_MoveToMaterial1(void);
|
||||
|
||||
/**
|
||||
* @brief 步进电机从中心点移动到2#料位置
|
||||
* @note 从原点(215.0mm)向左移动149.6mm到达2#料(65.4mm)
|
||||
* @retval 0: 成功, 1: 失败(到达限位点)
|
||||
*/
|
||||
uint8_t Machine_MoveToMaterial2(void);
|
||||
|
||||
/**
|
||||
* @brief 步进电机从中心点移动到3#料位置
|
||||
* @note 从原点(215.0mm)向右移动153.4mm到达3#料(61.6mm)
|
||||
* @retval 0: 成功, 1: 失败(到达限位点)
|
||||
*/
|
||||
uint8_t Machine_MoveToMaterial3(void);
|
||||
|
||||
/**
|
||||
* @brief 步进电机从中心点移动到4#料位置
|
||||
* @note 从原点(215.0mm)向右移动26.4mm到达4#料(188.6mm)
|
||||
* @retval 0: 成功, 1: 失败(到达限位点)
|
||||
*/
|
||||
uint8_t Machine_MoveToMaterial4(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MACHINE_H */
|
||||
138
Core/Inc/main.h
138
Core/Inc/main.h
@@ -1,69 +1,69 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.h
|
||||
* @brief : Header for main.c file.
|
||||
* This file contains the common defines of the application.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2026 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ET */
|
||||
|
||||
/* USER CODE END ET */
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EC */
|
||||
|
||||
/* USER CODE END EC */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EM */
|
||||
|
||||
/* USER CODE END EM */
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void Error_Handler(void);
|
||||
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
/* USER CODE END EFP */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MAIN_H */
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : main.h
|
||||
* @brief : Header for main.c file.
|
||||
* This file contains the common defines of the application.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2026 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MAIN_H
|
||||
#define __MAIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "machine.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ET */
|
||||
|
||||
/* USER CODE END ET */
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EC */
|
||||
|
||||
/* USER CODE END EC */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EM */
|
||||
|
||||
/* USER CODE END EM */
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void Error_Handler(void);
|
||||
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
/* USER CODE END EFP */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MAIN_H */
|
||||
|
||||
Reference in New Issue
Block a user