给你改了一下试试
- #include <reg51.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define DATA P0 //定义LCM数据总线P0
- sbit RS=P3^5; //寄存器选择位(0指令1数据)
- sbit RW=P3^6; //设置读写位置(0写入1读取)
- sbit E=P3^7; //使能为(0禁止1允许)
- sbit BF=P3^3;
- sbit s1=P1^0;
- sbit s2=P1^1;
- sbit s3=P1^2;
- sbit led=P1^7;
- unsigned char n,count,mu;
- unsigned char ss=23,ff=58,mm=55;
- unsigned char tab[]=" 23:58:48";
- unsigned char tab2[]="TIME ";
- uchar line1[]="LCD TEST PROGRAM By "; //第一行显示字符
- uchar line2[]="Everything is OK! XY";
- uchar line3[]="aaaaaaaaaaaaaaaabcde";
- uchar line4[]="bbbbbbbbbbbbbbbcdefg";
- void Init_LCD(void); //声明液晶初始化函数
- void write_cmd(uchar);
- void write_data(uchar);
- void check_BF(void);
- void delay_1ms(uint);
- void set_xy(uchar x,uchar y);
- void display(uchar x,uchar y,uchar *s); //声明显示函数
- /*********************1ms延时函数****************************************************************/
- void delay_1ms(uint x)
- {
- uint i,j;
- for(i=1;i<x;i++)
- for(j=1;j<110;j++);
- }
- /*********************液晶初始化函数*************************************************************/
- void Init_LCD(void)
- {
- write_cmd(0x38); //设置8位格式,2行5X7
- write_cmd(0x08); //Tune off screen
- write_cmd(0x01);
- write_cmd(0x06);
- write_cmd(0x0c);
- }
- /**********************写指令函数******************************************************************/
- void write_cmd(uchar com)
- {
- check_BF();
- E=0;
- RS=0;
- RW=0;
- DATA=com;
- delay_1ms(2);
- E=1;
- delay_1ms(2);
- E=0;
- RS=1;
- RW=1;
- }
- /**********************写数据函数*******************************************************************/
- void write_data(uchar chardata)
- {
- check_BF();
- E=0;
- RS=1;
- RW=0;
- DATA=chardata;
- delay_1ms(2);
- E=1;
- delay_1ms(2);
- E=0;
- RS=0;
- RW=1;
- }
- /**********************判忙函数******************************************************************/
- void check_BF(void)
- {
- P1=0xff;
- E=0;
- RS=0;
- RW=1;
- E=1;
- //while((P2&0x80)==0x80);
- E=0;
- RS=1;
- RW=0;
- }
- /**********************定义显示位置*******************************************************************/
- void set_xy(uchar x,uchar y)
- {
- uchar add;
- if(x==0)
- add=0x80+y;
- else
- if(x==1)
- add=0xc0+y;
- else
- if(x==2)
- add=0x94+y;
- else
- if(x==3)
- add=0xd4+y;
- write_cmd(add);
- }
- /**********************显示函数***********************************************************************/
- void displayL(uchar x,uchar y,uchar *s)
- {
- set_xy(x,y);
- while(*s)
- {
- DATA=*s;
- write_data(*s);
- s++;
- }
-
- }
- void write_sj(unsigned add,unsigned date)
- {
- unsigned char shi,ge;
- shi=date/10;
- ge=date%10;
- write_cmd(0x80+add);
- write_data(shi+0x30);
- write_data(ge+0x30);
- }
- void key()
- {
- if(s1==0)
- {
- delay_1ms(5);
- if(s1==0)
- {
- mu++;
- while(!s1);
- }
- if(mu==1)
- {
- TR0=0;
- write_cmd(0x80+9);
- write_cmd(0x0f);// ??????
- }
- if(mu==2)
- {
- write_cmd(0x80+6);
- write_cmd(0x0f);// ??????
- }
- if(mu==3)
- {
- write_cmd(0x80+3);
- write_cmd(0x0f);// ??????
- }
- }
- if(mu==4)
- {
- TR0=1;
- mu=0;
- write_cmd(0x0c);// ?????
- }
- if(mu!=0)
- {
- if(s2==0)
- {
- delay_1ms(5);
- if(s2==0)
- {
- while(!s2);
- if(mu==1)
- {
- mm++;
- if(mm==60)
- mm=0;
- write_sj(8,mm);
- write_cmd(0x80+9);
- }
- if(mu==2)
- {
- ff++;
- if(ff==60)
- ff=0;
- write_sj(5,ff);
- write_cmd(0x80+6);
- }
- if(mu==3)
- {
- ss++;
- led=!led;
- if(ss==24)
- ss=0;
- write_sj(2,ss);
- write_cmd(0x80+3);
- }
- }
- }
- if(s3==0)
- {
- delay_1ms(5);
- if(s3==0)
- {
- while(!s3);
- if(mu==1)
- {
- mm--;
- if(mm==-1)
- mm=59;
- write_sj(8,mm);
- write_cmd(0x80+9);
- }
- if(mu==2)
- {
- ff--;
- if(ff==-1)
- ff=59;
- write_sj(5,ff);
- write_cmd(0x80+6);
- }
- if(mu==3)
- {
- ss--;
- led=!led;
- if(ss==-1)
- ss=23;
- write_sj(2,ss);
- write_cmd(0x80+3);
- }
- }
- }
- }
- }
- /**********************主函数*************************************************************************/
- void main()
- {
- Init_LCD();
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TMOD=0x01;
- EA=1;
- ET0=1;
- TR0=1;
- write_cmd(0x80); // ???λ??
- for(n=0;n<10;n++)
- {
- write_data(tab[n]);
- delay_1ms(1);
- }
- write_cmd(0x80+0x40+3);// ????????
- for(n=0;n<16;n++)
- {
- write_data(tab2[n]);
- delay_1ms(1);
- }
- displayL(2,0,line3);
- displayL(3,0,line4);
- while(1)
- {
- //displayL(0,0,line1);
- //delay_1ms(10);
- //displayL(1,0,line2);
- key();
- write_sj(2,ss);
- write_sj(5,ff);
- write_sj(8,mm);
- }
- }
- void time0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- count++;
- if(count==20)
- {
- count=0;
- mm++;
- if(mm==60)
- {
- mm=0;
- ff++;
- if(ff==60)
- {
- ff=0;
- ss++;
- if(ss==24)
- {
- ss=0;
- }
- }
- }
- }
- }
复制代码 |