|
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar miao,fen,shi,ri,zhou,yue,nian,key_num,time_flag;
sbit DS_cs = P2^7;
sbit DS_as = P2^3;
sbit DS_rw = P3^6;
sbit DS_ds = P3^7;
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
sbit key1 = P2^0;
sbit key2 = P2^1;
uchar code table1[]={" 20 - - "};
uchar code table2[]={" : : "};
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//void ds12887_judge_busy( void)
//{
// while(P1&0x80);
//}
void DS_write(uchar add,uchar date)
{
DS_as=1;
DS_ds=1;
DS_rw=1;
DS_cs=0;
P1=add;
DS_as=0;
DS_rw=0;
P1=date;
DS_rw=1;
DS_as=1;
DS_cs=1;
//ds12887_judge_busy; // 不知道要不要忙时检测/?????
}
uchar DS_read(uchar add) // 有错吗???
{
uchar DS_read_dat;
DS_as=1;
DS_ds=1;
DS_rw=1;
DS_cs=0;
P1=add;
DS_as=0;
DS_ds=0;
P1=0xff;
DS_read_dat = P1;
DS_ds=1;
DS_as=1;
DS_cs=1;
return(DS_read_dat);
//ds12887_judge_busy;
}
/*************************************************************
**功能:判断LCD忙 **
**参数:无 **
*************************************************************/
void check_busy(void)
{
while(1)
{
P0=0xff;
E=0;
_nop_();
RS=0;
_nop_();
_nop_();
RW=1;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
if((P0&0x80)==0)
{
break;
}
E=0;
}
}
//******************************************
// 将数据码写入LCD数据寄存器
//******************************************
void write_command(uchar tempdata)
{
E=0;
_nop_();
_nop_();
RS=0;
_nop_();
_nop_();
RW=0;
P0=tempdata;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
check_busy();
}
//*******************************************
// 写LCD1602使能程序
//*******************************************
void write_data(uchar tempdata)
{
E=0;
_nop_();
_nop_();
RS=1;
_nop_();
_nop_();
RW=0;
P0=tempdata;
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
check_busy();
}
void Display_Btime(uchar add,uchar date)
{
write_command(add);
;
write_data(date);
}
void Display_BCDtime(uchar add,uchar date)
{
uchar gw,sw;
sw=date/10;
gw=date%10;
write_command(0x80+add);
write_data(0x30+sw);
write_data(0x30+gw);
}
void BCD_time()
{
miao=DS_read(0x00);
Display_BCDtime(0x4a,miao);
fen=DS_read(0x02);
Display_BCDtime(0x47,fen);
shi=DS_read(0x04);
Display_BCDtime(0x44,shi);
ri=DS_read(0x07);
Display_BCDtime(0x0a,ri);
yue=DS_read(0x08);
Display_BCDtime(0x07,yue);
nian=DS_read(0x09);
Display_BCDtime(0x04,nian);
zhou=DS_read(0x06);
switch(zhou)
{
case 1:
{
Display_Btime(0x80+0x0d,'M');
Display_Btime(0x80+0x0e,'O');
Display_Btime(0x80+0x0f,'N');
}
break;
case 2:
{
Display_Btime(0x80+0x0d,'T');
Display_Btime(0x80+0x0e,'U');
Display_Btime(0x80+0x0f,'E');
}
break;
case 3:
{
Display_Btime(0x80+0x0d,'W');
Display_Btime(0x80+0x0e,'E');
Display_Btime(0x80+0x0f,'D');
}
break;
case 4:
{
Display_Btime(0x80+0x0d,'T');
Display_Btime(0x80+0x0e,'H');
Display_Btime(0x80+0x0f,'U');
}
break;
case 5:
{
Display_Btime(0x80+0x0d,'F');
Display_Btime(0x80+0x0e,'R');
Display_Btime(0x80+0x0f,'I');
}
break;
case 6:
{
Display_Btime(0x80+0x0d,'S');
Display_Btime(0x80+0x0e,'A');
Display_Btime(0x80+0x0f,'T');
}
break;
case 7:
{
Display_Btime(0x80+0x0d,'S');
Display_Btime(0x80+0x0e,'U');
Display_Btime(0x80+0x0f,'N');
}
break;
default :break;
}
}
void keyscan()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
key_num++;
time_flag=1;
switch(key_num)
{
case 1:
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+0x40+11);
write_command(0x0f);
}
break;
case 2 :
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+0x40+8);
write_command(0x0f);
}
break;
case 3 :
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+0x40+5);
write_command(0x0f);
}
break;
case 4:
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+15);
write_command(0x0f);
}
break;
case 5 :
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+11);
write_command(0x0f);
}
break;
case 6 :
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+0x08);
write_command(0x0f);
}
break;
case 7 :
{
while(!key1);
delay(10);
while(!key1);
write_command(0x80+0x05);
write_command(0x0f);
}
break;
case 8 :
{
write_command(0x0c);
key_num=0;
time_flag=0;
while(!key1);
delay(10);
while(!key1);
DS_write(0x09,nian);
DS_write(0x08,yue);
DS_write(0x07,ri);
DS_write(0x06,zhou);
DS_write(0x04,shi);
DS_write(0x02,fen);
DS_write(0x00,miao);
}
break;
default :break;
}
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
switch(key_num)
{
case 1:
{
miao++;
if(miao==60)
{
miao=0;
}
while(!key2);
delay(10);
while(!key2);
Display_BCDtime(0x4a,miao);
write_command(0x80+0x40+11);
write_command(0x0f);
}
break;
case 2:
{
fen++;
if(fen==60)
{
fen=0;
}
while(!key2);
delay(10);
while(!key2);
Display_BCDtime(0x47,fen);
write_command(0x80+0x40+8);
write_command(0x0f);
}
break;
case 3:
{
shi++;
if(shi==24)
{
shi=0;
}
while(!key2);
delay(10);
while(!key2);
Display_BCDtime(0x44,shi);
write_command(0x80+0x40+5);
write_command(0x0f);
}
break;
case 4:
{
zhou++;
if(zhou==8)
{
zhou=1;
}
while(!key2);
delay(10);
while(!key2);
switch(zhou)
{
case 1:
{
Display_Btime(0x80+0x0d,'M');
Display_Btime(0x80+0x0e,'O');
Display_Btime(0x80+0x0f,'N');
}
break;
case 2:
{
Display_Btime(0x80+0x0d,'T');
Display_Btime(0x80+0x0e,'U');
Display_Btime(0x80+0x0f,'E');
}
break;
case 3:
{
Display_Btime(0x80+0x0d,'W');
Display_Btime(0x80+0x0e,'E');
Display_Btime(0x80+0x0f,'D');
}
break;
case 4:
{
Display_Btime(0x80+0x0d,'T');
Display_Btime(0x80+0x0e,'H');
Display_Btime(0x80+0x0f,'U');
}
break;
case 5:
{
Display_Btime(0x80+0x0d,'F');
Display_Btime(0x80+0x0e,'R');
Display_Btime(0x80+0x0f,'I');
}
break;
case 6:
{
Display_Btime(0x80+0x0d,'S');
Display_Btime(0x80+0x0e,'A');
Display_Btime(0x80+0x0f,'T');
}
break;
case 7:
{
Display_Btime(0x80+0x0d,'S');
Display_Btime(0x80+0x0e,'U');
Display_Btime(0x80+0x0f,'N');
}
break;
default :break;
}
write_command(0x80+15);
write_command(0x0f);
}
break;
case 5:
{
ri++;
if(ri==32)
{
ri=0;
}
while(!key2);
delay(10);
while(!key2);
Display_BCDtime(0x0a,ri);
write_command(0x80+11);
write_command(0x0f);
}
break;
case 6:
{
yue++;
if(yue==13)
{
yue=0;
}
while(!key2);
delay(10);
while(!key2);
Display_BCDtime(0x07,yue);
write_command(0x80+0x08);
write_command(0x0f);
}
break;
case 7:
{
nian++;
if(nian==100)
{
nian=0;
}
while(!key2);
delay(10);
while(!key2);
Display_BCDtime(0x04,nian);
write_command(0x80+0x08);
write_command(0x0f);
}
break;
default:break;
}
}
}
}
void init_dis(uchar add,uchar table[])
{
uchar *app ;
write_command(0x80+add);
for(app=table;app<(table+16);app++)
{
write_data(*app);
}
}
void set_time()
{
DS_write(0x09,11);
DS_write(0x08,12);
DS_write(0x07,15);
DS_write(0x06,2);
DS_write(0x04,23);
DS_write(0x02,59);
DS_write(0x00,35);
}
void init()
{
write_command(0x01);
write_command(0x38);
write_command(0x0C);
write_command(0x06);
init_dis(0,table1);
init_dis(0x40,table2);
DS_write(0x0a,0x20); // 打开内部晶振,寄存器A设置
DS_write(0x0b,0x06); // 寄存器B设置
time_flag=0;
key_num=0;
set_time(); //实际应用中set_alarm()和set_time()仅第一次烧写需要,然后屏蔽掉重烧第二次,12887才正常工作,否则每次开机必须重设时间。
}
void main()
{
init();
while(1)
{
keyscan();
if(time_flag==0)
{
BCD_time();
}
}
}
proteus 中仿真可以的 下载到at89s52中 时钟就是不走啊 秒都不跳一下 跪求高手帮忙 ****** |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|