#include"reg52.h"
#include"lcd1602.h"
#define uint unsigned int
#define uchar unsigned char
sbit IN0=P1^0;
sbit IN1=P1^1;
//sbit key1=P1^1;
sbit key0=P3^1;
sbit ST=P3^0;
sbit inctime=P3^2;
sbit dectime=P3^3;
uint max_time=300;//定时最大值,可以修改
uchar s[]={"0123456789"};
uchar str[]={"MODE:"};
uchar str1[]={"TIME:"};
int dtime=0;//设定的时间
uint count=0,num=0;
char flag=0,flag0=0,flag1=0,flag2=0,flag3=0;
char mode=0;
void motor_forward()
{
IN0=1;
IN1=0;
}
void motor_flip()
{
IN0=0;
IN1=1;
}
void inittimer()
{
TMOD=0x10;
TH1=0x3c;//设置定时器1为50ms延时
TL1=0xb0;
ET1=1;
EA=1;
}
void machine_mode()
{
if(mode==1)
{
motor_forward();
}
if(mode==2)
{
motor_flip();
}
}
void datdeal()
{
if(key0==0&&flag==0)//模式加
{
flag=1;
}
if(flag==1&&key0==1)
{
mode++;
flag=0;
}
if(ST==0&&flag1==0)//开始运行
{
flag1=1;
}
if(flag1==1&&ST==1)
{
machine_mode();
if(dtime>0)
{
TR1=1;
}
else TR1=0;
flag1=0;
}
if(inctime==0&&flag2==0)//增加设定时间
{
flag2=1;
}
if(flag2==1&&inctime==1)
{
dtime++;
flag2=0;
}
if(dectime==0&&flag3==0)//减少设定时间
{
flag3=1;
}
if(flag3==1&&dectime==1)
{
dtime--;
flag3=0;
}
if(mode>1)
{
mode=0;
}
else if(mode<0)
{
mode=0;
}
if(dtime<0)//当时间为0时,电机停止运行
{
dtime=0;
TR1=0;
IN0=0;
}
if(dtime>max_time)
{
dtime=max_time;
}
}
void display()
{
uchar temp0=0;
uchar ttemp0=0,ttemp1=0,ttemp2=0;
uchar i=0,j=0;
temp0=mode%10;
ttemp0=dtime/100;
ttemp1=dtime%100/10;
ttemp2=dtime%10;
writecom(0x80);//第一行显示mode
while(str[ i]!='\0')
{
writedat(str[ i]);
i++;
}
writedat(':');
writedat(s[temp0]);
writecom(0x80+0x40);//第二行显示time
while(str1[j]!='\0')
{
writedat(str1[j]);
j++;
}
writedat(s[ttemp0]);
writedat(s[ttemp1]);
writedat(s[ttemp2]);
writedat('s');
}
void initex()//初始化外部中断0
{
IE0=1;
EX0=1;
}
void main()
{
initex();//初始化外部中断
inittimer();//初始化定时器
initlcd();//初始化lcd
IN0=0;
while(1)
{
datdeal();
display();
}
}
void ex_isr() interrupt 0//停止运行电机
{
TR1=0;
IN0=0;
IN1=0;
}
void timer1_isr() interrupt 3//每一秒钟设定时间减1
{
TH1=0x3c;
TL1=0xb0;
num++;
if(num==20)
{
dtime--;
num=0;
}
} |