單片機(jī)設(shè)計(jì)LCD數(shù)字鐘(萬(wàn)年歷)程序與電路如下:
#include
#include //庫(kù)函數(shù)頭文件,代碼中引用了_nop_()函數(shù)
// 定義控制信號(hào)端口
sbit RS=P2^4; //P2.4
sbit RW=P2^5; //P2.5
sbit E=P2^6; //P2.6
sbit set=P3^4; //設(shè)置鍵
sbit enter=P3^5; //確認(rèn)鍵
sbit add1=P3^6; //加1鍵
sbit sub1=P3^7; //減1鍵
bit k=0,f=0;//k為0表示運(yùn)行狀態(tài),k為1表示設(shè)置狀態(tài);f為0表示第一行顯示,f為1表示第二行顯示
char sec,min,hour,week,day,month,year,n,m;
unsigned char count,key;
unsigned char lcdd[]="0123456789";
/*聲明調(diào)用函數(shù)*/
void dispd(); //日期顯示函數(shù)
void dispt(); //時(shí)間顯示函數(shù)
unsigned char keys();//按鍵掃描函數(shù)
void lcd_w_cmd(unsigned char com); //寫(xiě)命令字函數(shù)
void lcd_w_dat(unsigned char dat); //寫(xiě)數(shù)據(jù)函數(shù)
unsigned char lcd_r_start(); //讀狀態(tài)函數(shù)
void int1(); //LCD初始化函數(shù)
void delay(unsigned char t); //可控延時(shí)函數(shù)
void delay1(); //軟件實(shí)現(xiàn)延時(shí)函數(shù),5個(gè)機(jī)器周期
/*主函數(shù)*/
void main()
{
TMOD=0x01; //設(shè)置為定時(shí)器模式1
TH0=0x3c; //晶振6MHz,定時(shí)時(shí)間100ms
TL0=0xb0;
IE=0x82; //開(kāi)全局中斷和定時(shí)中斷
TR0=1; //啟動(dòng)定時(shí)器
sec=0; //秒
min=0; //分
hour=0; //時(shí)
week=0; //星期
day=1; //日
month=1; //月
year=0; //年
count=0; //中斷次數(shù)清0
n=-3; //設(shè)置鍵按下次數(shù),第一行日期
m=-3; //設(shè)置鍵按下次數(shù),第二行時(shí)間
P0=0xff; // 送全1到P0口
int1(); // 初始化LCD
delay(255);
while(1)
{
key=keys(); //讀取按鍵
switch(key)
{
case 0xe0: //按下設(shè)置鍵
{
TR0=0;
k=1;
if(f==0)
{
n=n+3;
if(n==9)
{
n=0;
m=0;
f=1;
}
}
else
{
m=m+3;
if(m==12)
{
m=0;
n=0;
f=0;
}
}
if(f==0)
{
lcd_w_cmd(0x0d);
lcd_w_cmd(0x86+n);
}
else
{
lcd_w_cmd(0x0d);
lcd_w_cmd(0xc4+m);
}
} break;
case 0xd0: //按下確認(rèn)鍵
{
k=0;
TR0=1;
n=-3;
m=-3;
f=0;
} break;
case 0xb0: //按下加1鍵
{
if(k==1)
{
if(f==0)
{
if(n==0){year++;if(year==100) year=0;}
else if(n==3) {month++;if(month==13) month=1;}
else {day++;if(day==32) day=1;}
dispd(); //調(diào)用第一行顯示函數(shù)
lcd_w_cmd(0x0d); //光標(biāo)閃爍
lcd_w_cmd(0x86+n);//返回設(shè)置值顯示地址
}
else
{
if(m==0){hour++;if(hour==24) hour=0;}
else if(m==3) {min++;if(min==60) min=0;}
else if(m==6){sec++;if(sec==60) sec=0;}
else {week++;if(week==7) week=0;}
dispt(); //調(diào)用第二行顯示函數(shù)
lcd_w_cmd(0x0d); //光標(biāo)閃爍
lcd_w_cmd(0xc4+m);//返回設(shè)置值顯示地址
}
}
} break;
case 0x70: //按下減1鍵
{
if(k==1)
{
if(f==0)
{
if(n==0){year--;if(year
/*****定時(shí)中斷函數(shù)*****/
void timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
count++;
if(count==10)
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
week++;
day++;
if(week==7) week=0;
if(day==29&&month==2&&year%4!=0) {day=1;month++;}
else if(day==30&&month==2&&year%4==0) {day=1;month++;}
else if(day==31&&(month==4||month==6||month==9||month==11)) {day=1;month++;}
else if(day==32&&(month==1||month==3||month==5||month==7||month==8||month==10||month==12)) {day=1;month++;}
if(month==13)
{
month=1;
year++;
if(year==100) year=0;
}
}
}
}
}
}
/*按鍵掃描函數(shù)*/
unsigned char keys()
{
unsigned char cod,del;
P3=0xf0;
cod=P3&0xf0; //讀入P3口鍵值
if(cod!=0xf0) //先檢測(cè)有無(wú)按鍵按下
{
delay(100); //去抖
if(cod!=0xf0)
{
cod=P3&0xf0; //讀入鍵值
do //等待鍵釋放
{
P3=0xf0;
del=P3&0xf0;
}while(del!=0xf0);
return(cod);//返回鍵值
}
}
return(0xf0); //返回該值
}
/*第一行顯示日期函數(shù)*/
void dispd()
{
lcd_w_cmd(0x0c); //設(shè)置光標(biāo)不顯示、不閃爍
delay(20);
lcd_w_cmd(0x83); //第一行起始顯示地址0x80+0x03
delay(20);
lcd_w_dat('2');
delay(2);
lcd_w_dat('0');
delay(2);
lcd_w_dat(lcdd[year/10]);
delay(2);
lcd_w_dat(lcdd[year%10]);
delay(2);
lcd_w_dat('-');
delay(2);
lcd_w_dat(lcdd[month/10]);
delay(2);
lcd_w_dat(lcdd[month%10]);
delay(2);
lcd_w_dat('-');
delay(2);
lcd_w_dat(lcdd[day/10]);
delay(2);
lcd_w_dat(lcdd[day%10]);
delay(2);
}
/*第二行顯示時(shí)間、星期函數(shù)*/
void dispt()
{
lcd_w_cmd(0x0c); //設(shè)置光標(biāo)不顯示、不閃爍
delay(20);
lcd_w_cmd(0xc3); //第二行起始顯示地址0x80+0x43
delay(20);
lcd_w_dat(lcdd[hour/10]);
delay(2);
lcd_w_dat(lcdd[hour%10]);
delay(2);
lcd_w_dat(':');
delay(2);
lcd_w_dat(lcdd[min/10]);
delay(2);
lcd_w_dat(lcdd[min%10]);
delay(2);
lcd_w_dat(':');
delay(2);
lcd_w_dat(lcdd[sec/10]);
delay(2);
lcd_w_dat(lcdd[sec%10]);
delay(2);
lcd_w_dat(' ');
delay(2);
lcd_w_dat('W');
delay(2);
lcd_w_dat(lcdd[week]);
delay(2);
}
評(píng)論
查看更多