//奉上,希望對(duì)初學(xué)者有幫助//
本文引用地址:http://m.butianyuan.cn/article/201611/322998.htm#include
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^7;
sbit lcdrs=P2^6;
sbit W=P2^5;
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
uchar num,count,k1num;
char miao,fen,shi;
uchar code table[]=" 2015-01-5 SAT" ;
uchar code table1[]=" 00:00:00";
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) // 寫(xiě)命令
{
W=0;
lcdrs=0; //RS選擇寫(xiě)命令還是寫(xiě)數(shù)據(jù),寫(xiě)命令
P0=com; //將寫(xiě)入的命令送到數(shù)據(jù)總線上
delay(5); //稍作延時(shí)以待數(shù)據(jù)穩(wěn)定
lcden=1; //使能端給一個(gè)高脈沖,因?yàn)槌跏蓟瘮?shù)已將lcden置為0
delay(5); //稍作延時(shí)
lcden=0; //將使能端置0以完成高脈沖
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,uchar date) //將數(shù)據(jù)寫(xiě)到某個(gè)地址上
{
uchar shi,ge;
shi=date/10;
ge=date;
write_com(0x80+0x40+add); //地址指針指在第二行的第add+1個(gè)
write_data(0x30+shi); //送數(shù)據(jù),將數(shù)據(jù)data的十位數(shù)送過(guò)去
write_data(0x30+ge); //送數(shù)據(jù),將數(shù)據(jù)data的個(gè)位數(shù)送過(guò)去
}
void keyscan()
{
if(k1==0)
{
delay(5);
if(k1==0)//去抖
{
k1num++; //計(jì)數(shù),計(jì)算k1按了幾次
while(!k1); //松手檢測(cè)
if(k1num==1) //當(dāng)按了一次
{
TR0=0; //計(jì)時(shí)器停止,時(shí)間停止
write_com(0x0f); //開(kāi)顯示,顯示光標(biāo),光標(biāo)閃爍
write_com(0x80+0x49); //數(shù)據(jù)指針指在第二行秒的個(gè)位
}
if(k1num==2) //當(dāng)k1按了兩次
{
// write_com(0x0f);
write_com(0x80+0x46); //數(shù)據(jù)指針指在第二行分的個(gè)位
}
if(k1num==3) //當(dāng)k1按了三次
{
// write_com(0x0f);
write_com(0x80+0x43); //數(shù)據(jù)指針指在第二行時(shí)的個(gè)位
}
if(k1num==4) //如果被按了四次
{
TR0=1; //啟動(dòng)計(jì)時(shí)器
k1num=0; //清零
write_com(0x0c); //關(guān)閉光標(biāo)
}
// if(k1num==5)
// {
// write_com(0x80+0x14)
// }
}
}
if(k1num!=0) //如果k1已經(jīng)按下過(guò)
{
if(k2==0) //如果k2按下
{
delay(5);
if(k2==0)
{
while(!k2); //松手檢測(cè)
if(k1num==1) //當(dāng)k1按了一次,調(diào)秒
{
miao++; //每按一次k2秒加一
if(miao==60)
{
miao=0;
}
write_sfm(8,miao); //數(shù)據(jù)顯示在第二行第9位和第10位
write_com(0x80+0x40+9); //k1中已設(shè)置光標(biāo),數(shù)據(jù)指針指在0x48
}
if(k1num==2) //如果k1被按過(guò)兩次
{
fen++;
if(fen==60)
fen=0;
write_sfm(5,fen); //數(shù)據(jù)送到數(shù)據(jù)指針指在第六位++
write_com(0x80+0x40+6); //數(shù)據(jù)指針指在第七位
}
if(k1num==3) //如果k1被按過(guò)3次
{
shi++;
if(shi==24)
shi=0;
write_sfm(2,shi); // 數(shù)據(jù)送到第3++
write_com(0x80+0x40+3); //地址指針指在第四位
}
}
}
if(k3==0) //k1按過(guò)后,如果k3按下
{
delay(5);
if(k3==0)
{
while(!k3);
if(k1num==1) //如果k1按下一次
{
miao--;
if(miao==-1)
{
miao=59;
}
write_sfm(8,miao); //數(shù)據(jù)送到第二行第九位++
write_com(0x80+0x40+9); //指針指在第十位
}
if(k1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(5,fen);
write_com(0x80+0x40+6);
}
if(k1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(2,shi);
write_com(0x80+0x40+3);
}
}
}
}
}
void init() //初始化函數(shù)
{
// dula=0;
// wela=0;
lcden=0;
write_com(0x38); //設(shè)置16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
write_com(0x0c ); //開(kāi)顯示,不顯示光標(biāo)
write_com(0x06); //當(dāng)寫(xiě)一個(gè)數(shù)據(jù)后地址指針加1,且光標(biāo)加1,當(dāng)顯示一個(gè)數(shù)據(jù)時(shí),整屏顯示不移動(dòng)
write_com(0x01); //數(shù)據(jù)指針清0,顯示清0
write_com(0x80); //顯示在屏內(nèi)
TMOD=0x01;
TH0=(655536-50000)/256;
TL0=(655536-50000)%6;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
init(); //初始化
for(num=0;num<15;num++)
{
write_data(table[num]);
delay(10);
}
write_com(0x80+0x40); //開(kāi)始時(shí)第二行顯s
for(num=0;num<10;num++)
{
write_data(table1[num]);
delay(5);
}
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(655536-50000)/256;
TL0=(655536-50000)%6;
count++;
if(count==18) //一秒時(shí)間到
{
count=0;
miao++;
if(miao==60)
{ miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(2,shi);
}
write_sfm(5,fen);
}
write_sfm(8,miao);
}
}
評(píng)論