1. Project Introduction:
This project uses the 51 microcontroller to implement a stepper motor control system, which can realize the forward and reverse rotation, acceleration and deceleration, start and stop of the stepper motor, and display the working status of the motor through LCD. Through this system, you can control the direction and speed of stepper motor start, rotation.
2. Main functions:
3. Hardware composition:
Components list:
Component | Model | Quantity |
Microcontroller | AT89C51 | 1 |
Crystal Oscillator | 12MHZ | 1 |
Resistor | 10k | 1 |
Capacitor | 10uf | 1 |
Capacitor | 30pf | 1 |
Button | 7 | |
Driver | ULN2003 | 1 |
Stepper Motor | 4-phase 5-wire | 1 |
Display | LCD1602 | 1 |
Resistor Network | 10k | 1 |
4. Software design:
5. Project advantages:
6. Part of the code
#include "reg51.h"
#include "lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
sbit AA=P2^4;
sbit BB=P2^5;
sbit CC=P2^6;
sbit DD=P2^7;
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit k4=P1^3;
sbit k5=P1^4;
sbit k6=P1^5;
uchar flag=0,fang=0;
uchar speed=0,time=0;
uchar start=0;
uchar sec=0;
uchar disp1[]="sudu:0 ";
void main()
{
init_1602();
TMOD|=0X01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
TR0=1;
while(1)
{
if(!k1)
{
start=1;
}
if(!k2)
{
start=0;
}
if(!k3)
{
fang=0;
}
if(!k4)
{
fang=1;
}
if(!k5)
{
if(speed<9)
speed++;
while(!k5);
}
if(!k6)
{
if(speed>0)
speed--;
while(!k6);
}
}
}
void Timer0() interrupt 1
{
if(start==1)
{
if(time<10-speed)
{
time++;
}
else
{
time=0;
if(fang==0)
{
switch(flag)
{
case 0:BB=1;break;
case 2:CC=1;break;
case 3:BB=0;break;
case 4:DD=1;break;
case 5:CC=0;break;
case 6:AA=1;break;
case 7:DD=0;
}
if(flag<7)
flag++;
else
flag=0;
}
else
{
switch(flag)
{
case 0:DD=1;break;
case 1:AA=0;break;
case 2:CC=1;break;
case 3:DD=0;break;
case 4:BB=1;break;
case 5:CC=0;break;
case 6:AA=1;break;
case 7:BB=0;
}
if(flag<7) flag++;
else
flag=0;
}
}
}
if(sec<50)
sec++;
else
{
sec=0;
disp1[5]=speed+0x30;
write_string(1,0,disp1);
if(start)
{
if(fang==0)
{
write_string(2,6,"right");
}
else
{
write_string(2,6,"left ");
}
}
else
{
write_string(2,6,"stop ");
}
}
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
}