是基于AT89C52
单片机的
电子时钟设计,显示年月日 星期 时间 可设置闹钟,能显示温度!!!下边是程序,程序是别人给的,谁鉴定下呢,我是新手,不懂。。。。。
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
////////时钟
芯片控制接口///////////////////////
sbit sck=P1^0;
sbit io=P1^1;
sbit rst=P1^2;
///////1602液晶控制接口/////////////////
sbit rs=P2^6;
sbit en=P2^7;
////////按键////////////////////////////
sbit key1=P3^3;
sbit key2=P3^4;
sbit key3=P3^5;
sbit key4=P3^6;
///////////////////////////////////////
sbit beep=P2^0;//蜂鸣器接口
sbit DQ=P2^1;//温度传感器接口
uint wen_du;
//////////////////////////
uchar write
time[7];
//////////////时钟读写地址/////////////////////////////////
uchar writeadd[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar readadd[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar nian,zhou,yue,ri,shi,fen,miao;//时间暂存变量
uchar shi1,fen1;//闹钟暂存变量
uchar date_flag;//调整年月日时分秒
uchar al
ARM_flag;//调整闹钟
uchar num,cc;//闹钟提示音次数
uchar num1;//整点报时
void delay(uchar ms)//延时
{
uchar x,y;
for(x=ms;x>0;x--)
for(y=110;y>0;y--);
}
/***********ds18b20延迟子函数(晶振12MHz )*******/
void delay_18B20(uint i)
{
while(i--);
}
/**********ds18b20初始化函数**********************/
void Init_DS18B20()
{
uchar x=0;
DQ=1; //DQ复位
delay_18B20(8); //稍做延时
DQ=0; //单片机将DQ拉低
delay_18B20(80); //精确延时 大于 480us
DQ=1; //拉高总线
delay_18B20(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay_18B20(20);
}
/***********ds18b20读一个字节**************/
uchar ReadOneChar()
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay_18B20(4);
}
return(dat);
}
/*************ds18b20写一个字节****************/
void WriteOneChar(uchar dat)
{
uchar i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay_18B20(5);
DQ = 1;
dat>>=1;
}
}
/**************读取ds18b20当前温度************/
void deal_with()
{
uint temp1,temp2;
uchar a=0;
uchar b=0;
uchar t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay_18B20(100); // this message is wery important
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
delay_18B20(100);
a=ReadOneChar(); //读取温度值低位
b=ReadOneChar(); //读取温度值高位
temp1=b<<4;
temp1+=(a&0xf0)>>4;
temp2=a&0x0f;
wen_du=((b*256+a)>>4)*10; //当前采集温度值除16得实际温度值
}
////////////////////////////////////////////////////////
void ds1302_write_byte(uchar date)//ds1302写字节
{
uchar i;
for(i=0;i<8;i++)
{
sck=0;
io=date&0x01;
date=date>>1;
sck=1;
}
}
void ds1302_write(uchar add,uchar date)//向ds1302的地址里面写数据
{
rst=0;_nop_();
sck=0;_nop_();
rst=1;_nop_();
ds1302_write_byte(add);
ds1302_write_byte(date);
rst=0;_nop_();
sck=0;_nop_();
io=1;sck=1;
}
uchar ds1302_read(uchar add) //从ds1302的地址里面读出数据
{
uchar i,date;
rst=0;_nop_();
sck=0;_nop_();
rst=1;_nop_();
ds1302_write_byte(add);
for(i=0;i<8;i++)
{
date=date>>1;
sck=0;
if(io)
date=date|0x80;
sck=1;
}
rst=0;_nop_();
sck=0;_nop_();
io=1;sck=1;
return date;
}
void yj1602_write_com(uchar com) //液晶写地址
{
en=0;delay(1);
en=1;delay(1);
rs=0;delay(1);
P0=com;
en=0;delay(1);
}
void yj1602_write_date(uchar date) //液晶写数据
{
en=0;delay(1);
en=1;delay(1);
rs=1;delay(1);
P0=date;
en=0;delay(1);
}
void yj1602_init() //液晶初始化
{
yj1602_write_com(0x38);
yj1602_write_com(0x0c);
yj1602_write_com(0x06);
yj1602_write_com(0x01);
}
void write_1602(uchar add,uchar date)//时钟读取数据液晶显示转换
{
uchar shi,ge;
shi=date/16;
ge=date%16;
yj1602_write_com(0x80+add);
yj1602_write_date(0x30+shi); //0x30码制转换
yj1602_write_date(0x30+ge);
}
void write_alarm(uchar add,uchar date)//闹钟数据液晶显示转换
{
uchar shi,ge;
shi=date/10;
ge=date%10;
yj1602_write_com(0x80+add);
yj1602_write_date(0x30+shi); //0x30码制转换
yj1602_write_date(0x30+ge);
}
void write_1602_tem(uchar add,uint date)//环境温度数据液晶显示转换
{
uchar bai,shi,ge,dian;
bai=date/1000;
shi=date%1000/100;
ge=date%100/10;
dian=date%10;
yj1602_write_com(0x80+add);
if(bai==1)yj1602_write_date(0x30+bai);
yj1602_write_date(0x30+shi); //0x30码制转换
yj1602_write_date(0x30+ge);
yj1602_write_date('.');
yj1602_write_date(0x30+dian);
yj1602_write_date(0Xdf);
yj1602_write_date('C');
}
void display() //显示年月日星期时分秒 温度
{
yj1602_write_com(0x80);
yj1602_write_date('2');
yj1602_write_date('0');
writetime[0]=ds1302_read(readadd[0]);
write_1602(2,writetime[0]);
yj1602_write_date('-');
writetime[2]=ds1302_read(readadd[2]);
write_1602(5,writetime[2]);
yj1602_write_date('-');
writetime[3]=ds1302_read(readadd[3]);
write_1602(8,writetime[3]);
writetime[1]=ds1302_read(readadd[1]);
if(writetime[1]==1)
{
yj1602_write_com(0x80+12);
yj1602_write_date('M');
yj1602_write_date('O');
yj1602_write_date('N');
}
if(writetime[1]==2)
{
yj1602_write_com(0x80+12);
yj1602_write_date('T');
yj1602_write_date('U');
yj1602_write_date('E');
}
if(writetime[1]==3)
{
yj1602_write_com(0x80+12);
yj1602_write_date('W');
yj1602_write_date('E');
yj1602_write_date('D');
}
if(writetime[1]==4)
{
yj1602_write_com(0x80+12);
yj1602_write_date('T');
yj1602_write_date('H');
yj1602_write_date('U');
}
if(writetime[1]==5)
{
yj1602_write_com(0x80+12);
yj1602_write_date('F');
yj1602_write_date('R');
yj1602_write_date('I');
}
if(writetime[1]==6)
{
yj1602_write_com(0x80+12);
yj1602_write_date('S');
yj1602_write_date('A');
yj1602_write_date('T');
}
if(writetime[1]==7)
{
yj1602_write_com(0x80+12);
yj1602_write_date('S');
yj1602_write_date('U');
yj1602_write_date('N');
}
writetime[4]=ds1302_read(readadd[4]);
write_1602(0x41,writetime[4]);
yj1602_write_date(':');
writetime[5]=ds1302_read(readadd[5]);
write_1602(0x44,writetime[5]);
yj1602_write_date(':');
writetime[6]=ds1302_read(readadd[6]);
write_1602(0x47,writetime[6]);
write_1602_tem(0x4a,wen_du);
}
void set_display()//a设置闹钟时的显示函数
{
yj1602_write_com(0x80);
yj1602_write_date('S');
yj1602_write_date('e');
yj1602_write_date('t');
yj1602_write_date('-');
yj1602_write_date('a');
yj1602_write_date('l');
yj1602_write_date('a');
yj1602_write_date('r');
yj1602_write_date('m');
yj1602_write_date('-');
yj1602_write_date('c');
yj1602_write_date('l');
yj1602_write_date('o');
yj1602_write_date('c');
yj1602_write_date('k');
write_alarm(0x44,shi1);
yj1602_write_date(':');
write_alarm(0x47,fen1);
yj1602_write_date(':');
yj1602_write_date('0');
yj1602_write_date('0');
}
void tui_chu()//退出时间调整
{
if(key4==0)
{
delay(10);
if(key4==0)
{
date_flag=0;
yj1602_write_com(0x82);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x85);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x88);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x8c);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+0x41);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+0x44);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+0x47);
yj1602_write_date('*');
yj1602_write_date('*');
ds1302_write(0x8e,0x80);
}
while(!key4);
}
}
void didi() //滴滴声
{
beep=0;delay(500);
beep=1;delay(500);
}
void keyscan() //按键扫描控制
{
uchar j,k;
if(key1==0)
{
yj1602_init();
delay(10);
if(key1==0)
{ didi(); //滴滴声
display();
ds1302_write(0x8e,0x00);
date_flag=1;
yj1602_write_com(0x82);
yj1602_write_date('*');//调整年
yj1602_write_date('*');
nian=ds1302_read(readadd[0]);
j=nian/16;
k=nian%16;
nian=j*10+k;
}
while(!key1);
}
////////////////////////调整年///////////////////////////////////////
while(date_flag==1)
{
display();
tui_chu();//退出时间调整
if(key1==0)
{
delay(10);
if(key1==0)
{ didi();//滴滴声
date_flag=2;
yj1602_write_com(0x80+5);
yj1602_write_date('*');//调整月
yj1602_write_date('*');
yue=ds1302_read(readadd[2]);
j=yue/16;
k=yue%16;
yue=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{ didi();//滴滴声
nian++;
yj1602_write_com(0x82);
yj1602_write_date('*');
yj1602_write_date('*');
if(nian>=100) //年
nian=0;
j=nian/10;
k=nian%10;
writetime[0]=j*16+k;
ds1302_write(writeadd[0],writetime[0]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{ didi();//滴滴声
if(nian==0)
nian=100;
yj1602_write_com(0x82);
yj1602_write_date('*');
yj1602_write_date('*');
nian--;
j=nian/10;
k=nian%10;
writetime[0]=j*16+k;
ds1302_write(writeadd[0],writetime[0]);
}
while(!key3);
}
}
///////////// 调整月///////////////////////////////////////////////
while(date_flag==2)
{
display();
tui_chu();//退出时间调整
if(key1==0)
{
delay(10);
if(key1==0)
{ didi();//滴滴声
date_flag=3;
yj1602_write_com(0x88);
yj1602_write_date('*');//调整日
yj1602_write_date('*');
ri=ds1302_read(readadd[3]);
j=ri/16;
k=ri%16;
ri=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
didi();//滴滴声
yj1602_write_com(0x85);
yj1602_write_date('*');
yj1602_write_date('*');
yue++;
if(yue>=13)
yue=1;
j=yue/10;
k=yue%10;
writetime[2]=j*16+k;
ds1302_write(writeadd[2],writetime[2]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{ didi();//滴滴声
if(yue<=1)
yue=13;
yj1602_write_com(0x85);
yj1602_write_date('*');
yj1602_write_date('*');
yue--;
j=yue/10;
k=yue%10;
writetime[2]=j*16+k;
ds1302_write(writeadd[2],writetime[2]);
}
while(!key3);
}
}
///////////// 调整日///////////////////////////////////////////////
while(date_flag==3)
{
display();
tui_chu();//退出时间调整
if(key1==0)
{
delay(10);
if(key1==0)
{ didi();//滴滴声
date_flag=4;
yj1602_write_com(0x80+12);
yj1602_write_date('*');//调整星期
yj1602_write_date('*');
yj1602_write_date('*');
shi=ds1302_read(readadd[1]);
j=zhou/16;
k=zhou%16;
zhou=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
didi();//滴滴声
yj1602_write_com(0x88);
yj1602_write_date('*');
yj1602_write_date('*');
ri++;
if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12))
{
if(ri==32)
ri=1;
}
if(yue==2)
{
if(nian%4==0)
{
if(ri==30)
ri=1;
}
else
{
if(ri==29)
ri=1;
}
}
if((yue==4)||(yue==6)||(yue==9)||(yue==11))
{
if(ri==31)
ri=1;
}
j=ri/10;
k=ri%10;
writetime[3]=j*16+k;
ds1302_write(writeadd[3],writetime[3]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{ didi();//滴滴声
if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12))
{
if(ri==1)
ri=32;
}
if(yue==2)
{
if(nian%4==0)
{
if(ri==1)
ri=30;
}
else
{
if(ri==1)
ri=29;
}
}
if((yue==4)||(yue==6)||(yue==9)||(yue==11))
{
if(ri==1)
ri=31;
}
yj1602_write_com(0x88);
yj1602_write_date('*');
yj1602_write_date('*');
ri--;
j=ri/10;
k=ri%10;
writetime[3]=j*16+k;
ds1302_write(writeadd[3],writetime[3]);
}
while(!key3);
}
}
///////////////调整周//////////////////////////////////////////////////////
while(date_flag==4)
{
display();
tui_chu();//退出时间调整
if(key1==0)
{
delay(10);
if(key1==0)
{ didi();//滴滴声
date_flag=5;
yj1602_write_com(0x80+0x40+1);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
shi=ds1302_read(readadd[4]);
j=shi/16;
k=shi%16;
shi=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{ didi();//滴滴声
yj1602_write_com(0x80+12);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_date('*');
zhou++;
if(zhou==8)zhou=1;
j=zhou/10;
k=zhou%10;
writetime[1]=j*16+k;
ds1302_write(writeadd[1],writetime[1]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{ didi();//滴滴声
if(shi==0)zhou=7;
yj1602_write_com(0x80+12);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_date('*');
zhou--;
if(zhou==0)zhou=7;
j=zhou/10;
k=zhou%10;
writetime[1]=j*16+k;
ds1302_write(writeadd[1],writetime[1]);
}
while(!key3);
}
}
///////////// 调整时///////////////////////////////////////////////
while(date_flag==5)
{
display();
tui_chu();//退出时间调整
if(key1==0)
{
delay(10);
if(key1==0)
{ didi();//滴滴声
date_flag=6;
yj1602_write_com(0x80+0x44);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
fen=ds1302_read(readadd[5]);
j=fen/16;
k=fen%16;
fen=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{ didi();//滴滴声
yj1602_write_com(0x80+0x41);
yj1602_write_date('*');
yj1602_write_date('*');
shi++;
if(shi==24)shi=0;
j=shi/10;
k=shi%10;
writetime[4]=j*16+k;
ds1302_write(writeadd[4],writetime[4]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{ didi();//滴滴声
if(shi==0)shi=24;
yj1602_write_com(0x80+0x41);
yj1602_write_date('*');
yj1602_write_date('*');
shi--;
j=shi/10;
k=shi%10;
writetime[4]=j*16+k;
ds1302_write(writeadd[4],writetime[4]);
}
while(!key3);
}
}
///////////// 调整分///////////////////////////////////////////////
while(date_flag==6)
{
display();
tui_chu();//退出时间调整
if(key1==0)
{
delay(10);
if(key1==0)
{ didi();//滴滴声
date_flag=7;
yj1602_write_com(0x80+0x47);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
miao=ds1302_read(readadd[6]);
j=miao/16;
k=miao%16;
miao=j*10+k;
}
while(!key1);yj1602_init();
}
一周热门 更多>