/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_delay.h"
#include "bsp_key.h"
#include "bsp_motor.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim8;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM8_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM8_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
delay_init(168);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET); /* 设置方向 */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); /* 使能ENABLE */
g_motor_state = MOTOR_STOP;
uint32_t tick_start_run; /* 开始运行时刻 */
uint32_t tick;
uint8_t buffer_usart_send[16];
uint8_t i;
uint16_t sum;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(KEY1_StateRead()==KEY_DOWN)
{
motor_linear_interpolation(100, 50, 10);
tick_start_run = HAL_GetTick();
}
if (KEY2_StateRead() == KEY_DOWN)
{
motor_linear_interpolation(-100, -50, 10);
tick_start_run = HAL_GetTick();
}
tick = HAL_GetTick();
if (MOTOR_STOP != g_motor_state)
{
buffer_usart_send[0] = 0x01; /* 帧头 */
buffer_usart_send[1] = 0x67; /* 帧头 */
buffer_usart_send[2] = 0x42; /* 帧头 */
buffer_usart_send[3] = 0xc0; /* 帧头 */
buffer_usart_send[4] = (tick - tick_start_run) >> 8; /* 时刻 */
buffer_usart_send[5] = (tick - tick_start_run) & 0xff; /* 时刻 */
buffer_usart_send[6] = g_rel_position_x >> 24; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[7] = g_rel_position_x >> 16; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[8] = g_rel_position_x >> 8; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[9] = g_rel_position_x & 0xff; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[10] = g_rel_position_y >> 24; /* 电机已经运行的步数 */
buffer_usart_send[11] = g_rel_position_y >> 16; /* 电机已经运行的步数 */
buffer_usart_send[12] = g_rel_position_y >> 8; /* 电机已经运行的步数 */
buffer_usart_send[13] = g_rel_position_y & 0xff; /* 电机已经运行的步数 */
sum = 0;
for (i = 4; i < 14; i++)
{
sum += buffer_usart_send[i];
}
buffer_usart_send[14] = sum >> 8;
buffer_usart_send[15] = sum & 0xff;
HAL_UART_Transmit(&huart1, buffer_usart_send, 16, 1000);
//delay_ms(10);
}
else
{
tick_start_run = HAL_GetTick();
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief TIM8 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM8_Init(void)
{
/* USER CODE BEGIN TIM8_Init 0 */
/* USER CODE END TIM8_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM8_Init 1 */
/* USER CODE END TIM8_Init 1 */
htim8.Instance = TIM8;
htim8.Init.Prescaler = 20;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.Period = 65535;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim8) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OnePulse_Init(&htim8, TIM_OPMODE_SINGLE) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 65535;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_DISABLE_OCxPRELOAD(&htim8, TIM_CHANNEL_1);
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_DISABLE_OCxPRELOAD(&htim8, TIM_CHANNEL_2);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM8_Init 2 */
__HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
/* USER CODE END TIM8_Init 2 */
HAL_TIM_MspPostInit(&htim8);
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11|GPIO_PIN_3|GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pins : PE2 PE3 PE4 PE0
PE1 */
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_0
|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pin : PF11 */
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pins : PD11 PD3 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_3|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#ifndef BSP_MOTOR_H
#define BSP_MOTOR_H
#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MOTOR_LEAD 40 /* 电机模组导程,单位0.1mm */
#define STEP_PER_CIRCLE 200 /* 细分为1时,电机走一圈需要的步数 */
#define MOTOR_DRIVER_SUBDIVISION 32 /*电机驱动器细分*/
#define TIM_CLK 8000000 /* 定时器时钟频率 */
#define TIM_ARR_MAX 65535
#define MOTOR_S_SHAPE_ACCELERATION_STEP_MAX 20480 /* 电机S曲线加速最长的步数 */
#define MOTOR_X_DIR_FORWARD \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET); \
} while(0)
#define MOTOR_X_DIR_REVERSE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET); \
} while(0)
#define MOTOR_X_ENABLE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); \
} while(0)
#define MOTOR_X_DISABLE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); \
} while(0)
#define MOTOR_Y_DIR_FORWARD \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_RESET); \
} while(0)
#define MOTOR_Y_DIR_REVERSE \
do { \
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_SET); \
} while(0)
#define MOTOR_Y_ENABLE \
do { \
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_RESET); \
} while(0)
#define MOTOR_Y_DISABLE \
do { \
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET); \
} while(0)
typedef enum
{
MOTOR_STOP = 0,
MOTOR_ACCELERATE,
MOTOR_UNIFORM,
MOTOR_DECELERATE
}motor_run_state_typedef;
typedef enum
{
MOTOR_FORWARD = 0,
MOTOR_REVERSE
}motor_dir_typedef;
typedef enum
{
MOTOR_X = 0,
MOTOR_Y
}motor_name_typedef;
typedef struct {
__IO uint8_t x_dir; /* X轴方向 */
__IO uint8_t y_dir; /* Y轴方向 */
__IO uint32_t end_x; /* 终点坐标X */
__IO uint32_t end_y; /* 终点坐标Y */
__IO uint32_t end_pulse; /* 终点位置的脉冲数 */
__IO uint32_t active_axis; /* 进给轴 */
__IO int32_t f_e; /* 函数方程 */
}interpolation_typedef;
extern motor_run_state_typedef g_motor_state;
extern motor_dir_typedef motor_x_dir, motor_y_dir;
extern uint32_t g_steps_total_x;
extern uint32_t g_steps_total_y;
extern uint32_t g_step_timer_pulse_num;
extern interpolation_typedef g_line;
extern __IO int32_t g_rel_position_x;
extern __IO int32_t g_rel_position_y;
extern __IO uint32_t g_steps_done_x;
extern __IO uint32_t g_steps_done_y;
uint8_t motor_linear_interpolation(int32_t distance_x, int32_t distance_y, uint32_t speed);
uint8_t line_inc_move(uint32_t inc_x, uint32_t inc_y);
#ifdef __cplusplus
}
#endif
#endif /* BSP_MOTOR_H */
#include "bsp_motor.h"
#include <math.h>
/**
* 电机运行状态
* 在main函数中会引用到
* 每当电机为运行状态时下位机通过串口将电机状态发送给上位机
*/
motor_run_state_typedef g_motor_state;
motor_dir_typedef motor_x_dir, motor_y_dir; /* 电机运行方向 */
uint32_t g_steps_total_x; /* 电机总共需要运行的步数 */
uint32_t g_steps_total_y; /* 电机总共需要运行的步数 */
uint32_t g_step_timer_pulse_num; /* 电机运行一步需要定时器的脉冲数 */
interpolation_typedef g_line;
__IO int32_t g_rel_position_x; /* 当前位置 单位:脉冲数 */
__IO int32_t g_rel_position_y; /* 当前位置 单位:脉冲数 */
__IO uint32_t g_steps_done_x; /* 已经运行的步数 单位:脉冲数 */
__IO uint32_t g_steps_done_y; /* 已经运行的步数 单位:脉冲数 */
/**
* @brief 电机直线运行
* @param distance_x:X轴运行的距离,单位:0.1mm,有符号数,正表示方向向前,负数表示方向向后
* @param distance_y:Y轴运行的距离,单位:0.1mm,有符号数,正表示方向向前,负数表示方向向后
* @param speed:匀速运行的速度,单位:0.1mm/s
* @note:
* @retval 0:执行成功
*/
uint8_t motor_linear_interpolation(int32_t distance_x, int32_t distance_y, uint32_t speed)
{
if (distance_x > 0)
{
motor_x_dir = MOTOR_FORWARD;
MOTOR_X_DIR_FORWARD;
}
else if (distance_x < 0)
{
motor_x_dir = MOTOR_REVERSE;
MOTOR_X_DIR_REVERSE;
distance_x = 0 - distance_x;
}
else /* distance_x == 0,参数有问题 */
{
return 1;
}
if (distance_y > 0)
{
motor_y_dir = MOTOR_FORWARD;
MOTOR_Y_DIR_FORWARD;
}
else if (distance_y < 0)
{
motor_y_dir = MOTOR_REVERSE;
MOTOR_Y_DIR_REVERSE;
distance_y = 0 - distance_y;
}
else /* distance_y == 0,参数有问题 */
{
return 1;
}
g_steps_total_x = distance_x * STEP_PER_CIRCLE * MOTOR_DRIVER_SUBDIVISION / MOTOR_LEAD; /* 将距离转换为步数 */
g_steps_total_y = distance_y * STEP_PER_CIRCLE * MOTOR_DRIVER_SUBDIVISION / MOTOR_LEAD; /* 将距离转换为步数 */
g_step_timer_pulse_num = TIM_CLK * MOTOR_LEAD * 1.0 / speed / STEP_PER_CIRCLE / MOTOR_DRIVER_SUBDIVISION;
line_inc_move(g_steps_total_x, g_steps_total_y);
return 0;
}
/**
* @brief 直线插补增量运动,从当前坐标(0,0)走到(inc_x,inc_y)
* @param inc_x:X坐标增量,单位:步
* @param inc_y:Y坐标增量,单位:步
* @note:
* @retval 0:执行成功
*/
uint8_t line_inc_move(uint32_t inc_x, uint32_t inc_y)
{
g_line.f_e = 0; //偏差方程置零
g_rel_position_x = 0; //将当前位置视为原点
g_rel_position_y = 0;
g_steps_done_x = 0;
g_steps_done_y = 0;
g_line.end_x = inc_x; //终点坐标对应的脉冲数位置
g_line.end_y = inc_y; //终点坐标对应的脉冲数位置
g_line.end_pulse = g_line.end_y + g_line.end_x;
g_line.active_axis = MOTOR_X; //第一步进给X轴
g_line.f_e = g_line.f_e - g_line.end_y;
g_motor_state = MOTOR_UNIFORM;
__HAL_TIM_CLEAR_IT(&htim8, TIM_IT_UPDATE);
if (MOTOR_X == g_line.active_axis)
{
TIM8->CCR1 = g_step_timer_pulse_num >> 1;
TIM8->ARR = g_step_timer_pulse_num;
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_1);
}
else if (MOTOR_Y == g_line.active_axis)
{
TIM8->CCR2 = g_step_timer_pulse_num >> 1;
TIM8->ARR = g_step_timer_pulse_num;
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_2);
}
else
{
return 1;
}
return 0;
}
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32f4xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_motor.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim8;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex-M4 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
while (1)
{
}
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}
/**
* @brief This function handles Pre-fetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */
/* USER CODE END SVCall_IRQn 0 */
/* USER CODE BEGIN SVCall_IRQn 1 */
/* USER CODE END SVCall_IRQn 1 */
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */
/* USER CODE END PendSV_IRQn 0 */
/* USER CODE BEGIN PendSV_IRQn 1 */
/* USER CODE END PendSV_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/**
* @brief This function handles TIM8 update interrupt and TIM13 global interrupt.
*/
void TIM8_UP_TIM13_IRQHandler(void)
{
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 0 */
/* USER CODE END TIM8_UP_TIM13_IRQn 0 */
HAL_TIM_IRQHandler(&htim8);
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 1 */
/* USER CODE END TIM8_UP_TIM13_IRQn 1 */
}
/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (MOTOR_X == g_line.active_axis)
{
HAL_TIM_PWM_Stop_IT(&htim8, TIM_CHANNEL_1);
g_steps_done_x++;
if (MOTOR_FORWARD == motor_x_dir)
{
g_rel_position_x++;
}
else
{
g_rel_position_x--;
}
}
else if (MOTOR_Y == g_line.active_axis)
{
HAL_TIM_PWM_Stop_IT(&htim8, TIM_CHANNEL_2);
g_steps_done_y++;
if (MOTOR_FORWARD == motor_y_dir)
{
g_rel_position_y++;
}
else
{
g_rel_position_y--;
}
}
else /* ERROR */
{
g_motor_state = MOTOR_STOP;
return;
}
/* 根据上一次的偏差判断进给方向,同时计算下一次的偏差 */
if (g_line.f_e >= 0)
{
g_line.active_axis = MOTOR_X;
g_line.f_e = g_line.f_e - g_line.end_y; // 第一象限的X轴进给时,偏差计算
}
else
{
g_line.active_axis = MOTOR_Y;
g_line.f_e = g_line.f_e + g_line.end_x; // 第一象限的Y轴进给时,偏差计算
}
/* 插补结束判断 */
if ((g_steps_done_x + g_steps_done_y) == g_line.end_pulse)
{
g_motor_state = MOTOR_STOP;
}
else
{
if (MOTOR_X == g_line.active_axis)
{
TIM8->CCR1 = g_step_timer_pulse_num >> 1;
TIM8->ARR = g_step_timer_pulse_num;
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_1);
}
else if (MOTOR_Y == g_line.active_axis)
{
TIM8->CCR2 = g_step_timer_pulse_num >> 1;
TIM8->ARR = g_step_timer_pulse_num;
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_2);
}
else /* ERROR */
{
g_motor_state = MOTOR_STOP;
return;
}
}
}
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
总结:
1、
2、
3、
4、
5、
6、
7、
8、
9、
10、