想通过上位机控制p1口的两个步进电机,但是电机就是转不了
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
//------8ÅÄ-----
uchar code zz[]={0x55,0x11,0x99,0x88,0xaa,0x22,0x66,0x44}; //Õýת
uchar code fz[]={0x44,0x66,0x22,0xa,0x88,0x99,0x11,0x55}; //·´×ª
uchar state=0;
uint Time=60;
unsigned char receiveData='0';
void UsartConfiguration();
void delay(uint ms)
{
uint t;
while(ms--)
for(t=0;t<120;t++);
}
void motor_zz()
{
uint j;
for(j=0;j<8;j++)
{
P1=zz[j];
delay(Time);
}
}
void motor_fz()
{
uint j;
for(j=0;j<8;j++)
{
P1=fz[j];
delay(Time);
}
}
void main()
{
UsartConfiguration();
while(1)
{
while(state==0)
{
if(receiveData=='z')
{
state=1;
receiveData='0';
break;
}
if(receiveData=='f')
{
state=2;
receiveData='0';
break;
}
P1=0x11;
}
while(state==1)
{
if(receiveData=='t')
{
state=0;
receiveData='0';
break;
}
if(receiveData=='z')
{
state=1;
receiveData='0';
break;
}
if(receiveData=='f')
{
state=2;
receiveData='0';
break;
}
if(receiveData=='+')
{
Time=Time-5;
if(Time<20)
Time=20;
}
if(receiveData=='-')
{
Time=Time+5;
if(Time>100)
Time=100;
}
motor_zz();
}
while(state==2)
{
if(receiveData=='t')
{
state=0;
receiveData='0';
break;
}
if(receiveData=='z')
{
state=1;
receiveData='0';
break;
}
if(receiveData=='f')
{
state=2;
receiveData='0';
break;
}
if(receiveData=='+')
{
Time=Time-5;
if(Time<20)
Time=20;
}
if(receiveData=='-')
{
Time=Time+5;
if(Time>100)
Time=100;
}
motor_fz();
}
}
}
void UsartConfiguration()
{
SCON=0X50;
TMOD=0X21;
PCON=0X00;
TH1=0XFD;
TL1=0XFD;
ES=1;
EA=1;
TR1=1;
}
void Usart() interrupt 4
{
receiveData=SBUF;
RI = 0;
}
|