#include<reg51.h>
#include"lcd.h"
#define floor1 0x0C
#define floor2 0x18
#define floor3 0x5E
#define floor4 0x08
#define speed 2
#define beet 25
#define up 17
#define down 24
sbit IRIN=P3^2;
sbit beet_b=P3^7;
unsigned char code CDIS1[13]={" it is the "};
unsigned char code CDIS2[13]={" floor "};
unsigned char table2[8]={0x04,0x04,0x04,0x04,0x04,0x15,0x0e,0x04};
unsigned char table1[8]={0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x04};
unsigned char IrValue[6];
unsigned char motor_l[]={0x09,0x0a,0x06,0x05};
unsigned char motor_r[]={0x06,0x0a,0x09,0x05};
unsigned char motor_stop=0x00;
unsigned char Time=0;
unsigned char motor_Time=0;
unsigned char high=0;
unsigned char floor=1,layer=1;
unsigned char a=0,b=0;
void IrInit();
void t0init();
void lcd_init_up();
void lcd_init_down();
void DelayMs(unsigned int x);
void delay(int time);
void motor(unsigned char n,unsigned char o);
void main()
{
unsigned char i;
IrInit();
t0init();
LcdInit();
lcd_init_up();
lcd_init_down();
LcdWriteCom(0x80);
for(i=0;i<13;i++)
{
LcdWriteData(CDIS1[i]);
}
LcdWriteCom(0x80+0x40);
for(i=0;i<13;i++)
{
LcdWriteData(CDIS2[i]);
}
while(1)
{
switch (IrValue[2])
{
case(floor1):
floor=1;break;
case(floor2):
floor=2;break;
case(floor3):
floor=3;break;
case(floor4):
floor=4;break;
}
if(floor>layer)
{
a=floor-layer;
b=1;
LcdWriteCom(0x8e);
LcdWriteData(0x00);
LcdWriteCom(0xce);
LcdWriteData(CDIS1[0]);
motor(a,b);
layer=floor;
}
if(floor<layer)
{
a=layer-floor;
b=0;
LcdWriteCom(0xce);
LcdWriteData(0x01);
LcdWriteCom(0x8e);
LcdWriteData(CDIS1[0]);
motor(a,b);
layer=floor;
}
LcdWriteCom(0xc0+0x03);
LcdWriteData(floor+0x30);
}
}
void DelayMs(unsigned int x)
{
unsigned char i;
while(x--)
{
for (i = 0; i<13; i++)
{}
}
}
void delay(int time)
{
int i,j;
for(j=0; j <= time; j++)
for(i =0 ; i <= 120; i++);
}
void IrInit()
{
IT0=1;
EX0=1;
EA=1;
IRIN=1;
}
void t0init()
{
TMOD = 0x01;
TH0 = 0x0B8;
TL0 = 0x00;
EA = 1;
ET0 = 1;
TR0 = 0;
}
void beet_a()
{
high=beet;
TR0=1;
while(TR0)
{
beet_b=1;
delay(5);
beet_b=0;
delay(5);
}
}
void lcd_init_up()
{
unsigned char i;
LcdWriteCom(0x40);
for(i=0;i<8;i++)
{
LcdWriteData(table1[i]);
}
}
void lcd_init_down()
{
unsigned char i;
LcdWriteCom(0x48);
for(i=0;i<8;i++)
{
LcdWriteData(table2[i]);
}
}
void motor(unsigned char n,unsigned char o)
{
if(o==0)
{
unsigned char j;
high=up;
for(j=0;j<n;j++)
{
TR0=1;
while(TR0)
{
unsigned char i;
for(i=0;i<4;i++)
{
P1=motor_l[i];
delay(speed);
//P1=motor_stop;
}
}
}
}
if(o==1)
{
unsigned char j;
high=down;
for(j=0;j<n;j++)
{
TR0=1;
while(TR0)
{
unsigned char i;
for(i=0;i<4;i++)
{
P1=motor_r[i];
delay(speed);
//P1=motor_stop;
}
}
}
}
beet_a();
}
void ReadIr() interrupt 0
{
unsigned char j,k;
unsigned int err;
Time=0;
DelayMs(70);
if(IRIN==0)
{
err=1000;
while((IRIN==0)&&(err>0))
{
DelayMs(1);
err--;
}
if(IRIN==1)
{
err=500;
while((IRIN==1)&&(err>0))
{
DelayMs(1);
err--;
}
for(k=0;k<4;k++)
{
for(j=0;j<8;j++)
{
err=60;
while((IRIN==0)&&(err>0))
{
DelayMs(1);
err--;
}
err=500;
while((IRIN==1)&&(err>0))
{
DelayMs(1);
Time++;
err--;
if(Time>30)
{
EX0=1;
return;
}
}
IrValue[k]>>=1;
if(Time>=8)
{
IrValue[k]|=0x80;
}
Time=0;
}
}
}
if(IrValue[2]!=~IrValue[3])
{
return;
}
}
}
void motor_time1() interrupt 1
{
TH0 = 0x0B8;
TL0 = 0x00;
motor_Time++;
P1=motor_stop;
if(motor_Time==high)
{
TR0=0;
motor_Time=0;
}
}
|