#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
//四项八拍正转励磁顺序:A->AB—>B->BC->C->CD->D->DA
uchar code FFW[]={0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x09};
//反转励磁顺序:AD->D->CD->C->BC->B->AB->A
uchar code REV[]={0x09,0x08,0x0C,0x04,0x06,0x02,0x03,0x01};
sbit K1=P3^0; //正转
sbit K2=P3^1; //反转
sbit K3=P3^2; //停止
//延时
void delay(uintx)
{
uchar i;
while(x--) 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];
delay(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];
delay(25);
}
}
}
//主程序
void main()
{
uchar N=3; //运转圈数
while(1)
{
if(K1==0)
{
P0=0xFE; //LED1点亮
SETR_MOTOR_FFW(N); //电机正传
if(K3==0) break;
}
else if(K2==0)
{
P0=0xFD;
SETP_MOTOR_REV(N); //电机反转
if(K3==0) break;
}
else
{
P0=0xFB; //LED3点亮
P1=0x03;
}
}
}
有一个错误BUJINGDIANJI.C(20): error C202: 'x': undefined identifier 怎么解决?
|