#include <reg52.h>
#include <intrins.h>
typedef unsigned char uchar;
uchar table[] = {"Time:"};
uchar table1[] = {"Date:"};
#define DS1302_W_ADDR 0x80
#define DS1302_R_ADDR 0x81
/****************ÑÓʱº¯Êý*********************************/
void delay_us(unsigned char t)
{
while(--t);
}
void delay_ms(unsigned char t)
{
while(t--)
{
delay_us(245);
delay_us(245);
}
}
/*******************DS1302********************/
// Ãë ·Ö Ê± ÈÕ Ô ÐÇÆÚ Äê
uchar TIME[7] = {00,00,00,06,01,07,19};
sbit SCLK = P1^0;
sbit IO = P1^1;
sbit RST = P1^2;
//д×Ö½Ú
void DS1302write_byte(uchar byte)
{
uchar i;
for(i = 0; i < 8; i++)
{
SCLK = 0; //ÀµÍʱÖÓÏß
IO = byte & 0x01;
byte >>= 1;
SCLK = 1; //À¸ßʱÖÓÏß ²úÉúÉÏÉýÑØ Ð´ÈëÊý¾Ý
}
}
//¶Á×Ö½Ú
uchar DS1302read_byte()
{
uchar i,dat;
for(i = 0; i < 8; i++)
{
SCLK = 0;
dat >>= 1;
if(IO) dat |= 0x80;
SCLK = 1;
}
return dat;
}
//дÊý¾Ý
void DS1302write_dat(uchar addr,uchar dat)
{
DS1302write_byte(addr);
DS1302write_byte(dat);
}
//¶ÁÊý¾Ý
uchar DS1302read_dat(uchar addr)
{
uchar dat;
DS1302write_byte(addr);
dat = DS1302read_byte();
return dat;
}
//Çå³ýд±£»¤
void DS1302clear_wp()
{
RST = 0;
SCLK = 0;
RST = 1;
DS1302write_dat(0x8e,0x00);
}
//ÉèÖÃд±£»¤
void DS1302set_wp()
{
DS1302write_dat(0x8e,0x80);
SCLK = 0;
RST = 0;
}
// ÉèÖÃÆðʼʱ¼ä
//Ê®½øÖÆתBCD
void DS1302set_time(uchar addr)
{
uchar i,j;
DS1302clear_wp();
for(i = 0; i < 7; i++)
{
j=TIME[i]/10;
TIME[i]=TIME[i]%10;
TIME[i]=TIME[i]+j*16;
DS1302write_dat(addr,TIME[i]);
addr += 2;
}
DS1302set_wp();
}
//¶ÁDS1302µÄÊý¾Ý
//¶ÁÈ¡BCDÂë
void DS1302read_time(uchar addr)
{
uchar i;
DS1302clear_wp();
for(i = 0; i < 7; i++)
{
TIME[i] = DS1302read_dat(addr);
addr += 2;
}
DS1302set_wp();
}
/*********************LCD1602ÏÔʾ***********************************/
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^4;
//ÅÐæ
void read_busy()
{
uchar busy;
RS = 0;
RW = 1;
P0 = 0xff;
do{
EN = 1;
busy = P0;
EN = 0;
}while(busy & 0x80);
}
//дÃüÁî
void write_cmd(uchar cmd)
{
read_busy();
RS = 0;
RW = 0;
P0 = cmd;
EN = 1;
EN = 0;
}
//дÊý¾Ý
void write_dat(uchar dat)
{
read_busy();
RS = 1;
RW = 0;
P0 = dat;
EN = 1;
EN = 0;
}
//ÏÔʾһ¸ö×Ö·û
void dis_onechar(uchar x,uchar y,uchar dat)
{
if(y) x |= 0xc0;
x |= 0x80;
write_cmd(x);
write_dat(dat);
}
//ÏÔʾһ¸ö×Ö·û´®
void dis_str(uchar x,uchar y,uchar *str)
{
if(y) x |= 0xc0;
x |= 0x80;
write_cmd(x);
while(*str != '\0')
{
write_dat(*str++);
}
}
void LCD1602_init()
{
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x06);
write_cmd(0x01);//ÇåÆÁ
}
//ÏÔʾʱ¼äº¯Êý
void dis_time()
{
DS1302read_time(0x81);
dis_onechar(5,1,TIME[2]/16 + '0'); //ʱ
dis_onechar(6,1,TIME[2]%16 + '0');
dis_onechar(7,1,':');
dis_onechar(8,1,TIME[1]/16 + '0'); //·Ö
dis_onechar(9,1,TIME[1]%16 + '0');
dis_onechar(10,1,':');
dis_onechar(11,1,TIME[0]/16 + '0');//Ãë
dis_onechar(12,1,TIME[0]%16 + '0');
dis_onechar(5,0,TIME[6]/16 + '0'); //Äê
dis_onechar(6,0,TIME[6]%16 + '0');
dis_onechar(7,0,'-');
dis_onechar(8,0,TIME[4]/16 + '0'); //ÔÂ
dis_onechar(9,0,TIME[4]%16 + '0');
dis_onechar(10,0,'-');
dis_onechar(11,0,TIME[3]/16 + '0'); //ÈÕ
dis_onechar(12,0,TIME[3]%16 + '0');
dis_onechar(13,0,' ');
dis_onechar(14,0,TIME[5]/16 + '0'); //ÐÇÆÚ
dis_onechar(15,0,TIME[5]%16 + '0');
}
/***************************¼üÅÌɨÃè***********************************/
//¼üÅÌɨÃ躯Êý
uchar key_scan()
{
uchar key;
P3 = 0xff;
if(P3 == 0xfe) //µÚÒ»¸ö°´¼ü P30
delay_ms(5);
if(P3 == 0xfe)
{
key = 1;
while(P3 == 0xfe);
delay_ms(5);
while(P3 == 0xfe);
return key;
}
else if(P3 == 0xfd) //µÚ¶þ¸ö°´¼ü P31
delay_ms(5);
if(P3 == 0xfd)
{
key = 2;
while(P3 == 0xfd);
delay_ms(5);
while(P3 == 0xfd);
return key;
}
else if(P3 == 0xf7) //µÚËĸö°´Å¥ P33
delay_ms(5);
if(P3 != 0xf7)
{
key = 4;
while(P3 == 0xf7);
delay_ms(5);
while(P3 == 0xf7);
return key;
}
return 0xff;
}
/***************ÍⲿÖжÏ***********************************/
//³õʼ»¯
void External_init0()
{
IT0 = 1; //ÍⲿÒý½ÅϽµÑزúÉúÖжÏ
EA = 1;
EX0 = 1; //ÔÊÐíÍⲿÖжÏ
}
//ÖжϷþÎñ³ÌÐò
void External_INT0() interrupt 0
{
uchar keyvalue;
LCD1602_init();
keyvalue = key_scan(); //ɨÃè¶ÀÁ¢¼üÅ
if(keyvalue == 1) //ʱ
{
TIME[2] = (TIME[2]/16)*10 + TIME[2]%16;
TIME[2]++;
if(TIME[2] == 24) TIME[2] = 0;
TIME[2] = (TIME[2]/10)*16 + TIME[2]%10;
dis_onechar(5,1,TIME[2]/16 + '0'); //ʱ
dis_onechar(6,1,TIME[2]%16 + '0');
dis_onechar(7,1,':');
DS1302write_dat(0x84,TIME[2]);
}
else if(keyvalue == 2) //·Ö
{
TIME[1] = (TIME[1]/16)*10 + TIME[1]%16;
TIME[1]++;
if(TIME[1] == 60) TIME[1] = 0;
TIME[1] = (TIME[1]/10)*16 + TIME[1]%10;
dis_onechar(8,1,TIME[1]/16 + '0'); //·Ö
dis_onechar(9,1,TIME[1]%16 + '0');
dis_onechar(10,1,':');
DS1302write_dat(0x82,TIME[1]);
}
else if(keyvalue == 4) //Ãë
{
TIME[0] = (TIME[0]/16)*10 + TIME[0]%16;
TIME[0]++;
if(TIME[1] == 60) TIME[1] = 0;
TIME[0] = (TIME[0]/10)*16 + TIME[0]%10;
dis_onechar(11,1,TIME[0]/16 + '0');//Ãë
dis_onechar(12,1,TIME[0]%16 + '0');
DS1302write_dat(0x80,TIME[0]);
}
}
/**********************Ö÷º¯Êý****************************************/
void main()
{
External_init0();
LCD1602_init();
dis_str(0,1,table);
dis_str(0,0,table1);
DS1302set_time(0x80);
while(1)
{
dis_time();
}
}
哎 帖子凉凉