#include
#define uchar unsigned char
#define XS P2
sbit RS = P1^0 ;
sbit RW = P1^1 ;
sbit EN = P1^2 ;
sbit io="P1"^3;
sbit sck="P1"^4;
sbit ce= P1^5;
uchar minute="0",second=0 ,hour=0,month=0 ,day=0 ,xinqi=0 ,year=0;
void LCD_delay() reentrant
{ uchar i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
;
}
void LCD_command(uchar command) //reentrant using 3
{
XS="command";
RS=0;
RW=0;
EN=0;
LCD_delay();
EN=1;
}
void LCD_data(uchar dat) //reentrant
{
XS="dat";
RS=1;
RW=0;
EN=0;
LCD_delay();
EN="1";
}
void LCD_write_char(uchar dat) // reentrant
{
LCD_data(dat);
}
void initlcd()
{
LCD_command(0x01);
LCD_command(0x38);
LCD_command(0x0c);
LCD_command(0x06);
LCD_command(0x01);
}
void showtime(void)
{
LCD_command(0x85);
//LCD_write_char('X');
//LCD_write_char('-');
//LCD_data(xinqi%10+0x30);
//LCD_write_char(' ');
LCD_write_char('Y');
LCD_write_char('-');
LCD_data(year/10+0x30);
LCD_data(year%10+0x30);
LCD_write_char(' ');
LCD_write_char('M');
LCD_write_char('-');
LCD_data(month/10+0x30);
LCD_data(month%10+0x30);
LCD_write_char(' ');
LCD_write_char('D');
LCD_write_char('-');
LCD_data(day/10+0x30);
LCD_data(day%10+0x30);
LCD_command(0xc5);
LCD_data(hour/10+0x30);
LCD_data(hour%10+0x30);
LCD_write_char(':');
LCD_data(minute/10+0x30);
LCD_data(minute%10+0x30);
LCD_write_char(':');
LCD_data(second/10+0x30);
LCD_data(second%10+0x30);
}
void write_ds1302_byte(uchar temp)
{
uchar i;
for(i=0;i<8;i++)
{
sck = 0;
io=(bit)(temp&0x01);
sck="1";
temp >>=1;
}
}
void write_ds1302(uchar address,uchar dat)
{
ce="0";
sck="0";
ce="1";
write_ds1302_byte(address);
write_ds1302_byte(dat);
ce="0";
}
unsigned char read_ds1302(uchar address)
{
uchar i,temp=0x00,dat1,dat2;
ce="0";
sck="0";
ce="1";
write_ds1302_byte(address);
for(i=0;i<8;i++)
{
if( io )
temp|=0x80;
sck = 1;
temp>>=1;
sck="0";
}
ce="0";
dat1=temp;
dat2=dat1/16;
dat1=dat1%16;
dat1=dat1+dat2*10;
return (dat1);
}
void read_time(void)
{
uchar j;
j="0x81";
second="read"_ds1302(j);
j="j"+2;
minute="read"_ds1302(j);
j="j"+2;
hour="read"_ds1302(j);
j="j"+2;
day= read_ds1302(j);
j="j"+2;
month="read"_ds1302(j);
j="j"+2;
xinqi="read"_ds1302(j);
j="j"+2;
year="read"_ds1302(j);
}
void set_time(void)
{
uchar j;
j=0x80;
write_ds1302(j,second);
j="j"+2;
write_ds1302(j,minute);
j="j"+2;
write_ds1302(j,hour);
j="j"+2;
write_ds1302(j,day);
j="j"+2;
write_ds1302(j,month);
j="j"+2;
write_ds1302(j,xinqi);
j="j"+2;
write_ds1302(j,year);
j="j"+2;
}
void time_menu()
{
LCD_command(0x01);
LCD_command(0x80);
LCD_write_char('D');
LCD_write_char('A');
LCD_write_char('T');
LCD_write_char('E');
LCD_command(0xC0);
LCD_write_char('T');
LCD_write_char('I');
LCD_write_char('M');
LCD_write_char('E');
}
void ds1302_init(void)
{
write_ds1302(0x8e,0x00);
write_ds1302(0x80,second&0x00);
set_time();
write_ds1302(0x8e,0x80);
}
void main()
{
initlcd();
ds1302_init();
time_menu();
while(1)
{
read_time();
showtime();
}
}
文章评论(0条评论)
登录后参与讨论