TMC2300步进电机驱动(独立步进模式)Demo程序

12 篇文章 9 订阅

目录

 

概述

一、平台介绍

二、STM32CubeMx配置

三、编码


概述

       本篇主要是讲TMC2300简单驱动程序,入门级教程。先来了解一些什么是TMC,全称是Trinamic,请移步到官网,全是英文,那我们先使用百度百科来科普一下。
根据百度百科讲述:
      TRINAMIC总部位于德国汉堡,经过近十几年的发展在半导体行业被称作是一个神话,主要致力与运动控制产品的设计与研发(步进和直流无刷系统)主要产品包括芯片,模块和系统。加上官网的一些介绍:实现自动目标定位的灵活斜坡发生器和业界最先进的步进电机驱动器结合在一起。个人能力有限,可能有出错的地方,请高人指出谢谢,共同进步,待后续更新一篇全面点文章。

 

官网:https://www.trinamic.com/
github:https://github.com/trinamic

一、平台介绍

1、硬件平台:

       STM32F103CBT6 + TMC2300

2、软件平台:

       VS2017 + STM32CubeMx 

3、datasheet需要自行阅读了,在此直接上干货,撸代码。

二、STM32CubeMx配置

 

三、编码

在这,关于怎么使用VS开发STM32,请移步到这篇文章《基于Visual Studio IDE + STM32CubeMX搭建STM32开发环境(详细介绍搭建过程)》

开发前需创建两个文件,分别是:motor.c与motor.h

motor.c文件
 

#include "motor.h"
#include "tim.h"
#include "main.h"


#define STEP_START() HAL_GPIO_WritePin(STEP_GPIO_Port, STEP_Pin, GPIO_PIN_SET);
#define STEP_STOP() HAL_GPIO_WritePin(STEP_GPIO_Port, STEP_Pin, GPIO_PIN_RESET);

uint16_t step = 1500; //12800;
uint8_t subdivide = 64;

void Subdivide_Set(uint8_t subdivide);


void DelayNop(uint8_t i)
{
	uint16_t j;
	do
	{
		for (j = 0; j < 100; j++){;}
	} while (i--);
}

void EN_Motor(uint8_t num)
{
	if (num == 0)
		HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
	else if (num ==	1)
		HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET);
}

void DIR_Set(uint8_t dir)
{
	if (dir == 0)
		HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_RESET);
	else if (dir == 1)
		HAL_GPIO_WritePin(DIR_GPIO_Port, DIR_Pin, GPIO_PIN_SET);
}

void Mode_Set(uint8_t mode)
{
	if (mode == 0)
		HAL_GPIO_WritePin(MODE_GPIO_Port, MODE_Pin, GPIO_PIN_RESET);
	else if (mode == 1)
		HAL_GPIO_WritePin(MODE_GPIO_Port, MODE_Pin, GPIO_PIN_SET);
}

void DIAG_Set(uint8_t diag)
{
	if (diag == 0)
		HAL_GPIO_WritePin(DIAG_GPIO_Port, DIAG_Pin, GPIO_PIN_RESET);
	else if (diag == 1)
		HAL_GPIO_WritePin(DIAG_GPIO_Port, DIAG_Pin, GPIO_PIN_SET);
}

void VCC_IO(uint8_t vcc_io)
{
	if (vcc_io == 0)
		HAL_GPIO_WritePin(VCC_IO_GPIO_Port, VCC_IO_Pin, GPIO_PIN_RESET);
	else if (vcc_io == 1)
		HAL_GPIO_WritePin(VCC_IO_GPIO_Port, VCC_IO_Pin, GPIO_PIN_SET);
}


void Motor_Init(void)
{
	VCC_IO(1);
	EN_Motor(1);
	DIR_Set(1);
	Mode_Set(1);
	DIAG_Set(0);
	
	Subdivide_Set(subdivide); 	//细分配置
	STEP_START();		//开始
	HAL_Delay(100);
}


void Motor_Handle(uint8_t dir, uint32_t Step)
{
	uint8_t		j = 10;
	uint32_t 	i;
	
	//电机使能
	HAL_GPIO_WritePin(GPIOA, EN_Pin, GPIO_PIN_SET);
	
	if (dir)
	{
		//正转
		HAL_GPIO_WritePin(GPIOA, DIR_Pin, GPIO_PIN_SET);
	}
	else
	{
		//反转
		HAL_GPIO_WritePin(GPIOA, DIR_Pin, GPIO_PIN_RESET);
	}
	
	for (i = 0; i < Step; i++)
	{
		//发送脉冲
		HAL_GPIO_WritePin(GPIOA, STEP_Pin, GPIO_PIN_RESET);
		
		if (j > 1)	j--;
		else ;		
		DelayNop(j);
		HAL_GPIO_WritePin(GPIOA, STEP_Pin, GPIO_PIN_SET);
		DelayNop(j);
	}
	
	HAL_Delay(5);
	//电机非使能
	HAL_GPIO_WritePin(GPIOA, EN_Pin, GPIO_PIN_RESET);
	
}

/*
	细分配置
*/
void Subdivide_Set(uint8_t subdivide)
{
	if (subdivide == 8)
	{
		HAL_GPIO_WritePin(GPIOB, MS1_AD0_Pin | MS2_AD1_Pin, GPIO_PIN_RESET);
	}
	else if (subdivide == 16)
	{
		HAL_GPIO_WritePin(GPIOB, MS1_AD0_Pin | MS2_AD1_Pin, GPIO_PIN_SET);
	}
	else if (subdivide == 32)
	{
		HAL_GPIO_WritePin(GPIOB, MS1_AD0_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOB, MS2_AD1_Pin, GPIO_PIN_SET);
	}
	else if (subdivide == 64)
	{
		HAL_GPIO_WritePin(GPIOB, MS1_AD0_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOB, MS2_AD1_Pin, GPIO_PIN_RESET);
	}
}

void Motor_Run(void) 
{

	Subdivide_Set(subdivide); 	//细分配置
	Motor_Handle(0, step); 	//正转
	HAL_Delay(1000);
	Motor_Handle(1, step); 	//反转
	HAL_Delay(1000);

		
}



motor.h文件

#ifndef MOTOR_H
#define MOTOR_H

#include "stdio.h"
#include "tim.h"




void Motor_Init(void);
void Motor_Run(void);





#endif // !MOTOR_H

main.c文件

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(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_TIM1_Init();
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
  Motor_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      Motor_Run();
    /* 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};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** 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.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* 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 */

  /* 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,
     tex: 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****/

 

传送门->代码

总结:功能实现,马达能正反转动,好了就介绍到这,同时也给自己做个笔记方便后续查阅。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ch_champion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值