#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint count0=0;
uchar x,y=4;
uint motor_speed;
uint counter_motor,motor_measure,timer_limit;
uint num1,a=0,temp=0,flag=0;
uint counter_10ms,counter_1s;
uchar speed_set[3];
//uchar a;
sbit MOTOR_1B=P3^4;
sbit MOTOR_2B=P3^5;
sbit MOTOR_3B=P3^6;
sbit MOTOR_4B=P3^7;
bit zhuanxiang;//1正转0反转
bit flag_100ms,flag_10ms,flag_1s,start,enter_lock=1;
uchar code turn[]={0x10,0x20,0x40,0x80};//步进电机控制模型
uchar code LED_CS[4]={0xfe,0xfd,0xfb,0xf7};//位选1234
uchar code LED_CC_D[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴
uchar code LED_motor[2]={0x54,0x73};//n,p
uchar code table[]={//运算数字输入数组
7,8,9,0,
4,5,6,0,
1,2,3,0,
0,0,0,0};
void delayus(uchar delayus);
void display();
void init();
void delayms(uchar x);
void keyscan();//按键扫描
char code dx516[3] _at_ 0x003b;
/************************ 主函数 **************************/
void main()
{
init();
while (1)
{
if(flag_1s)
{
motor_measure=count0*10; //(600/600)*count0
count0=0;
flag_1s=0;
}
if(flag_10ms)
{
keyscan();
flag_10ms=0;
}
display();
}
}
/******************** 中断初始化函数 **************************/
void init()
{
TMOD=0x01;
TH0=0xfc; //定时1ms
TL0=0x66;
ET0=1;
EX0=1;
IT0=1;
EA=1;
TR0=1;
}
/************************ 中断函数 **************************/
//外部中断0服务函数
void init0() interrupt 0
{
count0++;
}
//定时器0中断服务函数
void timer0init() interrupt 1
{
TH0=0xfc; //定时1ms
TL0=0x66;
timer_limit=2500/motor_speed;
if(++counter_1s>=1000)
{
flag_1s=1;
counter_1s=0;
}
if(++counter_motor>=timer_limit)
{
counter_motor=0;
if(start)
{
if(zhuanxiang)
{
P3=turn[x];
x++;
if(x==4) x=0;
}
if(!zhuanxiang)
{
y--;
P3=turn[y];
if(y==0) y=4;
}
}
}
if(++counter_10ms>=10)
{
flag_10ms=1;
counter_10ms=0;
}
}
/************************ 显示函数 **************************/
void display()
{
uint bai,shi,ge;
bai=motor_speed/100;
shi=motor_speed%100/10;
ge=motor_speed%10;
P2=LED_CS[0];P0=LED_motor[zhuanxiang];delayms(5);
if(start==1)
{
bai=motor_measure/100;
shi=motor_measure%100/10;
ge=motor_measure%10;
P2=LED_CS[1];P0=LED_CC_D[bai];delayms(5);
P2=LED_CS[2];P0=LED_CC_D[shi];delayms(5);
P2=LED_CS[3];P0=LED_CC_D[ge];delayms(5);
}
else
{
P2=LED_CS[1];P0=LED_CC_D[bai];delayms(5);
P2=LED_CS[2];P0=LED_CC_D[shi];delayms(5);
P2=LED_CS[3];P0=LED_CC_D[ge];delayms(5);
}
}
/************************ 延时函数 **************************/
void delayms(uchar x)
{
uchar i ;
while(x--)
for(i=0;i<120;i++) ;
}
/****************矩阵键盘扫描函数******************/
void keyscan(void)
{
P1=0xfe;
if(P1!=0xfe)
{
delayms(5);// 延迟5ms
if(P1!=0xfe)
{
temp=P1&0xf0;
switch(temp)
{
case 0xe0:num1=0; break; //7
case 0xd0:num1=1; break; //8
case 0xb0:num1=2; break; //9
case 0x70:num1=3; break; //÷
}
} while(P1!=0xfe);
if(num1==0||num1==1||num1==2)//如果按下的是'7','8'或'9
{
if(flag==1){flag=0;a=0;}
{ a=a*10+table[num1]; } //按下数字存储到a
}
else//如果按下的是'/' 除法
{zhuanxiang=1;}//正转
}
P1=0xfd;
if(P1!=0xfd)
{
delayms(5);
if(P1!=0xfd){ temp=P1&0xf0;
switch(temp)
{
case 0xe0:num1=4; break; //4
case 0xd0:num1=5; break; //5
case 0xb0:num1=6; break; //6
case 0x70:num1=7; break; //×
}
} while(P1!=0xfd);//等待按键释放
if(num1==4||num1==5||num1==6&&num1!=7)//如果按下的是'4','5'或'6'
{ if(flag==1){flag=0;a=0;}
a=a*10+table[num1]; }
else//如果按下的是'×'
{ zhuanxiang=0;//反转
}
}
P1=0xfb;
if(P1!=0xfb){ delayms(5);
if(P1!=0xfb) { temp=P1&0xf0;
switch(temp)
{
case 0xe0:num1=8; break; //1
case 0xd0:num1=9; break; //2
case 0xb0:num1=10; break; //3
case 0x70:num1=11; break; //-
}
} while(P1!=0xfb);
if(num1==8||num1==9||num1==10)//如果按下的是'1','2'或'3'
{
if(flag==1){flag=0;a=0;}
a=a*10+table[num1];
}
else if(num1==11)//如果按下的是'-'
{}
}
P1=0xf7;
if(P1!=0xf7){ delayms(5);
if(P1!=0xf7){ temp=P1&0xf0;
switch(temp)
{
case 0xe0:num1=12; break; //清0键
case 0xd0:num1=13; break; //数字0
case 0xb0:num1=14; break; //等于键
case 0x70:num1=15; break; //加
}
} while(P1!=0xf7);
switch(num1)
{
case 12:{a=0;}break;//按下的是"清零"
case 13:{a=a*10+table[num1];}break;
case 14:{motor_speed=a;enter_lock=0;start=1;}break;
case 15:{motor_speed=0;start=0;}break; //加键
}
}
motor_speed=a;
}
/***************************************************/
|