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);
		}
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM