/* 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 */
uint8_t motor_pos_init_ing = 0; /* 电机正在复位标志 */
/* 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;
uint32_t speed_step_per_second;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if((KEY1_StateRead()==KEY_DOWN) && (motor_pos_init_ing == 0))
{
motor_s_shape_acceleration_init(300, 100, 10);
tick_start_run = HAL_GetTick();
}
if ((KEY2_StateRead() == KEY_DOWN) && (motor_pos_init_ing == 0))
{
motor_s_shape_acceleration_init(-300, 100, 10);
tick_start_run = HAL_GetTick();
}
if (KEY3_StateRead() == KEY_DOWN) /* 电机复位 */
{
HAL_TIM_PWM_Stop_IT(&htim8, TIM_CHANNEL_1);
motor_pos_init_ing = 1;
}
if (motor_pos_init_ing)
{
motor_pos_init(100, 10);
}
tick = HAL_GetTick();
if (MOTOR_STOP != g_motor_state)
{
speed_step_per_second = TIM_CLK / g_step_timer_pulse_num;
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] = speed_step_per_second >> 24; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[7] = speed_step_per_second >> 16; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[8] = speed_step_per_second >> 8; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[9] = speed_step_per_second & 0xff; /* 电机运行一步需要定时器的脉冲数 */
buffer_usart_send[10] = g_steps_done_total >> 24; /* 电机已经运行的总步数 */
buffer_usart_send[11] = g_steps_done_total >> 16; /* 电机已经运行的总步数 */
buffer_usart_send[12] = g_steps_done_total >> 8; /* 电机已经运行的总步数 */
buffer_usart_send[13] = g_steps_done_total & 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_DIV
STM32F407 步进电机复位找原点
于 2022-09-17 18:13:21 首次发布

最低0.47元/天 解锁文章
2290

被折叠的 条评论
为什么被折叠?



