终于搞定了 艰难,困苦啊。。。 虽然苦 虽然 纠结 虽然带了许淡淡的蛋疼。但确实学到了重要的知识。更清楚的了解了串口。
代码我贴出来 希望大家参考
#include <reg52.h> #include<intrins.h> #define uint unsigned int #define uchar unsigned char char shi, fen, miao,yue,ri,Sendate,o,p,b4num; int menu,menu_aj,menu_yp1,menu_ads,menu_yp2,menu_guang1,menu_guang2,menu_tiao,menu_tiao2,menu_kai; void wangxin_write(char *mes,int len) { int i; for(i=0;i<len;i++) { SBUF=*mes++; while(!TI); TI=0; } } uchar num,a,flag,s4num,num1,s5num,s6num,af,bf; unsigned char t,j,m,n; uchar aa; uint count,s1num,nian,i; uchar code table2[5]={0}; uchar code table3[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; // uchar *string="menu"; sbit rs= P2^6; sbit rw = P2^5; sbit en = P2^7; sbit s1=P1^0; sbit s2=P1^1; sbit s3=P1^2; sbit s4=P1^3; sbit s5=P1^4; sbit s6=P1^5; sbit s8=P1^7; void meau(); uchar code table[]=" 2012-11-30"; uchar code table1[]=" 23:59:40"; void delay(uint z) //延时函数 { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void write_com(uchar aa) //写命令 { rs=0; rw=0; P0=aa; delay(5); en=1; delay(5); en=0; } ////////////////////////////////////////////// void write_data(uchar bb) //写数据 { rs=1; rw=0; P0=bb; delay(5); en=1; delay(5); en=0; } void write_sfm(uchar add,uchar bb) //写时间 { uchar shi,ge; shi=bb/10; ge=bb%10; write_com(0x80+0x50+add); write_data(0x30+shi); write_data(0x30+ge); } void write_yr(uchar add1,uchar bb) //写日期 { uchar s,g; s=bb/10; g=bb%10; write_com(0x80+0x10+add1); write_data(0x30+s); write_data(0x30+g); } void write_n(uchar add2,uchar bb) //写年 { uchar qi,ba,sh,gg; qi=bb/1000; ba=bb%1000/100; sh=bb%100/10; gg=bb%10; write_com(0x80+0x10+add2); write_data(0x30+qi); write_data(0x30+ba); write_data(0x30+sh); write_data(0x30+gg); } ////////////////////////////////////////////////////////// void init() //初始化 { miao=40; fen=59; shi=23; nian=2012; yue=11; ri=30; en=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80+0x10); for(num=0;num<12;num++) { write_data(table[num]); delay(5); } write_com(0x80+0x50); for(num=0;num<10;num++) { write_data(table1[num]); delay(5); } for(num=0;num<16;num++) { write_com(0x18); } delay(200); TMOD=0x20; TH0=(256-200)/256; TL0=(256-200)%256; EA=1; ET0=1; TR0=1; } ///////////////////////////////////////////////////// void zuoyi() { for(o=0;o<1;o++) { write_com(0x18); } } void youyi() { for(p=0;p<1;p++) { write_com(0x1c); } } void guangbiaozuo() { for(o=0;o<1;o++) { write_com(0x0e); write_com(0x10); } } void tiaoshijian1() { miao++; if(miao==60) { miao=0; fen++; if(fen==60) { fen=0; shi++; if(shi==24) { shi=0; ri++; if(ri==31) { ri=1; yue++; if(yue==13) { yue=1; nian++; if(nian==9999) { nian=0; } write_n(2,nian); } write_yr(7,yue); } write_yr(10,ri); } write_sfm(2,shi); } write_sfm(5,fen); } write_sfm(8,miao); } void tiaoshijian2() { miao--; if(miao==-1) { miao=60; fen--; if(fen==-1) { fen=60; shi--; if(shi==-1) { shi=24; ri--; if(ri==-1) { ri=31; yue--; if(yue==-1) { yue=12; nian--; if(nian==-1) { nian=9999; } write_n(2,nian); } write_yr(7,yue); } write_yr(10,ri); } write_sfm(2,shi); } write_sfm(5,fen); } write_sfm(8,miao); } void guangbiaoyou() { for(p=0;p<1;p++) { write_com(0x0e); write_com(0x14); } } /////////////////////////////////////// void keyscan() //调节日期时间 { if(s1==0) { delay(5); if(s1==0) { s1num++; while(!s1); if(s1num==1) { TR0=0; write_com(0x80+0x50+8); write_com(0x0f); } } if(s1num==2) { write_com(0x80+0x50+5); } if(s1num==3) { write_com(0x80+0x50+2); } if(s1num==4) { write_com(0x80+0x10+10); } if(s1num==5) { write_com(0x80+0x10+7); } if(s1num==6) { write_com(0x80+0x10+2); } if(s1num==7) { s1num=0; write_com(0x0c); TR0=1; } } if(s1num!=0) { if(s2==0) { delay(5); if(s2==0) { while(!s2); if(s1num==1) { miao++; if(miao==60) miao=0; write_sfm(8,miao); write_com(0x80+0x50+8); } if(s1num==2) { fen++; if(fen==60) fen=0; write_sfm(5,fen); write_com(0x80+0x50+5); } if(s1num==3) { shi++; if(shi==24) shi=0; write_sfm(2,shi); write_com(0x80+0x50+2); } if(s1num==4) { ri++; if(ri==31) ri=1; write_yr(10,ri); write_com(0x80+0x10+10); } if(s1num==5) { yue++; if(yue==13) yue=1; write_yr(7,yue); write_com(0x80+0x10+7); } if(s1num==6) { nian++; if(nian==9999) nian=1; write_n(2,ri); write_com(0x80+0x10+2); } } } if(s3==0) { delay(5); if(s3==0) { while(!s3); if(s1num==1) { miao--; if(miao==-1) miao=59; write_sfm(8,miao); write_com(0x80+0x50+8); } if(s1num==2) { fen--; if(fen==-1) fen=59; write_sfm(5,fen); write_com(0x80+0x50+5); } if(s1num==3) { shi--; if(shi==-1) shi=23; write_sfm(2,shi); write_com(0x80+0x50+2); } if(s1num==4) { ri--; if(ri==0) ri=30; write_yr(10,ri); write_com(0x80+0x10+10); } if(s1num==5) { yue--; if(yue==0) shi=12; write_yr(7,yue); write_com(0x80+0x10+7); } if(s1num==6) { nian--; if(nian==0) nian=1; write_n(2,nian); write_com(0x80+0x10+2); } } } } } void init1() { TMOD=0x20; TH1=0xf3; TL1=0xf3; PCON=0x80; TR1=1; SCON=0x50; ES=1; EA=1; } ///////////////////////////////////////// void main() //主函数 { init(); while(1) { // keyscan(); init1(); while(1) { if(flag==1) { flag=0; if(menu==1) { ES=0; menu=0; wangxin_write("a调时间 b左移 c右移 d光标左移 e光标右移 f时间增加 g时间减少 h恢复时间 ",133); ES=1 ; } if(menu_aj==1) { ES=0; menu_aj=0; while(1) { keyscan(); } ES=1; } if(menu_yp1==1) { ES=0; menu_yp1=0; zuoyi(); ES=1; } if(menu_yp2==1) { ES=0; menu_yp2=0; youyi(); ES=1; } if(menu_guang1==1) { ES=0; TR0=0; menu_guang1=0; guangbiaozuo(); ES=1; } if(menu_guang2==1) { ES=0; TR0=0; menu_guang2=0; guangbiaoyou(); ES=1 ; } if(menu_tiao==1) { ES=0; TR0=0; menu_tiao=0; tiaoshijian1(); ES=1; } if(menu_tiao2==1) { ES=0; TR0=0; menu_tiao2=0; tiaoshijian2(); ES=1; } if(menu_kai==1) { ES=0; TR0=1; menu_kai=0; ES=1; } } } } } void timer0() interrupt 1 { TH0=(256-200)/256; TL0=(256-200)%256; count++; if(count==250) { count=0; miao++; if(miao==60) { miao=0; fen++; if(fen==60) { fen=0; shi++; if(shi==24) { shi=0; ri++; if(ri==31) { ri=1; yue++; if(yue==13) { yue=1; nian++; if(nian==9999) { nian=0; } write_n(2,nian); } write_yr(7,yue); } write_yr(10,ri); } write_sfm(2,shi); } write_sfm(5,fen); } write_sfm(8,miao); } } void ser() interrupt 4 { RI=0; aa=SBUF; flag=1; if(aa=='1') menu=1; else if(aa=='a') menu_aj=1; else if(aa=='b') menu_yp1=1; else if(aa=='c') menu_yp2=1; else if(aa=='d') menu_guang1=1; else if(aa=='e') menu_guang2=1; else if(aa=='f') menu_tiao=1; else if(aa=='g') menu_tiao2=1; else if(aa=='h') menu_kai=1; }