STM32F103+步进电机28BYJ-48+ULN2003 实现简单的正反转demo

@

前言

注意:浇水由LED1的亮灭进行模拟
源码参考:
    步进电机28BYJ-48的驱动程序(stm32f103c8t6)
    正点原子按键实验
开发板:正点原子 STM32F103 精英版
语言:C语言
开发环境:Keil5
开发板使用了 LED KEY 步进电机28BYJ-48 ULN2003驱动

代码下载:

码云 GitHub

功能介绍:

LED0约1秒1反转。
按KEY0,翻转LED1,电机反转1圈
按KEY1,翻转LED1,电机正转1圈
按KEY_UP,翻转LED1,电机正转半圈

拓展应用

STM32F103+步进电机28BYJ-48 简单应用之摇头、转圈、自定义模式demo
STM32F103+DHT11模块+步进电机28BYJ-48 简单实现 智能浇水系统demo

参考资料

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

接线

+    —>   5V
-    —>   GND
IN1  —>   PF1
IN2  —>   PF2
IN3  —>   PF3
IN4  —>   PF4

在这里插入图片描述

效果图

按下KEY0,LED1翻转,电机反转一圈。风车折法参考 传送门
在这里插入图片描述

核心代码

main.c

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "beep.h"
#include "step.h"

//IN4: PB6  d
//IN3: PB5  c
//IN2: PB4  b
//IN1: PB3  a

int main(void)
{
	vu8 key = 0;
	uint8_t time = 0;
	delay_init(); //延时函数初始化
	LED_Init();	  //初始化与LED连接的硬件接口
	BEEP_Init();  //初始化蜂鸣器端口
	KEY_Init();	  //初始化与按键连接的硬件接口
	Step_Motor_GPIO_Init();
	
	LED0 = 0;
	BEEP = 0;
	
	while (1)
	{
		key = KEY_Scan(0); //得到键值
		if (key)
		{
			switch (key)
			{
				case WKUP_PRES: // 翻转LED1,电机正转半圈
					LED1 = !LED1;
					/*
						功能:转1/64圈
						步距角5.625 360/5.625=64 减速比1/64
						故64*64个脉冲转一圈
						n 圈数
						direction 方向 1正转 非1反转
						delay delay时长ms >= 2
					*/
					motor_circle(32, 1, 2);
					break;
				case KEY1_PRES: // 翻转LED1,电机正转1圈
					LED1 = !LED1;
					motor_circle(64, 1, 2);
					break;
				case KEY0_PRES: // 翻转LED1,电机反转1圈
					LED1 = !LED1;
					motor_circle(64, 0, 2);
					break;
			}
		}
		
		time++;
		if(time % 100 == 0)
		{
			LED0 = !LED0;
		}
	
		delay_ms(10);
	}
}

step.h

#ifndef  __STEP_H__
#define __STEP_H__
 
#include "stm32f10x.h"
 
void Step_Motor_GPIO_Init(void);
/*
	功能:转1/64圈
	步距角5.625 360/5.625=64 减速比1/64
	故64*64个脉冲转一圈
	n 圈数
	direction 方向 1正转 非1反转
	delay delay时长 >= 2
*/
void motor_circle(int n, int direction, int delay);

#endif

step.c

#include "sys.h"
#include "delay.h"
#include "step.h"

//IN4: PF4  d
//IN3: PF3  c
//IN2: PF2  b
//IN1: PF1  a

u8 forward[4] = {0x03,0x06,0x0c,0x09}; // 正转
u8 reverse[4]= {0x03,0x09,0x0c,0x06}; // 反转

//引脚初始化
void Step_Motor_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1;
    GPIO_Init(GPIOF, &GPIO_InitStructure);
}

//根据输入数据改变引脚电平
void SetMotor(unsigned char InputData)
{
	if(InputData == 0x03)
	{
		GPIO_SetBits(GPIOF,GPIO_Pin_1);
		GPIO_SetBits(GPIOF,GPIO_Pin_2);
		GPIO_ResetBits(GPIOF,GPIO_Pin_3);
		GPIO_ResetBits(GPIOF,GPIO_Pin_4);
	}
	else if(InputData == 0x06)
	{
		GPIO_ResetBits(GPIOF,GPIO_Pin_1);
		GPIO_SetBits(GPIOF,GPIO_Pin_2);
		GPIO_SetBits(GPIOF,GPIO_Pin_3);
		GPIO_ResetBits(GPIOF,GPIO_Pin_4);
	}
	else if(InputData == 0x09)
	{
		GPIO_SetBits(GPIOF,GPIO_Pin_1);
		GPIO_ResetBits(GPIOF,GPIO_Pin_2);
		GPIO_ResetBits(GPIOF,GPIO_Pin_3);
		GPIO_SetBits(GPIOF,GPIO_Pin_4);
	}
	else if(InputData == 0x0c)
	{	
		GPIO_ResetBits(GPIOF,GPIO_Pin_1);
		GPIO_ResetBits(GPIOF,GPIO_Pin_2);
		GPIO_SetBits(GPIOF,GPIO_Pin_3);
		GPIO_SetBits(GPIOF,GPIO_Pin_4);
	}
	else if(InputData == 0x00)
	{
		GPIO_ResetBits(GPIOF,GPIO_Pin_1);
		GPIO_ResetBits(GPIOF,GPIO_Pin_2);
		GPIO_ResetBits(GPIOF,GPIO_Pin_3);
		GPIO_ResetBits(GPIOF,GPIO_Pin_4);
	}
}

/*
	功能:转1/64圈
	步距角5.625 360/5.625=64 减速比1/64
	故64*64个脉冲转一圈
	n 圈数
	direction 方向 1正转 非1反转
	delay delay时长ms >= 2
*/
void motor_circle(int n, int direction, int delay)
{
    int i, j;
    for(i = 0; i < n * 8; i++)
    {
		for(j = 0; j < 4; j++)
		{
			if(1 == direction)
			{
				SetMotor(0x00);
				SetMotor(forward[j]);
			}
			else
			{
				SetMotor(0x00);
				SetMotor(reverse[j]);
			}
			
			delay_ms(delay > 2 ? delay : 2);
		}
    }
}

原文地址:https://www.cnblogs.com/ikaros-521/p/15323434.html