概述
目录
0 参考资料出处
参考博客
参考视频
1 实验预期效果
2 硬件学习
2.2 28BYJ-48步进电机
2.2.1 规格书
2.2.2 原理认识
2.3 ULN2003驱动板
2.3.1 电路图
2.3.2 原理图
3 STM32CubeMX的相关配置与接线
3.1 STM32CubeMX的配置
3.2 STM32与ULN2003的接线方式
4 代码编写
5 后记
5.1 电机内部结构
5.2 一个发现
0 参考资料出处
参考博客:
1、STM32驱动小型4相步进电机(ULN2003+20BYJ46)_PegasusYu的博客-CSDN博客;
2、【常用芯片】ULN2003工作原理及中文资料_Yngz_Miao的博客;
参考视频:
1、第20讲-步进电机控制_哔哩哔哩_bilibili;
1 实验预期效果
完成步进电机的正转(不通过串口控制)。
2 硬件学习
2.2 28BYJ-48步进电机
2.2.1 规格书
2.2.2 原理认识
【该部分为视频第20讲-步进电机控制_哔哩哔哩_bilibili的学习记录】【强推看原视频】
电机参数:
1~4为相线,5为电源线
同一根绕线,面对面通电励磁,吸引转子转动 :
该图中,定子8个齿,平均相间45°(即360°/8);转子6个齿,平均相间60°(即360°/6)。所以相差15°。根据系列计算可知,得出的结果与规格书并不相符。
所以:货不对板!!!!!!!!!!! (即上面那张图片中的齿数与实际齿数并不一致)
根据up主的电机拆解结果可知,一共被分成了32份,平均相间11.25°(即360°/32。而32是通过8*4得到,因为一共ABCD 4组,一组8个齿)
哪一个给“-”就相当于哪一个有电流通过
四拍:
八拍:
以分配顺序2为例,AB同时通电,则将转子齿吸引到两个定子齿中间。该种比上面一种更平滑。
则可得Px的对应十六进制码,控制端可自定:
可得对应驱动代码(可作参考):
减速比:
对应代码:
总的减速代码:
【以上是适用于89C52的代码,stm32的相关代码请看下文】
2.3 ULN2003驱动板
【具体细节请参阅博客:【常用芯片】ULN2003工作原理及中文资料_Yngz_Miao的博客,及视频第20讲-步进电机控制_哔哩哔哩_bilibili】
2.3.1 电路图
如下图,Input置1时,电路导通,电流励磁:
2.3.2 原理图
3 STM32CubeMX的相关配置与接线
3.1 STM32CubeMX的配置
本次使用PD4~PD7这四个GPIO,初始电平均置为低电平,按默认设置即可。
CubeMX的其他基础配置略(相关见以前写的博客:【STM32】1—零基础硬件软件配置)
完成配置并GENERATE CODE后,可在生成的系列文件中找到gpio.c,其中自动生成的代码如下:
#include "gpio.h"
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pins : PD4 PD5 PD6 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
3.2 STM32与ULN2003的接线方式
PD4 —— IN4 PD5 —— IN3 PD6 —— IN2 PD7 —— IN1
+ —— +5V - —— GND
另:ULN2003与步进电机的接线只需槽对齐插入即可。
4 代码编写
根据八拍的分配顺序:
连线序号 | 导线颜色 | 控制端 | 分配顺序 | |||||||
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |||
5 | 红 | VCC | VCC | VCC | VCC | VCC | VCC | VCC | VCC | VCC |
4 | 橙 | PD4 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
3 | 黄 | PD5 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
2 | 粉 | PD6 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
1 | 蓝 | PD7 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
【该代码基于博客STM32驱动小型4相步进电机_PegasusYu的博客-CSDN博客中的代码进行修改】
可得对应代码:
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
//1
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_SET);
//2
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
//3
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET);
//4
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_RESET);
//5
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET);
//6
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_RESET);
//7
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
//8
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET);
最终main.c文件代码如下 :
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint32_t free_time_delay;
/* 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();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/*rotate 8-step*/
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
//1
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_SET);
//2
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
//3
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET);
//4
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_RESET);
//5
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET);
//6
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_RESET);
//7
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
//8
free_time_delay = 240000; while(--free_time_delay>0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET);
/* 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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
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();
}
}
/* 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 %drn", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
包含文件如下:
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) 2022 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 "stm32f4xx_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 -----------------------------------------------------------*/
#define LD_Pin GPIO_PIN_4
#define LD_GPIO_Port GPIOD
#define LC_Pin GPIO_PIN_5
#define LC_GPIO_Port GPIOD
#define LB_Pin GPIO_PIN_6
#define LB_GPIO_Port GPIOD
#define LA_Pin GPIO_PIN_7
#define LA_GPIO_Port GPIOD
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
gpio.h:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file gpio.h
* @brief This file contains all the function prototypes for
* the gpio.c file
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_GPIO_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /*__ GPIO_H__ */
编写代码过程中,发现一个现象: 随着free_time_delay的数值从6000调到240000,电机的嗡嗡声逐渐增大,从原来只嗡嗡鸣响而不转动到后来可以转动。
程序编译下载后,效果如下:
ULN2003驱步进电机28BYJ-48(被拆开.jpg)
5 后记
5.1 电机内部结构
自己斗胆拆解了一下,发现装不回去了……
同前文up主所见一致,确为四层相间:
转子:
放入转子:
相啮齿轮:
5.2 一个发现
ULN2003上的 LED灯的亮度,在串口下载所用的5V与GND插上后,明显增亮。
最后
以上就是无奈招牌为你收集整理的【STM32】实战1—用STM32与ULN2003驱动步进电机28BYJ-48(一)0 参考资料出处1 实验预期效果2 硬件学习3 STM32CubeMX的相关配置与接线4 代码编写5 后记的全部内容,希望文章能够帮你解决【STM32】实战1—用STM32与ULN2003驱动步进电机28BYJ-48(一)0 参考资料出处1 实验预期效果2 硬件学习3 STM32CubeMX的相关配置与接线4 代码编写5 后记所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复