液晶显示的时钟
液晶显示的时钟(初级版,不包括年月日)
#include<reg52.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit lcden = P2^6;//这几个端口要和硬件电路先联系 sbit lcdrw = P2^5; sbit lcdrs = P2^4; sbit dula = P3^1; sbit wela = P3^2; sbit key1 = P3^5; sbit key2 = P3^6; sbit key3 = P3^7; uchar code table[]=" 2013-10-01"; uchar code table1[]=" 23:59:53"; uchar miao,fen,shi; uint num,key1num; void delay(uint z) { uint x,y; for(x= z;x>0;x--) for(y =110;y>0;y--); } void write_com(uchar com)//写命令函数 { lcdrs = 0;//使能端置0低脉冲写指令,高脉冲写数据 _nop_();//推迟一个机器周期 lcdrw = 0;//低电平往里面写数据 P0= com; delay(5); lcden = 1;//使能端给一个高电平读走数据 delay(5); lcden = 0;//使能端给一个低电平回位 } void write_date(uchar date)//写数据函数 { lcdrs = 1;//使能端置0低脉冲写指令,高脉冲写数据 _nop_();//推迟一个机器周期 lcdrw = 0; P0= date; delay(5); lcden = 1; delay(5); lcden = 0; } void init() { dula = 0; wela = 0;//关闭数码管 fen = 59; miao = 53; shi = 23; lcden = 0; write_com(0x38);//显示模式设置 write_com(0x0c);//显示开关以及光标设置 write_com(0x06); write_com(0x80); for(num=0;num<13; num++) { write_date(table[num]); delay(5); } write_com(0x80+0x40);//设置数据指针 for(num=0;num<12; num++) { write_date(table1[num]); delay(5); } TMOD = 0x02; TH0 = 6; TL0 = 6; EA = 1; ET0 = 1; TR0 = 1; key1num = 0; } void write_sfm(uchar date,uchar addr) { uchar gw,sw; gw = date%10; sw = date/10; write_com(0x80+0x40+addr); write_date(0x30+sw); write_date(0x30+gw); } void key_scan() { P3 = 0xff; if(key1==0) { delay(5); if(key1 == 0) { while(!key1); key1num++; if(key1num==1) { TR0 = 0; write_com(0x80+0x40+11); write_com(0x0f); } if(key1num == 2) { write_com(0x80+0x40+8); write_com(0x0f); } if(key1num == 3) { write_com(0x80+0x40+5); write_com(0x0f); } if(key1num == 4) { key1num=0; TR0 = 1; write_com(0x0c); } } } if(key2==0) { delay(5); if(key2==0) { while(!key2); if(key1num!=0) { switch(key1num) { case 1: { miao++; if(miao == 60) { miao = 0; } write_sfm(miao,10); write_com(0x80+0x40+11); break; } case 2: { fen++; if(fen == 60) { fen = 0; } write_sfm(fen,7); write_com(0x80+0x40+8); break; } case 3: { shi++; if(shi == 24) { shi = 0; } write_sfm(shi,4); write_com(0x80+0x40+5); break; } } } } } if(key3==0) { delay(5); if(key3==0) { while(!key3); if(key1num!=0) { switch(key1num) { case 1: { miao--; if(miao == 0) { miao = 60; } write_sfm(miao,10); write_com(0x80+0x40+11); break; } case 2: { fen--; if(fen == -1) { fen = 59; } write_sfm(fen,7); write_com(0x80+0x40+8); break; } case 3: { shi--; if(shi == -1) { shi = 23; } write_sfm(shi,4); write_com(0x80+0x40+5); break; } } } } } } void main() { init(); while(1) { key_scan(); } } void T0_timer() interrupt 1 { num++; if(num ==4000) { num =0; miao++; if(miao==60) { miao = 0; fen++; if(fen == 60) { fen = 0; shi++; if(shi == 24) { shi =0; } write_sfm(shi,4); } write_sfm(fen,7); } write_sfm(miao,10); } }
液晶显示时钟(高级版,包括年月日还有星期)
#include<reg52.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit lcden = P2^6;//这几个端口要和硬件电路先联系 sbit lcdrw = P2^5; sbit lcdrs = P2^4; sbit dula = P3^1; sbit wela = P3^2; sbit key1 = P3^5; sbit key2 = P3^6; sbit key3 = P3^7; uchar code table[]=" 2821-12-31Sun"; uchar code table1[]=" 23:59:58"; uchar code table2[] ={"MonTueWedTurFriSatSun"}; uchar miao,fen,shi,day,month,xi; uint year; uint num,key1num; void delay(uint z) { uint x,y; for(x= z;x>0;x--) for(y =110;y>0;y--); } void write_com(uchar com)//写命令函数 { lcdrs = 0;//使能端置0低脉冲写指令,高脉冲写数据 _nop_();//推迟一个机器周期 lcdrw = 0;//低电平往里面写数据 P0= com; delay(5); lcden = 1;//使能端给一个高电平读走数据 delay(5); lcden = 0;//使能端给一个低电平回位 } void write_date(uchar date)//写数据函数 { lcdrs = 1;//使能端置0低脉冲写指令,高脉冲写数据 _nop_();//推迟一个机器周期 lcdrw = 0; P0= date; delay(5); lcden = 1; delay(5); lcden = 0; } void init() { dula = 0; wela = 0;//关闭数码管 fen = 59; miao = 58; shi = 23; lcden = 0; write_com(0x38);//显示模式设置 write_com(0x0c);//显示开关以及光标设置 write_com(0x06); write_com(0x80); for(num=0;num<15; num++) { write_date(table[num]); delay(5); } write_com(0x80+0x40);//设置数据指针 for(num=0;num<12; num++) { write_date(table1[num]); delay(5); } TMOD = 0x02; TH0 = 6; TL0 = 6; EA = 1; ET0 = 1; TR0 = 1; key1num = 0; year =2821; month = 12; day=31; xi=6; } void write_sfm(uchar date,uchar addr) { uchar gw,sw; gw = date%10; sw = date/10; write_com(0x80+0x40+addr); write_date(0x30+sw); write_date(0x30+gw); } void write_nyr(uchar date,uchar addr) { uchar gw,sw; gw = date%10; sw = date/10; write_com(0x80+addr); write_com(0x0f); write_date(0x30+sw); write_date(0x30+gw); } void write_nnn(uint date,uchar addr)//这里形式参数也要是uint型才能 { uchar gw,sw,bw,qw; gw = date%10; sw = date%100/10; bw = date%1000/100; qw = date/1000; write_com(0x80+addr); write_date(0x30+qw); write_date(0x30+bw); write_date(0x30+sw); write_date(0x30+gw); } void write_xqi(uchar date,uchar addr) { uchar i,pchar[3]; for(i=0;i<3;i++) pchar[i] = table2[3*date+i]; write_com(0x80+addr); for(i=0;i<3;i++) { write_date(pchar[i]); } } void key_scan() { P3 = 0xff; if(key1==0) { delay(5); if(key1 == 0) { while(!key1); key1num++; if(key1num==1) { TR0 = 0; write_com(0x80+0x40+11); write_com(0x0f); } if(key1num == 2) { write_com(0x80+0x40+8); write_com(0x0f); } if(key1num == 3) { write_com(0x80+0x40+5); write_com(0x0f); } if(key1num == 4) { write_com(0x80+11); write_com(0x0f); } if(key1num ==5) { write_com(0x80+8); write_com(0x0f); } if(key1num == 6) { write_com(0x80+5); write_com(0x0f); } if(key1num == 7) { write_com(0x80+13); write_com(0x0f); } if(key1num == 8) { key1num =0; write_com(0x0c); TR0 = 1; } } } if(key2==0) { delay(5); if(key2==0) { while(!key2); if(key1num!=0) { switch(key1num) { case 1: { miao++; if(miao == 60) { miao = 0; } write_sfm(miao,10); write_com(0x80+0x40+11); break; } case 2: { fen++; if(fen == 60) { fen = 0; } write_sfm(fen,7); write_com(0x80+0x40+8); break; } case 3: { shi++; if(shi == 24) { shi = 0; } write_sfm(shi,4); write_com(0x80+0x40+5); break; } case 4: { day++; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { if(day == 32) day =1; } break; case 4: case 6: case 9: case 11: { if(day == 31) day =1; } break; case 2: { if((year%4==0&&year%100!=0)||(year%400==0))//闰年 { if(day ==30) day = 1; } else { if(day == 29) day = 1; } } break; } write_nyr(day,10); write_com(0x0f); write_com(0x80+0x11); } break; case 5: { month ++; if(month==13) { month =1; } write_com(0x0f); write_nyr(month,7); write_com(0x80+0x8); } break; case 6: { year ++; if(year==9999) year = 1000; write_com(0x0f); write_nnn(year,2); write_com(0x80+0x5); } break; case 7: { xi ++; if(xi == 7) xi = 0; write_xqi(xi,12); write_com(0x0f); write_com(0x80+0x13); } break; } } } } if(key3==0) { delay(5); if(key3==0) { while(!key3); if(key1num!=0) { switch(key1num) { case 1: { miao--; if(miao == 0) { miao = 60; } write_sfm(miao,10); write_com(0x80+0x40+11); break; } case 2: { fen--; if(fen == -1) { fen = 59; } write_sfm(fen,7); write_com(0x80+0x40+8); break; } case 3: { shi--; if(shi == -1) { shi = 23; } write_sfm(shi,4); write_com(0x80+0x40+5); break; } case 4: { day--; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { if(day == 0) day =31; } break; case 4: case 6: case 9: case 11: { if(day == 0) day =30; } break; case 2: { if((year%4==0&&year%100!=0)||(year%400==0))//闰年 { if(day ==0) day = 29; } else { if(day == 0) day = 28; } } break; } write_com(0x0f); write_nyr(day,10); write_com(0x80+0x11); } break; case 5: { month --; if(month==0) { month =12; } write_com(0x0f); write_nyr(month,7); write_com(0x80+0x8); } break; case 6: { year --; if(year==9999) year = 1000; write_com(0x0f); write_nnn(year,2); write_com(0x80+0x5); } break; case 7: { //write_com(0x0f); xi--; if(xi == -1) xi = 6; write_xqi(xi,12); //write_com(0x80+0x13); } break; } } } } } void main() { init(); while(1) { key_scan(); } } void T0_timer() interrupt 1 { num++; if(num ==4000) { num =0; miao++; if(miao==60) { miao = 0; fen++; if(fen == 60) { fen = 0; shi++; if(shi == 24) { shi =0; day++; xi++;//xi 这个变量用来表示星期数 if(xi==7) { xi=0; } switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(day ==32) { day =1; month ++; if(month ==13) { month =1; year ++; write_nnn(year,2); } write_nyr(month,7); } break; case 4: case 6: case 9: case 11: if(day ==31) { day =1; month++; write_nyr(month,7); } break; case 2: if((year%4==0&&year%100!=0)||(year%400==0))//闰年 { if(day==30) { day=1; month++; write_nyr(month,7); } } else { if(day==29) { day=1; month++; write_nyr(month,7); } } break; } write_nyr(day,10); write_xqi(xi,12); } write_sfm(shi,4); } write_sfm(fen,7); } write_sfm(miao,10); } }
版权所有,转载请注明链接地址:http://www.cnblogs.com/fengdashen/p/3349354.html