?? 1302.c
字號:
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit ds_rst=P2^3;
sbit ds_io=P2^4;
sbit ds_clk=P2^5;
sbit s0=P3^0;
sbit s1=P3^1;
sbit s2=P3^2;
sbit s3=P3^3;
sbit LCD_EN=P3^4;
sbit LCD_RS=P3^5;
bit flag;
void LCD_display();
uchar taa[]={"20 - - W "};
uchar tab[]={" : : @_@! "};
uchar tad[7];
uchar nian,yue,ri,zhou,sh,fen,miao;
void delayms(uint z)
{
uint x,y;
for(x=0;x<=110;x++)
for(y=0;y<=z;y++);
}
void break_init()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
/***************時鐘1302***********************************/
void DS_write_byte(uchar date)
{
uchar i;
ds_rst=1;
for(i=0;i<8;i++)
{
if((date&0x01)==1)
ds_io=1;
else
ds_io=0;
date>>=1;
ds_clk=1;
ds_clk=0;
}
}
uchar DS_read_byte()
{
uchar date,i;
ds_rst=1;
for(i=0;i<8;i++)
{
if(ds_io==1)
date=(date>>1)|0x80;
else
date=date>>1;
ds_clk=1;
ds_clk=0;
}
return date;
}
void DS_write_com(uchar add,uchar date)
{
ds_rst=0;
ds_clk=0;
ds_rst=1;
DS_write_byte(add);
delayms(15);
DS_write_byte(date);
ds_rst=0;
}
uchar DS_read(uchar add)
{
uchar date;
ds_rst=0;
ds_clk=0;
ds_rst=1;
DS_write_byte(add);
delayms(15);
date=DS_read_byte();
ds_rst=0;
return date;
}
/***************液晶1062**************************************/
void LCD_write_com(uchar com)
{
P0=com; //寫入命令
LCD_RS=0;
delayms(5);
LCD_EN=1;
delayms(5);
LCD_EN=0;
}
void LCD_write_data(uchar date)
{
P0=date; //寫入數據
LCD_RS=1;
delayms(5);
LCD_EN=1;
delayms(5);
LCD_EN=0;
}
void LCD_init()
{
uchar i;
LCD_write_com(0x38);//顯示16*2接口
LCD_write_com(0x01);//清屏
LCD_write_com(0x0c);//00001DCB,D開顯示C顯示光標B光標閃爍
LCD_write_com(0x06);//000001NS,N1指針、光標加一,N0指針、光標減一。S1整屏左移N1/右移N0,S0整屏不移動
LCD_write_com(0x80);
for(i=0;i<16;i++)
{
LCD_write_data(taa[i]);
delayms(5);
}
LCD_write_com(0x80+0x40);
for(i=0;i<16;i++)
{
LCD_write_data(tab[i]);
delayms(5);
}
}
void LCD_sfm(uchar add,uchar line,uchar date)
{
uchar shi,ge;
shi=(date>>4)&0x0f;
ge=date&0x0f;
if(ge==10)
shi++;
if(line==0)
LCD_write_com(0x80+add);
else
LCD_write_com(0x80+0x40+add);
LCD_write_data(0x30+shi);
LCD_write_data(0x30+ge);
}
void LCD_sfm1(uchar add,uchar line,uchar date,uchar addr)
{
uchar shi,ge,temp;
shi=date/10;
ge=date%10;
temp=(temp&0x0f)|shi;
temp<<=4;
temp=temp|ge;
DS_write_com(addr,temp);
if(line==0)
LCD_write_com(0x80+add);
else
LCD_write_com(0x80+0x32+add);
LCD_write_data(0x30+shi);
LCD_write_data(0x30+ge);
}
/*****************按鍵控制***************************************************/
LCD_key()
{
uchar add;
while(s0==0)
{
delayms(50);
while(s0==0)
{
flag=1;
if(add==0)
{
LCD_write_com(0x80+0x54);
LCD_write_com(0x0f);
}
if((add<15)&(add!=0)==1)
LCD_write_com(0x81+add);
if(add>=15)
LCD_write_com(0x80+0x33+add);
while(s0!=1);
add++;
while(add==24)
add=0;
}
}
while(s1==0)
{
delayms(5);
while(s1==0)
{
switch(add)
{
case 3:nian++;while(nian==100) nian=0;LCD_sfm1(2,0,nian,0x8c);LCD_write_com(0x80+3);break;
case 6:yue++;while(yue==13) yue=0;LCD_sfm1(5,0,yue,0x88);LCD_write_com(0x80+6);break;
case 9:ri++;while(ri==31) ri=0;LCD_sfm1(8,0,ri,0x86);LCD_write_com(0x80+9);break;
case 14:zhou++;while(zhou==8) zhou=0;LCD_sfm1(13,0,zhou,0x8a);LCD_write_com(0x80+13);break;
case 17:sh++;while(sh==24) sh=0;LCD_sfm1(16,1,sh,0x84);LCD_write_com(0x80+0x40+3);break;
case 20:fen++;while(fen==60) fen=0;LCD_sfm1(19,1,fen,0x82);LCD_write_com(0x80+0x40+6);break;
case 23:miao++;while(miao==60) miao=0;LCD_sfm1(22,1,miao,0x80);LCD_write_com(0x80+0x40+9);break;
}
while(s1!=1);
}
}
while(s2==0)
{
delayms(5);
while(s2==0)
{
switch(add)
{
case 3:nian--;while(nian==0) nian=99;LCD_sfm1(2,0,nian,0x8c);LCD_write_com(0x80+3);break;
case 6:yue--;while(yue==0) yue=12;LCD_sfm1(5,0,yue,0x88);LCD_write_com(0x80+6);break;
case 9:ri--;while(ri==0) ri=31;LCD_sfm1(8,0,ri,0x86);LCD_write_com(0x80+9);break;
case 14:zhou--;while(zhou==0) zhou=7;LCD_sfm1(13,0,zhou,0x8a);LCD_write_com(0x80+13);break;
case 17:sh--;while(sh==0) sh=23;LCD_sfm1(16,1,sh,0x84);LCD_write_com(0x80+0x40+3);break;
case 20:fen--;while(fen==0) fen=59;LCD_sfm1(19,1,fen,0x82);LCD_write_com(0x80+0x40+6);break;
case 23:miao--;while(miao==0) miao=59;LCD_sfm1(22,1,miao,0x80);LCD_write_com(0x80+0x40+9);break;
}
while(s1!=1);
}
}
while(s3==0)
{
delayms(5);
while(s3==0)
{
add=1;
LCD_write_com(0x80+0x40+add);
LCD_write_com(0x0c);
flag=0;
while(s3!=1);
}
}
}
/******************顯示********************************************/
void DS_clock()
{
uchar add=0x81,i;
for(i=0;i<7;i++)
{
tad[i]=DS_read(add);
add+=2;
}
}
void LCD_display() //顯示數據
{
LCD_sfm(2,0,tad[6]); //年
LCD_sfm(5,0,tad[4]); //月
LCD_sfm(8,0,tad[3]); //日
LCD_sfm(13,0,tad[5]); //周
LCD_sfm(2,1,tad[2]); //時
LCD_sfm(5,1,tad[1]); //分
LCD_sfm(8,1,tad[0]); //秒
nian=tad[6];yue=tad[4];ri=tad[3];zhou=tad[5];sh=tad[2];fen=tad[1];miao=tad[0];
}
/********************主函數********************************************************/
main()
{
break_init();
LCD_init();
while(1)
{
while(flag==0)
{
DS_clock();
LCD_display();
}
}
}
time0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
LCD_key();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -