1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 基于51单片机的电子时钟万年历带农历温度proteus仿真

基于51单片机的电子时钟万年历带农历温度proteus仿真

时间:2022-03-29 11:41:51

相关推荐

基于51单片机的电子时钟万年历带农历温度proteus仿真

硬件设计

本设计由数据显示模块、温度采集模块、时间处理模块和调整设置模块四个模块组成。系统以AT89S52单片机为控制器,以串行时钟日历芯片DS1302记录日历和时间,它可以对年、月、日、时、分、秒进行计时,还具有闰年补偿等多种功能。温度采集选用DS18B20芯片,万年历采用直观的数字显示,数据显示采用1602A液晶显示模块,可以在LCD上同时显示年、月、日、星期、时、分、秒,还具有时间校准等功能。此万年历具有读取方便、显示直观、功能多样、电路简洁。

硬件框图:

(1)用4个按键实现所有功能,计时准确。

(2)可以设定闹钟功能。

(3)有阴历功能,平年闰年准确无误。

(4)液晶能显示年、月、日、星期、时、分、秒、温度。

仿真图:

农历:

程序设计

#include<reg52.h>//#include"DS18B20_3.H"#include <string.h>#include <intrins.h>#define uint unsigned int#define uchar unsigned char#define wd 1//定义是否有温度功能 =0时无温度,=1时有温度#include "eeprom52.h"#define yh 0x80 //LCD第一行的初始位置,因为LCD1602字符地址首位D7恒定为1(100000000=80)#define er 0x80+0x40 //LCD第二行初始位置(因为第二行第一个字符位置地址是0x40)//液晶屏的与C51之间的引脚连接定义(显示数据线接C51的P0口)sbit en=P2^7;sbit rw=P2^6; //如果硬件上rw接地,就不用写这句和后面的rw=0了sbit rs=P2^5;//校时按键与C51的引脚连接定义sbit set=P3^0; //设置键sbit add=P3^1; //加键sbit dec=P3^2; //减键sbit seeNL_NZ=P3^3;//查看农历/闹钟sbit DQ=P3^7;//sbit buzzer=P2^0;//蜂鸣器,通过三极管8550驱动,端口低电平响sbit led=P2^4; //LCD背光开关bit led1=1;bit NZ_sdgb=1;unsigned char temp_miao;unsigned char bltime; //背光亮的时间//DS1302时钟芯片与C51之间的引脚连接定义sbit IO=P1^1;sbit SCLK=P1^0;sbit RST=P1^2;char a,miao,shi,fen,ri,yue,nian,week,setn,temp;uint flag;//flag用于读取头文件中的温度值,和显示温度值bit c_moon;char nz_shi,nz_fen,setNZn; //定义闹钟变量uchar shangyimiao,bsn,temp_hour;//记录上一秒时间uchar T_NL_NZ;//计数器bit timerOn=0;//闹钟启用标志位bit baoshi=0;//整点报时标志位bit p_r=0; //平年/润年 =0表示平年,=1表示润年data uchar year_moon,month_moon,day_moon;sbit ACC0=ACC^0;sbit ACC7=ACC^7;/************************************************************ACC累加器=AACC.0=E0H ACC.0就是ACC的第0位。Acc可以位寻址。累加器ACC是一个8位的存储单元,是用来放数据的。但是,这个存储单元有其特殊的地位,是单片机中一个非常关键的单元,很多运算都要通过ACC来进行。以后在学习指令时,常用A来表示累加器。但有一些地方例外,比如在PUSH指令中,就必须用ACC这样的名字。一般的说法,A代表了累加器中的内容、而ACC代表的是累加器的地址。 ***************************************************************//******************把数据保存到单片机内部eeprom中******************/void write_eeprom(){SectorErase(0x2000);byte_write(0x2000, nz_shi);byte_write(0x2001, nz_fen);byte_write(0x2002, timerOn);byte_write(0x2060, a_a);}/******************把数据从单片机内部eeprom中读出来*****************/void read_eeprom(){nz_shi = byte_read(0x2000);nz_fen = byte_read(0x2001);timerOn = byte_read(0x2002);a_a= byte_read(0x2060);}/**************开机自检eeprom初始化*****************/void init_eeprom() {read_eeprom();//先读if(a_a != 1)//新的单片机初始单片机内问eeprom{nz_shi = 12;nz_fen = 30;timerOn=0;a_a = 1;write_eeprom(); //保存数据}}///月份数据表code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};code uint day_code2[3]={0x111,0x130,0x14e};/*函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年)调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun)如:计算10月16日Conversion(0,0x4,0x10,0x16);c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世纪,c_sun=1为19世纪调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据*/bit c_moon;//子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0bit get_moon_day(uchar month_p,uint table_addr){uchar temp10;switch (month_p){case 1:{temp10=year_code[table_addr]&0x08;if (temp10==0)return(0);else return(1);}case 2:{temp10=year_code[table_addr]&0x04;if (temp10==0)return(0);else return(1);}case 3:{temp10=year_code[table_addr]&0x02;if (temp10==0)return(0);else return(1);}case 4:{temp10=year_code[table_addr]&0x01;if (temp10==0)return(0);else return(1);}case 5:{temp10=year_code[table_addr+1]&0x80;if (temp10==0) return(0);else return(1);}case 6:{temp10=year_code[table_addr+1]&0x40;if (temp10==0)return(0);else return(1);}case 7:{temp10=year_code[table_addr+1]&0x20;if (temp10==0)return(0);else return(1);}case 8:{temp10=year_code[table_addr+1]&0x10;if (temp10==0)return(0);else return(1);}case 9:{temp10=year_code[table_addr+1]&0x08;if (temp10==0)return(0);else return(1);}case 10:{temp10=year_code[table_addr+1]&0x04;if (temp10==0)return(0);else return(1);}case 11:{temp10=year_code[table_addr+1]&0x02;if (temp10==0)return(0);else return(1);}case 12:{temp10=year_code[table_addr+1]&0x01;if (temp10==0)return(0);else return(1);}case 13:{temp10=year_code[table_addr+2]&0x80;if (temp10==0)return(0);else return(1);}default:return(2);}}/*函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年)调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun)如:计算10月16日Conversion(0,0x4,0x10,0x16);c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世纪,c_sun=1为19世纪调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据*/void Conversion(bit c,uchar year,uchar month,uchar day){//c=0 为21世纪,c=1 为19世纪 输入输出数据均为BCD数据uchar temp1,temp2,temp3,month_p;uint temp4,table_addr;bit flag2,flag_y;temp1=year/16; //BCD->hex 先把数据转换为十六进制temp2=year%16;// year=temp1*10+temp2;year=temp1*16+temp2;temp1=month/16;temp2=month%16;//month=temp1*10+temp2;month=temp1*16+temp2;temp1=day/16;temp2=day%16;//day=temp1*10+temp2;day=temp1*16+temp2;//定位数据表地址if(c==0){ table_addr=(year+0x64-1)*0x3;}else {table_addr=(year-1)*0x3;}//定位数据表地址完成//取当年春节所在的公历月份temp1=year_code[table_addr+2]&0x60; temp1=_cror_(temp1,5);//取当年春节所在的公历月份完成//取当年春节所在的公历日temp2=year_code[table_addr+2]&0x1f; //取当年春节所在的公历日完成// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月if(temp1==0x1){ temp3=temp2-1; } else{temp3=temp2+0x1f-1; }// 计算当年春年离当年元旦的天数完成//计算公历日离当年元旦的天数,为了减少运算,用了两个表//day_code1[9],day_code2[3]//如果公历月在九月或前,天数会少于0xff,用表day_code1[9],//在九月后,天数大于0xff,用表day_code2[3]//如输入公历日为8月10日,则公历日离元旦天数为day_code1[8-1]+10-1//如输入公历日为11月10日,则公历日离元旦天数为day_code2[11-10]+10-1if (month<10){ temp4=day_code1[month-1]+day-1;}else{temp4=day_code2[month-10]+day-1;}if ((month>0x2)&&(year%0x4==0)){ //如果公历月大于2月并且该年的2月为闰月,天数加1temp4+=1;}//计算公历日离当年元旦的天数完成//判断公历日在春节前还是春节后if (temp4>=temp3){ //公历日在春节后或就是春节当日使用下面代码进行运算temp4-=temp3;month=0x1;month_p=0x1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月flag2=get_moon_day(month_p,table_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0flag_y=0;if(flag2==0)temp1=0x1d; //小月29天else temp1=0x1e; //大小30天temp2=year_code[table_addr]&0xf0;temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0则该年无闰月while(temp4>=temp1){temp4-=temp1;month_p+=1;if(month==temp2){flag_y=~flag_y;if(flag_y==0)month+=1;}else month+=1;flag2=get_moon_day(month_p,table_addr);if(flag2==0)temp1=0x1d;else temp1=0x1e;}day=temp4+1;}else{ //公历日在春节前使用下面代码进行运算temp3-=temp4;if (year==0x0){year=0x63;c=1;}else year-=1;table_addr-=0x3;month=0xc;temp2=year_code[table_addr]&0xf0;temp2=_cror_(temp2,4);if (temp2==0)month_p=0xc; elsemonth_p=0xd; ////month_p为月份指向,如果当年有闰月,一年有十三个月,月指向13,无闰月指向12flag_y=0;flag2=get_moon_day(month_p,table_addr);if(flag2==0)temp1=0x1d;else temp1=0x1e;while(temp3>temp1){temp3-=temp1;month_p-=1;if(flag_y==0)month-=1;if(month==temp2)flag_y=~flag_y;flag2=get_moon_day(month_p,table_addr);if(flag2==0)temp1=0x1d;else temp1=0x1e;}day=temp1-temp3+1;}c_moon=c; //HEX->BCD ,运算结束后,把数据转换为BCD数据temp1=year/10;temp1=_crol_(temp1,4);temp2=year%10;year_moon=temp1|temp2;temp1=month/10;temp1=_crol_(temp1,4);temp2=month%10;month_moon=temp1|temp2;temp1=day/10;temp1=_crol_(temp1,4);temp2=day%10;day_moon=temp1|temp2;}

附:http://www.jh-/archives/7125

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。