#include
#include
#define uint unsigned int
sbit p27=P2^7;
sbit p26=P2^6;
sbit p25=P2^5;
sbit d0=P0^0;
sbit d1=P0^1;
sbit d2=P0^2;
sbit d3=P0^3;
sbit d4=P0^4;
sbit d5=P0^5;
sbit d6=P0^6;
sbit d7=P0^7;
#define LED7 XBYTE[0x9000]
#define LED6 XBYTE[0x9001]
#define LED5 XBYTE[0x9f02]
#define LED4 XBYTE[0x9f03]
#define LED3 XBYTE[0x9f04]
#define LED2 XBYTE[0x9f05]
#define LED1 XBYTE[0x9f06]
#define LED0 XBYTE[0x9f07]
uint tt[]= //数码显示表(89c52)
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x40
};
uint z=50;
// uint m=5;
uint flag=0;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void motor_z() //正传
{
P1=0x01;
delay(z);
P1=0x02;
delay(z);
P1=0x04;
delay(z);
P1=0x08;
delay(z);
}
void motor_f()//反转
{
P1=0x08;
delay(z);
P1=0x04;
delay(z);
P1=0x02;
delay(z);
P1=0x01;
delay(z);
}
void motor_stop()//停止
{
P1=0x00;
}
void init()
{
LED0=0;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
LED5=0;
LED6=0;
LED7=0; //关掉数码管
flag=2; //使步进电机一开始就处于停止状态
}
void keyscan()
{
p27=1;
p26=0;
p25=1;
/////////////////////////////key1正转
if(d1==0)
{
delay(5);
if(d1==0)
flag=0;
}
/////////////////////////////key2 反转
if(d2==0)
{
delay(5);
if(d2==0)
flag=1;
}
/////////////////////////////key3停止
if(d3==0)
{
delay(5);
if(d3==0)
flag=2;
}
/////////////////////////////key4减速
if(d4==0)
{
//delay(100);
while(~d4);//修改处
delay(5);
if(d4==1)
z+=5;
if(z<=0)
z=50;
}
/////////////////////////////key5 加速
if(d5==0)
{
//delay(100);
while(~d5);
delay(5);
if(d5==1)
z-=5;
if(z<=0)
z=50;
}
/////////////////////////////key6直接加速至10档
if(d6==0)
{
delay(5);
if(d6==0)
z=10;
}
}
void display(uint z)
{
LED7=tt[z/100];
LED6=tt[z%100/10];
LED5=tt[z%10];
}
void main()
{
init();//初始化
while(1)
{
switch(flag)
{
case 0:
motor_z();
break;
case 1:
motor_f();
break;
default:
motor_stop();
break;
}
keyscan(); //键盘扫描
display(z);
}
}
阅读(1365) | 评论(0) | 转发(0) |