#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
void Input(uchar ucdat);
void delay(uchar);
void miaobiao();
void scan();
void display(uchar,uchar,uchar,uchar,uchar,uchar);
void display1();
unsigned char table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00,0x80};
unsigned char table2[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef,0x00,0x80};
uchar a,acc,disp,ucdat,i,add,n,systime,systime2,
sec,min,hr,day,month,week,year,
Dsec,Dmin,Dhr,Dday,Dmonth,Dweek,Dyear,
a1,a2,a3,a4,a5,a6,a7,
nmin,nDmin,nhr,nDhr,
s0,s1,m0,m1,h0,h1;
uchar n11,n12,n13,n14,n15,n16,
n21,n22,n23,n24,
n31,n32,n33,n34,
n41,n42,n43,n44,
n51,n52,n53,n54,
n61,n62,n63,n64,
n71,n72,n73,n74,
n81,n82,n83,n84,
n91,n92,n93,n94;
void _nop_();
sbit clk=P1^2; //AD读引角
sbit dat=P1^1; //AD写引角
sbit rst=P1^0;
sbit menu=P3^4;
sbit key1=P3^3;
sbit key2=P3^2;
sbit we1=P2^4;
sbit we2=P2^5;
sbit we3=P2^6;
sbit we4=P2^3;
sbit we5=P2^2;
sbit we6=P2^1;
sbit acc0=ACC^0;
sbit acc1=ACC^1;
sbit acc2=ACC^2;
sbit acc3=ACC^3;
sbit acc4=ACC^4;
sbit acc5=ACC^5;
sbit acc6=ACC^6;
sbit acc7=ACC^7;
void writecontrol(uchar num);
void edable(void);
void disable(void);
void readb(void);
void writeb(void);//uchar readRAM(uchar add);
uchar readRTC(uchar add); //uchar writeRAM(uchar add,uchar num);//uchar writeRTC(uchar add,uchar num);
uchar readsec(void);
uchar readmin(void);
uchar readhr(void);
uchar readday(void);
uchar readweek(void);
uchar readmonth(void);
uchar readyear(void);
/*************************
/*************************/
void writecontrol(uchar num);
void edable(void);
void disable(void);
void writeb(void)//写
{
//ACC=add;
//ACC<<=1;
//ACC|=0x80;
dat=acc0; //命令字
clk=1; //时钟上升 发送数据有效
clk=0; //清零始终总线
dat=acc1; //命令字
clk=1; clk=0;
dat=acc2;
clk=1; clk=0;
dat=acc3;
clk=1; clk=0;
dat=acc4;
clk=1; clk=0;
dat=acc5;
clk=1; clk=0;
dat=acc6;
clk=1; clk=0;
dat=acc7;
clk=1; clk=0;
}
void readb(void)
{
acc0=dat;
clk=1; clk=0;
acc1=dat;
clk=1; clk=0;
acc2=dat;
clk=1; clk=0;
acc3=dat;
clk=1; clk=0;
acc4=dat;
clk=1; clk=0;
acc5=dat;
clk=1; clk=0;
acc6=dat;
clk=1; clk=0;
acc7=dat;
clk=1; clk=0;
}
uchar readRTC(uchar add) //从1302中读取一字节
{
rst=0;
clk=0;
rst=1;
ACC=add;
writeb();
dat=1;
readb();
return ACC; //返回读取直
}
writeRTC(uchar add,uchar num) //向1302中写入一字节
{
clk=0;
rst=0;
rst=1;
ACC=add;
writeb();
ACC=num;
writeb();
rst=0;
clk=1;
}
void writecontrol(uchar num)
{
writeRTC(0x8e,num);
}
void disable(void)
{
writeRTC(0x8e,0x00);
}
void enable(void)
{
writeRTC(0x8e,0x80);
}
/***************主程序********************/
void main(void)
{
EA=1; //开总中断
ET1=1; //开定时器中断0
TMOD=0x10; //设置工作模式 方式1
TH1=(65536-10000)/256; //装初值 求整
TL1=(65536-10000)%256; //求余
TR1=0;
//disable(); //禁止写保护
readRTC(0x81); sec=ACC;
readRTC(0x83); min=ACC; //读分
readRTC(0x85); hr=ACC; //读
readRTC(0x87); day=ACC; //读
readRTC(0x89); month=ACC; //读
readRTC(0x8D); year=ACC;
readRTC(0x8B); week=ACC;
/*******************
disable();
writeRTC(0x80,sec); //初始化秒
writeRTC(0x82,min); //初始化分
writeRTC(0x84,hr); //初始化小时
writeRTC(0x86,day); //初始化日
writeRTC(0x88,month); //初始化月
writeRTC(0x8a,week); //初始化星期
writeRTC(0x8c,year); //初始化年
/*****************************************/
disable();
writeRTC(0x80,0x00); //初始化秒
writeRTC(0x82,0x00); //初始化分
writeRTC(0x84,0x12); //初始化小时
writeRTC(0x86,0x15); //初始化日
writeRTC(0x88,0x11); //初始化月
writeRTC(0x8a,0x04); //初始化星期
writeRTC(0x8c,0x07); //初始化年
/*******************************************/
writecontrol(0xaa); //充电允许
enable(); //启动写保护
nmin=nDmin=nhr=nDhr=1; //初始化闹零
while(1)
{
readRTC(0x81); //读秒
sec=(a1=ACC)&0x0f; //把读出来的值给acc然后保留低四位,执行后acc的值没变但ACC内容变了,这就是为什么要先把值给acc的原因
Dsec=(a1>>4); //把高四位移到低四位,此时高四位为0,把sec,Dsec(十秒的意思)作为查表的变量
readRTC(0x83); min=(a2=ACC)&0x0f; Dmin=(a2>>4); //读分
readRTC(0x85); hr=(a3=ACC)&0x0f; Dhr=(a3>>4); //读
readRTC(0x87); day=(a4=ACC)&0x0f; Dday=(a4>>4); //读
readRTC(0x89); month=(a5=ACC)&0x0f; Dmonth=(a5>>4); //读
readRTC(0x8D); year=(a6=ACC)&0x0f; Dyear=(a6>>4); //读
//readRTC(0x8B); week=(a7=ACC)&0x0f; Dweek=(a7>>4); //读星期
if(systime==0)
{scan();}
if(systime>0)
{systime--;}
if(disp==0)
{
display(sec,Dsec,min,Dmin,hr,Dhr);
}
else if(disp==1)
{
display(day,Dday,month,Dmonth,year,Dyear);
}
else if(disp==2)
{
display(nmin,nDmin,nhr,nDhr,11,11);
}
else if(disp==3)
{
display(sec,Dsec,min,Dmin,hr,Dhr);
}
else if(disp==4)
{
miaobiao();
display(s0,s1,m0,m1,h0,h1);
}
else if(disp==5)
{
if(systime2>0)
{systime2--;}
else disp=0;
display(nmin,nDmin,nhr,nDhr,10,10);
}
else if(disp==6)
{
if(systime2>0)
{systime2--;}
else disp=0;
display(day,Dday,month,Dmonth,year,Dyear);
}
}
}
/*********************显示程序***********************/
void display(uchar we1,uchar we2,uchar we3,uchar we4,uchar we5,uchar we6) //显示程序
{
P0=table[we1]; //秒显
P2=0x10;
delay(5);
P0=table[we2]; //分显
P2=0x20;
delay(5);
P0=table2[we3]; //显
P2=0x40;
delay(5);
P0=table[we4]; //显
P2=0x08;
delay(5);
P0=table2[we5]; //显
P2=0x04;
delay(5);
P0=table[we6]; //显
P2=0x02;
delay(5);
}
/**************中断程序*******************/
void t1() interrupt 3
{
TH1=(65536-10000)/256; //装初值 求整
TL1=(65536-10000)%256; //求余
s0++; //位求反
}
/**************秒表计时程序************************************
void miaobiao()
{
if(s0==10)
{ s0=0;
s1++;
if(s1==10)
{ s1=0;
m0++;
if(m0==10)
{ m0=0;
m1++;
if(m1==6)
{ m1=0;
h0++;
if(h0==10)
{
h0=0;
h1++;
}
if(h1==6)
{
h1=0;
}
}
}
}
}
}
/*****************键盘扫描程序**********************************/
void scan()
{
if(menu==0) //menu这个按键只管显示模式,不同的disp值进入不同的显示模式
{
delay(10); //消斗
if(menu==0)
{ systime2=255;
disp++;
if(disp>4)
disp=0;
}
}
/**************key1按键在不同显示模式下有不同的作用********/
if(key1==0)
{
delay(10); //消斗
if(key1==0) //确认键按下的
{
if(disp==0)
{ systime2=255;
disp=5;
}
if(disp==1)
{
if((a5&0x0f)<9) // 月加1
{ a5=a5+1;
disable();
writeRTC(0x88,a5);
a5--;
}
if((a5&0x0f)>=9)
{ a5=a5+7;
disable();
writeRTC(0x88,a5);
}
if(a5==0x12)
{
a5=0;
disable();
writeRTC(0x88,a5);
}
}
if(disp==2)
{
nhr++;
if(nhr==10)
{ nhr=0;
nDhr++;
}
if(nDhr==2&nhr==4)
{nhr=0;nDhr=0;}
}
if(disp==3)
{
if((a3&0x0f)<9) // 小时加1
{ a3=a3+1;
disable();
writeRTC(0x84,a3);
a3--;
}
if((a3&0x0f)>=9)
{ a3=a3+7;
disable();
writeRTC(0x84,a3);
}
if(a3==0x23)
{
a3=0;
disable();
writeRTC(0x84,a3);
}
}
if(disp==4)
{
TR1=~TR1;
}
}
}
/**********key2按键在不同显示模式下有不同的作用***************/
if(key2==0)
{
delay(10); //消斗
if(key2==0) //确认键按下的
{
if(disp==0)
{ systime2=255;
disp=6;
}
if(disp==1)
{
if((a4