#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define WRITE_DATE 0x86
#define WRITE_MONTH 0x88
#define WRITE_DAY 0x8A
#define WRITE_YEAR 0x8C
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define READ_DATE 0x87
#define READ_MONTH 0x89
#define READ_DAY 0x8B
#define READ_YEAR 0x8D
#define WRITE_PROTECT 0x8E
#define pos_year L1+0
#define pos_month L1+3
#define pos_date L1+6
#define pos_day L1+9
#define pos_hour L2+0
#define pos_minute L2+3
#define pos_sec L2+6
#define DATA P3
sbit RS1602=P1^0;
sbit RW1602=P1^1;
sbit E1602=P1^2;
#define L1 0x80
#define L2 0xc0
sbit sclk=P1^3;
sbit clk_io=P1^4;
sbit clk_rst=P1^5;
sbit func_key=P3^2;
sbit inc_key=P3^1;
sbit dec_key=P1^0;
void delay(uchar);
void write1602_comd(uchar);
void write1602_data(uchar);
void init1602_lcd();
void keyscan();
void Init_DS1302();
void update_time();
uchar flag=0;
uchar code table1[]="09-10-30/5";
uchar code table2[]="12:00:00";
void init1602_lcd()
{
int i;
write1602_comd(0x01);
write1602_comd(0x0c);
write1602_comd(0x06);
write1602_comd(0x3C);
write1602_comd(0x80);
for(i=0;i<10;i++)
{
write1602_data(table1[i]);
delay(20);
}
write1602_comd(0xc0);
for(i=0;i<8;i++)
{
write1602_data(table2[i]);
delay(20);
}
}
void delay(uchar z)
{
uchar i,j;
for(j=z;j>0;j--)
for(i=125;i>0;i--);
}
void write1602_comd(uchar com)
{
RS1602=0;
DATA=com;
RW1602=0;
delay(5);
E1602=1;
delay(5);
E1602=0;
}
void write1602_data(uchar datatowrite)
{
RS1602=1;
DATA=datatowrite;
RW1602=0;
delay(5);
E1602=1;
delay(5);
E1602=0;
}
void writeds1602_byte(uchar dat)
{
uchar i,dat_w;
dat_w=dat;
for(i=0;i<8;i++)
{
clk_io=0;
if(dat_w&0x01)
{clk_io=1;}
else
{clk_io=0;}
dat_w=dat_w>>1;
sclk=0;
sclk=1;
}
sclk=0;
}
uchar readds1602_byte()
{
uchar i,k;
k=0;
for(i=0;i<8;i++)
{
k=k>>1;
if(clk_io)
{k=k|0x80;}
sclk=0;
sclk=1;
}
sclk=0;
return (k);
}
void Write1602(uchar add,uchar dat)
{
clk_rst=0;
sclk=0;
clk_rst=1;
writeds1602_byte(add);
writeds1602_byte((dat/10<<4)|(dat%10));
sclk=0;
clk_rst=0;
}
uchar Read1302(uchar add)
{
uchar temp,dat1,dat2;
clk_rst=0;
sclk=0;
clk_rst=1;
writeds1602_byte(add);
temp=readds1602_byte();
sclk=0;
clk_rst=0;
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return (temp);
}
void Init_DS1302(void)
{
Write1602 (WRITE_PROTECT,0X00); delay(1);
Write1602 (WRITE_SECOND,0x00); delay(1);
Write1602 (WRITE_MINUTE,0x00);delay(1);
Write1602 (WRITE_HOUR,0x0C); delay(1);
Write1602 (WRITE_DAY,0x01); delay(1);
Write1602 (WRITE_DATE,0x12);delay(1);
Write1602 (WRITE_MONTH,0x05); delay(1);
Write1602 (WRITE_YEAR,0x09); delay(1);
Write1602 (WRITE_PROTECT,0x80); delay(1);
}
void read_write_date_time(uchar _1302_addr, _1602_addr)
{
uchar high,low,get_data;
Write1602 (WRITE_PROTECT,0x00);
delay(1);
get_data=Read1302(_1302_addr);
high=get_data/10;
low=get_data%10;
write1602_comd(_1602_addr);
write1602_data(high+0X40);
write1602_data(low+0X40);
Write1602 (WRITE_PROTECT,0x80); delay(1);
}
void write_lcd(uchar cusr_addr,time_data )
{
uchar hour_wei,ge_wei;
hour_wei=time_data/10;
ge_wei=time_data%10;
write1602_comd(cusr_addr);
write1602_data(hour_wei+0X40);
write1602_data(ge_wei+0X40);
}
void update_time()
{
uchar xq;
read_write_date_time(READ_HOUR,L2);
read_write_date_time(READ_MINUTE,L2+3);
read_write_date_time(READ_SECOND,L2+6);
read_write_date_time(READ_YEAR,L1);
read_write_date_time(READ_MONTH,L1+3);
read_write_date_time(READ_DATE,L1+6);
xq=Read1302(READ_DAY);
write1602_comd(L1+9);
write1602_data(xq+0X40);
Write1602 (WRITE_PROTECT,0x80);
}
uchar first=1,num,second,minute,hour,day,date,month,year;
void keyscan()
{
if(first==1)
{
second=Read1302(READ_SECOND);
minute=Read1302(READ_MINUTE);
hour=Read1302(READ_HOUR);
day=Read1302(READ_DAY);
date=Read1302(READ_DATE);
month=Read1302(READ_MONTH);
year=Read1302(READ_YEAR);
}
first=0;
if(func_key==0)
{
delay(5);
if(func_key==0)
{
num++;
flag=1;
while(!func_key);
if(num==1)
{
clk_rst=0;
write1602_comd(pos_sec);
write1602_comd(0x0f);
}
}
if(num==2)
{
write1602_comd(pos_minute);
}
if(num==3)
{
write1602_comd(pos_hour);
}
if(num==4)
{
write1602_comd(pos_day);
}
if(num==5)
{
write1602_comd(pos_date);
}
if(num==6)
{
write1602_comd(pos_month);
}
if(num==7)
{
write1602_comd(pos_year);
}
if(num==8)
{
num=0;
write1602_comd(0x0c);
flag=0;
clk_rst=1;
Write1602 (WRITE_PROTECT,0x00);delay(1);
Write1602(WRITE_SECOND,second);delay(1);
Write1602(WRITE_MINUTE,minute); delay(1);
Write1602(WRITE_HOUR,hour); delay(1);
Write1602(WRITE_DAY,day); delay(1);
Write1602(WRITE_DATE,date); delay(1);
Write1602(WRITE_MONTH,month); delay(1);
Write1602(WRITE_YEAR,year); delay(1);
Write1602 (WRITE_PROTECT,0x80); delay(1);
first=1;
}
}
if(num!=0)
{
if(inc_key==0)
{
delay(1);
if(inc_key==0)
{
while(!inc_key);
if(num==1)
{
second++;
if(second==60)
second=0;
write_lcd(pos_sec,second);
write1602_comd(pos_sec);
}
if(num==2)
{
minute++;
if(minute==60)
minute=0;
write_lcd(pos_minute,minute);
write1602_comd(pos_minute);
}
if(num==3)
{
hour++;
if(hour==24)
hour=0;
write_lcd(pos_hour,hour);
write1602_comd(pos_hour);
}
if(num==4)
{
day++;
if(day==8)
day=1;
write1602_comd(pos_day);
write1602_data(day+0X40);
}
if(num==5)
{
date++;
if(date==32)
date=1;
write_lcd(pos_date,date);
write1602_comd(pos_date);
}
if(num==6)
{
month++;
if(month==13)
month=1;
write_lcd(pos_month,month);
write1602_comd(pos_month);
}
if(num==7)
{
year++;
if(year==99)
hour=0;
write_lcd(pos_year,year);
write1602_comd(pos_year);
}
}
}
if(dec_key==0)
{
delay(1);
if(dec_key==0)
{
while(!dec_key);
if(num==1)
{
second--;
if(second==-1)
second=59;
write_lcd(pos_sec,second);
write1602_comd(pos_sec);
}
if(num==2)
{
minute--;
if(minute==-1)
minute=59;
write_lcd(pos_minute,minute);
write1602_comd(pos_minute);
}
if(num==3)
{
hour--;
if(hour==-1)
hour=23;
write_lcd(pos_hour,hour);
write1602_comd(pos_hour);
}
if(num==4)
{
day--;
if(day==0)
day=7;
write1602_comd(pos_day);
write1602_data(day+0X40);
}
if(num==5)
{
date--;
if(date==0)
date=31;
write_lcd(pos_date,date);
write1602_comd(pos_date);
}
if(num==6)
{
month--;
if(month==0)
month=12;
write_lcd(pos_month,month);
write1602_comd(pos_month);
}
if(num==7)
{
year--;
if(year==0)
hour=15;
write_lcd(pos_year,year);
write1602_comd(pos_year);
}
}
}
}
}
void main()
{
Init_DS1302();
init1602_lcd();
while(1)
{
keyscan();
delay(10);
if(flag==0)
update_time();
}
}
这里的flag是什么意思,有什么功能?定义的是什么
write1602_comd(_1602_addr);// 设置第二行地址指针
write1602_data(high+0X30);
write1602_data(low+0X30);
这里的加0x30对吗?不是该加0x40吗? |
|