1.关于stm32f4上对(28byj-48)步进电机的应用控制和解读。
(1).ULN2003驱动模块电路
步进电机不能直接和单片机连接,需要对应驱动模块提供电机所需的电流。
电路图分析:
IN1~7为单片机的输出io连接端。
1C~7C为相数对应(A ~ G).
根据电路图将接地和供电分别接入。
从左往右(分别接入GND,VCC,空,VCC);
vcc为5v电压。
驱动模块的接线图。
(2).28byj48.h头文件
#ifndef _28byj48_H
#define _28byj48_H
#include "system.h"
#include "SysTick.h"
void Motor_Init(void);
void Motorcw(void);
void motorNCircle(int n,int position,int x);
void SetMotor(unsigned char InputData);
#endif
(3).28byj48.c文件内容
#include "28byj48.h"
void Motor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStructure.Pin =GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP ;
HAL_GPIO_Init(GPIOC,&GPIO_InitStructure);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
}
void Motorcw(void)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
delay_ms(10);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
delay_ms(10);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
delay_ms(10);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
delay_ms(10);
}
unsigned char F_Rotation[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09} ;
unsigned char B_Rotation[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08} ;
void SetMotor(unsigned char InputData)
{
if(InputData & 0x08)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_SET);
}
if(InputData & 0x04)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
}
if(InputData & 0x02)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
}
if(InputData & 0x01)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
}
}
void motorNCircle(int n,int position,int x)
{
int i=0;
int j=0;
int k=0;
for(j=0;j<n;j++)
{
for(i=0;i<64*8;i++)
{
for(k=0;k<8;k++)
{
if(1 == position)
{
SetMotor(F_Rotation[k]);
}
else
{
SetMotor(B_Rotation[k]);
}
delay_ms(x);
}
}
}
}
(4).main函数
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "pwm.h"
#include "28byj48.h"
int main()
{
HAL_Init();
SystemClock_Init(8,336,2,7);
SysTick_Init(168);
Motor_Init();
while(1
{
motorNCircle(10,1,90);
}
}
(5).运行效果:
源码:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)