#include<reg51.h> //头文件
#define uchar unsigned char //数据格式宏定义
#define uint unsigned int //数据格式宏定义
sbit rs=P3^5; //lcd寄存器选择
sbit lcden=P3^4; //lcd使能端
sbit s1=P1^0; //开关s1
sbit s2=P1^1; //开关s2
sbit s3=P1^2; //开关s3
sbit beep=P1^3; //蜂鸣器
sbit dscs=P1^4; //ds12c887片选
sbit dsas=P1^5; // ds12c887地址选通输入脚
sbit dsrw=P1^6; // ds12c887读/写输入端
sbit dsds=P1^7; // ds12c887数据选择或读输入脚
sbit dsirq=P3^3; // ds12c887中断请求输入
uchar count,s1num,flag,flag1; //状态变量
char miao,shi,fen,nian,yue,ri,xingqi,amiao,afen,ashi; //数据变量
uchar code table[]=" 20 - - "; //年月日显示格式
uchar code table1[]=" : : "; //时间显示格式
uchar code table2[]="MONTHUWENTHRFRISTASUN"; //星期表,每3位为一个
uchar code table3[]=" ALARM ON "; //开闹钟提示
uchar code table4[]=" ALARM OFF "; //关闹钟提示
uchar code table5[]="SET ALARM PUSH "; //闹钟状态选择提示
void write_ds(uchar,uchar); //函数声明
void set_alarm(uchar,uchar,uchar);
uchar read_ds(uchar);
void delay(uint z) //延时子函数
{ uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void beezzer() //蜂鸣器发声控制函数(闹钟响铃),beep=0发声
{
beep=0;
delay(50);
beep=1;
delay(100);
beep=0;
delay(50);
beep=1;
}
void write_com(uchar com) //向lcd写入控制字com,根据lcd1602时序图进行操作,rs为寄存器选择
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //向lcd写入数据date,根据lcd1602时序图进行操作
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init() //初始化单片机
{
uchar num;
EA=1; //开总中断
EX1=1; //允许外部中断1
IT1=1; //低电平触发
beep=1;
flag1=0;
lcden=0;
write_ds(0x0A,0x20); //向ds12c887控制寄存器A发送初始化控制字,启动振荡器
write_ds(0x0B,0x26); //向ds12c887控制寄存器B发送初始化控制字,设定工作模式bcd,24小时制
read_ds(0x0c); //读芯片时间数据
write_com(0x38);
write_com(0x0c); //初始化lcd
write_com(0x06);
write_com(0x01);
write_com(0x80); //在第一行显示数据
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40); //在第二行显示数据
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
}
void write_sfm(uchar add,uchar date) //向lcd写入时分秒数据
{
uchar shi,ge; //shi:待发数据十位;ge:待发数据个位
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_nyr(uchar add,uchar date) //向lcd写入年月日数据
{
uchar shi,ge; //shi:待发数据十位;ge:待发数据个位
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_xingqi(uchar add,uchar date) //向lcd写入星期数据
{
write_com(0x80+add);
date=(date-1)*3; //如星期一读表123个字母MON显示,星期二读456字母THU等等
write_date(table2[date]);
write_date(table2[++date]);
write_date(table2[++date]);
}
void keyscan() //键盘扫描子程序
{
if(flag1==1)
{
if(s2==0) //“加”键
{
delay(5);
if(s2==0)
{
while(!s2);
flag1=0;
}
}
if(s3==0) //“减”键
{
delay(5);
if(s3==0)
{
while(!s3);
flag1=0;
}
}
}
if(s1==0) //功能选择键,统计按下次数在s1num中
{
delay(5);
if(s1==0)
{
s1num++;
flag=1;
flag1=0;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
}
if(s1num==2) //以下if语句是使光标在对应位置闪烁
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
write_com(0x80+14);
}
if(s1num==5)
{
write_com(0x80+10);
}
if(s1num==6)
{
write_com(0x80+7);
}
if(s1num==7)
{
write_com(0x80+4);
}
if(s1num==8) //此判断语句显示table5表内容,闹钟状态显示
{
uchar n;
write_com(0x80);
for(n=0;n<15;n++)
{
write_date(table5[n]);
}
write_com(0x80+14);
}
if(s1num==9)
{
write_com(0x80+0x40+11);
}
if(s1num==10)
{
write_com(0x80+0x40+8);
}
if(s1num==11)
{
write_com(0x80+0x40+5);
}
if(s1num==12) //执行时间写入ds12c887芯片操作
{
uchar num;
s1num=0;
write_com(0x0c); //设置控制寄存器,状态设置为写
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,xingqi);
write_ds(7,ri);
write_ds(8,yue);
write_ds(9,nian);
set_alarm(ashi,afen,amiao);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
}
}
if(s1num!=0) //根据s1num按下次数执行相应的数据设置
{
if(s2==0) //当s2按下时,如果
{
delay(1);
if(s2==0)
{
while(!s2);
if(s1num==1) //按s1按键1次改秒
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2) //按s1按键2次改分
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3) //按s1按键3次改时
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4) //按s1按键4次改星期
{
xingqi++;
if(xingqi==8)
xingqi=1;
write_xingqi(12,xingqi);
write_com(0x80+14);
}
if(s1num==5) //按s1按键5次改日期
{
ri++;
if(ri==32)
ri=1;
write_nyr(9,ri);
write_com(0x80+10);
}
if(s1num==6) //按s1按键6次改月
{
yue++;
if(yue==13)
yue=1;
write_nyr(6,yue);
write_com(0x80+7);
}
if(s1num==7) //按s1按键7次改年
{
nian++;
if(nian==100)
nian=0;
write_nyr(3,nian);
write_com(0x80+4);
}
if(s1num==8) //按s1按键8次改闹钟状态开还是关
{
uchar n;
write_ds(0x0B,0x26); //向B控制寄存器写入控制字
write_com(0x80);
for(n=0;n<15;n++)
{
write_date(table3[n]);
}
write_com(0x80+14);
}
if(s1num==9) //按s1按键9次改闹钟时间秒
{
amiao++;
if(amiao==60)
amiao=0;
write_sfm(10,amiao);
write_com(0x80+0x40+11);
}
if(s1num==10) //按s1按键10次改闹钟时间分
{
afen++;
if(afen==60)
afen=0;
write_sfm(7,afen);
write_com(0x80+0x40+8);
}
if(s1num==11) //按s1按键11次改闹钟时间时
{
ashi++;
if(ashi==24)
ashi=0;
write_sfm(4,ashi);
write_com(0x80+0x40+5);
}
}
}
if(s3==0)
{
delay(1);
if(s3==0) //当s3按下时,如果:
{
while(!s3);
if(s1num==1) //按s1按键1次改秒
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2) //按s1按键2次改分
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3) //按s1按键3次改时
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4) //按s1按键4次改星期
{
xingqi--;
if(xingqi==0)
xingqi=7;
write_xingqi(12,xingqi);
write_com(0x80+14);
}
if(s1num==5) //按s1按键5次改日期
{
ri--;
if(ri==0)
ri=31;
write_nyr(9,ri);
write_com(0x80+10);
}
if(s1num==6) //按s1按键6次改月
{
yue--;
if(yue==0)
yue=12;
write_nyr(6,yue);
write_com(0x80+7);
}
if(s1num==7) //按s1按键7次改年
{
nian--;
if(nian==-1)
nian=99;
write_nyr(3,nian);
write_com(0x80+4);
}
if(s1num==8) //按s1按键8次改闹钟状态
{
uchar n;
write_ds(0x0B,0x06);
write_com(0x80);
for(n=0;n<15;n++)
{
write_date(table4[n]);
}
write_com(0x80+14);
}
if(s1num==9) //按s1按键9次改闹钟秒
{
amiao--;
if(amiao==-1)
amiao=59;
write_sfm(10,amiao);
write_com(0x80+0x40+11);
}
if(s1num==10) //按s1按键10次改闹钟分
{
afen--;
if(afen==-1)
afen=59;
write_sfm(7,afen);
write_com(0x80+0x40+8);
}
if(s1num==11) //按s1按键11次改闹钟时
{
ashi--;
if(ashi==-1)
ashi=23;
write_sfm(4,ashi);
write_com(0x80+0x40+5);
}
}
}
}
}
void write_ds(uchar add,uchar date) //向DS12c887写入数据子函数
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add; //写地址
dsas=0;
dsrw=0;
P0=date; //写数据
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add) //读DS12C887芯片数据,add为寄存器地址,详见表格
{
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;
dsas=0;
dsds=0;
// P0=0xff;
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void set_alarm(uchar ashi,uchar afen,uchar amiao) //向ds12c887写入设置闹钟定时时间子函数
{
write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);
}
void main() //主函数
{
init(); //初始化函数
while(1)
{
keyscan();
if(flag1==1)
beezzer();
if(flag==0&flag1==0)
{
amiao=read_ds(0);
afen=read_ds(2);
ashi=read_ds(4);
miao=read_ds(0);
fen=read_ds(2);
shi=read_ds(4);
xingqi=read_ds(6);
ri=read_ds(7);
yue=read_ds(8);
nian=read_ds(9);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
write_xingqi(12,xingqi);
write_nyr(9,ri);
write_nyr(6,yue);
write_nyr(3,nian);
}
}
}
void exter() interrupt 2 //外部中断1响应处理函数(改变flag值使蜂鸣器发声)
{ uchar c;
flag1=1;
c=read_ds(0x0c);
}
|