假设步进电机在端口 1.0 连接到端口 1.3。调整延迟将增加或减少电机的速度。
#include <REG2051.H>
#define stepper P1
void delay()
{
unsigned char i,j,k;
for(i=0;i<6;i++) {
for(j=0;j<255;j++)
for(k=0;k<255;k++);
}
}
void main()
{
while (1) {
stepper = 0x0C;
delay();
stepper = 0x06;
delay();
stepper = 0x03;
delay();
stepper = 0x09;
delay();
}
}
org 0H
stepper equ P1
main:
mov stepper, #0CH
acall delay
mov stepper, #06H
acall delay
mov stepper, #03H
acall delay
mov stepper, #09H
acall delay
sjmp main
delay:
mov r7,#4
wait2:
mov r6,#0FFH
wait1:
mov r5,#0FFH
wait:
djnz r5,wait
djnz r6,wait1
djnz r7,wait2
ret
end
|