单片机编程的万年历带仿真图.docx
- 文档编号:24315533
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:22
- 大小:35.21KB
单片机编程的万年历带仿真图.docx
《单片机编程的万年历带仿真图.docx》由会员分享,可在线阅读,更多相关《单片机编程的万年历带仿真图.docx(22页珍藏版)》请在冰豆网上搜索。
单片机编程的万年历带仿真图
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodestr1[]="2011-02-28week7";
ucharcodestr2[]="23:
59:
55";
ucharcodestr3[]="0-22:
00";
ucharcodestr4[]="0-23:
00";
ucharc1=0,c2=0;//闹钟标志当c1=1或者c2=1时,闹钟才会响
sbitRS=P2^0;
sbitRW=P2^1;
sbitE=P2^2;
bits=0,d=1;//s是开光标标志位,d=0时切换到闹钟屏
bitjia,jian;
ucharcodestring[]={0x0c,0xc7,0xc4,0xc1,0x89,0x86,0x83,0x8f,0x80,0x83,0x86,0xc0,0xc3,0xc6};//光标位置调节
ucharsecond=58,minute=59,hour=23,month=2,day=28,week=7,count=0;
ucharminute1=0,hour1=22,minute2=0,hour2=23;//闹钟
uchart_symbol;//温度的符号位,为1表示负温,为0表示正温
uintT;
unsignedintyear=2011;//0-65535
intm=0;
sbitkey1=P2^3;//按键设置
sbitkey2=P2^4;//加按键
sbitkey3=P2^5;//减按键
sbitkey4=P2^6;//闹钟按键
sbitkey5=P2^7;//复位键
sbitbeep=P1^0;//蜂鸣器
sbitDQ=P3^3;//温度引脚
voiddelayms()
{uchari;
for(i=0;i<122;i++);
}
voidbusy_check()
{P0=0xff;
RS=0;
RW=1;
E=1;
E=0;
while(P0&0x80)
{E=0;
E=1;
}
delayms();
}
voidwrite_LCD_com(ucharcmd)
{
busy_check();
E=0;
RS=0;
RW=0;
E=1;
P0=cmd;
E=0;
delayms();
}
voidwrite_LCD_data(uchardat)
{busy_check();
E=0;
RS=1;
RW=0;
E=1;
P0=dat;
E=0;
delayms();
}
voidSET_LCD_pos(ucharx,uchary)
{if(y==0)
write_LCD_com(0x80|x);
if(y==1)
write_LCD_com(0xc0|x);
}
voidshow_LCD(uchar*str)
{uchari;
for(i=0;i<16;i++)
write_LCD_data(str[i]);
}
/****初始化LCD****/
voidinit_LCD()
{write_LCD_com(0x01);
write_LCD_com(0x38);
write_LCD_com(0x0c);
write_LCD_com(0x06);
}
voiddelay_18B20(unsignedinti)//延时1-255微秒
{while(i--);}
uchards1820rst()//ds1820复位
{unsignedcharg=0;
DQ=1;//DQ复位
delay_18B20(8);//延时
DQ=0;//发送复位脉冲
delay_18B20(80);//延时大于480us
DQ=1;//DQ拉高
delay_18B20(8);
g=DQ;//若g=0,则ds1820存在,否则不存在
delay_18B20(20);
return(g);
}
uchards1820_rd()//读数据
{unsignedchare=0;
unsignedchardat=0;//暂存读取的数据
for(e=8;e>0;e--)
{DQ=0;//给脉冲信号
dat>>=1;
DQ=1;//给脉冲信号
if(DQ==1)
dat|=0x80;
delay_18B20(10);
}
return(dat);//返回读取的值
}
voidds1820wr(ucharwdate)//写数据
{unsignedchari=0;
for(i=8;i>0;i--)
{DQ=0;
DQ=wdate&0x01;//低位在前
delay_18B20(10);
DQ=1;
wdate>>=1;//准备发送下一位
}
}
voidread_temp()//读取温度值并转换
{uchara=0,b=0;
ds1820rst();//ds1820复位
ds1820wr(0xcc);//跳过读序列号
ds1820wr(0x44);//启动温度转换
delay_18B20(150);
ds1820rst();//ds1820复位
ds1820wr(0xcc);//跳过读序列号
ds1820wr(0xbe);//读取温度
delay_18B20(150);
a=ds1820_rd();//读出温度的低八位
b=ds1820_rd();//读出温度的高八位
T=b;
T<<=8;
T=T|a;
if(T<0x0fff)//比较温度
t_symbol=0;
else
{T=~T+1;//负温度,取反加一
t_symbol=1;}
T=T*(0.625);//温度值扩大10倍返?
位小数
}
voidds1820_show()//温度值显示
{ucharflagdat,T0,T1,T2;
T0=T/100+'0';//十位数
T1=T%100/10+'0';//个位数
T2=T%10+'0';//小数位
read_temp();
if(t_symbol==0)
flagdat=0x20;//正温度不显示符号
else
flagdat=0x2d;//负温度显示负号:
-
if(T0==0x30)
{T0=0x20;}//如果十位为0,不显示
}
/****字符显示****/
voidzifushow1()
{ucharflagdat,T0,T1,T2;
SET_LCD_pos(9,1);
write_LCD_data(flagdat);//显示符号位
SET_LCD_pos(10,1);
write_LCD_data(T0);//显示十位
SET_LCD_pos(11,1);
write_LCD_data(T1);//显示个位
SET_LCD_pos(12,1);
write_LCD_data(0xa5);//显示小数点
SET_LCD_pos(13,1);
write_LCD_data(T2);//显示小数位
SET_LCD_pos(15,1);
write_LCD_data('C');
SET_LCD_pos(14,1);
write_LCD_data(0xdf);
SET_LCD_pos(7,1);
write_LCD_data(second%10+'0');
SET_LCD_pos(6,1);
write_LCD_data(second/10+'0');
SET_LCD_pos(5,1);
write_LCD_data(':
');
SET_LCD_pos(4,1);
write_LCD_data(minute%10+'0');
SET_LCD_pos(3,1);
write_LCD_data(minute/10+'0');
SET_LCD_pos(2,1);
write_LCD_data(':
');
SET_LCD_pos(1,1);
write_LCD_data(hour%10+'0');
SET_LCD_pos(0,1);
write_LCD_data(hour/10+'0');
SET_LCD_pos(9,0);
write_LCD_data(day%10+'0');
SET_LCD_pos(8,0);
write_LCD_data(day/10+'0');
SET_LCD_pos(7,0);
write_LCD_data('-');
SET_LCD_pos(6,0);
write_LCD_data(month%10+'0');
SET_LCD_pos(5,0);
write_LCD_data(month/10+'0');
SET_LCD_pos(4,0);
write_LCD_data('-');
SET_LCD_pos(3,0);
write_LCD_data(year%10+'0');
SET_LCD_pos(2,0);
write_LCD_data(year/10%10+'0');
SET_LCD_pos(1,0);
write_LCD_data(year/100%10+'0');
SET_LCD_pos(0,0);
write_LCD_data(year/1000+'0');
SET_LCD_pos(11,0);
write_LCD_data('w');
SET_LCD_pos(12,0);
write_LCD_data('e');
SET_LCD_pos(13,0);
write_LCD_data('e');
SET_LCD_pos(14,0);
write_LCD_data('k');
SET_LCD_pos(15,0);
write_LCD_data(week%8+'0');
}
/*******蜂鸣器*******/
voidspeaker()
{
if(c1==1)
if(hour1==hour&&minute1==minute)
beep=!
beep;
if(c2==1)
if(hour2==hour&&minute2==minute)
beep=!
beep;
}
/*******闹钟字符显示*******/
voidzifushow2()
{
SET_LCD_pos(0,0);
write_LCD_data(c1+'0');
SET_LCD_pos(1,0);
write_LCD_data('-');
SET_LCD_pos(2,0);
write_LCD_data(hour1/10+'0');
SET_LCD_pos(3,0);
write_LCD_data(hour1%10+'0');
SET_LCD_pos(4,0);
write_LCD_data(':
');
SET_LCD_pos(5,0);
write_LCD_data(minute1/10+'0');
SET_LCD_pos(6,0);
write_LCD_data(minute1%10+'0');
SET_LCD_pos(0,1);
write_LCD_data(c2+'0');
SET_LCD_pos(1,1);
write_LCD_data('-');
SET_LCD_pos(2,1);
write_LCD_data(hour2/10+'0');
SET_LCD_pos(3,1);
write_LCD_data(hour2%10+'0');
SET_LCD_pos(4,1);
write_LCD_data(':
');
SET_LCD_pos(5,1);
write_LCD_data(minute2/10+'0');
SET_LCD_pos(6,1);
write_LCD_data(minute2%10+'0');
}
/***时间和日期的行驶*****/
voidtime_date()
{if(second==60)
{second=0;
minute++;}
if(minute==60)
{minute=0;
hour++;}
if(hour==24)
{hour=0;
day++;
week++;}
if(week==8)
{week=1;}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//大月份判断
{if(day==32)
{month++;
day=1;}
}
if(month==4||month==6||month==9||month==11)//小月份判断
{if(day==31)
{day=1;
month++;}}
if(month==2)//闰年的月份判断
{
{if(year%400==0||(year%4==0&&year%100!
=0))
{if(day==30)
{day=1;
month++;}}
else
{if(day==29)
{day=1;
month++;}
}
}
}
if(month==13)
{year++;
month=0;}
}
/****按键加****/
voidjiakey()
{key2=1;
if(key2==0)
{delayms();
if(key2==0);
while(key2==0);
{switch(count)
{case1:
second++;
if(second==60){second=0;}
jia=1;
break;
case2:
minute++;
if(minute==60){minute=0;}
jia=1;
break;
case3:
hour++;
if(hour==24){hour=0;}
jia=1;
break;
case4:
day++;week++;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{if(day==32){day=1;}}
if(month==4||month==6||month==9||month==11){if(day==31){day=1;}}
if(month==2&&year%4==0&&day==30){;day=1;}
if(month==2&&year%4!
=0&&day==29){;day=1;}
if(week==8){week=1;}
jia=1;
break;
case5:
month++;
if(month==13){month=1;}
jia=1;
break;
case6:
year++;
if(year==2100){year=2000;}
jia=1;
break;
case7:
if(d==1){
week++;
if(week==8){week=1;}
jia=1;}
break;
case8:
c1=!
c1;
jia=1;
break;
case9:
hour1++;
if(hour1==24){hour1=0;}
jia=1;break;
case10:
minute1++;
if(minute1==60){minute1=0;}
jia=1;break;
case11:
c2=!
c2;
jia=1;break;
case12:
hour2++;
if(hour2==24){hour2=0;}
jia=1;break;
case13:
minute2++;
if(minute2==60){minute2=0;}
jia=1;break;
}
}
}
}
/****按键减****/
voidjiankey()
{
key3=1;
if(key3==0)
{
delayms();
if(key3==0)
while(key3==0);
{switch(count)
{case1:
second--;
if(second==-1){second=59;}
jian=1;
break;
case2:
minute--;
if(minute==-1){minute=59;}
jian=1;
break;
case3:
hour--;
if(hour==-1){hour=23;}
jian=1;
break;
case4:
day--;week--;
if(week==0){week=7;}
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{if(day==32||day==0){day=31;}}
if(month==4||month==6||month==9||month==11){if(day>=31||day==0){day=30;}}
if(month==2&&year%4==0&&day==0){day=29;}
if(month==2&&year%4!
=0&&day==0){day=28;}
if(month==13||month==0){month=12;}
jian=1;
break;
case5:
month--;
if(month==13||month==0){month=12;}
jian=1;
break;
case6:
year--;
if(year==1999){year=2099;}
jian=1;
break;
case7:
if(d==1)
{
week--;
if(week==0){week=7;}
jian=1;}
break;
case8:
c1=!
c1;
jian=1;
break;
case9:
hour1--;
if(hour1==-1){hour1=23;}
jian=1;
break;
case10:
minute1--;
if(minute1==-1){minute1=59;}
jian=1;
break;
case11:
c2=!
c2;
jian=1;break;
case12:
hour2--;
if(hour2==-1){hour2=23;}
jian=1;break;
case13:
minute2--;
if(minute2==-1){minute2=59;}
jian=1;break;
}
}
}
}
/*****闹钟调节按键***********/
voidclock_key()
{
key4=1;
if(key4==0)
{delayms();
if(key4==0)
while(key4==0);
{d=~d;
init_LCD();
if(d==1)count=0;
if(d==0)count=8;
}
}
}
/*****暂停调节并运行按键******/
voidzantingkey()
{key5=1;
if(key5==0)
{
delayms();
if(key5==0);
while(key5==0);
{s=0;
count=0;
}
}
}
/***按键的设置*******/
voidkeyset()
{
key1=1;
if(key1==0)
{
delayms();
if(key1==0)
while(key1==0);
{
s=1;
count=count+1;
}
}
}
/****光标的移动显示****/
voidguangbiao()
{
if(s==1){
write_LCD_com(string[count]);
write_LCD_com(0x0f);
delayms();
write_LCD_com(0x0c);
delayms();
}
}
/****按键的运行****/
voidkeyrun()
{
keyset();
jiakey();
jiankey();
clock_key();
zantingkey();
if(d==1&&count>=8)count=1;
if(d==0&&count==14)count=8;
guangbiao();
if(jia==1||jian==1)
{time_date();
jia=0;jian=0;
}
}
/*****主函数*************/
voidmain()
{if(d==1)
{init_LCD();
write_LCD_com(0x80);
show_LCD(str1);
write_LCD_com(0xc0);
show_LCD(str2);}
if(d==0)
{init_LCD();
write_LCD_com(0x80);
show_LCD(str3);
write_LCD_com(0xc0);
show_LCD(str4);}
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1;
while
(1)
{
if(key1==0){TR0=0;}
if(key5==0){TR0=1;}
keyrun();
tim
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单片机 编程 万年历 仿真
![提示](https://static.bdocx.com/images/bang_tan.gif)