STC51单片机 DS130218b20实现万年历Word格式.docx
《STC51单片机 DS130218b20实现万年历Word格式.docx》由会员分享,可在线阅读,更多相关《STC51单片机 DS130218b20实现万年历Word格式.docx(31页珍藏版)》请在冰点文库上搜索。
4.7k
20k滑动变阻器
电池盒
8脚插座
40脚插座
1
4,源程序清单:
#include<
reg52.h>
stdio.h>
#defineuintunsignedint
#defineucharunsignedchar
sbitLCD_RS=P2^4;
//1602
sbitLCD_RW=P2^5;
sbitLCD_EN=P2^6;
sbitrst=P1^7;
//DS1302
sbitio=P1^6;
sbitsclk=P1^5;
sbitACC_7=ACC^7;
sbitK1=P2^3;
//按键
sbitK2=P2^2;
sbitK3=P2^1;
sbitK4=P2^0;
sbitBEEP=P1^0;
//蜂鸣器
sbitDQ=P3^1;
//温度传感器
bitflag=0;
bitflag1=0;
ucharyes;
//ucharcodetable2[]="
SONGyou"
;
//ucharcodetable3[]="
zhanpeng"
ucharcodetable4[]="
0123456789"
chartable5[7];
uchartable6[2];
chartable7[2];
charleap;
//延时函数部分
voidDelay_ms(uintxms)//延时
{
uinti,j;
for(i=xms;
i>
0;
i--)
for(j=110;
j>
j--);
}
voidDelayus(uchart)//延时1用于18b20
{
while(--t);
voidDelayms(uchart)//延时2用于18b20
while(t--)
{
//大致延时1mS
Delayus(245);
}
//18b20部分
bitinit18b20(void)//初始化18b20
bitdat=0;
DQ=1;
Delayus(5);
DQ=0;
Delayus(200);
Delayus(50);
dat=DQ;
Delayus(25);
returndat;
ucharread18b20(void)//读18b20函数
uchari=0,dat=0;
for(i=8;
//给脉冲信号
dat>
>
=1;
if(DQ)
dat|=0x80;
return(dat);
voidwrite18b20(uchardat)//写18b20函数
uchari=0;
for(i=8;
i>
i--)
DQ=dat&
0x01;
Delayus(25);
uintget_18b20(void)//读取温度函数
uchara=0;
uintb=0,t=0;
init18b20();
write18b20(0xCC);
//跳过读序号列号的操作
write18b20(0x44);
//启动温度转换
Delayms(10);
//跳过读序号列号的操作
write18b20(0xBE);
//读取温度寄存器等(共可读9个寄存器)前两个就是温度
a=read18b20();
//低位
b=read18b20();
//高位
b<
<
=8;
t=a+b;
return(t);
//蜂鸣器部分
voidbeep()
BEEP=0;
Delay_ms(100);
BEEP=1;
//LCD1602部分
voidwrite1602(uchardat)//1602写数据函数
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
LCD_EN=1;
Delay_ms
(1);
voidwrite1602c(ucharcom)//1602写指令函数
LCD_RS=0;
P0=com;
voidinit1602()//1602初始化
write1602c(0x38);
//16*2显示
write1602c(0x0f);
//显示光标
write1602c(0x06);
//光标右移
write1602c(0x01);
//清屏
write1602c(0x80);
//DS1302部分
voidwrite1302(ucharaddr,dat)//1302写数据函数
uchari,temp;
rst=0;
sclk=0;
rst=1;
for(i=0;
i<
8;
i++)//写地址,写数据
temp=addr;
io=(bit)(temp&
0x01);
addr>
sclk=1;
}
for(i=0;
temp=dat;
dat>
ucharread1302(ucharaddr)//1302读数据函数
uchari,temp,dat1,dat2;
rst=0;
sclk=0;
rst=1;
i++)
{
}
for(i=0;
ACC_7=io;
ACC>
sclk=0;
dat1=ACC;
dat2=dat1%16;
dat1=dat1/16;
dat1=dat1*10+dat2;
return(dat1);
voidinit1302()//1302初始化函数
write1302(0x8e,0x00);
write1302(0x80,0x00);
//秒初始化
write1302(0x8e,0x80);
/*voidinit1602B()
write1602c(0x80+16);
write1602(0xef);
while(table2[i]!
='
\0'
)
write1602(table2[i]);
i++;
write1602c(0x80+0x40+20);
i=0;
while(table3[i]!
write1602(table3[i]);
16;
write1602c(0x18);
Delay_ms(800);
Delay_ms(2000);
}*/
//显示部分
voidget_time()//获取时间函数
table5[5]=read1302(0x8d);
table5[4]=read1302(0x89);
table5[3]=read1302(0x87);
table5[2]=read1302(0x85);
table5[1]=read1302(0x83);
table5[0]=read1302(0x81);
table5[6]=read1302(0x8b);
voidDisplay_time(ucharyear,yue,day,hour,min,sec,xq)//显示函数
write1602('
2'
);
0'
write1602(table4[year/10]);
write1602(table4[year%10]);
-'
write1602(table4[yue/10]);
write1602(table4[yue%10]);
write1602(table4[day/10]);
write1602(table4[day%10]);
'
w'
e'
k'
write1602(table4[xq]);
write1602c(0x80+0x40);
write1602(table4[hour/10]);
write1602(table4[hour%10]);
:
'
write1602(table4[min/10]);
write1602(table4[min%10]);
write1602(table4[sec/10]);
write1602(table4[sec%10]);
T'
write1602(table7[0]);
write1602(table7[1]);
write1602(0xdf);
c'
//调时部分
voidKeyprocess()
staticucharnum=0;
ucharyear1,yue1,day1,hour1,min1,xq1;
write1302(0x80,0x80);
if(K3==0)
Delay_ms(5);
while(!
K3);
beep();
num++;
if(num>
6)
num=0;
if(num)
if(K1==0)
K1);
if(num==1)
{
table5[1]++;
if(table5[1]==60)
table5[1]=0;
min1=table5[1]/10*16+table5[1]%10;
write1302(0x82,min1);
if(num==2)
table5[2]++;
if(table5[2]==24)
table5[2]=0;
hour1=table5[2]/10*16+table5[2]%10;
write1302(0x84,hour1);
if(num==3)
table5[6]++;
if(table5[6]==8)
table5[6]=1;
xq1=table5[6]/10*16+table5[6]%10;
write1302(0x8a,xq1);
if(num==4)
if(table5[5]==0)
switch(table5[4])
case1:
table5[3]++;
if(table5[3]==32)table5[3]=1;
break;
case2:
if(table5[3]==30)table5[3]=1;
case3:
case4:
if(table5[3]==31)table5[3]=1;
case5:
case6:
case7:
case8:
case9:
case10:
case11:
case12:
elseif(leap&
&
table5[4]==2)
if(table5[3]==30)
table5[3]=1;
elseif(leap==0&
if(table5[3]==29)
else
day1=table5[3]/10*16+table5[3]%10;
write1302(0x86,day1);
if(num==5)
table5[4]++;
if(table5[4]==13)
table5[4]=1;
yue1=table5[4]/10*16+table5[4]%10;
write1302(0x88,yue1);
if(num==6)
table5[5]++;
if(table5[5]==100)
table5[5]=0;
leap=table5[5]%4==0&
table5[5]%100!
=0;
year1=table5[5]/10*16+table5[5]%10;
write1302(0x8c,year1);
if(K2==0)
K2);
if(num==1)
table5[1]--;
if(table5[1]==-1)
table5[1]=59;
table5[2]--;
if(table5[2]==-1)
table5[2]=23;
table5[6]--;
if(table5[6]==0)
table5[6]=7;
table5[3]--;
if(table5[3]==0)table5[3]=31;
if(table5[3]==0)table5[3]=29;
if(table5[3]==0)table5[3]=30;
if(table5[3]==0)
table5[3]=29;
table5[3]=28;
if(table5[3]==0)table5[3]=31;
if(table5[3]==0)table