/**********1602**********/
#include<reg52.h>
#include<intrins.h>
sbit RS = P2^2;
sbit RW = P2^1;
sbit E = P2^0;
sbit k1 = P1^0;
sbit k2 = P1^1;
sbit k3 = P1^2;
sbit k4 = P1^3;
sbit p1 = P3^5;
sbit p2 = P3^6;
sbit p3 = P3^7;
char mode=1;
char bai,shi,ge;
char t1=50; char t2=50; char t3=50;
char t_t1,t_t2,t_t3,t_t;
char tt1,tt2;
#define Data P0 //数据端口
/******************************************************************/
/* 函数声明 */
/******************************************************************/
void Delay_ms(unsigned int ms)//delay_ms
{
unsigned char i;
while(--ms)
for(i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void Write_Command(unsigned char c)
{
Delay_ms(5);//short delay before operation
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
/****************************************************************/
void Write_Data(unsigned char c)
{
Delay_ms(5); //short delay before operation
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
/**************************指定位置显示字节*******************************/
void Show_Char(unsigned char pos,unsigned char c)
{
unsigned char p;
if (pos>0x10)
p=pos+0xaf; //是第二行则命令代码高4位为0xc
else
p=pos+0x7f; //是第一行则命令代码高4位为0x8
Write_Command (p);//write command
Write_Data (c); //write data
}
/*************************************************************************/
void Show_String (unsigned char pos,char *ptr)
{
while(*ptr!='\0')
{
Show_Char (pos++,*ptr++); //循环显示16个字符
}
}
/*********************************************************************/
void Init_Lcd1602()
{
Delay_ms(15);
Write_Command(0x38); //display mode
Write_Command(0x38); //display mode
Write_Command(0x38); //display mode
Write_Command(0x06); //显示光标移动位置
Write_Command(0x0c); //显示开及光标设置
Write_Command(0x01); //显示清屏
Delay_ms(15);
}
/*******************整屏右移********************
void Left_lcd1602()
{
unsigned char i;
for(i=0;i<16;i++)//整屏右移
{
Write_Command(0x18);
Delay_ms(300);
}
}
/*******************整屏左移**********************/
void Right_lcd1602()
{
unsigned char i;
for(i=0;i<24;i++) // 整屏左移
{
Write_Command(0x1c);
(300);
}
}
//******************************************************* /
void show_mode(char a)
{
if(a==1)
{
Show_String(1,"mode1 *");
Show_String(17,"mode2");
}
else if(a==2)
{
Show_String(1,"mode2 *");
Show_String(17,"mode3");
}
else if(a==3)
{
Show_String(1,"mode3 *");
Show_String(17,"mode1");
}
}
void show_m1()
{
Show_String(1,"angle:");
Show_String(17,"distance:");
}
int change(unsigned int a) //把票数转为字符型
{
switch(a)
{
case 0: a=0x30;break;
case 1: a=0x31;break;
case 2: a=0x32;break;
case 3: a=0x33;break;
case 4: a=0x34;break;
case 5: a=0x35;break;
case 6: a=0x36;break;
case 7: a=0x37;break;
case 8: a=0x38;break;
case 9: a=0x39;break;
}
return a;
}
void xianshi(char a,unsigned int d)
{
bai=d/100;
shi=(d-100*bai)/10;
ge=d%10;
bai=change(bai);
shi=change(shi);
ge=change(ge);
Show_Char(a,bai);
Delay_ms(5);
Show_Char(a+1,shi);
Delay_ms(5);
Show_Char(a+2,ge);
Delay_ms(5);
}
void bijiao()
{
t_t1=t1;t_t2=t2;t_t3=t3;
if(t_t1>t_t2)
{
t_t=t_t1;
t_t1=t_t2;
t_t2=t_t;
}
if(t_t2>t_t3)
{
t_t=t_t2;
t_t2=t_t3;
t_t3=t_t;
}
if(t_t1>t_t2)
{
t_t=t_t1;
t_t1=t_t2;
t_t2=t_t;
}
if(t_t2>t_t3)
{
t_t=t_t2;
t_t2=t_t3;
t_t3=t_t;
}
tt1=t_t2-t_t1;
tt2=t_t3-t_t2;
}
void main(void)
{
int ang=0; //0-360
int distance=30; //30-60
int zhijing=15; //15-25
Init_Lcd1602();
Delay_ms(15);
show_mode(mode);
bijiao();
while(1)
{
p1=1;
p2=1;
p3=1;
Delay_ms(t_t1);
if(t_t1==t1)
p1=0;
if(t_t1==t2)
p2=0;
if(t_t1==t3)
p3=0;
// p1=0;
if(tt1!=0)
Delay_ms(tt1);
if(t_t2==t1)
p1=0;
if(t_t2==t2)
p2=0;
if(t_t2==t3)
p3=0;
// p2=0;
if(tt2!=0)
Delay_ms(tt2);
if(t_t3==t1)
p1=0;
if(t_t3==t2)
p2=0;
if(t_t3==t3)
p3=0;
// p3=0;
Delay_ms(100-t_t3);
if(t1>98) ////可调
t1=1; ////
if(t2>98) ////可调
t2=1; ////
if(t3>98) ////可调
t3=1; ////
if(k1==0)
{
Delay_ms(100);
t1++;
bijiao();
}
if(k2==0)
{
Delay_ms(100);
t2++;
bijiao();
}
if(k3==0)
{
Delay_ms(100);
t3++;
bijiao();
}
/*
if(k1==0)
{
mode++;
show_mode(mode);
Delay_ms(700);
}
if(mode>=4)
{
mode=1;
show_mode(mode);
}
if(mode==1&&k2==0)
{
Write_Command(0x01); //清屏
show_m1();
while(1)
{
xianshi(7,ang);
if(k1==0)
{
Delay_ms(500);
ang+=10;
}
if(ang>=361)
ang=0;
xianshi(26,distance);
if(k3==0)
{
Delay_ms(500);
distance++;
}
if(distance>=61)
distance=30;
if(k4==0)
{
Write_Command(0x01);
show_mode(mode);
break;
}
}
}
if(mode==2&&k2==0)
{
Write_Command(0x01); //清屏
while(1)
{
Show_String(1,"Auto working.");
Delay_ms(1200);
if(k4==0)
{
Write_Command(0x01);
show_mode(mode);
break;
}
Show_String(1,"Auto working....");
Delay_ms(1200);
Write_Command(0x01);
if(k4==0)
{
Write_Command(0x01);
show_mode(mode);
break;
}
}
}
if(mode==3&&k2==0)
{
Write_Command(0x01); //清屏
Show_String(1,"diameter:");
while(1)
{
xianshi(10,zhijing);
if(k1==0)
{
zhijing++;
Delay_ms(500);
}
if(zhijing>=36)
zhijing=15;
if(k4==0)
{
Write_Command(0x01); //清屏
show_mode(mode);
break;
}
}
} */
}
}