#include "reg52.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit IN_A=P1^0;
sbit IN_B=P1^1;
sbit IN_C=P1^2;
sbit IN_D=P1^3;
sbit POWER_LED=P2^0; //电源灯
sbit fx_LED=P2^1; //方向灯
sbit key1=P3^1; //电源控制
sbit key2=P3^0; //方向控制
sbit key3=P3^2; //速度-
sbit key4=P3^3; //速度+
u8 pow=1;
u8 fx=1;
u8 vv=3;
void delay(u8 t)
{
while(t--);
}
void delay_ms(u8 t)
{
u8 i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void scan(u8 m) //按键扫描
{
static key=1;
if(m)
key=1;
if(key==1&&(key1==0||key2==0||key3==0||key4==0))
{
delay(10);
if(key==1&&(key1==0||key2==0||key3==0||key4==0))
{
key=0;
if(key1==0)
{
pow=!pow;
POWER_LED=pow;
}
else if(key2==0)
{ fx=!fx;
fx_LED=fx;}
else if(key3==0&&vv<5)
vv++;
else if(key4==0&&vv>1)
vv--;
}
}
else if(key1==1&&key2==1&&key3==1&&key4==1)
{
key=1;
}
}
void mada(u8 step,bit f) //电机驱动
{
u8 st=step;
if(f==0)
st=7-step;
switch(st)
{
case 0: IN_A=1;IN_B=0;IN_C=0;IN_D=0;break;
case 1: IN_A=1;IN_B=1;IN_C=0;IN_D=0;break;
case 2: IN_A=0;IN_B=1;IN_C=0;IN_D=0;break;
case 3: IN_A=0;IN_B=1;IN_C=1;IN_D=0;break;
case 4: IN_A=0;IN_B=0;IN_C=1;IN_D=0;break;
case 5: IN_A=0;IN_B=0;IN_C=1;IN_D=1;break;
case 6: IN_A=0;IN_B=0;IN_C=0;IN_D=1;break;
case 7: IN_A=1;IN_B=0;IN_C=0;IN_D=1;break;
default: IN_A=0;IN_B=0;IN_C=0;IN_D=0;break;
}
}
void main()
{
u8 bushu=1;
POWER_LED=pow;
while(1)
{
scan(0);
if(pow==1)
{
mada(bushu++,fx);
if(bushu==8)
bushu=0;
delay_ms(vv);
}
}
}
typedef unsigned char u8;
typedef unsigned int u16;
sbit IN_A=P1^0;
sbit IN_B=P1^1;
sbit IN_C=P1^2;
sbit IN_D=P1^3;
sbit POWER_LED=P2^0; //电源灯
sbit fx_LED=P2^1; //方向灯
sbit key1=P3^1; //电源控制
sbit key2=P3^0; //方向控制
sbit key3=P3^2; //速度-
sbit key4=P3^3; //速度+
u8 pow=1;
u8 fx=1;
u8 vv=3;
void delay(u8 t)
{
while(t--);
}
void delay_ms(u8 t)
{
u8 i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void scan(u8 m) //按键扫描
{
static key=1;
if(m)
key=1;
if(key==1&&(key1==0||key2==0||key3==0||key4==0))
{
delay(10);
if(key==1&&(key1==0||key2==0||key3==0||key4==0))
{
key=0;
if(key1==0)
{
pow=!pow;
POWER_LED=pow;
}
else if(key2==0)
{ fx=!fx;
fx_LED=fx;}
else if(key3==0&&vv<5)
vv++;
else if(key4==0&&vv>1)
vv--;
}
}
else if(key1==1&&key2==1&&key3==1&&key4==1)
{
key=1;
}
}
void mada(u8 step,bit f) //电机驱动
{
u8 st=step;
if(f==0)
st=7-step;
switch(st)
{
case 0: IN_A=1;IN_B=0;IN_C=0;IN_D=0;break;
case 1: IN_A=1;IN_B=1;IN_C=0;IN_D=0;break;
case 2: IN_A=0;IN_B=1;IN_C=0;IN_D=0;break;
case 3: IN_A=0;IN_B=1;IN_C=1;IN_D=0;break;
case 4: IN_A=0;IN_B=0;IN_C=1;IN_D=0;break;
case 5: IN_A=0;IN_B=0;IN_C=1;IN_D=1;break;
case 6: IN_A=0;IN_B=0;IN_C=0;IN_D=1;break;
case 7: IN_A=1;IN_B=0;IN_C=0;IN_D=1;break;
default: IN_A=0;IN_B=0;IN_C=0;IN_D=0;break;
}
}
void main()
{
u8 bushu=1;
POWER_LED=pow;
while(1)
{
scan(0);
if(pow==1)
{
mada(bushu++,fx);
if(bushu==8)
bushu=0;
delay_ms(vv);
}
}
}