#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
***it rs=P2^5; //0输入指令,1输入数据
***it rw=P2^6; //0读,1写
***it en=P2^7; //使能
***it p***=P2^4;
uchar hour=13;
uchar min=40;
uchar sec=10;
uchar flag=0;
uchar count=0;
uchar num=0;
uchar i=6;
uchar nian=2015;
uchar mon=8;
uchar ri=2;
***it fun=P1^3;
***it ok=P1^4;
***it inc=P1^5;
***it dec=P1^6;
***it led1=P2^0;
***it led2=P2^1;
***it led3=P2^2;
***it gz1=P3^2;
***it gz2=P3^3;
***it gz3=P3^4;
***it fuwei=P3^5;
***it rst=P1^0; //复位
***it io=P1^1; //数据
***it sclk=P1^2; //时钟
uchar rehour,remin,resec,rehour1,remin1,resec1,rehour4,remin4,resec4,
rehour2,remin2,resec2,rehour3,remin3,resec3,num1,num2,num3,num4,num5,
num6, rehour5,remin5,resec5,rehour6,remin6,resec6,hour8;
uchar sum,flag1=0,flag2,flag3,sum1,sum2;
/************延时*********/
void delay(uint i)
{
uint j;
for(;i>0;i--)
for(j=0;j<125;j++);
}
/**********ds1302*********/
void write_ds1302_byte(uchar temp)//写字节
{
uint i;
for(i=0;i<8;i++)
{
sclk=0;
io=temp&0x01;
temp>>=1;
sclk=1;
}
}
/*uchar read_ds1302_byte() //读字节
{
uchar i,byte,t;
for(i=0;i<8;i++)
{
byte>>=1;
t=io;
byte|=t<<7;
sclk=1;
sclk=0;
}
return byte/16*10+byte%16;
} */
void write_ds1302(uchar address,uchar dat) //写数据
{
rst=0;
sclk=0;
rst=1;
write_ds1302_byte(address);
write_ds1302_byte(dat);
sclk=1;
rst=0;
}
uchar read_ds1302(uchar address) //读数据
{
uchar i,temp=0x00;
rst=0;
sclk=0;
rst=1;
write_ds1302_byte(address);
for(i=0;i<8;i++)
{
temp>>=1;
sclk=0;
if(io)
temp=temp|0x80;
sclk=1;
}
rst=0;
io=0;
return temp;
}
/*********lcd***********/
void check_busy(void)
{
rw=1;
rs=0;
en=1;
//while(P2&0x80);
}
void write_com(uchar com) //写命令
{
check_busy();
rw=0;
rs=0;
P0=com;
en=1;
delay(1);
en=0;
}
void write_dat(uchar dat) //写数据
{
check_busy();
rw=0;
rs=1;
P0=dat;
en=1;
delay(1);
en=0;
}
void lcd_pos(uchar x,uchar y)
{
uchar tmp;
x&=0x0f;
y&=0x03;
tmp=x;
if(y==0)
tmp|=0x80;
if(y==1)
tmp|=0x90;
if(y==2)
tmp|=0x88;
if(y==3)
tmp|=0x98;
write_com(tmp);
}
void write_lcd_string(uchar x,uchar y,uchar *s)
{
lcd_pos(x,y);
lcd_pos(x,y);
while(*s!='\0')
{
write_dat(*s);
s++;
delay(1);
}
}
void lcd_rst(void)
{
p***=1;
write_com(0x30); //基本指令
check_busy(); //检查忙的状态
write_com(0x01); //清屏
delay(5);
write_com(0x06);
delay(5);
write_com(0x0c);//显示开,关光标
delay(5);
}
void updat_
time(void)
{
lcd_pos(5,0);write_dat(0x30+(mon/10));write_dat(0x30+(mon%10));
lcd_pos(7,0);write_dat(0x30+(ri/10));write_dat(0x30+(ri%10));
lcd_pos(0,0);write_dat(0x30+(hour/10));write_dat(0x30+(hour%10));
lcd_pos(1,0);write_dat(':');write_dat(0x30+(min/10));
lcd_pos(2,0);;write_dat(0x30+(min%10));write_dat(':');
lcd_pos(3,0);write_dat(0x30+(sec/10));write_dat(0x30+(sec%10));
}
void write_time(void)
{
write_ds1302(0x8e,0x00);//禁止写保护
write_ds1302(0x8c,(nian/10)*16+nian%10); //0x84表示月
write_ds1302(0x88,(mon/10)*16+mon%10); //0x84表示月
write_ds1302(0x86,(ri/10)*16+ri%10); //0x82表示天数
write_ds1302(0x84,(hour/10)*16+hour%10); //0x84表示小时
write_ds1302(0x82,(min/10)*16+min%10); //0x82表示分钟
write_ds1302(0x80,(sec/10)*16+sec%10);
write_ds1302(0x8e,0x80); //写保护
}
void read_time(void)
{
nian=(read_ds1302(0x8d)>>4)*10+read_ds1302(0x8d)%16;
mon=(read_ds1302(0x89)>>4)*10+read_ds1302(0x89)%16;
ri=(read_ds1302(0x87)>>4)*10+read_ds1302(0x87)%16;
hour=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
min=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
sec=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
updat_time();
}
void guzhang_time(void)
{
if(gz1==0)
{
delay(20);
if(gz1==0)
{
while(!gz1);
sum++;
if(sum==3)
sum=1;
}
if(sum==1)
{
led1=0;
flag1++;
lcd_pos(0,1);write_dat(0x30+(flag1/10));write_dat(0x30+(flag1%10));
write_lcd_string(1,1,"次");
rehour1=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
remin1=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
resec1=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
delay(20);
lcd_pos(5,1);write_dat(0x30+(rehour1/10));write_dat(0x30+(rehour1%10));
lcd_pos(6,1);write_dat(':');write_dat(0x30+(remin1/10));
lcd_pos(7,1);write_dat(0x30+(remin1%10));//write_dat(':');
//lcd_pos(6,2);write_dat(0x30+(resec1/10));write_dat(0x30+(resec1%10));
//lcd_pos(7,2);
}
if(sum==2)
{
led1=1;
rehour=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
remin=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
resec=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
delay(10);
num1=abs((rehour-rehour1)*60+(remin-remin1));
//lcd_pos(0,3);write_dat(0x30+(num1/10));write_dat(0x30+(num1%10));
num2+=num1;
if(num2==1440)
{
num2=0;
flag1=0;
}
lcd_pos(2,1);write_dat(':');
write_dat(0x30+((num2/1000)%10));
write_dat(0x30+((num2/100)%10));
write_dat(0x30+((num2/10)%10));
write_dat(0x30+(num2%10));
}
}
if(gz2==0)
{
delay(20);
if(gz2==0)
{
while(!gz2);
sum1++;
if(sum1==3)
sum1=1;
}
if(sum1==1)
{
flag2++;
lcd_pos(0,2);write_dat(0x30+(flag2/10));write_dat(0x30+(flag2%10));
write_lcd_string(1,2,"次");
led2=0;
rehour2=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
remin2=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
resec2=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
delay(20);
lcd_pos(5,2);write_dat(0x30+(rehour2/10));write_dat(0x30+(rehour2%10));
lcd_pos(6,2);write_dat(':');write_dat(0x30+(remin2/10));
lcd_pos(7,2);write_dat(0x30+(remin2%10));//write_dat(':');
//lcd_pos(6,2);write_dat(0x30+(resec1/10));write_dat(0x30+(resec1%10));
//lcd_pos(7,2);
}
if(sum1==2)
{
led2=1;
rehour3=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
remin3=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
resec3=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
delay(10);
num3=abs((rehour3-rehour2)*60+(remin3-remin2));
//lcd_pos(0,3);write_dat(0x30+(num1/10));write_dat(0x30+(num1%10));
num4+=num3;
if(num4==1440)
{
num4=0;
flag2=0;
}
lcd_pos(2,2);write_dat(':');
write_dat(0x30+((num4/1000)%10));
write_dat(0x30+((num4/100)%10));
write_dat(0x30+((num4/10)%10));
write_dat(0x30+(num4%10));
}
}
if(gz3==0)
{
delay(20);
if(gz3==0)
{
while(!gz3);
sum2++;
if(sum2==3)
sum2=1;
}
if(sum2==1)
{
flag3++;
lcd_pos(0,3);write_dat(0x30+(flag3/10));write_dat(0x30+(flag3%10));
write_lcd_string(1,3,"次");
led3=0;
rehour4=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
remin4=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
resec4=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
delay(20);
lcd_pos(5,3);write_dat(0x30+(rehour4/10));write_dat(0x30+(rehour4%10));
lcd_pos(6,3);write_dat(':');write_dat(0x30+(remin4/10));
lcd_pos(7,3);write_dat(0x30+(remin4%10));//write_dat(':');
//lcd_pos(6,2);write_dat(0x30+(resec1/10));write_dat(0x30+(resec1%10));
//lcd_pos(7,2);
}
if(sum2==2)
{
led3=1;
rehour5=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
remin5=(read_ds1302(0x83)>>4)*10+read_ds1302(0x83)%16;
resec5=(read_ds1302(0x81)>>4)*10+read_ds1302(0x81)%16;
delay(10);
num5=abs((rehour5-rehour4)*60+(remin5-remin4));
//lcd_pos(0,3);write_dat(0x30+(num1/10));write_dat(0x30+(num1%10));
num6+=num5;
if(num6==1440)
{
num6=0;
flag3=0;
}
lcd_pos(2,3);write_dat(':');
write_dat(0x30+((num6/1000)%10));
write_dat(0x30+((num6/100)%10));
write_dat(0x30+((num6/10)%10));
write_dat(0x30+(num6%10));
}
}
}
void set_time(void)
{
if(fun==0)
{
delay(10);
if(fun==0)
{
while(!fun)
flag=1;
num++;
if(num==4)
num=1;
}
}
if(flag)
{
if(num==1)
{
lcd_pos(0,0);
write_com(0x0d);
if(inc==0)
{
delay(10);
if(inc==0)
{
while(!inc)
hour++;
if(hour==24)
hour=0;
//write_ds1302(0x84,(hour/10)*16+hour%10); //0x84表示小时
//hour8=(read_ds1302(0x85)>>4)*10+read_ds1302(0x85)%16;
//write_time();
//read_time();
//delay(20);
//lcd_pos(0,0);write_dat(0x30+(hour/10));write_dat(0x30+(hour%10));
updat_time();
}
}
if(dec==0)
{
delay(10);
if(dec==0)
{
while(!dec)
hour--;
if(min==-1)
hour=23;
write_time();
read_time();
updat_time();
}
}
}
if(num==2)
{
lcd_pos(2,0);
write_com(0x0d);
if(inc==0)
{
delay(10);
if(inc==0)
{
while(!inc)
min++;
if(min==60)
min=0;
write_time();
read_time();
updat_time();
}
}
if(dec==0)
{
delay(10);
if(dec==0)
{
while(!dec)
min--;
if(min==-1)
min=59;
write_time();
read_time();
updat_time();
}
}
}
if(num==3)
{
lcd_pos(4,0);
write_com(0x0d);
if(inc==0)
{
delay(10);
if(inc==0)
{
while(!inc)
sec++;
if(sec==60)
sec=0;
write_time();
read_time();
updat_time();
}
}
if(dec==0)
{
delay(10);
if(dec==0)
{
while(!dec)
sec--;
if(sec==-1)
sec=59;
write_time();
read_time();
updat_time();
}
}
}
}
if(ok==0)
{
delay(10);
if(ok==0)
{
while(!ok)
flag=0;
num=0;
write_com(0x0c);
write_time();
}
}
}
void system_init(void)
{
lcd_rst();
write_lcd_string(0,1,"系统初始化");
lcd_pos(5,1); ////////budong budong
while(i--)
{
write_dat(0x3e);//??????????
delay(300);
}
write_com(0x01); //清屏
write_lcd_string(0,1,"欢迎您使用计时器");
delay(2500);
write_com(0x01);//清屏
write_lcd_string(0,0,"13时40");
write_lcd_string(5,0,"08月01");
}
void main()
{
read_time();
system_init();
write_time();
while(1)
{
set_time();
if(!flag)
{
read_time();
guzhang_time();
}
}
}
|