求 用AT89S52单片机控制的 两相四线步进电机 的程序,要C语言的

2024-12-02 02:22:12
推荐回答(1个)
回答1:

#include
sfr StepMotor=P0;
sbit keyup=P1^0;
sbit keydn=P1^1;
unsigned char Steprun[8]={0x01,0x05,0x04,0x06,0x02,0x0a,0x08,0x09};
//控制方式 A AB B B/A /A /A/B /B /BA
unsigned char num;
void t0_isr() interrupt 1
{
TH0=(65536-speed)/256;
TL0=(65536-speed)%256;
StepMotor=Steprun[num];
num++;
num%=8;
}
void delay(unsigned char x)
{
unsigned char i,j;
for(i=0;i for(j=0;j<125;j++);
}
main()
{
num=0;
speed=55536;
TH0=(65536-speed)/256;
TL0=(65536-speed)%256;
while(1)
{
if(keyup==0)
{
delay(10);
if(keyup==0)speed-=1000;
}
if(keydn==0)
{
delay(10);
if(keydn==0)speed+=1000;
}
}
}