【单片机】步进电机控制
一、操作目的
了解步进电机控制的基本原理,掌握步进电机转动编程方法。
二、操作内容
读取显示器上显示的正、反转命令,转速(16级)和转动步数后执行,转动步数减为零时停止转动。了解步进电机控制的基本原理,掌握步进电机转动编程方法。
三、预备知识
步进电机驱动原理是通过它每相线圈中的电流的顺序切换来使电机作步进式旋转,驱动电路由脉冲信号来控制,所以调节脉冲信号的频率便可改变步进电机的转速,微电脑控制步进电机最适合。
四、操作接线图
五、程序框图
六、程序清单
#0FFH
MOV 7EH,#11H
MOV 7DH,#10H
MOV 7CH,#10H
MOV 7BH,#10H
MOV 7AH,#10H
MOV 79H,#10H
MOV A,#89H
MOV DPTR,#0FF23H
MOVX @DPTR,A
mov r0,#59h
mov a,#7eh
movx @r0,a
DOJ0:
MOV SP,#53H
DOJ6:
LCALL X2 ;调键扫显示子程序
JNC DOJ5 ;功能键转
LCALL X3 ;调数字键处理子程序
MOV R1,#7EH
SJMP DOJ6
DOJ5:
CJNE A,#16H,DOJ6 ;不是执行键转
MOV A,7AH
SWAP A
ORL A,79H
MOV R6,A ;低字节步距数送R6
MOV A,7CH
SWAP A
ORL A,7BH
MOV R7,A ;高字节步距数据送R7
MOV A,7EH
CJNE A,#00H,DOJ2 ;判转动方向
DOJ1:
MOV P1,#03H ;顺时针转动
LCALL DEL0Y
LCALL GGJ0
MOV P1,#06H
LCALL DEL0Y
LCALL GGJ0
MOV P1,#0CH
LCALL DEL0Y
LCALL GGJ0
MOV P1,#09H
LCALL DEL0Y
LCALL GGJ0
SJMP DOJ1
DOJ2:
MOV P1,#09H ;逆时针转动
LCALL DEL0Y
LCALL GGJ0
MOV P1,#0CH
LCALL DEL0Y
LCALL GGJ0
MOV P1,#06H
LCALL DEL0Y
LCALL GGJ0
MOV P1,#03H
LCALL DEL0Y
LCALL GGJ0
SJMP DOJ2
DEL0Y:
MOV A,7DH ;根据(7D)内容改变延时时间
SWAP A
MOV R2,A
MOV R5,#80H
DEL1Y:
DJNZ R5,DEL1Y
LCALL SSEE
DJNZ R2,DEL1Y
RET
GGJ0:
CJNE R7,#00H,GGJ1 ;步距数为0停止
CJNE R6,#00H,GGJ1 ;不为0,减1后显示
AJMP DOJ4
GGJ1:
DJNZ R6,DOJ3
CJNE R7,#00H,DDJ8
DOJ4:
LCALL DOJ7
SJMP DOJ4
DDJ8:
DJNZ R7,DOJ3
AJMP DOJ4
DOJ3:
LCALL DOJ7
RET
DOJ7:
MOV R0,#79H
MOV A,R6
LCALL PTDS5
MOV A,R7
LCALL PTDS5
LCALL SSEE ;显示
RET
PTDS5:
MOV R1,A
ACALL PTDS6
MOV A,R1
SWAP A
PTDS6:
ANL A,#0FH
MOV @R0,A
INC R0
RET
ORG 1D00H
X3:
MOV R4,A
MOV R0,#59H
MOVX A,@R0
MOV R1,A
MOV A,R4
MOV @R1,A
CLR A
POP DPH
POP DPL
MOVC A,@A+DPTR
INC DPTR
CJNE A,01H,X30
CLR A
MOVC A,@A+DPTR
X31:
MOVX @R0,A
INC DPTR
PUSH DPL
PUSH DPH
RET
X30:
DEC R1
MOV A,R1
SJMP X31
X2:
MOV R6,#50H
X0:
ACALL XLE
JNB ACC.5,XX0
DJNZ R6,X0
MOV R6,#20H
MOV R0,#59H
MOVX A,@R0
MOV R0,A
MOV A,@R0
MOV R7,A
MOV A,#10H
MOV @R0,A
X1:
ACALL XLE
JNB ACC.5,XX1
DJNZ R6,X1
MOV A,R7
MOV @R0,A
SJMP X2
XX1:
MOV R6,A
MOV A,R7
MOV @R0,A
MOV A,R6
XX0:
RET
XLE:
ACALL DIS
ACALL KEY
MOV R4,A
MOV R1,#48H
MOVX A,@R1
MOV R2,A
INC R1
MOVX A,@R1
MOV R3,A
MOV A,R4
XRL A,R3
MOV R3,04H
MOV R4,02H
JZ X10
MOV R2,#88H
MOV R4,#88H
X10:
DEC R4
MOV A,R4
XRL A,#82H
JZ X11
MOV A,R4
XRL A,#0EH
JZ X11
MOV A,R4
ORL A,R4
JZ X12
MOV R4,#20H
DEC R2
SJMP X13
X12:
MOV R4,#0FH
x11:
MOV R2,04H
MOV R4,03H
X13:
MOV R1,#48H
MOV A,R2
MOVX @R1,A
INC R1
MOV A,R3
MOVX @R1,A
MOV A,R4
RET
LS3:
DB 07H,04H,08H,05H,09H,06H,0AH
DB 0BH,01H,00H,02H,0FH,03H,0EH
DB 0CH,0DH
DIS:
PUSH DPH
PUSH DPL
SETB RS1
MOV R0,#7EH
MOV R2,#20H
MOV R3,#00H
MOV DPTR,#LS0
LS2:
MOV A,@R0
MOVC A,@A+DPTR
MOV R1,#21H
MOVX @R1,A
MOV A,R2
CPL A
DEC R1
MOVX @R1,A
CPL A
DEC R0
LS1:
DJNZ R3,LS1
CLR C
RRC A
MOV R2,A
JNZ LS2
INC R1
MOV A,#0FFH
MOVX @R1,A
CLR RS1
POP DPL
POP DPH
RET
LS0:
DB 0C0H,0F9H,0A4H,0B0H,99H,92H
DB 82H,0F8H,80H,90H,88H,83H,0C6H
DB 0A1H,86H,8EH,0FFH,0CH,89H,7FH,0BFH
KEY:
SETB RS1
MOV R2,#0FEH
MOV R3,#08H
MOV R0,#00H
LP1:
MOV A,R2
MOV R1,#20H
MOVX @R1,A
RL A
MOV R2,A
MOV R1,#22H
MOVX A,@R1
CPL A
ANL A,#0FH
JNZ LP0
INC R0
DJNZ R3,LP1
MOV A,#20H ;19H
SJMP XP3
XP33:
MOV A,#20H
XP3:
CLR RS1
RET
LP0:
CPL A
JB ACC.0,XP0
MOV A,#00H
SJMP LPP
XP0:
JB ACC.1,XP1
MOV A,#08H
SJMP LPP
XP1:
JB ACC.2,XP2
MOV A,#10H
SJMP LPP
XP2:
JB ACC.3,XP33
MOV A,#18H
LPP:
ADD A,R0
CLR RS1
CJNE A,#10H,LX0
LX0:
JNC XP35
MOV DPTR,#LS3
MOVC A,@A+DPTR
XP35:
RET
ORG 0D50H
SSEE:
SETB RS1
MOV R5,#05H
SSE2:
MOV 30H,#20H
MOV 31H,#7EH
MOV R7,#06H
SSE1:
MOV R1,#20H
MOV A,30H
CPL A
MOVX @R1,A
MOV R0,31H
MOV A,@R0
MOV DPTR,#DDFF
MOVC A,@A+DPTR
MOV R1,#21H
MOVX @R1,A
MOV A,30H
RR A
MOV 30H,A
DEC 31H
MOV A,#0FFH
七、代码详解
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
unsigned char code mon51[3] _at_ 0x3b; //保留0X3B开始三个程序空间作全速中断用
sbit P10 = P1 ^ 0;
sbit P11 = P1 ^ 1;
sbit P12 = P1 ^ 2;
sbit P13 = P1 ^ 3;
void delay(unsigned int j)
{
while (j--)
;
}
void main(void)
{
uchar aa;
P1 = 0x00; //P1口初始化
while (1)
{
uchar k = 0;
for (k = 0; k < 200; k++)
{
P10 = 0;
P11 = 1;
P12 = 1;
P13 = 1;
delay(250);
P10 = 1;
P11 = 0;
P12 = 1;
P13 = 1;
delay(250);
P10 = 1;
P11 = 1;
P12 = 0;
P13 = 1;
delay(250);
P10 = 1;
P11 = 1;
P12 = 1;
P13 = 0;
delay(250);
}
for (k = 0; k < 200; k++)
{
P10 = 1;
P11 = 1;
P12 = 1;
P13 = 0;
delay(250);
P10 = 1;
P11 = 1;
P12 = 0;
P13 = 1;
delay(250);
P10 = 1;
P11 = 0;
P12 = 1;
P13 = 1;
delay(250);
P10 = 0;
P11 = 1;
P12 = 1;
P13 = 1;
delay(250);
}
}
}