数字时钟资料.docx
《数字时钟资料.docx》由会员分享,可在线阅读,更多相关《数字时钟资料.docx(23页珍藏版)》请在冰豆网上搜索。
![数字时钟资料.docx](https://file1.bdocx.com/fileroot1/2023-6/12/20a06ffb-234c-4b29-9984-750be68b89b2/20a06ffb-234c-4b29-9984-750be68b89b21.gif)
数字时钟资料
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitdula=P2^6;
sbitwela=P2^7;
sbitrs=P3^5;
sbitlcden=P3^4;
sbits1=P1^0;//功能键
sbits2=P1^1;//增加键
sbits3=P1^2;//减小键
sbits4=P1^3;//闹钟查看键
sbitrd=P3^7;
sbitbeep=P2^3;//蜂鸣器
sbitdscs=P1^4;
sbitdsas=P1^5;
sbitdsrw=P1^6;
sbitdsds=P1^7;
sbitdsirq=P3^3;
sbitLCD_RW=P2^0;
bitflag1,flag_ri;//定义两个位变量
uchars1num,flag;//其它变量定义
charmiao,shi,fen,year,month,day,week,amiao,afen,ashi;
ucharcodetable[]="20--";//液晶固定显示内容
ucharcodetable1[]=":
:
";
voidwrite_ds(uchar,uchar);//函数申明
voidset_alarm(uchar,uchar,uchar);voidread_alarm();
ucharread_ds(uchar);voidset_time();
sbitDQ=P2^2;//定义DS18B20端口DQ
bitpresence;
ucharcodecdis2[]={".C"};
unsignedchardatatemp_data[2]={0x00,0x00};
unsignedchardatadisplay[5]={0x00,0x00,0x00,0x00,0x00};
unsignedcharcodeditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,
0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
unsignedcharcodemytab[8]={0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};
#definedelayNOP();{_nop_();_nop_();_nop_();_nop_();};
voidwrite_com(uchar);
voidwrite_date(uchar);
/*******************************************************************/
voiddelay(uintz)//延时函数
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidyanshi(uintj)
{
j--;
}
/*自定义字符写入CGRAM*/
/*******************************************************************/
voidwritetab()
{
unsignedchari;
write_com(0x40);//写CGRAM
for(i=0;i<8;i++)
write_date(mytab[i]);
}
/*us级延时函数*/
Init_DS18B20(void)
{
DQ=1;//DQ复位
yanshi(8);//稍做延时
DQ=0;//单片机将DQ拉低
yanshi(90);//精确延时大于480us
DQ=1;//拉高总线
yanshi(8);
presence=DQ;//如果=0则初始化成功=1则初始化失败
yanshi(100);
DQ=1;
return(presence);//返回信号,0=presence,1=nopresence
}
/*读一个字节*/
/*******************************************************************/
ReadOneChar(void)
{
unsignedchari=0;
unsignedchardat=0;
for(i=8;i>0;i--)
{
DQ=0;//给脉冲信号
dat>>=1;
DQ=1;//给脉冲信号
if(DQ)
dat|=0x80;
yanshi(4);
}
return(dat);
}
/*写一个字节*/
/*******************************************************************/
WriteOneChar(unsignedchardat)
{
unsignedchari=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
yanshi(5);
DQ=1;
dat>>=1;
}
}
/*读取温度*/
/*******************************************************************/
Read_Temperature(void)
{
Init_DS18B20();
WriteOneChar(0xCC);//跳过读序号列号的操作
WriteOneChar(0x44);//启动温度转换
Init_DS18B20();
WriteOneChar(0xCC);//跳过读序号列号的操作
WriteOneChar(0xBE);//读取温度寄存器
temp_data[0]=ReadOneChar();//温度低8位
temp_data[1]=ReadOneChar();//温度高8位
}
/*数据转换与温度显示*/
/*******************************************************************/
Disp_Temperature()
{
display[4]=temp_data[0]&0x0f;
display[0]=ditab[display[4]]+0x30;//查表得小数位的值
display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4);
display[3]=display[4]/100+0x30;
display[1]=display[4]%100;
display[2]=display[1]/10+0x30;
display[1]=display[1]%10+0x30;
if(display[3]==0x30)//高位为0,不显示
{
display[3]=0x20;
if(display[2]==0x30)//次高位为0,不显示
display[2]=0x20;
}
write_com(0x80+0x4c);
write_date(display[3]);
write_com(0x80+0x4d);
write_date(display[2]);
write_com(0x80+0x4e);
write_date(display[1]);
write_com(0x80+0x4f);
write_date(display[0]);
}
voiddi()//蜂鸣器报警声音
{
beep=0;
delay(100);
beep=1;
}
bitlcd_busy()
{
bitresult;
rs=0;
LCD_RW=1;
lcden=1;
delayNOP();
result=(bit)(P0&0x80);
lcden=0;
return(result);
}
voidwrite_com(ucharcom)
{//写液晶命令函数
while(lcd_busy());
LCD_RW=0;
rs=0;
lcden=0;
P0=com;
delay
(2);
lcden=1;
delay
(2);
lcden=0;
}
voidwrite_date(uchardate)
{//写液晶数据函数
while(lcd_busy());
LCD_RW=0;
rs=1;
lcden=0;
P0=date;
delay
(2);
lcden=1;
delay
(2);
lcden=0;
}
voidinit()
{//初始化函数
ucharnum;
EA=1;//打开总中断
EX1=1;//开外部中断1
IT1=1;//设置负跳变沿触发中断
flag1=0;//变量初始化
flag=0;
s1num=0;
week=1;
lcden=0;
//以下几行在首次设置DS12C887时使用,以后不必再写入
write_ds(0x0A,0x20);//打开振荡器
write_ds(0x0B,0x24);//设置12小时模式,数据二进制格式
//开启闹铃中断
set_time();//设置上电默认时间
write_com(0x38);//1602液晶初始化
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)//写入液晶固定部分显示
{
write_date(table[num]);
delay
(1);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table1[num]);
delay
(1);
}
}
voidwrite_sfm(ucharadd,chardate)
{//1602液晶刷新时分秒函数4为时,7为分,10为秒
charshi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
voidwrite_nyr(ucharadd,chardate)
{//1602液晶刷新年月日函数3为年,6为分,9为秒
charshi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
voidwrite_week(charweek)
{//写液晶星期显示函数
write_com(0x80+12);
switch(week)
{
case1:
write_date('M');delay(5);
write_date('O');delay(5);
write_date('N');
break;
case2:
write_date('T');delay(5);
write_date('U');delay(5);
write_date('E');
break;
case3:
write_date('W');delay(5);
write_date('E');delay(5);
write_date('D');
break;
case4:
write_date('T');delay(5);
write_date('H');delay(5);
write_date('U');
break;
case5:
write_date('F');delay(5);
write_date('R');delay(5);
write_date('I');
break;
case6:
write_date('S');delay(5);
write_date('A');delay(5);
write_date('T');
break;
case7:
write_date('S');delay(5);
write_date('U');delay(5);
write_date('N');
break;
}
}
voidkeyscan()
{
if(flag_ri==1)
{//这里用来取消闹钟报警,任一键取消报警
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
delay(5);
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
while(!
(s1&&s2&&s3&&s4))di();
flag_ri=0;//清除报警标志
}
}
}
if(s1==0)//检测S1
{
delay(5);
if(s1==0)
{
s1num++;//记录按下次数
if(flag1==1)
if(s1num==4)
s1num=1;
flag=1;
while(!
s1);di();
switch(s1num)
{//光标闪烁点定位
case1:
write_com(0x80+0x40+10);
write_com(0x0f);
break;
case2:
write_com(0x80+0x40+7);
break;
case3:
write_com(0x80+0x40+4);
break;
case4:
write_com(0x80+12);
break;
case5:
write_com(0x80+9);
break;
case6:
write_com(0x80+6);
break;
case7:
write_com(0x80+3);
break;
case8:
s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,week);
write_ds(7,day);
write_ds(8,month);
write_ds(9,year);
break;
}
}
}
if(s1num!
=0)//只有当S1按下后,才检测S2和S3
{
if(s2==0)
{
delay
(1);
if(s2==0)
{
while(!
s2);di();
switch(s1num)
{//根据功能键次数调节相应数值
case1:
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
break;
case2:
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
break;
case3:
shi++;
if(shi==12)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
break;
case4:
week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+12);
break;
case5:
day++;
if(day==32)
day=1;
write_nyr(9,day);
write_com(0x80+9);
break;
case6:
month++;
if(month==13)
month=1;
write_nyr(6,month);
write_com(0x80+6);
break;
case7:
year++;
if(year==100)
year=0;
write_nyr(3,year);
write_com(0x80+3);
break;
}
}
}
if(s3==0)
{
delay
(1);
if(s3==0)
{
while(!
s3);di();
switch(s1num)
{//根据功能键次数调节相应数值
case1:
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
break;
case2:
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
break;
case3:
shi--;
if(shi==-1)
shi=11;
write_sfm(4,shi);
write_com(0x80+0x40+4);
break;
case4:
week--;
if(week==0)
week=7;
write_week(week);
write_com(0x80+12);
break;
case5:
day--;
if(day==0)
day=31;
write_nyr(9,day);
write_com(0x80+9);
break;
case6:
month--;
if(month==0)
month=12;
write_nyr(6,month);
write_com(0x80+6);
break;
case7:
year--;
if(year==-1)
year=99;
write_nyr(3,year);
write_com(0x80+3);
break;
}
}
}
}
if(s4==0)//检测S4
{
delay(5);
if(s4==0)
{
flag1=~flag1;
while(!
s4);di();
if(flag1==0)
{//退出闹钟设置时保存数值
flag=0;
write_com(0x80+0x40);
write_date('');
write_date('');
write_com(0x0c);
write_ds(1,miao);
write_ds(3,fen);
write_ds(5,shi);
}
else
{//进入闹钟设置
read_alarm();//读取原始数据
miao=amiao;//重新赋值用以按键调节
fen=afen;
shi=ashi;
write_com(0x80+0x40);
write_date('R');//显示标志
write_date('i');
write_com(0x80+0x40+3);
write_sfm(4,ashi);//送液晶显示闹钟时间
write_sfm(7,afen);
write_sfm(10,amiao);
}
}
}
}
voidwrite_ds(ucharadd,uchardate)
{//写12C887函数
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;//先写地址
dsas=0;
dsrw=0;
P0=date;//再写数据
dsrw=1;
dsas=1;
dscs=1;
}
ucharread_ds(ucharadd)
{//读12C887函数
uchards_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;