① 单片机和lcd1602编写万年历C语言程序,求高手啊
下面是我的程序,还有仿真图,不懂可以再问我,祝你成功
/*******************************************
ds1302计时+lcd1602显示
*******************************************/
#include<reg51.h>
#include<intrins.h>
#defineucharunsignedchar
#defineuintunsignedint
sbitrs=P2^0;
sbitrw=P2^1;
sbite=P2^2;
sbitT_RST=P1^5;
sbitT_CLK=P1^6;
sbitT_IO=P1^7;
sbitACC0=ACC^0;
sbitACC7=ACC^7;
/*******************************************
向1302写一个字节
*******************************************/
voidinput_BYTE(uchardat)
{
uchari;
ACC=dat;
for(i=8;i>0;i--)
{
T_IO=ACC0;
T_CLK=1;
T_CLK=0;
ACC=(ACC>>1);
}
}
/*******************************************
1302读出一个字节
*******************************************/
ucharoutput_BYTE()
{
uchari;
for(i=8;i>0;i--)
{
ACC=(ACC>>1);
ACC7=T_IO;
T_CLK=1;
T_CLK=0;
}
return(ACC);
}
/*******************************************
写数据
*******************************************/
voidwrite_1302(ucharadd,uchardat)
{
T_RST=0;
T_CLK=0;
T_RST=1;
input_BYTE(add);
input_BYTE(dat);
T_CLK=1;
T_RST=0;
}
/*******************************************
读数据
*******************************************/
ucharread_1302(ucharadd)
{
ucharinf;
T_RST=0;
T_CLK=0;
T_RST=1;
input_BYTE(add);
inf=output_BYTE();
T_CLK=1;
T_RST=0;
return(inf);
}
voidinit_1302()
{
write_1302(0x8e,0x00);//关闭写保护;
/*write_1302(0x90,0xaa);//设置充电方式;
write_1302(0x80,0x00);//秒寄存器初始化;
write_1302(0x82,0x46);//分.......
write_1302(0x84,0x22);//时.......
write_1302(0x86,0x17);//日........
write_1302(0x88,0x03);//月.......
write_1302(0x8a,0x04);//星期...
write_1302(0x8c,0x11);//年......*/
write_1302(0x8e,0x80);//打开写保护;
}
ucharcom,inf;
ucharcodetable[]="DS1302";
ucharcodetable1[]="0123456789";
ucharcodetable2[]="WEEK-";
voiddelay1(uchart)
{
ucharx,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
/*******************************************
给1602写指令
*******************************************/
voidwrite_com(ucharcom)
{
rs=0;
P0=com;
delay1(5);
e=1;
delay1(5);
e=0;
}
/*******************************************
给1602写数据
*******************************************/
voidwrite_date(uchardate)
{
rs=1;
P0=date;
delay1(5);
e=1;
delay1(5);
e=0;
}
/*******************************************
1602初始化
*******************************************/
voinit()
{
e=0;
rw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
/*******************************************
主函数
*******************************************/
voidmain()
{
uchari;
uchart_sec,sec1,sec2;
uchart_min,min1,min2;
uchart_hour,hour1,hour2;
uchart_mon,mon1,mon2;
uchart_day,day1,day2;
uchart_year,year1,year2;
ucharweek,week1;
ucharnum;
unit();
init_1302();
while(1)
{
t_sec=read_1302(0x81);//miao;
sec1=t_sec&0x0f;
sec2=(t_sec>>4);
t_min=read_1302(0x83);//fen;
min1=t_min&0x0f;
min2=(t_min>>4);
t_hour=read_1302(0x85);//shi;
hour1=t_hour&0x0f;
hour2=(t_hour>>4);
t_day=read_1302(0x87);//ri;
day1=t_day&0x0f;
day2=(t_day>>4);
t_mon=read_1302(0x89);//yue;
mon1=t_mon&0x0f;
mon2=(t_mon>>4);
week=read_1302(0x8b);//xingqi;
week1=week&0x0f;
t_year=read_1302(0x8d);//nian;
year1=t_year&0x0f;
year2=(t_year>>4);
write_com(0x80);
for(i=0;i<6;i++)
{
write_date(table[i]);
}
write_com(0x80+0x40);
for(i=0;i<5;i++)
{
write_date(table2[i]);
}
write_com(0x80+0x07);
write_date(table1[year2]);
write_com(0x80+0x08);
write_date(table1[year1]);
write_com(0x80+0x09);
write_date('/');
write_com(0x80+0x0a);
write_date(table1[mon2]);
write_com(0x80+0x0b);
write_date(table1[mon1]);
write_com(0x80+0x0c);
write_date('/');
write_com(0x80+0x0d);
write_date(table1[day2]);
write_com(0x80+0x0e);
write_date(table1[day1]);
write_com(0x80+0x40+0x05);
write_date(table1[week1]);
write_com(0x80+0x40+0x07);
write_date(table1[hour2]);
write_com(0x80+0x40+0x08);
write_date(table1[hour1]);
write_com(0x80+0x40+0x09);
write_date(':');
write_com(0x80+0x40+0x0a);
write_date(table1[min2]);
write_com(0x80+0x40+0x0b);
write_date(table1[min1]);
write_com(0x80+0x40+0x0c);
write_date(':');
write_com(0x80+0x40+0x0d);
write_date(table1[sec2]);
write_com(0x80+0x40+0x0e);
write_date(table1[sec1]);
}
}
② 单片机万年历的c语言程序
没分啊 那也给你发一个吧
DS1302+DS18B20 用1602显示
我在PROTUES仿真上做的 你可以根据程序画电路图 很简单
程序 采用模块化方式
/*初始化*/
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]=" 20 - - " ;
uchar code table[]=" : : ";
uchar code ttable[]={"Now temperature:"} ;
sbit RS=P3^0;
sbit E=P3^1;
sbit button1=P1^0;
sbit button2=P1^3;
sbit button3=P1^6;
sbit RST=P2^5;
sbit SCLK=P2^6;
sbit IO=P2^7;
sbit ACC7=ACC^7;
sbit DQ=P3^6;
char hour,minute,second,n,k,week,day,month,year1=20 ,year,nn,flag;
uchar key=0;
uint tem,a;
void delay(uint x)
{
while(x--);
}
void write_zl(uchar zl)
{E=0;
RS=0;
P0=zl;
E=1;
delay(500);
E=0;
}
void write_sj(uchar sj)
{E=0;
RS=1;
P0=sj;
E=1;
delay(500);
E=0;
}
void init_lcd()
{
write_zl(0x38);//开显示
write_zl(0x01);//清屏
write_zl(0x06);//整屏不移动
write_zl(0x0C);//不开光标
}
/*18B20*/部分
void init_18b20()
{ DQ=1;
delay(8); //延时
DQ=0;
delay(80);
DQ=1;
delay(20);
}
void write_dat(uchar dat)
{ uchar i,k;
k=dat;
for(i=0;i<8;i++)
{ DQ=0;
DQ=k&0x01;//从低位开始
delay(5);
DQ=1;
k>>=1;
}
delay(4);
}
int read_sj()
{
uchar j,date;
for(j=0;j<8;j++)
{
DQ=1;
delay(1);
DQ=0;
date=date>>1;
DQ=1;
if(DQ==1) //
date|=0x80;
delay(4);}
return(date);
}
void temperaturechange()
{ uint tempH,tempL;
init_18b20();
write_dat(0xcc);//只对一个操作就跳过读序列号操作
write_dat(0x44); //启动温度转换
delay(60000);
delay(33750);//需要93.75ms
init_18b20();
write_dat(0xcc);
write_dat(0xbe);//读温度寄存器
tempL=read_sj();//读取温度低位LSB
tempH=read_sj();//读取温度高位MSB
if(tempH&0x80) //高5位是1为负数
{ a=1;
tem=tempH;
tem<<=8; //共16位
tem=tem|tempL; //合并高低位数
tem=~tem+1;
tem=tem*(0.0625);
}
else
{
a=0;
tem=((tempH*256)+tempL)*0.0625;//转换实际温度
}
}
void write_temperature(uint temp) //被调用的函数必须在前
{ uint ,shi,ge;
=temp/100;
shi=temp%100/10;
ge=temp%10;
if(a==1)
{ if(shi>0)
{write_zl(0x80+0x50+3);
write_sj('-');
write_zl(0x80+0x50+4);
write_sj(0x30+shi); }
else
{
write_zl(0x80+0x50+3);
write_sj(0x00);
write_zl(0x80+0x50+4);
write_sj('-');}
}
else
{
if(>0) //若显示shi肯定显示
{
write_zl(0x80+0x50+3);
write_sj(0x30+);
write_zl(0x80+0x50+4);
write_sj(0x30+shi);
}
else
{
if(shi>0) //否则判断shi是否显示
{
write_zl(0x80+0x50+4);
write_sj(0x30+shi);
}
else
{
write_zl(0x80+0x50+4);
write_sj(0x00);
}
write_zl(0x80+0x50+3);
write_sj(0x00);
}
}
write_zl(0x80+0x50+5); //个位公用总的显示
write_sj(0x30+ge);
}
/*DS1302部分*/
void DS1302_write(uchar shju)
{ uchar s,j;
s=shju;
for(j=0;j<8;j++)
{ IO=s&0x01;
SCLK=0;
SCLK=1;
s=s>>1;
}}
uchar DS1302_readbyte()
{ uchar i,R=0;
SCLK=0;
delay(10);
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=IO;
SCLK=1;
SCLK=0;
}
return(ACC);
/*{
R=R>>1;
if(IO==1)
R=R|0x80;
SCLK=1;
SCLK=0;
}
return(R); */}
void DS1302write(uchar dz,uchar shij)//初始化1302
{ RST=1;
SCLK=0;
DS1302_write(dz);
DS1302_write(shij);
SCLK=1;
RST=0;
}
void start1302()
{ uchar num;
for(num=0;num<13;num++)
{ write_sj(tab[num]);}
for(num=13;num<16;num++)
{ write_sj(tab[num]);}
write_zl(0x80+0x40);
for(num=0;num<12;num++)
{ write_sj(table[num]);
}}
void write_hms(uchar add,uchar sj) //被调用的函数必须在前
{ uchar shi,ge;
shi=sj/10;
ge=sj%10;
write_zl(0x80+0x40+add);
write_sj(0x30+shi);
write_sj(0x30+ge); }
void write_nyr(uchar add,uchar sj)
{ uchar shi,ge;
shi=(sj/10);
ge=(sj%10);
write_zl(0x80+add);
write_sj(0x30+shi);
write_sj(0x30+ge); }
void write_XQ(uchar wk)
{ write_zl(0x80+13);
switch(wk)
{ case 2: write_sj('M'); write_sj('O');write_sj('N'); break;
case 3: write_sj('T'); write_sj('U');write_sj('E'); break;
case 4: write_sj('W'); write_sj('E');write_sj('D'); break;
case 5: write_sj('T'); write_sj('H');write_sj('U'); break;
case 6: write_sj('F'); write_sj('R');write_sj('I'); break;
case 7: write_sj('S'); write_sj('A');write_sj('T'); break;
case 1: write_sj('S'); write_sj('U');write_sj('N'); break;
}
}
void write_day(uchar rue)
{
switch(rue)
{
case 1: nn=31;break;
case 2: if((year%4==0&&year%100!=0)||year%400==0)
nn=29;
else
nn=28;break;
case 3: nn=31;break;
case 4: nn=30;break;
case 5: nn=31;break;
case 6: nn=30;break;
case 7: nn=31;break;
case 8: nn=31;break;
case 9: nn=30;break;
case 10: nn=31;break;
case 11: nn=30;break;
case 12: nn=31;break;
}}
void keyboard()
{
if(button1==0)
{ delay(5);
if(button1==0)
{ while(button1==0);
key++; flag=1;
switch(key)
{ case 1: write_zl(0x80+0x40+10);
write_zl(0x0f); break;//shan shuo
case 2: write_zl(0x80+0x40+7); break;
case 3: write_zl(0x80+0x40+4); break;
case 4: write_zl(0x80+10); break;
case 5: write_zl(0x80+7); break;
case 6: write_zl(0x80+4); break;
case 7: write_zl(0x80+2); break;
case 8: key=0;
write_zl(0x0c);
flag=0;
DS1302write(0x80,second);
DS1302write(0x82,minute);
DS1302write(0x84,hour);
DS1302write(0x86,day);
DS1302write(0x88,month);
DS1302write(0x8A,week);
DS1302write(0x8C,year);break;
}
}}
if(button2==0)
{ delay(10);
if(button2==0)
{ while(button2==0);
switch(key)
{case 1: second++;if(second==60)second=0;
write_hms(10,second); write_zl(0x80+0x40+10); break;
case 2: minute++;if(minute==60)minute=0;
write_hms(7,minute);write_zl(0x80+0x40+7); break;
case 3: hour++;if(hour==24)hour=0;
write_hms(4,hour); write_zl(0x80+0x40+4); break;
case 4: day++;week++;
if(week==8)week=1;write_XQ(week);
write_day(month); if(day==nn+1)day=1;
write_nyr(10,day);write_zl(0x80+10); break;
case 5: month++;if(month==13)month=1;
write_nyr(7,month);write_zl(0x80+7); break;
case 6: year++ ;if(year==100)year=0;
write_nyr(4,year);write_zl(0x80+4); break;
case 7: year1++ ;if(year1==100)year1=0;
write_nyr(2,year1);write_zl(0x80+2); break;
}}}
if(button3==0)
{delay(10);
if(button3==0)
{ while(button3==0);
switch(key)
{case 1: second--;if(second<0)second=59;
write_hms(10,second); write_zl(0x80+0x40+10); break;
case 2: minute--;if(minute<0)minute=59;
write_hms(7,minute);write_zl(0x80+0x40+7); break;
case 3: hour--;if(hour<0)hour=23;
write_hms(4,hour);write_zl(0x80+0x40+4); break;
case 4: week--;if(week==0)week=7;write_XQ(week);
day--; write_day(month); if(day==0)day=nn;
write_nyr(10,day);write_zl(0x80+10); break;
case 5: month--;if(month==0)month=12;
write_nyr(7,month);write_zl(0x80+7); break;
case 6: year-- ;if(year<0)year=99;
write_nyr(4,year);write_zl(0x80+4); break;
case 7: year1-- ;if(year1<0)year1=99;
write_nyr(2,year1);write_zl(0x80+2); break;}
}}
}
void DS1302_sfm()
{ uchar a,b;
RST=1; //高电平期间启动一次数据转换
DS1302_write(0x81);
second=DS1302_readbyte();
RST=0;// 结束转换
a=second/16;//BCD码转换
b=second%16;
second=a*10+b;
write_hms(10,second);
//分
RST=1;
DS1302_write(0x83);
minute= DS1302_readbyte();
a=minute/16;
b=minute%16;
minute=a*10+b;
write_hms(7,minute);
RST=0;
//时
RST=1;
DS1302_write(0x85);
hour= DS1302_readbyte();
RST=0;
a=hour/16;
b=hour%16;
hour=a*10+b;
write_hms(4,hour);
//日
RST=1;
DS1302_write(0x87);
day= DS1302_readbyte();
RST=0;
a=day/16;
b=day%16;
day=a*10+b;
write_nyr(10,day);
//月
RST=1;
DS1302_write(0x89);
month= DS1302_readbyte();
RST=0;
a=month/16;
b=month%16;
month=a*10+b;
write_nyr(7,month);
// 周
RST=1;
DS1302_write(0x8B);
week= DS1302_readbyte();
RST=0;
a=week/16;
b=week%16;
week=a*10+b;
write_XQ(week);
// 年
SCLK=0;
delay(5);
RST=1;
DS1302_write(0x8D);
IO=1;
year= DS1302_readbyte();
RST=0;
delay(5);
SCLK=1;
a=year/16;
b=year%16;
year=a*10+b;
write_nyr(4,year);
}
/* 总程序*/
#include<reg51.h>
#include<init.h>
#include<ds1302.h>
#include<18b20.h>
void main()
{
init_lcd();
start1302();
while(1)
{
RST=0;
keyboard();
if(flag==0) //标志位为0才能读取1302,否则按键无效
DS1302_sfm();
write_zl(0x80+0x10);
for(n=0;n<16;n++)
write_sj(ttable[n]);
temperaturechange();
write_temperature(tem);
}
}
③ 怎样制作万年历单片机
今天碰到了N个白痴问题。
万年历是能够显示年月日、二十四节气、农历之类的东西,单片机是一块芯片。万年历可以用单片机来做,但是我真不知道“万年历单片机”怎么做。我想你应该想问的是怎么用单片机来做万年历,我提供思路。
1、单片机最小系统一块。可以用STC89C52芯片+外围电路。可以烧入用户编写的程序,实现万年历所需的逻辑。
2、实时时钟电路一块,可以用DS1302芯片。提供年月日、时分秒数据给单片机。
3、显示器电路一块。用来显示这些数据。
4、一些必要的按键,用来调整数据。
④ C51单片机的万年历程序设计
我这有个数码管显示的程序
以前做的,。。
你可以在我这个程序上修改修改
包括键盘扫描,还有动态显示
。
。
【。。。】
#include
//常量参数
#define TMODW 0x01;
#define SCONW 0x00;
#define xplay 0x04;//显示分频系数
//显示位选
unsigned char data stb;
//键值缓存,0xFF无键命令
unsigned char data keynum;
//显示字型变量
unsigned char data play[8];
//工作参数
unsigned char data l,m;
//字型码
unsigned char code BCDPC[10]=
{0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};
//字位码
unsigned char code STBCODE[8]=
{0x01,0x02,0x04,0x08,
0x10,0x20,0x40,0x80 };
//400Hz xplay分频计数
unsigned char data cttime;
//时钟参数
unsigned char data hr,min,sec,sec100;
//调整时钟参数(时钟“走”)
void ct1()
{sec100++;
if (sec100==100)
{sec100=0;sec++;
if (sec==60)
{sec=0;min++;
if (min==60)
{min=0;hr++;
if (hr==24) hr=0;
}
}
}
}
//时钟参数→LED 显示缓存7段参数转换函数;
void xcplay()
{play[0]=BCDPC[hr/10];
play[1]=BCDPC[hr%10];
play[2]=BCDPC[min/10];
play[3]=BCDPC[min%10];
play[4]=BCDPC[sec/10];
play[5]=BCDPC[sec%10];
play[6]=BCDPC[sec100/10];
play[7]=BCDPC[sec100%10];
}
//显示扫描
void cplay()
{T0=1;//T0-高电平消隐
T1=0;//T1-低电平准备发脉冲前沿
TI=0;//?
P1=0;//?
SBUF=STBCODE[stb];
while (TI==0)
{
};
TI=0;
SBUF=play[stb];
while (TI==0)
{
};
T1=1;
T0=0;
stb=++stb&0x07;
}
extern void cthl0();
//定时器0中断处理程序
void ct0(void) interrupt 1 using 1
{cthl0();
cttime--;
if (cttime==0)
{cttime=xplay;
ct1();//调用时钟“走”函数
xcplay();//调用时钟参数→Led显示缓存转换函数
};
cplay();
}
void w20ms()
{for (l=0;l<41;l++)
{for (m=0;m<81;m++)
{
}
}
}
void tkey()
{P1=0xF0;
keynum=0xFF;
if (P1!=0xF0)
{w20ms();
P1=0xF0;
if (P1!=0xF0)
{P1=0xFE;
switch (P1)
{case 0xEE:keynum=0;break;
case 0xDE:keynum=1;break;
case 0xBE:keynum=2;break;
case 0x7E:keynum=3;break;
}
P1=0xFD;
switch (P1)
{case 0xED:keynum=4;break;
case 0xDD:keynum=5;break;
case 0xBD:keynum=6;break;
case 0x7B:keynum=7;break;
}
P1=0xFB;
switch (P1)
{
case 0xEB:keynum=8;break;
}
};
};
P1=0x00;
}
void command()
{switch (keynum)
{
case 0:{hr=hr+1;
if (hr==24)
hr=0;
}
break;
case 1:{min=min+1;
if (min==60)
min=0;
}
break;
case 2:{sec=sec+1;
if (sec==60)
sec=0;
}
break;
case 3:{sec100=0;
}
break;
case 4:{
while(!(P1=0xED))
{
hr=0;
min=0;
sec=0;
}
}
break;
case 5:{hr=hr-1;
if (hr==00)
hr=24;
}
break;
case 6:{min=min-1;
if (min==00)
min=59;
}
break;
case 7:{sec=sec-1;
if (sec==00)
sec=0;
}
break;
case 0xFF:break;
}
keynum=0xFF;
}
main ()
{ hr=8;
min=5;
sec=8;
sec100=0;
TMOD=TMODW;
SCON=SCONW;
ET0=1;
TR0=1;
EA=1;
cttime=xplay;
while (1)
{w20ms();
tkey();
command();
};
}
⑤ 单片机万年历
今天碰到了N个白痴问题。万年历是能够显示年月日、二十四节气、农历之类的东西,单片机是一块芯片。万年历可以用单片机来做,但是我真不知道“万年历单片机”怎么做。我想你应该想问的是怎么用单片机来做万年历,我提供思路。 1、单片机最小系统一块。可以用STC89C52芯片+外围电路。可以烧入用户编写的程序,实现万年历所需的逻辑。 2、实时时钟电路一块,可以用DS1302芯片。提供年月日、时分秒数据给单片机。 3、显示器电路一块。用来显示这些数据。 4、一些必要的按键,用来调整数据。
⑥ 跪求 51单片机+12864液晶+1302时钟制成的万年历c程序
顶层文件 万年历.C
#include<reg51.h>
#include "LCD1602.h"
#include "DS1302.h"
#define uchar unsigned char
#define uint unsigned int
sbit speaker=P2^4;
bit key_flag1=0,key_flag2=0;
SYSTEMTIME adjusted;
uchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0;
uchar data_alarm[7]={0};
/************键盘控制******************************/
int key_scan() //扫描是否有键按下
{ int i=0;
uint temp;
P1=0xf0;
temp=P1;
if(temp!=0xf0)
i=1;
else
i=0;
return i;
}
uchar key_value() //确定按键的值
{
uint m=0,n=0,temp;
uchar value;
uchar v[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'} ;
P1=0xfe; temp=P1; if(temp!=0xfe)m=0;
P1=0xfd;temp=P1 ;if(temp!=0xfd)m=1;
P1=0xfb;temp=P1 ;if(temp!=0xfb)m=2;
P1=0xf7;temp=P1 ;if(temp!=0xf7)m=3;
P1=0xef;temp=P1 ;if(temp!=0xef)n=0;
P1=0xdf;temp=P1 ;if(temp!=0xdf)n=1;
P1=0xbf;temp=P1 ;if(temp!=0xbf)n=2;
value=v[m][n];
return value;
}
/***************************设置闹铃函数*******************************/
void naoling(void)
{
uchar i=0,l=0,j;
init1602();
while(key_flag2&&i<12)
if(key_scan()){j=key_value();write_data(j);if(i%2==0)data_alarm[l]=(j-'0')*10;else {data_alarm[l]+=(j-'0');l++;}i++;delay(600);}
write_com(0x01);
}
uchar according(void)
{ uchar k;
if(data_alarm[0]==adjusted.Year&&data_alarm[1]==adjusted.Month&&data_alarm[2]==adjusted.Day&&data_alarm[3]==adjusted.Hour&&data_alarm[4]==adjusted.Minute&&data_alarm[5]==adjusted.Second)
k=1;
else k=0;
return k;
}
void speak(void)
{uint i=50;
while(i)
{speaker=0;
delay(1);
speaker=1;
delay(1);
i--;
}
}
void alarm(void)
{uint i=10;
while(i)
{
speak();
delay(10);
i--;
}
}
/**************************修改时间操作********************************/
void reset(void)
{
sec_add=0;
min_add=0;
hou_add=0;
day_add=0;
mon_add=0;
yea_add=0 ;
}
void adjust(void)
{
if(key_scan()&&key_flag1)
switch(key_value())
{case '0':sec_add++;break;
case '1':min_add++;break;
case '2':hou_add++;break;
case '3':day_add++;break;
case '4':mon_add++;break;
case '5':yea_add++;break;
case 'b':reset();break;
default: break;
}
adjusted.Second+=sec_add;
adjusted.Minute+=min_add;
adjusted.Hour+=hou_add;
adjusted.Day+=day_add;
adjusted.Month+=mon_add;
adjusted.Year+=yea_add;
if(adjusted.Second>59) adjusted.Second=adjusted.Second%60;
if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60;
if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24;
if(adjusted.Day>31) adjusted.Day=adjusted.Day%31;
if(adjusted.Month>12) adjusted.Month=adjusted.Month%12;
if(adjusted.Year>100) adjusted.Year=adjusted.Year%100;
}
/**************************中断处理函数*********************************/
void changing(void) interrupt 0 using 0 //需要修改时间和日期,或者停止修改
{
if(key_flag1)key_flag1=0;
else key_flag1=1;
}
void alarming(void) interrupt 3 using 0 //需要设置闹铃或者停止设置
{
if(key_flag2)key_flag2=0;
else key_flag2=1;
}
/********************************主函数***********************************/
main()
{uint i;
uchar *l;
uchar p1[]="D:",p2[]="T:";
SYSTEMTIME T;
EA=1;
EX0=1;
IT0=1;
EA=1;
EX1=1;
IT1=1;
init1602();
Initial_DS1302() ;
while(1)
{ write_com(0x80);
write_string(p1,2);
write_com(0xc0);
write_string(p2,2);
DS1302_GetTime(&T) ;
adjusted.Second=T.Second;
adjusted.Minute=T.Minute;
adjusted.Hour=T.Hour;
adjusted.Week=T.Week;
adjusted.Day=T.Day;
adjusted.Month=T.Month;
adjusted.Year=T.Year;
for(i=0;i<9;i++)
{
adjusted.DateString[i]=T.DateString[i];
adjusted.TimeString[i]=T.TimeString[i];
}
adjust();
if(key_flag2)naoling();
if(according())alarm();
DateToStr(&adjusted);
TimeToStr(&adjusted);
write_com(0x82);
write_string(adjusted.DateString,8);
write_com(0xc2);
write_string(adjusted.TimeString,8);
delay(10);
}
(二)头文件1 显示模块 LCD1602.H
#ifndef LCD_CHAR_1602_2009_5_9
#define LCD_CHAR_1602_2009_5_9
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs = P2^0;
sbit lcdrw = P2^1;
sbit lcden = P2^2;
void delay(uint z) // 延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) // 写入指令数据到 lcd
{
lcdrw=0;
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date) // 写入字符显示数据到 lcd
{
lcdrw=0;
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init1602() // 初始化设定
{
lcdrw=0;
lcden=0;
write_com(0x3C);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void write_string(uchar *pp,uint n)
{
int i;
for(i=0;i<n;i++)
write_data(pp[i]);
}
#endif
(三)头文件2 时钟模块 DS1302.H
#ifndef _REAL_TIMER_DS1302_2009_5_20_
#define _REAL_TIMER_DS1302_2003_5_20_
sbit DS1302_CLK = P2^6; //实时时钟时钟线引脚
sbit DS1302_IO = P2^7; //实时时钟数据线引脚
sbit DS1302_RST = P2^5; //实时时钟复位线引脚
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
typedef struct SYSTEM_TIME
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[9]; //用这两个字符串来放置读取的时间
unsigned char TimeString[9];
}SYSTEMTIME; //定义的时间类型
#define AM(X) X
#define PM(X) (X+12) // 转成24小时制
#define DS1302_SECOND 0x80
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_WEEK 0x8A
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_YEAR 0x8C
#define DS1302_RAM(X) (0xC0+(X)*2) //用于计算 DS1302_RAM 地址的宏
/******内部指令**********/
void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0;
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1; //因为在前面已经定义了ACC0 = ACC^0;以便再次利用DS1302_IO = ACC0;
}
}
unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1;
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}
/********************************/
void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 写1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302OutputByte(); // 读1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}
void DS1302_SetProtect(bit flag) //是否写保护
{
if(flag)
Write1302(0x8E,0x10);
else
Write1302(0x8E,0x00);
}
void DS1302_SetTime(unsigned char Address, unsigned char Value) // 设置时间函数
{
DS1302_SetProtect(0);
Write1302(Address, ((Value/10)<<4 | (Value%10))); //将十进制数转换为BCD码
} //在DS1302中的与日历、时钟相关的寄存器存放的数据必须为BCD码形式
void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = Read1302(DS1302_SECOND);
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //将BCD码转换为十进制数
ReadValue = Read1302(DS1302_MINUTE);
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_WEEK);
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_YEAR);
Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
}
unsigned char *DataToBCD(SYSTEMTIME *Time)
{
unsigned char D[8];
D[0]=Time->Second/10<<4+Time->Second%10;
D[1]=Time->Minute/10<<4+Time->Minute%10;
D[2]=Time->Hour/10<<4+Time->Hour%10;
D[3]=Time->Day/10<<4+Time->Day%10;
D[4]=Time->Month/10<<4+Time->Month%10;
D[5]=Time->Week/10<<4+Time->Week%10;
D[6]=Time->Year/10<<4+Time->Year%10;
return D;
}
void DateToStr(SYSTEMTIME *Time)
{
//将十进制数转换为液晶显示的ASCII值
Time->DateString[0] = Time->Year/10 + '0';
Time->DateString[1] = Time->Year%10 + '0';
Time->DateString[2] = '-';
Time->DateString[3] = Time->Month/10 + '0';
Time->DateString[4] = Time->Month%10 + '0';
Time->DateString[5] = '-';
Time->DateString[6] = Time->Day/10 + '0';
Time->DateString[7] = Time->Day%10 + '0';
Time->DateString[8] = '\0';
}
void TimeToStr(SYSTEMTIME *Time)
{
//将十进制数转换为液晶显示的ASCII值
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
Time->TimeString[2] = ':';
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
Time->TimeString[5] = ':';
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
Time->DateString[8] = '\0';
}
void Initial_DS1302(void)
{
unsigned char Second;
Second=Read1302(DS1302_SECOND);
if(Second&0x80) //初始化时间
DS1302_SetTime(DS1302_SECOND,0);
}
void DS1302_TimeStop(bit flag) // 是否将时钟停止
{
unsigned char Data;
Data=Read1302(DS1302_SECOND);
DS1302_SetProtect(0);
if(flag)
Write1302(DS1302_SECOND, Data|0x80);
else
Write1302(DS1302_SECOND, Data&0x7F);
}
#endif