自己前几天闲着无事想做点东西,就做这么一个万年历,自己新手一个,写的不咋地,讲究看吧!
单片机源程序
- #include<reg52.h>
- #include"DS18b20.h"
- #include"ds1302.h"
- #include"LCD1602.h"
- uchar naozhong[16] = {" -~ 00:00:00 "};
- uchar c=0,d=0,flag=0,flag1=0,e1=0,e2=0,e3=0,e4=0;
- uint aa=0;
- bit temp=0,temp1=0,temp2=0,temp3=0;
- sbit k1=P3^2;
- sbit k3=P3^3;
- sbit k2=P3^7;
- sbit beep=P3^1;
- void LcdDisplay();
- void datapros(int temp);
- void keys();
- void Int0Configuration();
- void dingnao();
- void jiance();
- void main()
- {
- beep=0;
- Int0Configuration();
- LcdInit();
- Ds18b20Init();
- // Ds1302Init();
- Ds18b20ReadTemp();
- Delay1ms(20);
- Ds18b20ReadTemp();
- Ds1302ReadTime();
- Delay1ms(20);
- Delay1ms(1500);
-
- while(1)
- {
- // 55 AA
- datapros(Ds18b20ReadTemp());
- Ds1302ReadTime();
- LcdDisplay();
- keys();
- dingnao();
- jiance();
- }
- }
- void Int0() interrupt 0
- {
- beep=0;
- Delay1ms(20);
- if((k1==0)&(temp==0))
- {
- while(k1==0);
- flag++;
- if(flag>=8) flag=0;
- }
- if((k1==0)&(temp==1))
- {
- while(k1==0);
- flag1++;
- if(flag1>=5)
- {
- flag1=0;
- LcdWriteCom(0x0c);
- }
- }
- }
- void Int1() interrupt 2
- {
- beep=0;
- Delay1ms(20);
- if((k3==0)&(flag==0)&(flag1==0))
- {
- while(k3==0);
- temp=~temp;
- temp1=1;
- }
- }
- void jiance()
- {
- if(naozhong[3]=='*')
- {
- LcdWriteCom(0x80);
- LcdWriteData('*');
- if(naozhong[4]=='0'+(TIME[2]/16))
- {
- if(naozhong[5]=='0'+(TIME[2]&0x0f))
- {
- if(naozhong[7]=='0'+(TIME[1]/16))
- {
- if(naozhong[8]=='0'+(TIME[1]&0x0f))
- {
- if(naozhong[10]=='0'+(TIME[0]/16))
- {
- if(naozhong[11]=='0'+(TIME[0]&0x0f))
- {
- beep=1;
- }
- }
- }
- }
- }
- }
- }
- if(beep==1)
- {
- if((k1==0)||(k2==0)||(k3==0)) beep=1;
- aa++;
- if(aa>1000)
- {
- aa=0;
- beep=0;
- }
- }
- }
- void dingnao()
- {
- while(temp)
- {
- uchar i;
- if(temp1==1)
- {
- temp1=0;
- LcdWriteCom(0x07);
- LcdWriteCom(0x80+0X10);
- for(i=0;i<16;i++) LcdWriteData(0x02);
- LcdWriteCom(0x06);
- LcdWriteCom(0x80+0X50);
- for(i=0;i<16;i++)
- {
- LcdWriteData(naozhong[i]);
- }
- }
- temp3=1;temp2=1;
- /************/
- if(flag1!=0)
- {
- LcdWriteCom(0x0f);
- if(flag1==1)
- {
- LcdWriteCom(0x80+0X53);
- if(k2==0)
- {
- Delay1ms(10);
- if(k2==0)
- {
- e1++;
- if(e1==60) e1=0;
- if(e1%2==0)
- {
- naozhong[3]='*';
- LcdWriteData('@');
- }
- else
- {
- naozhong[3]=' ';
- }
- LcdWriteCom(0x80+0X50);
- for(i=0;i<16;i++)
- {
- LcdWriteData(naozhong[i]);
- }
- LcdWriteCom(0x80+0X53);
- }
- }
- }
- if(flag1==2)
- {
- LcdWriteCom(0x80+0X55);
- if(k2==0)
- {
- Delay1ms(10);
- if(k2==0)
- {
- e2++;
- if(e2==24) e2=0;
- naozhong[4]=e2/10+'0';
- naozhong[5]=e2%10+'0';
- }
- LcdWriteCom(0x80+0X50);
- for(i=0;i<16;i++)
- {
- LcdWriteData(naozhong[i]);
- }
- LcdWriteCom(0x80+0X55);
- }
- }
- if(flag1==3)
- {
- LcdWriteCom(0x80+0X58);
- if(k2==0)
- {
- Delay1ms(10);
- if(k2==0)
- {
- e3++;
- if(e3==60) e3=0;
- naozhong[7]=e3/10+'0';
- naozhong[8]=e3%10+'0';
- }
- LcdWriteCom(0x80+0X50);
- for(i=0;i<16;i++)
- {
- LcdWriteData(naozhong[i]);
- }
- LcdWriteCom(0x80+0X58);
-
- }
- }
- if(flag1==4)
- {
- LcdWriteCom(0x80+0X5b);
- if(k2==0)
- {
- Delay1ms(10);
- if(k2==0)
- {
- e4++;
- if(e4==60) e4=0;
- naozhong[10]=e4/10+'0';
- naozhong[11]=e4%10+'0';
- }
- LcdWriteCom(0x80+0X50);
- for(i=0;i<16;i++)
- {
- LcdWriteData(naozhong[i]);
- }
- LcdWriteCom(0x80+0X5b);
- }
- }
- }
- }
- if(temp3==1)
- {
- EA=0;
- temp3=0;
- LcdWriteCom(0x01);
- LcdWriteCom(0x02);
- EA=1;
- }
- }
- void Int0Configuration()
- {
- //设置INT0
- IT0=1;
- IT1=1;//跳变沿出发方式(下降沿)
- EX0=1;
- EX1=1;//打开INT0的中断允许。
- EA=1;//打开总中断
- }
- void datapros(int temp)
- {
- float tp;
- if(temp < 0) //当温度值为负数
- {
- LcdWriteCom(0x80+0X4b);
- LcdWriteData('+'); //温度+
- //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
- temp=temp-1;
- temp=~temp;
- tp=temp;
- temp=tp*0.0625+0.5;
- }
- else
- {
- LcdWriteCom(0x80+0X4b);
- LcdWriteData('+'); //温度+
- tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
- temp=tp*0.0625+0.5;
- }
- c=temp%100/10;
- d=temp%10/1;
- }
- void LcdDisplay()
- {
- LcdWriteCom(0x82);
- LcdWriteData('2');
- LcdWriteData('0');
- LcdWriteData('0'+TIME[6]/16); //年
- LcdWriteData('0'+(TIME[6]&0x0f));
- LcdWriteData('-');
- LcdWriteData('0'+TIME[5]/16); //月
- LcdWriteData('0'+(TIME[5]&0x0f));
- LcdWriteData('-');
- LcdWriteData('0'+TIME[4]/16); //日
- LcdWriteData('0'+(TIME[4]&0x0f));
- LcdWriteCom(0x80+0X0e);
- LcdWriteData(0x02);
- LcdWriteData('0'+(TIME[3]&0x07)); //星期
-
- LcdWriteCom(0x80+0X41);
- LcdWriteData('0'+TIME[2]/16); //时
- LcdWriteData('0'+(TIME[2]&0x0f));
- LcdWriteData(0x3a);
- LcdWriteData('0'+TIME[1]/16); //分
- LcdWriteData('0'+(TIME[1]&0x0f));
- LcdWriteData(0x3a);
- LcdWriteData('0'+TIME[0]/16); //秒
- LcdWriteData('0'+(TIME[0]&0x0f));
- LcdWriteCom(0x80+0X4c);
- LcdWriteData('0'+c); //温度
- LcdWriteData('0'+d);
- LcdWriteData('C');
- }
- void keys()
- {
- while(flag)
- {
- LcdWriteCom(0x0f);
- if(flag==1)
- {
- LcdWriteCom(0x80+0x04);
- LcdWriteData('0'+TIME[6]/16); //年
- LcdWriteData('0'+(TIME[6]&0x0f));
- LcdWriteCom(0x80+0x05);
- }
-
- else if(flag==2)
- {
- LcdWriteCom(0x80+0x07);
- LcdWriteData('0'+TIME[5]/16); //月
- LcdWriteData('0'+(TIME[5]&0x0f));
- LcdWriteCom(0x80+0x08);
- }
-
- else if(flag==3)
- {
- LcdWriteCom(0x80+0x0a);
- LcdWriteData('0'+TIME[4]/16); //日
- LcdWriteData('0'+(TIME[4]&0x0f));
- LcdWriteCom(0x80+0x0b);
- }
-
- else if(flag==4)
- {
- LcdWriteCom(0x80+0x0f); //周
- LcdWriteData('0'+(TIME[3]&0x07));
- LcdWriteCom(0x80+0x0f);
- }
-
- else if(flag==5)
- {
- LcdWriteCom(0x80+0x41);
- LcdWriteData('0'+TIME[2]/16); //时
- LcdWriteData('0'+(TIME[2]&0x0f));
- LcdWriteCom(0x80+0x42);
- }
-
- else if(flag==6)
- {
- LcdWriteCom(0x80+0x44);
- LcdWriteData('0'+TIME[1]/16); //分
- LcdWriteData('0'+(TIME[1]&0x0f));
- LcdWriteCom(0x80+0x45);
- }
-
- else if(flag==7)
- {
- LcdWriteCom(0x80+0x47);
- LcdWriteData('0'+TIME[0]/16); //秒
- LcdWriteData('0'+(TIME[0]&0x0f));
- LcdWriteCom(0x80+0x48);
- }
- /********************************************/
- if(k2==0)
- {
- Delay1ms(30);
- if(k2==0)
- {
- TIME[7-flag]++;
- if((TIME[7-flag]&0x0f)>9) //换成BCD码。
- {
- TIME[7-flag]=TIME[7-flag]+6;
- }
- if((TIME[7-flag]>=0x99)&&(flag==1)) //年只能到99
- {
- TIME[7-flag]=0;
- }
- if((TIME[7-flag]>=0x13)&&(flag==2)) //月只能到12
- {
- TIME[7-flag]=1;
- }
- if((TIME[7-flag]>=0x32)&&(flag==3)) //日只能到31
- {
- TIME[7-flag]=1;
- }
- if((TIME[7-flag]>=0x8)&&(flag==4)) //周只能到7
- {
- TIME[7-flag]=1;
- }
- if((TIME[7-flag]>=0x24)&&(flag==5)) //小时只能到23
- {
- TIME[7-flag]=0;
- }
- if((TIME[7-flag]>=0x60)&&(flag==6)) //分只能到59
- {
- TIME[7-flag]=0;
- }
- if((TIME[7-flag]>=0x60)&&(flag==7)) //秒只能到59
- {
- TIME[7-flag]=0;
- }
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
下载:
万年历.rar
(102.97 KB, 下载次数: 24)
|