51单片机 步进电机正反转控制+Proteus仿真

51单片机 步进电机正反转控制+Proteus仿真


  • Proteus仿真
    在这里插入图片描述

实例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code FFW[]=
{
0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09
};

uchar code REV[]=
{
0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01
};

sbit K1 = P3^0;
sbit K2 = P3^1;
sbit K3 = P3^2;

void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}

void SETP\_MOTOR\_FFW(uchar n)
{
uchar i,j;
for(i=0;i<5\*n;i++)
{
for(j=0;j<8;j++)
{
if(K3 == 0) break;
P1 = FFW[j];
DelayMS(25);
}
}
}

void SETP\_MOTOR\_REV(uchar n)
{
uchar i,j;
for(i=0;i<5\*n;i++)
{
for(j=0;j<8;j++)
{
if(K3 == 0) break;
P1 = REV[j];
DelayMS(25);
}
}
}

void main()
{
uchar N = 3;
while(1)
{
if(K1 == 0)
{
P0 = 0xfe;
SETP\_MOTOR\_FFW(N);
if(K3 == 0) break;
}
else if(K2 == 0)
{
P0 = 0xfd;
SETP\_MOTOR\_REV(N);
if(K3 == 0) break;
}
else
{
P0 = 0xfb;
P1 = 0x03;
}
}
}

仿真资源和程序源码

1
2
3
链接:https://pan.baidu.com/s/1Oygjc_OKvtdvTSLUdNlfpg 
提取码:2fht