#include<reg52.h>
#include<lcd1602.h>
#include<keyscan.h>
#include<motor.h>
#include<rs232.h>
#define uchar unsigned char
#define uint unsigned int
uchar num,dd,ee,keyvalue,fla,oldkey,ci,qq,sets;
uint tt,a,b,di;
int di0,di1;
uchar s_message[5],r_message[3];
uchar code table[] =" Control System ";
uchar code table1[] ="Welcom";
uchar code table2[] =" ";
uchar code table3[] ="NO.1";
uchar code table4[] ="Set_S:";
uchar code table5[] ="Speed:";
uchar code table6[] ="error";
uchar code table7[] ="OK!";
uchar code table8[] =" Speed setting ";
void init_timer()
{
TMOD |= 0x15; //使用模式1,16位定时器,T0:计数 T1:定时
TH0=0xff; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL0=0xff;
TH1=0xfc; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL1=0x66;
IT0=1;
EA=1; //总中断打开
ET0=1; //定时器0中断打开
TR0=0; //定时器0开关
ET1=1; //定时器1中断打开
TR1=0; //定时器1开关
ES =0;
}
void main()
{
init_timer();
init_lcd();
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table[num]);
}
write_com(0x80+0x44);
for(num=0;num<6;num++)
{
write_data(table1[num]);
}
delay(200);
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table8[num]);
}
write_com(0x80+0x41);
for(num=0;num<6;num++)
{
write_data(table5[num]);
}
//del0: write_com(0x80+0x47);
// write_com(0x0f);
// keyvalue=20;
// oldkey=keyvalue;
// while(oldkey==keyvalue)
// keys();
// if(keyvalue==14)
// goto del0;
// r_message[0]=keyvalue;
// write_data(0x30+keyvalue);
// delay(1000);
// write_com(0x80+0x48);
// write_com(0x0f);
// keyvalue=30;
// oldkey=keyvalue;
// while(oldkey==keyvalue)
// keys();
// if(keyvalue==14)
// goto del0;
// r_message[1]=keyvalue;
// write_data(0x30+keyvalue);
// delay(1000);
// write_com(0x80+0x49);
// write_com(0x0f);
// keyvalue=30;
// oldkey=keyvalue;
// while(oldkey==keyvalue)
// keys();
// if(keyvalue==14)
// goto del0;
// r_message[2]=keyvalue;
// write_data(0x30+keyvalue);
// write_com(0x0c);
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x81);
for(num=0;num<4;num++)
{
write_data(table3[num]);
}
write_com(0x87);
for(num=0;num<6;num++)
{
write_data(table4[num]);
}
write_com(0x80+0x41);
for(num=0;num<6;num++)
{
write_data(table5[num]);
}
TR0=1;
while(1)
{
s_message[0]=di/100;
s_message[1]=di%100/10;
s_message[2]=di%10;
// s_message[0]=1;
// s_message[1]=1;
// s_message[2]=2;
if(s_message[1]>9)
s_message[1]=0;
write_com(0x80+0x47);
write_data(s_message[0]+0x30);
write_data(s_message[1]+0x30);
write_data(s_message[2]+0x30);
write_com(0x8d);
write_data(r_message[0]+0x30);
write_data(r_message[1]+0x30);
write_data(r_message[2]+0x30);
keys();
if(keyvalue==15)
{
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table8[num]);
}
write_com(0x80+0x41);
for(num=0;num<6;num++)
{
write_data(table5[num]);
}
del1: write_com(0x80+0x47);
write_com(0x0f);
keyvalue=30;
oldkey=keyvalue;
while(oldkey==keyvalue)
keys();
if(keyvalue==14)
goto del1;
r_message[0]=keyvalue;
write_data(0x30+keyvalue);
delay(1000);
write_com(0x80+0x48);
write_com(0x0f);
keyvalue=30;
oldkey=keyvalue;
while(oldkey==keyvalue)
keys();
if(keyvalue==14)
goto del1;
r_message[1]=keyvalue;
write_data(0x30+keyvalue);
delay(1000);
write_com(0x80+0x49);
write_com(0x0f);
keyvalue=30;
oldkey=keyvalue;
while(oldkey==keyvalue)
keys();
if(keyvalue==14)
goto del1;
r_message[2]=keyvalue;
write_data(0x30+keyvalue);
write_com(0x0c);
sets=r_message[0]*100+r_message[1]*10+r_message[2];
write_com(0x80);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table2[num]);
}
write_com(0x81);
for(num=0;num<4;num++)
{
write_data(table3[num]);
}
write_com(0x87);
for(num=0;num<6;num++)
{
write_data(table4[num]);
}
write_com(0x80+0x41);
for(num=0;num<6;num++)
{
write_data(table5[num]);
}
}
ci++;
if(ci==10)
{
if(di>=sets)
{
down();
}
if(di<sets)
{
up();
}
}
if(ci==100)
{
ci=0;
com_init();
while(qq<=2)
{
SBUF =s_message[qq]+0x30;
if(TI)
{
qq++;
}
TI = 0;
delay(10);
}
init_timer();
if(qq==2)
qq=0;
}
}
}
void Timer0_isr(void) interrupt 1 using 1
{
TH0=0xff;
TL0=0xff;
a++;
if(a==1)
TR1=1;
// aa=a/100;
// bb=a%100/10;
// cc=a%10;
// write_com(0x84);
// write_data(aa+0x30); //测试时用,作用判断计数准确度
// write_data(bb+0x30);
// write_data(cc+0x30);
}
void Timer1_isr(void) interrupt 3 using 1
{
TH1=0xfc;
TL1=0x66;
b++;
if(a>=2)
{
TR1=0;
a=0;
tt=b;
b=0;
dd++;
if(dd%2==0)
{
dd=0;
di0=60000/tt;
}
if(dd%2==1)
{
di1=60000/tt;
}
if((di0>di1)&&((di0-di1)>=10))
{
di=di1;
}
else
di=(di0+di1)/2;
if((di1>di0)&&((di1-di0)>=10))
{
di=di0;
}
else
di=(di0+di1)/2;
}
}