我是靠谱客的博主 无奈招牌,最近开发中收集的这篇文章主要介绍【STM32】实战1—用STM32与ULN2003驱动步进电机28BYJ-48(一)0 参考资料出处1 实验预期效果2 硬件学习3 STM32CubeMX的相关配置与接线4 代码编写5 后记,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

目录

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 规格书

b5d673e7b0f7454f8c7a2d872ce5e245.png

2.2.2 原理认识

         【该部分为视频第20讲-步进电机控制_哔哩哔哩_bilibili的学习记录】【强推看原视频】

        电机参数:

11d6d9d0af8f4351a155106b99ce2714.png

         1~4为相线,5为电源线87cf6a6fac6c474d9ba842525113781b.png

       同一根绕线,面对面通电励磁,吸引转子转动 :7f7b545d442c405f8406f9f1fabbd740.png

       该图中,定子8个齿,平均相间45°(即360°/8);转子6个齿,平均相间60°(即360°/6)。所以相差15°。根据系列计算可知,得出的结果与规格书并不相符。

        所以:货不对板!!!!!!!!!!! (即上面那张图片中的齿数与实际齿数并不一致)

        根据up主的电机拆解结果可知,一共被分成了32份,平均相间11.25°(即360°/32。而32是通过8*4得到,因为一共ABCD 4组,一组8个齿)

0fbeedb88d3543a7b1c6be4930933f6b.png

         哪一个给“-”就相当于哪一个有电流通过

        四拍:

1a4a93b0a44d42f49831c9fa548d0024.png

         八拍:

        以分配顺序2为例,AB同时通电,则将转子齿吸引到两个定子齿中间。该种比上面一种更平滑。

0988e353d76a4052a75ef69862be4954.png

        则可得Px的对应十六进制码,控制端可自定: 

6a26a75f6fb7498b8b908b8ba6fc72f0.png

         可得对应驱动代码(可作参考):

852cc161e8d24b429391de14f3f7c88a.png

        减速比:

6b5a319146ea457aae782c11a5a225cd.png

        对应代码:

188d7a9414284235be22c02f67f4e466.png

         总的减速代码:

4f8320d9b5f741668da2f83f0d278487.png

        【以上是适用于89C52的代码,stm32的相关代码请看下文】

2.3 ULN2003驱动板

        【具体细节请参阅博客:【常用芯片】ULN2003工作原理及中文资料_Yngz_Miao的博客,及视频第20讲-步进电机控制_哔哩哔哩_bilibili】

2.3.1 电路图 

239a79f6a56b42d4a954a6d159defa64.png

        如下图,Input置1时,电路导通,电流励磁:

0ba570cfc3644f2fa0226d91d006eae4.png

2.3.2 原理图

a52ffdd20d91485c8128eb44e0796706.png

3 STM32CubeMX的相关配置与接线

3.1 STM32CubeMX的配置

        本次使用PD4~PD7这四个GPIO,初始电平均置为电平,按默认设置即可。 

65f1ef400a6c496ba00878b8714874b6.png         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 代码编写

        根据八拍的分配顺序:

连线序号导线颜色控制端分配顺序
12345678
5VCCVCCVCCVCCVCCVCCVCCVCCVCC
4PD411000001
3PD501110000
2PD600011100
1PD700000111

        【该代码基于博客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主所见一致,确为四层相间:

a73ec8b6abeb419c92fdf545e103111f.png

         转子:

7010f3697e87432f97a66dbf7a992cbd.png

        放入转子:

e85e68c9ddfa4ac091b038634afc25ac.png

        相啮齿轮:

 cc5cd043b5d64b2792e8c039370573f8.png

9ec14003dfeb4af4be30097a741c28c3.png

8b9db49c466d43729a625c52882babe5.png

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 后记所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部