单片机万年历程序

合集下载

单片机制作的12864液晶万年历_C语言程序

单片机制作的12864液晶万年历_C语言程序

#include<reg52.H>#include<intrins.h>#include<math.h>////////////////////////////////////////DS18B20#define uchar unsigned char#define uint unsigned intsbit DQ = P1 ^ 0; // 定义DS18B20的单总线#define Lcd_Bus P0sbit LCD_RS=P2^7; //数据/命令选择sbit LCD_RW=P2^6; //读写控制sbit LCD_EN=P2^5; //片选信号#define LCD_DA TA P0 //8 bit数据线#define LOW 0#define HIGH 1//初始化指令#define CLEAR_SCREEN 0x01 //清屏指令:清屏且AC值为00H#define AC_INIT 0x02 //将AC设置为00H。

且游标移到原点位置#define CURSE_ADD 0x06 //设定游标移到方向及图像整体移动方向(默认游标右移,图像整体不动)#define FUN_MODE 0x30 //工作模式:8位基本指令集#define DISPLAY_ON 0x0c //显示开,显示游标,且游标位置反白#define DISPLAY_OFF 0x08 //显示关#define CURSE_DIR 0x14 //游标向右移动:AC=AC+1#define SET_CG_AC 0x40 //设置AC,范围为:00H~3FH#define SET_DD_AC 0x80#define FUN_MODEK 0x36 //扩展指令集,8位指令#define X1address 0x80 //上半屏X轴起始地址#define X2address 0x88 //下半屏X轴起始地址#define Y address 0x80 //Y轴起始地址#define Set_Column 0x40#define Set_Page 0xB8uchar code shu[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};uchar code xingqi[]="星期";uchar code hanzishu[]="一二三四五六日";unsigned char TempData = 0;double DS18B20Data;uint jiemian=0;//////////////////////////////////////////DS1302sbit DS1302_RST = P1^4;sbit DS1302_IO = P1^3;sbit DS1302_SCK = P1^2;unsigned char table[]={0X00,0X18,0X0c,0X15,0X06,0X01,0X0a};//0s 24min 12hour 14ri 5yue 5zhou 2010nianunsigned char data table1[7];//uchar code table2[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};uchar code shuzi[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,};unsigned char aa,bb;uchar miaoge,miaoshi,fenge,fenshi,shige,shishi,rige,rishi,yuege,yueshi,zhou,niange,nianshi;uchar shezhinum=0;//unsigned char shi,ge;////////////////////////////////////////////////键盘扫描sbit key_mnue = P3^7;sbit key_jia = P2^0;sbit key_jian = P2^1;sbit key_enter= P2^2;uchar key_number=0;//int nian,yue,ri,shi,fen;/////////////////////////////////////////////////红外sbit HW_IRQ = P3 ^ 2; // 定义38KHz一体化红外接收头与CPU的连接unsigned char DT;static unsigned char data IRCode[3],IRCON,IRCON2;//////////////////////////////////////////////////蜂鸣器sbit BEEP =P1^1;/****************************************************************************** *************** 函数名称:DS18B20_Delay()* 功能:DS18B20软件延时专用* 入口参数:count 延时参数,值越大,延时越长* 出口参数:无******************************************************************************* **************/void DS18B20_Delay(uint count) //延时函数{while(count--);}/****************************************************************************** *************** 函数名称:DS18B20_Init()* 功能:DS18B20初始化* 入口参数:无* 出口参数:返回值为1则初始化成功* 返回值为0则初始化失败******************************************************************************* **************/uchar DS18B20_Init(void){uchar x = 0;DQ = 1; // DQ复位DS18B20_Delay(8); // 稍做延时DQ = 0; // 单片机将DQ拉低DS18B20_Delay(80); // 精确延时,大于480usDQ = 1; // 拉高总线DS18B20_Delay(14); // 稍做延时后x = DQ; // 若x=0则初始化成功,若x=1则初始化失败DS18B20_Delay(20);return (~x);}/****************************************************************************** *************** 函数名称:DS18B20_Read1Byte()* 功能:从DS18B20读一个字节* 入口参数:无* 出口参数:从DS18B20读出的一个字节******************************************************************************* **************/uchar DS18B20_Read1Byte(void){uchar i = 0;uchar dat = 0;for(i=8;i>0;i--){DQ = 0; // 给脉冲信号dat >>= 1;DQ = 1; // 给脉冲信号if(DQ) dat |= 0x80;DS18B20_Delay(4);}return (dat);}/****************************************************************************** *************** 函数名称:DS18B20_Write1Byte()* 功能:向DS18B20写一个字节* 入口参数:dat 要写入DS18B20的一字节数据* 出口参数:无******************************************************************************* **************/void DS18B20_Write1Byte(uchar dat){uchar i = 0;for(i=8;i>0;i--){DQ = 0;DQ = dat&0x01;DS18B20_Delay(5);DQ = 1;dat>>=1;}}/****************************************************************************** *************** 函数名称:Read_Disp_Temperature()* 功能:读取并显示温度* 入口参数:无* 出口参数:无******************************************************************************* **************/double Read_Disp_Temperature(void){uint a = 0, b = 0, c = 0,d = 0,e = 0, t = 0;double tt = 0;DS18B20_Init();DS18B20_Write1Byte(0xCC); // 跳过读序号列号的操作DS18B20_Write1Byte(0x44); // 启动温度转换DS18B20_Init();DS18B20_Write1Byte(0xCC); // 跳过读序号列号的操作DS18B20_Write1Byte(0xBE); // 读取温度寄存器a = DS18B20_Read1Byte();b = DS18B20_Read1Byte();t = b;t <<= 8;t = t | a;//tt = t * 0.0625;return t;}//***************************************************************** //函数名称:delay(uint t)延时函数。

单片机万年历温度检测C程序(1302+1820+1602+51)

单片机万年历温度检测C程序(1302+1820+1602+51)

1302+1820+1602+51万年历#include<reg51.H>#include<INTRINS.H>#define uchar unsigned char#define uint unsigned int#define TIME (0X10000-50000)#define FLAG 0XEF //闹钟标志//引脚连接图sbit rst=P3^5;sbit clk=P3^4;sbit io=P3^3;sbit rs=P1^5;sbit rw=P1^6;sbit e=P1^7;sbit DQ=P1^4; //温度输入口sbit beep=P3^2;sbit ACC_7=ACC^7;//全局变量及常量定义uchar i=20,j,time1[16];uchar alarm[2],time2[15],time[3];uchar code Day[]={31,28,31,30,31,30,31,31,30,31,30,31};//12个月的最大日期(非闰年)//音律表uint code table1[]={64260,64400,64524,64580,64684,64777,64820,64898,64968,65030,65058,65110,65157,65178,65217};//发声部分的延时时间uchar code table2[]={0x82,1,0x81,0xf4,0xd4,0xb4,0xa4,0x94,0xe2,1,0xe1,0xd4,0xb4,0xc4,0xb4,4,0};//LCD自建字uchar code tab[]={0x18,0x1b,5,4,4,5,3,0,0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,//月0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00};//日//*******温度小数部分用查表法**********//uchar codeditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x0 7,0x08,0x08,0x09,0x09};//闹钟中用的全局变量uchar th1,tl1;uchar temp_ioa[2]={0x00,0x00}; // 读出温度暂放bit flag; //18b20存在标志位/***********11微秒延时函数**********/delay(uint t){for(;t>0;t--);}/***********18B20复位函数**********/ow_reset(void){uchar i;DQ=1;_nop_();_nop_();DQ=0; //delay(50); // 550usDQ=1; //delay(6); // 66usfor(i=0;i<0x30;i++){if(!DQ)goto d1;}flag=0; //清标志位,表示ds1820不存在DQ=1;return;d1: delay(45); //延时500usflag=1;DQ=1; //置标志位,表示ds1820存在}/**********18B20写命令函数*********///向 1-WIRE 总线上写一个字节void write_byte(uchar val){uchar i;for (i=8; i>0; i--) //{DQ=1;_nop_();_nop_();DQ=0;_nop_();_nop_();_nop_();_nop_();_nop_();//5us DQ=val&0x01; //最低位移出delay(6); //66usval=val/2; //右移一位}DQ=1;delay(1);}/*********18B20读1个字节函数********///从总线上读取一个字节uchar read_byte(void){uchar i;uchar value=0;for (i=8;i>0;i--){DQ=1;_nop_();_nop_();value>>=1;DQ=0; //_nop_();_nop_();_nop_();_nop_(); //4usDQ=1;_nop_();_nop_();_nop_();_nop_(); //4usif(DQ)value|=0x80;delay(6); //66us}DQ=1;return(value);}/***********读出温度函数**********/read_temp(){ow_reset(); //总线复位if(!flag) //判断ds1820是否存在?若ds18b20不存在则返回 return;//write_byte(0xCC); // Skip ROM//write_byte(0x44); // 发转换命令//delay(70);write_byte(0xCC); //发Skip ROM命令write_byte(0xBE); //发读命令temp_ioa[0]=read_byte(); //温度低8位temp_ioa[1]=read_byte(); //温度高8位ow_reset();write_byte(0xCC); // Skip ROMwrite_byte(0x44); // 发转换命令}/***********温度数据处理函数**********/work_temp(){uchar n=0,m;if(temp_ioa[1]>127)//负温度求补码{temp_ioa[1]=(256-temp_ioa[1]);temp_ioa[0]=(256-temp_ioa[0]);n=1;}time2[13]=ditab[temp_ioa[0]&0x0f]+'0';time2[12]='.';m=((temp_ioa[0]&0xf0)>>4)|((temp_ioa[1]&0x0f)<<4);// if(n){m-=16;}time2[9]=m/100+'0';time2[11]=m%100;time2[10]=time2[11]/10+'0';time2[11]=time2[11]%10+'0';if(time2[9]=='0')//最高位为0时都不显示{time2[9]=0x20;if(n)//负温度时最高位显示"-"{time2[9]='-';}if(time2[10]=='0'){if(n){time2[10]='-';time2[9]=0x20;}elsetime2[10]=0x20;if(time2[11]=='0'&&time2[13]=='0')time2[11]=time2[12]=0x20;}}}delay1ms(uchar time) //延时1ms{uchar i,j;for(i=0;i<time;i++){for(j=0;j<250;j++);}}//LCD驱动部分enable(){rs=0;rw=0;e=0;delay1ms(3);e=1;}write2(uchar i){P0=i;rs=1;rw=0;e=0;delay1ms(2);e=1;}write1(uchar data *address,m) {uchar i,j;for(i=0;i<m;i++,address++) {j=*address;write2(j);}}//LCD显示lcdshow(){P0=0XC; //显示器开、光标关enable();P0=0x80; //写入显示起始地址enable();write1(time1,16);P0=0xc1; //写入显示起始地址enable();write1(time2,15);}//自建字zijianzi(){uchar i;P0=0x40;enable();for(i=0;i<32;i++){write2(tab[i]);delay1ms(2);}}//DS1302读写子程序write(uchar address){uchar i;clk=0;_nop_();rst=1;_nop_();for(i=0;i<8;i++){io=address&1;_nop_();clk=1;address>>=1;clk=0;}}uchar read(){uchar i,j=0;for(i=0;i<8;i++){j>>=1;_nop_();clk=0;_nop_();if(io)j|=0x80;_nop_();clk=1;}return(j);}//部分显示数据初始化timestart(){time1[1]=time1[13]=time2[8]=time2[9]=time2[10]=0x20,time2[14]=0; time1[6]=1,time1[9]=2,time1[12]=3,time1[2]='2',time1[3]='0'; time1[14]='W',time2[2]=time2[5]=':';write(0xc1);alarm[0]=read();rst=0;write(0xc3);alarm[1]=read();rst=0;write(0xc5);time1[0]=read();rst=0;}//读取时间readtime(){uchar i,m,n;write(0x8d); //读取年份m=read();rst=0;time1[4]=m/16+0x30;time1[5]=m%16+0x30;write(0x8b); //读取星期m=read();rst=0;time1[15]=m+0x30;for(i=7,n=0x89;i<11;i+=3,n-=2) //读取月份和日期{write(n);m=read();rst=0;time1[i]=m/16+0x30;time1[i+1]=m%16+0x30;}for(m=0,i=0,n=0x85;i<7;i+=3,n-=2,m++) //读取时,分,秒{write(n);time[m]=read();rst=0;time2[i]=time[m]/16+0x30;time2[i+1]=time[m]%16+0x30;}}time0() interrupt 1 using 1{i--;if(i==0){if(j!=0)j--;i=20;}TH0=TIME/256,TL0=TIME%256;}//闹钟部分intime1() interrupt 3{TH1=th1,TL1=tl1;beep=!beep;}showalarm(){uchar i,j,a,b,n;ET1=1;for(j=0;j<6;j++){i=0;while(1){a=table2[i];if(a==0)break;b=a&0xf;a>>=4;if(a==0){TR1=0;goto D1;}a=((--a)<<1)/2;TH1=th1=table1[a]/256,TL1=tl1=table1[a]%256; TR1=1;D1: do{b--;for(n=0;n<3;n++){readtime();lcdshow();P2=0xf7;if(P2==0xe7){delay1ms(100);if(P2==0xe7){TR1=0;ET1=0;return;}}}}while(b!=0);i++;}TR1=0;}ET1=0;}//根据日期的变动自动调整星期uchar setweek(){uchar i=5,j,n;j=(time1[4]&0xf)*10+(time1[5]&0xf); n=j/4;i=i+5*n;n=j%4;if(n==1)i+=2;else if(n==2)i+=3;else if(n==3)i+=4;j=(time1[7]&0xf)*10+(time1[8]&0xf); if(j==2)i+=3;else if(j==3)i+=3;else if(j==4)i+=6;else if(j==5)i+=1;else if(j==6)i+=4;else if(j==7)i+=6;else if(j==8)i+=2;else if(j==9)i+=5;else if(j==11)i+=3;else if(j==12)i+=5;if(n==0)if(j>2)i++;j=(time1[10]&0xf)*10+(time1[11]&0xf);i+=j;i%=7;if(i==0)i=7;return(i);}//设置时间settime(){uchar i=0x85,year,month,day,n;time2[6]=time2[7]=0x30,time1[14]=time1[15]=0x20; lcdshow();while(1){P0=0xe; //显示器开、光标开enable();P0=i; //定光标enable();P2=0xf7;if(P2!=0XF7){delay1ms(100); //延时0.1s去抖动if(P2!=0XF7){j=7;if(P2==0X77){i+=3;if(i==0x8e)i=0xc2;else if(i>0xc5)i=0x85;}else if(P2==0xb7){year=(time1[4]&0xf)*10+(time1[5]&0xf); month=(time1[7]&0xf)*10+(time1[8]&0xf); day=(time1[10]&0xf)*10+(time1[11]&0xf); if(i==0x85){year++;if(year>99)year=0;if((year%4)!=0)if(month==2&&day==29)day=28;}else if(i==0x88){month++;if(month>12)month=1;if(day>Day[month-1]){day=Day[month-1];if(month==2&&(year%4)==0)day=29;}}else if(i==0x8b){day++;if(day>Day[month-1]){if(month==2&&(year%4)==0){if(day>29)day=1;}if(month!=2)day=1;}}else if(i==0xc2){n=(time2[0]&0xf)*10+(time2[1]&0xf);n++;if(n>23)n=0;time2[0]=n/10+0x30;time2[1]=n%10+0x30;}else{n=(time2[3]&0xf)*10+(time2[4]&0xf);n++;if(n>59)n=0;time2[3]=n/10+0x30;time2[4]=n%10+0x30;}time1[4]=year/10+0x30;time1[5]=year%10+0x30;time1[7]=month/10+0x30;time1[8]=month%10+0x30;time1[10]=day/10+0x30;time1[11]=day%10+0x30;lcdshow();}else if(P2==0xd7){write(0x8c);write((time1[4]&0xf)*16+(time1[5]&0xf));rst=0;write(0x8a);write(setweek());rst=0;for(i=7,n=0x88;i<11;i+=3,n-=2){write(n);write((time1[i]&0xf)*16+(time1[i+1]&0xf)); rst=0;}for(i=0;i<7;i+=3,n-=2){write(n);write((time2[i]&0xf)*16+(time2[i+1]&0xf)); rst=0;}TR0=0;time1[14]='W';return;}else{TR0=0;time1[14]='W';return;}}}if(j==0){TR0=0;time1[14]='W';return;}}}//设置闹钟setalarm(){uchar i,n;for(i=1;i<16;i++){time1[i]=0x20;}time2[0]=alarm[0]/16+0x30;time2[1]=(alarm[0]&0xf)+0x30;time2[3]=alarm[1]/16+0x30;time2[4]=(alarm[1]&0xf)+0x30;time2[6]=time2[7]=0x30;lcdshow();i=0xc2;while(1){P0=0xe; //显示器开、光标开enable();P0=i; //定光标enable();P2=0xf7;if(P2!=0XF7){delay1ms(100); //延时0.1s去抖动 if(P2!=0XF7){j=7;if(P2==0X77){i+=3;if(i>0xc5)i=0xc2;}else if(P2==0xb7){if(i==0xc2){n=(time2[0]&0xf)*10+(time2[1]&0xf);n++;if(n>23)n=0;time2[0]=n/10+0x30;time2[1]=n%10+0x30;}else{n=(time2[3]&0xf)*10+(time2[4]&0xf);n++;if(n>59)n=0;time2[3]=n/10+0x30;time2[4]=n%10+0x30;}lcdshow();}else if(P2==0xd7){write(0xc0);write((time2[0]&0xf)*16+(time2[1]&0xf)); rst=0;write(0xc2);write((time2[3]&0xf)*16+(time2[4]&0xf)); rst=0;time1[0]=FLAG;write(0xc4);write(time1[0]);rst=0;TR0=0;timestart();return;}else{TR0=0;timestart();return;}}}if(j==0){TR0=0;timestart();return;}}}main(){IE=0X82;TMOD=0x11;write(0x8E); //禁止写保护write(0);rst=0;P0=1; //清屏并光标复位enable();P0=0X38; //设置显示模式:8位2行5x7点阵enable();P0=6; //文字不动,光标自动右移enable();zijianzi(); //自建字timestart();while(1){readtime(); //读取时间read_temp(); //读出18B20温度数据 work_temp(); //处理温度数据lcdshow(); //显示时间if(time1[0]!=0x20)if(time[0]==alarm[0])if(time[1]==alarm[1])if(time[2]==0)showalarm();P2=0xf7;if((P2&0XF0)!=0XF0){delay1ms(100); //延时0.1s去抖动 if((P2&0XF0)!=0XF0){j=7;TH0=TIME/256,TL0=TIME%256;TR0=1;if(P2==0x77){settime();}else if(P2==0XB7){setalarm();}else if(P2==0XD7){TR0=0;if(time1[0]==FLAG)time1[0]=0x20;elsetime1[0]=FLAG;write(0xc4);write(time1[0]);rst=0;}}}delay1ms(100);}}单片机万年历——基于1302和1602(1篇)2009-12-06 21:191302和1602的接法在我相册里面使用1602液晶显示DS1302+c51时钟[注12M晶振]=============================================================*/#include <AT89x52.h>#include <string.h>#include < intrins.h >#define delayNOP() ; {_nop_() ;_nop_() ;_nop_() ;_nop_() ;} ;#define LCM_RW P3_1 //定义引脚#define LCM_RS P3_0#define LCM_E P3_2#define LCM_Data P0#define Busy 0x80 //用于检测LCM状态字中的Busy标识#define uchar unsigned charsbit T_CLK=P2^4;sbit T_IO =P2^5;sbit T_RST=P2^6;sbit ACC0=ACC^0;sbit ACC7=ACC^7;sbit system=P1^1; //模式sbit TimerUp=P1^2; //时间加sbit TimerDown=P1^0; //时间减sbit Speaker=P2^1; //蜂鸣器void Set(uchar,uchar); //根据选择调整相应项目void RTInputByte(uchar); //输入 1Byte */uchar RTOutputByte(void); //输出 1Byte */void W1302(uchar, uchar); //向DS1302写入一个字节uchar R1302(uchar); //从DS1302读出一个字节void Set1302(uchar * ); //设置时间void KeySound(); //按键音void ClockSound(); //闹铃蜂鸣声uchar id,msec,model,LedTime,d;bit a,b; //闪烁标志位uchar inittime[7]={0x00,0x10,0x19,0x29,0x03,0x04,0x06}; //初始化后设置为:06年3月29日星期3 19点10分0秒void Out_LCM(void); //显示屏幕void model0(void); //显示输出void model1(void);void model2(void);void model3(void);void model4(void);void model5(void);void model6(void);void id_case1_key(); //项目调整void Modset(); //模式键处理void WriteDataLCM(uchar WDLCM);void WriteCommandLCM(uchar WCLCM,BuysC);uchar ReadStatusLCM(void);void LCMInit(void);void OutputInformation(uchar X, uchar Y, uchar DData);void DisplayListChar(uchar X, uchar Y, uchar code *DData);void Delay5Ms(void);void Delay400Ms(void);void systemsetup(void); //进入系统设置uchar code systemp[]={"System.setup"}; //字符串输出(系统设定)uchar code TIMER[]={"Time"}; //字符串输出(时间)uchar code DATE[]={"Date"}; //字符串输出(日期)uchar code alarmclock[]={"Clock"}; //字符串输出(闹铃)uchar code lamp[]={"Lamp"}; //字符串输出(背光灯)uchar code reset[]={"Reset"}; //字符串输出(时间归零)uchar code exit[]={"Exit"}; //字符串输出(退出)uchar code set[]={"Set"}; //字符串输出(设置)uchar code sec[]={"sec"}; //字符串输出(秒)uchar code ClockSet[]={"ClockSet"}; //字符串输出(闹铃设置)uchar code ClockOn[]={"ON"}; //字符串输出(ON)uchar code ClockOff[]={"OFF"}; //字符串输出(OFF)void timesetup(void); //时间设置void datesetup(void); //日期设置void alarmclockset(void); //闹铃设置void lampsetup(void); //背光灯设置void timereset(void); //时间清零/*******************************************************************/ void delay1(int ms){unsigned char y ;while(ms--){for(y = 0 ; y<250 ; y++){_nop_() ;_nop_() ;_nop_() ;_nop_() ;}}}void main(void){Speaker=1; //关闭蜂鸣器LED=0; //打开LED ,低电平有效,外接9012驱动Delay400Ms(); //启动等待,等LCM讲入工作状态Delay400Ms();LCMInit(); //LCM初始化TMOD=0x01; //16位定时TH0=0x3c; //50msTL0=0xb0;EA=1;TR0=1;ET0=1;while(1){if(TimerDown==0) //左移键按下后把背光灯打开{Delay5Ms();if(TimerDown==0){KeySound(); //蜂鸣声LED=0; //打开背光灯LedTime=R1302(0xc1)/16*10+R1302(0xc1)%16;} //取出背光灯时间并转换为十进制数}b=1; //模式键是否动作标志。

51单片机12864万年历(图片+主要程序C语言)

51单片机12864万年历(图片+主要程序C语言)

51单片机做的12864万年历这是我花了一个星期时间做的万年历;一开始是一些图片,后面是这个制作的C语言源程序;希望大家稀饭:Main.c#include "reg52.h"#include "macroconst.h"#include "keyscan.h"#include "screen.h"#include "18b20.h"#include "ds1302.h"#include "atao_12864.h"#include "24c02.h"idata unsigned char state1[1];unsigned char screennum=1;//屏幕号#define time_task_sec 200 //节拍200Hz#define clock 24000000 //晶振#define max_task 4 //最大任务数idata unsigned char task_delay[4];#define task_delay0 time_task_sec/1 //2赫兹18b20 #define task_delay1 time_task_sec/5 //10赫兹屏幕#define task_delay2 time_task_sec/3 //4赫兹1302 #define task_delay3 time_task_sec/20 //20赫兹键盘void init(){unsigned char i;init18b20();ds1302_init();initina1(); //---------------------------LCD字库初始化程序RdFromROM(state1,8,1);state[0]=state1[0]; //-------界面RdFromROM(state1,15,1);state[1]=state1[0];//------- 闹钟RdFromROM(state1,11,1);key_sound=state1[0];//-------按键音// state[2]=state1[2];// state[3]=state1[3];screennum=1;//------默认一号屏for(i=0;i<max_task;i++)//清除任务延时{task_delay[i]=0;}TMOD=0x11;EA=1;TH0=255-clock/time_task_sec/12/526;TL0=255-clock/time_task_sec/12%526;ET0=1;TR0=1;}void main(){init();while(1){ if(task_delay[0]==0){readtemp(); task_delay[0]=task_delay0;} if(task_delay[3]==0){MenuCheckKey();task_delay[3]=task_delay1;}if(task_delay[1]==0){screen_select(screennum);task_delay[1]=task_delay1;} if(task_delay[2]==0){ds1302_read_time(); task_delay[2]=task_delay0;} }}void timer0() interrupt 1{uchar i=0;TH0=255-clock/time_task_sec/12/526;TH1=255-clock/time_task_sec/12%526;for(i=0;i<max_task;i++)if(task_delay[i]) task_delay[i]--; // miao=21,fen=33,xiaomiao=94;if(ma_start==1){tt++;if(tt==8){tt=0;xiaomiao++;if(xiaomiao==99){xiaomiao=0;miao++;if(miao==60){miao=0;fen++;if(fen==99){fen=0;}}}}}}Screen.c:#include "reg52.h"#include "macroconst.h"#include "delay.h"#include "18b20.h"#include "atao_12864.h"#include "ds1302.h"#include "24c02.h"#include "festival.h"#include "keyscan.h"idata unsigned char miao=0,fen=78,xiaomiao=0,tt=0;idata unsigned char bai,shi,ge,shinian,genian,shiyue,geyue,shiri,geri,shishi,geshi,shifen,gefen,shimi ao,gemiao;idata unsigned char Number[4];unsigned char code mainmenu[]={" 主菜单""3.查询 4.温度""1.调时 2.秒表""5.设置 6.返回"};unsigned char code setmenu[]={" 设置""3.查温 4.按键""1.界面 2.闹铃""5.报时 6.返回"};void gettime(){shinian=(time_buf1[1]%100)/10;genian=(time_buf1[1]%100)%10;shiyue=(time_buf1[2])/10;geyue=(time_buf1[2])%10;shiri=(time_buf1[3])/10;geri=(time_buf1[3])%10;shishi=(time_buf1[4])/10;geshi=(time_buf1[4])%10;shifen=(time_buf1[5])/10;gefen=(time_buf1[5])%10;shimiao=(time_buf1[6])/10;gemiao=(time_buf1[6])%10;}void screen_select(uchar num){// initina1();gettime() ;if(num==1) //----------主界面{Lcd_Set_Post(1,0);TransferData(shinian+48,1);//----------------------年TransferData(genian+48,1);TransferData('/',1);TransferData(shiyue+48,1);//----------------------月TransferData(geyue+48,1);TransferData('/',1);TransferData(shiri+48,1);//----------------------日TransferData(geri+48,1);Lcd_Set_Post(1,4);if(state[1]==1)//----------------闹铃的状态图标{TransferData(' ',1);TransferData(14,1);}Lcd_Set_Post(1,5);if(key_sound==1){TransferData(13,1);}if(state[0]==0){Lcd_Set_Post(1,7); //----------------农历年nian(0);Lcd_Set_Post(2,0);//----------------------- 农历日月LCD_WRITE_CHAR("农历");Conversion(0,time_buf[1],time_buf[2],time_buf[3]);TransferData(month_moon/16+'0',1);TransferData(month_moon%16+'0',1);TransferData('/',1);TransferData(day_moon/16+'0',1);TransferData(day_moon%16+'0',1);}if(state[0]==1){Lcd_Set_Post(3,5);}else {Lcd_Set_Post(2,5);}LCD_WRITE_CHAR("星期");//----------------------星期if(time_buf1[7]==7) {LCD_WRITE_CHAR("日");}if(time_buf1[7]==6) {LCD_WRITE_CHAR("六");}if(time_buf1[7]==5) {LCD_WRITE_CHAR("五");}if(time_buf1[7]==4) {LCD_WRITE_CHAR("四");}if(time_buf1[7]==3) {LCD_WRITE_CHAR("三");}if(time_buf1[7]==2) {LCD_WRITE_CHAR("二");}if(time_buf1[7]==1) {LCD_WRITE_CHAR("一");}Lcd_Set_Post(3,0);TransferData(shishi+48,1);//----------------------时TransferData(geshi+48,1);TransferData(58,1);TransferData(shifen+48,1);//----------------------分TransferData(gefen+48,1);TransferData(58,1);TransferData(shimiao+48,1);//----------------------秒TransferData(gemiao+48,1);if(state[0]==0){bai=(temph/100); //---------------------------------温度shi=(temph%100)/10;ge=(temph%100)%10;Lcd_Set_Post(3,4);if(zerotemp==0){TransferData(' ',1);TransferData(' ',1);} Lcd_Set_Post(3,5);TransferData(shi+48,1);TransferData(ge+48,1);TransferData('.',1);TransferData(templ+48,1);TransferData(0xa1,1);TransferData(0xe6,1);Lcd_Set_Post(4,0);festival(time_buf[2],time_buf[3],time_buf[4]);//农历}}if(num==3)//----------主菜单{Lcd_Set_Post(1,0);//--------设置显示位置LCD_WRITE_CHAR(mainmenu);}if(num==4)//----------设置菜单{Lcd_Set_Post(1,0);//--------设置显示位置LCD_WRITE_CHAR(setmenu);}if(num==5)//----------时间设置{Lcd_Set_Post(1,2);//--------设置显示位置LCD_WRITE_CHAR("时间设置");Lcd_Set_Post(2,1);TransferData(shinian+48,1);//----------------------年TransferData(genian+48,1);LCD_WRITE_CHAR("年");TransferData(shiyue+48,1);//----------------------月TransferData(geyue+48,1);LCD_WRITE_CHAR("月");TransferData(shiri+48,1);//----------------------日TransferData(geri+48,1);LCD_WRITE_CHAR("日");TransferData(time_buf1[7]+48,1);//---------------星期Lcd_Set_Post(3,2);TransferData(shishi+48,1);//--------------------时TransferData(geshi+48,1);LCD_WRITE_CHAR("时");TransferData(shifen+48,1);//---------------------分TransferData(gefen+48,1);LCD_WRITE_CHAR("分");TransferData(shimiao+48,1);//--------------------秒TransferData(gemiao+48,1);}if(num==6)//----------秒表{uchar shi1,shi2,shi3,ge1,ge2,ge3;Lcd_Set_Post(1,3);//--------设置显示位置LCD_WRITE_CHAR("秒表");Lcd_Set_Post(3,2);shi1=fen/10;ge1=fen%10;TransferData(shi1+48,1);TransferData(ge1+48,1);TransferData(':',1);shi2=miao/10;ge2=miao%10;TransferData(shi2+48,1);TransferData(ge2+48,1);TransferData(':',1);shi3=xiaomiao/10;ge3=xiaomiao%10;TransferData(shi3+48,1);TransferData(ge3+48,1);}if(num==8){Lcd_Set_Post(1,2);//--------农历查询LCD_WRITE_CHAR("温度查询");Lcd_Set_Post(3,0);//--------农历查询LCD_WRITE_CHAR("最高温度"); TransferData(':',1);RdFromROM(Number,0,4);if(Number[3]==0){TransferData('-',1);}Lcd_Set_Post(3,5);TransferData(Number[0]+48,1); TransferData(Number[1]+48,1); TransferData('.',1);TransferData(Number[2]+48,1); TransferData(0xa1,1);TransferData(0xe6,1); Lcd_Set_Post(4,0);//--------农历查询LCD_WRITE_CHAR("最低温度"); TransferData(':',1);RdFromROM(Number,4,4);if(Number[3]==0){TransferData('-',1);}Lcd_Set_Post(4,5);TransferData(Number[0]+48,1); TransferData(Number[1]+48,1); TransferData('.',1);TransferData(Number[2]+48,1); TransferData(0xa1,1);TransferData(0xe6,1); }if(num==9)//----------界面菜单{Lcd_Set_Post(1,2);//--------设置显示位置LCD_WRITE_CHAR("界面选择");Lcd_Set_Post(2,0);TransferData(1+48,1);TransferData('.',1);LCD_WRITE_CHAR("多功能界面");Lcd_Set_Post(3,0);TransferData(2+48,1);TransferData('.',1);LCD_WRITE_CHAR("简洁界面");}。

单片机万年历C语言程序完整

单片机万年历C语言程序完整
sbit out = P2^3; //立刻跳出调整模式按钮
sbit DQ = P3^0;
char done,count,temp,flag,up_flag,down_flag,t_value;
uchar TempBuffer[5],week_value[2];
void show_time(); //液晶显示程序
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[11];
unsigned char TimeString[9];
}SYSTEMTIME;//定义的时间类型
#define AM(X)X
#define PM(X)(X+12)//转成24小时制
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); //地址,命令
ucData = DS1302OutputByte(); //读1Byte数据
DS1302_CLK = 1;
#define DS1302_SECOND0x80 //时钟芯片的寄存器位置,存放时间
#define DS1302_MINUTE0x82
#define DS1302_HOUR0x84
#define DS1302_WEEK0x8A
#define DS1302_DAY0x86
#define DS1302_MONTH0x88
#define uchar unsigned char

单片机实训--万年历程序

单片机实训--万年历程序

#include<reg52.h>typedef unsigned char uint8; #define clrdp 0x01#define enmode 0x06#define curback 0x02#define ondp 0x0f#define offdp 0x08#define movd 0x14#define funset 0x38#define bus P1#define contw 0x8e#define secw 0x80#define secr 0x81#define minw 0x82#define minr 0x83#define hourw 0x84#define hourr 0x85#define datw 0x86#define datr 0x87#define monthw 0x88#define monthr 0x89#define dayw 0x8a#define dayr 0x8b#define yearw 0x8c#define yearr 0x8dsbit K1=P2^7;sbit K2=P2^5;sbit K3=P2^4;sbit RS=P3^7;sbit RW=P3^6;sbit EN=P3^5;sbit DIO=P2^1;sbit RST=P2^2;sbit SCK=P2^0;bit flag=0;uint8 sel;uint8 time[7]={0};uint8 time_disp[32]={0}; void init_1602(void);void write_com(uint8 comd); void write_dat(uint8 wdata); void delay(void);void long_dleay(void);void init_1302(void);uint8 read_1302(uint8 addr);void write_1302(uint8 addr,uint8 wdata); void disp(void);void read_time(void);void sepr_time(void);void chk_key(void);uint8 bcd_dec(uint8 wdata);uint8 dec_bcd(uint8 wdata);void main(void){init_1302();init_1602();while(1){chk_key();read_time();sepr_time();disp();}}//1302初始化void init_1302(void){write_1302(contw,0);write_1302(secw,0);}//写1302寄存器void write_1302(uint8 addr,uint8 wdata) {uint8 i;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){DIO=wdata&0x01;SCK=1;SCK=0;wdata>>=1;}RST=0;}//读1302寄存器uint8 read_1302(uint8 addr) {uint8 i;uint8 temp;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){temp>>=1;if(! DIO){temp&=0x7f;}else{temp|=0x80;}SCK=1;SCK=0;}RST=0;return temp;}//读1302时间寄存器void read_time(void){uint8 i;uint8 temp;for(i=0;i<7;i++){temp=read_1302(0x81+i*2);time[i]=temp;}time[0]&=0x7f;}//显示时间void disp(void){uint8 i;write_com(0x80);for(i=0;i<16;i++){write_dat(time_disp[i]);}write_com(0xc0);for(i=16;i<32;i++){write_dat(time_disp[i]);}}//分离时间void sepr_time(void){time_disp[0]='-';time_disp[1]='-';time_disp[2]='2';time_disp[3]='0';time_disp[4]=(time[6]>>4)+0x30;time_disp[5]=(time[6]&0x0f)+0x30;time_disp[6]='-';time_disp[7]=(time[4]>>4)+0x30;time_disp[8]=(time[4]&0x0f)+0x30;time_disp[9]='-';time_disp[10]=(time[3]>>4)+0x30;time_disp[11]=(time[3]&0x0f)+0x30;time_disp[12]='-';time_disp[13]='-';time_disp[14]=(time[5]>>4)+0x30;time_disp[15]=(time[5]&0x0f)+0x30;time_disp[16]='-';time_disp[17]='-';time_disp[18]='-';time_disp[19]='-';time_disp[20]=(time[2]>>4)+0x30;time_disp[21]=(time[2]&0x0f)+0x30;time_disp[22]='-';time_disp[23]=(time[1]>>4)+0x30;time_disp[24]=(time[1]&0x0f)+0x30;time_disp[25]='-';time_disp[26]=(time[0]>>4)+0x30;time_disp[27]=(time[0]&0x0f)+0x30;time_disp[28]='-';time_disp[29]='-';time_disp[30]='-';time_disp[31]='-';}void init_1602(void){write_com(offdp);write_com(funset);write_com(enmode);write_com(movd);write_com(ondp);write_com(clrdp);}void write_com(uint8 comd){RS=0;RW=0;bus=comd;EN=1;EN=0;delay();}void write_dat(uint8 wdata){RS=1;RW=0;bus=wdata;EN=1;EN=0;delay();}void delay(void){uint8 t;for(t=0;t<250;t++);}void long_delay(void){uint8 t;for(t=0;t<200;t++){delay();}}void chk_key(void){uint8 buf;uint8 temp;if(!K1){long_delay();if(!K1){if(flag){if((sel>=0)&&(sel<8)){sel++;}else if(sel==8){temp=read_1302(secr);temp&=0x7f;write_1302(secw,temp);sel=0;flag=0;}else{sel=8;}}else{temp=read_1302(secr);temp|=0x80;write_1302(secw,temp);flag=1;}}}else if(!K2){long_delay();if(!K2){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf<99){buf++;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf<7){buf++;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf<12){buf++;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf<31){buf++;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf<23){buf++;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}else if(!K3){long_delay();if(!K3){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}}uint8 dec_bcd(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata/10;j=wdata%10;temp=i;temp=temp<<4;temp=temp|j;return temp;}uint8 bcd_dec(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata>>4;j=wdata&0x0f;temp=i*10+j;return temp;。

单片机万年历程序

单片机万年历程序

#include<reg52.h>#include<INTRINS.H>#include<STDLIB.H>sbit ring=P1^7;sbit key1=P1^0;sbit key2=P1^1;sbit key3=P1^2;sbit key4=P1^3;sbit key5=P1^4;sbit key6=P1^5;sbit key7=P1^6;unsigned int code table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};static int a[10]={0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};static char c[10]={0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};static char b[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};static char diapnum[8]={0,};static char doopnum[8]={0,};int fen=0,shi=0,y=0,tian=0;static int xingqi ;void delay(int t){int j,k;for(j=0;j<t;j++)for(k=0;k<240;k++);}idoth( int num){ diapnum[1]=num/10;diapnum[0]=num%10;} //数码管位的分配idoth1( int num){ doopnum[1]=num/10;doopnum[0]=num%10;} //数码管位的分配unsigned int week( unsigned int nian,unsigned int yue,unsigned int ri ) {unsigned int temp;unsigned int x;temp = nian / 4;temp = nian + temp;temp = temp % 0x07;temp = temp + ri-1 + table_week[ yue - 1 ];if( ( nian % 4 == 0 ) && ( yue <3 ) )temp -=1;x=temp%7;if(x==0)x=8;return( x );}itime(){TMOD=0x02;TH0=0xd8;TL0=0xf0;TR0=1;} //定时计数void main(){ unsigned int time_num=0;//8unsigned int time_num1=0;unsigned int time_num2=0;unsigned int kk,gg;unsigned int ri=1,yue=1,nian=10;int g=0, h=1, z=0 ;char flag=0;bit flag_dot=1;itime();while(1){if(TF0)//0{ //1TF0=0;time_num++;}//1if(time_num>=21){ time_num=0; //2flag++;flag_dot=!flag_dot;if(flag_dot){ //3diapnum[2]=0x7f;diapnum[5]=0x7f;doopnum[2]=0x7f;doopnum[5]=0x7f; //第五位第二位亮} //3else{ //4diapnum[2]=0xff;diapnum[5]=0xff;doopnum[2]=0xff;doopnum[5]=0xff; } //4} //2if(flag>=60){flag=0; //5time_num1++;if(time_num1>=60){ //6time_num1=0;time_num2++;if(time_num2>=24){ //7time_num2=0;ri++;if(ri>=31){yue++;ri=0;{if((nian%4==1)&& (ri==28)) { yue++;ri=0;}else if((nian%4==0)&&(ri==29)) {yue++;ri=0; }{if(ri>31)yue++;ri=0;{if(ri>30)yue++;ri=0;{if(ri>31)yue++;ri=0;{if(ri>30)yue++;ri=0;{if(ri>31)yue++;ri=0;{if(ri>30)yue++;ri=0;{if(ri>30)yue++;ri=0;{ if(ri>31)yue++;ri=0;{ if(ri>30)yue++;ri=0;{if(ri>31)yue++;ri=0;} //7{if(yue>12)nian++;} }}}}}} }}} } } }} //6} //5idoth(time_num2);diapnum[7]=diapnum[1];diapnum[6]=diapnum[0];idoth(time_num1);diapnum[4]=diapnum[1];diapnum[3]=diapnum[0];idoth(flag); //十分秒idoth1(nian);doopnum[7]=doopnum[1];doopnum[6]=doopnum[0];idoth1(yue);doopnum[4]=doopnum[1];doopnum[3]=doopnum[0];idoth1(ri); //年月日xingqi=week( nian,yue,ri) ;if( time_num2==0&&time_num1==10) { ring=~ring ;}if(key1==0){ delay(50) ; fen++ ;time_num1=fen;if(fen==60) fen=0;}if(key2==0){ delay(50) ; shi++ ;time_num2=shi ; } if(key5==0){ delay(50) ;if(fen!=0) fen--;if(shi!=0) shi-- ;if(tian!=0) tian-- ;if(y!=0) yue-- ;}if(key3==0){ delay(50) ; tian++ ;ri=tian;}if(key4==0){delay(50) ; y++ ; yue=y ;if(yue>12) {yue=0; nian++;}}if(key6!=0){ P3=b[7] ; gg=doopnum[0] ; P0=~c[gg] ; kk=diapnum[0] ; P2=~a[kk]; d elay(3) ;P3=b[6] ; kk=diapnum[1] ; P2=~a[kk]; gg=doopnum[1] ; P0=~c[gg] ; delay(3) ;P3=b[5] ; P2 =diapnum[2] ; P0=doopnum[2] ; d elay(3) ;P3=b[4] ; kk=diapnum[3] ; P2=~a[kk]; gg=doopnum[3] ; P0=~c[gg] ; delay(3) ;P3=b[3] ; kk=diapnum[4] ; P2=~a[kk]; gg=doopnum[4] ; P0=~c[gg] ; delay(3) ;P3=b[2] ; P2 =diapnum[5] ; P0=doopnum[5] ; d elay(3) ;P3=b[1] ; kk=diapnum[6] ; P2=~a[kk]; gg=doopnum[6] ; P0=~c[gg] ; delay(3) ;P3=b[0] ; kk=diapnum[7] ; P2=~a[kk]; gg=doopnum[7] ; P0=~c[gg] ; delay(3) ;} //年月日时分秒的显示else {delay(50); P3=b[7] ; P2=~a[xingqi]; P0=0xff ; }}}。

基于51单片机的12864液晶显示的万年历

基于51单片机的12864液晶显示的万年历

附录程序
/*****************说明*********************************** 基于 51 单片机的 12864 液晶显示的万年历
版权所有,如需转载请通知本人,不得用于商业用途 ,仅限学习交流之用
*****************************************************************/
3
图-1 主控制系统
2.3.2 时钟电路模块的设计 DS1302 是一种高性能、低功耗、带RAM的实时时钟电路,它可以对年、月、日、周日、
时、分、秒进行计时,具有闰年补偿功能,工作电压为2.5V~5.5V。采用三线接口与CPU进 行同步通信,并可采用突发方式一次传送多个字节的时钟信号或RAM数据。DS1302内部有一 个31×8的用于临时性存放数据的RAM寄存器。DS1302是DS1202的升级产品,与DS1202兼容, 但增加了主电源/后背电源双电源引脚,同时提供了对后背电源进行涓细电流充电的能力。
//延时 1MS/次
unsigned char
{
sec,min,hour,day,month,year,cen,week,
unsigned char i;
next,aa,bb,cc,dd,mm,temp0,LunarMonth,
while(--a)
LunarDay,LunarYear;
{
int temp;
9三三系统的软件设计系统的软件设计3131程序流程框图程序流程框图图图aa主程序流程图主程序流程图10图b计算阳历程序流程图计算阳历程序流程图1112图图cc时间调整程序流程图时间调整程序流程图yynnyynnyy图图dd设置温度报警闹钟的数据保存到设置温度报警闹钟的数据保存到at24c02at24c02中中是否进入温度报警上下限温度设置设置是否进入设置闹钟时间设置报警温度上下限开始设置闹钟的时间开始报警闹钟的开关是否开起at24c02存设置的功能保蜂鸣器开启闹钟报警功能开启结束设置13五五作品功能实现作品功能实现通过硬件的焊接与程序的编写本电子万年历终于完成了实现的功能如下

基于单片机的万年历源代码程序

基于单片机的万年历源代码程序

基于单片机的万年历源代码程序源程序代码如下:#include<reg52.h>#include<intrins.h>unsigned char code displaywelcome[]={" Welcome To My Lcd Timer"};//欢迎界面unsigned char code displaywish[]={" Happy Every Day ^_^"}; //欢迎界面unsigned char code overtemperature[]={"OVERTEMPERATURE!"};unsigned char code digit[]={"0123456789"}; //数字代码unsigned char mode,TH,TL,TN,TD,length,tempswitch,Maxtemp=40,amode,alarmmode,minutes,hours, minutea,seconds,houra=12;sbit SCLK=P1^0; //DS1302时钟输入sbit DATE=P1^1; //DS1302数据输入sbit REST=P1^2; //DS1302复位端口sbit SET=P1^4; //DS1302设置模式选择位sbit ADD=P1^5; //增加sbit RED=P1^6; //减小sbit CANL=P1^7;void delay1ms(int i)//1毫秒延时{int j,k;while(i--)for(j=76;j>1;j--);for(k=29;k>1;k--);}void delaynus(unsigned char n) //延时若干微秒{unsigned char i;for(i=0;i<n;i++);}/***********蜂鸣器模块************/sbit beep=P3^6; //位定义,定义P.6位fmpvoid dely500(void){unsigned char i;for(i=250;i>0;i--){_nop_();}}void baojing(unsigned char n){unsigned char x,i;while(n--){for(i=0;i<5;i++){for(x=0;x<200;x++){beep=~beep;dely500();}}for(i=0;i<3;i++){for(x=0;x<200;x++){beep=~beep;dely500();dely500();}}}}/*******DS1302模块*************/void Write1302(unsigned char date)//向1302写数据{unsigned char i;SCLK=0;delaynus(2);for(i=0;i<8;i++){DATE=date&0x01;SCLK=1;delaynus(2);SCLK=0;delaynus(2);date>>=1;}}void WriteSet1302(unsigned char cmd,unsigned char date) //根据相应的命令输入相应的数据{REST=0;SCLK=0;REST=1;Write1302(cmd);delaynus(5);Write1302(date);SCLK=1;REST=0;}unsigned char Read1302(void) //读取1302数据{unsigned char i,date;delaynus(2);for(i=0;i<8;i++){date>>=1;if(DATE==1)date|=0x80;SCLK=1;delaynus(2);SCLK=0;delaynus(2);}return date;}unsigned char ReadSet1302(unsigned char cmd)//根据命令读取1302相应的值{unsigned char date;REST=0;SCLK=0;REST=1;Write1302(cmd);delaynus(2);date=Read1302();SCLK=1;REST=0;return date;}void IntDS1302(void) //DS1302初始化{unsigned char flag;flag= ReadSet1302(0x81);if(flag&0x80) { //判断时钟芯片是否关闭WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((0/10)<<4|(0%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((0/10)<<4|(0%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((0/10)<<4|(0%10))); //根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((0/10)<<4|(0%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((10/10)<<4|(10%10))); //根据写年寄存器命令字,写入年的初始值WriteSet1302(0x90,0xa5); //打开充电功能选择2K电阻充电方式WriteSet1302(0x8E,0x80); //根据写状态寄存器命令字,写入保护指令}}/***************液晶显示模块*****************/sbit RS=P2^5;sbit RW=P2^6;sbit E=P2^7;sbit BF=P0^7;/*液晶忙检测*/bit BusyTest(void){bit result;RS=0;RW=1;E=1;_nop_();_nop_();_nop_();result=BF;_nop_();_nop_();_nop_();E=0;return result;/*写指令*/void Write_com(unsigned char command) {while(BusyTest()!=0);RS=0;RW=0;E=0;_nop_();_nop_();_nop_();P0=command;_nop_();_nop_();_nop_();E=1;_nop_();_nop_();_nop_();E=0;}/*写地址*/void Write_Address(unsigned char address) {Write_com(address|0x80);delay1ms(1);}/*写数据*/void Write_Date(unsigned char date){RS=1;RW=0;E=0;_nop_();_nop_();_nop_();P0=date;_nop_();_nop_();_nop_();E=1;_nop_();_nop_();_nop_();E=0;delay1ms(1);}/*初始化*/void Lcd_Int(void){Write_com(0x38);delay1ms(1);Write_com(0x38);delay1ms(1);Write_com(0x06);delay1ms(1);Write_com(0x0c);delay1ms(1);Write_com(0x01);}void displaymainpart(void)//显示液晶主要部分(不变化部分){Write_Address(0x01);delay1ms(1);Write_Date('D');Write_Date('A');Write_Date('T');Write_Date('E');Write_Date(':');delay1ms(1);Write_Address(0x0A);delay1ms(1);Write_Date('-');Write_Address(0x0D);Write_Date('-');Write_Address(0x42);Write_Date('T');Write_Date('I');Write_Date('M');Write_Date('E');Write_Date(':');Write_Address(0x4A);Write_Date('-');Write_Address(0x4D);Write_Date('-');Write_Address(0x06);Write_Date('2');Write_Date('0');}void display_Second(unsigned char second) //在液晶上显示秒{unsigned char i,j;i=second/10;j=second%10;Write_Address(0x4E);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Minute(unsigned char minute) //在液晶上显示分{unsigned char i,j;i=minute/10;j=minute%10;Write_Address(0x4B);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Hour(unsigned char hour) //在液晶上显示时{unsigned char i,j;i=hour/10;j=hour%10;Write_Address(0x48);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Day(unsigned char day) //在液晶上显示日{unsigned char i,j;i=day/10;j=day%10;Write_Address(0x0E);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Month(unsigned char month) //在液晶上显示月{unsigned char i,j;i=month/10;j=month%10;Write_Address(0x0B);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_Year(unsigned char year) //在液晶上显示年{unsigned char i,j;i=year/10;j=year%10;Write_Address(0x08);Write_Date(digit[i]);Write_Date(digit[j]);delay1ms(1);}void display_houra(unsigned char x) //闹钟小时部分显示{unsigned char i,j;i=x/10;j=x%10;Write_Address(0x44);Write_Date(digit[i]);Write_Date(digit[j]);}void display_minutea(unsigned char x)//闹钟分钟部分显示{unsigned char i,j;i=x/10;j=x%10;Write_Address(0x47);Write_Date(digit[i]);Write_Date(digit[j]);}void display_Time(void) //显示实时时间{unsigned char value,day,month,year;Write_com(0x0c);value=ReadSet1302(0x81);seconds=(((value&0x70)>>4)*10+(value&0x0f));display_Second(seconds);value=ReadSet1302(0x83);minutes=(((value&0x70)>>4)*10+(value&0x0f));display_Minute(minutes);value=ReadSet1302(0x85);hours=(((value&0x70)>>4)*10+(value&0x0f));display_Hour(hours);value=ReadSet1302(0x87);day=(((value&0x70)>>4)*10+(value&0x0f));display_Day(day);value=ReadSet1302(0x89);month=(((value&0x70)>>4)*10+(value&0x0f));display_Month(month);value=ReadSet1302(0x8D);year=(((value&0xf0)>>4)*10+(value&0x0f));display_Year(year);}void displaystar(void) //显示欢迎界面{unsigned char i,j;Write_Address(0x0f);while(displaywelcome[i]!='\0'){Write_Date(displaywelcome[i]);i++;delay1ms(1);}i=0;Write_Address(0x4f);while(displaywish[i]!='\0'){Write_Date(displaywish[i]);i++;delay1ms(1);}j=40;while(j--){Write_com(0x18);//循环左移delay1ms(700);}Write_com(0x01);delay1ms(10);}void gbdisplay(unsigned char address) //时间调整时光标闪烁{Write_Address(address);delay1ms(5);Write_com(0x0f);delay1ms(5);}void displaymaxt(unsigned char x) //显示最大温度{unsigned char i,j,k;Write_com(0x0c);delay1ms(2);Write_Address(0x44);i=x/100;j=x/10;k=x%10;Write_Date(digit[i]);Write_Date(digit[j]);Write_Date(digit[k]);}/*****************时间调整部分*********************/void hourset(void) //调时{unsigned char timevalue,hour;delay1ms(500); //防止多次触发WriteSet1302(0x8e,0x00);//将写保护去掉,确保能正常将调整后的数值写入DS1302timevalue=ReadSet1302(0x85); //读取此时的数值hour=(((timevalue&0x70)>>4)*10+(timevalue&0x0f));while(1){if(ADD==0){delay1ms(50);if(ADD==0){hour++;delay1ms(300);while(ADD==0);}}if(RED==0){delay1ms(50);if(RED==0){hour--;delay1ms(300);if(hour==0) hour=23;while(RED==0);}}timevalue=(((hour)/10)<<4|(hour%10));WriteSet1302(0x84,timevalue);delay1ms(1);display_Hour(hour);Write_Address(0x49);delay1ms(5);if(hour>=24) hour=0;delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void minuteset(void) //调分{unsigned char timevalue,minute;delay1ms(500);WriteSet1302(0x8e,0x00);timevalue=ReadSet1302(0x83);minute=(((timevalue&0x70)>>4)*10+(timevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){minute++;if(minute>=60) timevalue=0;while(ADD==0);}}if(RED==0)delay1ms(10);if(RED==0){minute--;delay1ms(300);if(minute==0) minute=59;while(RED==0);}}timevalue=((minute/10)<<4|(minute%10));WriteSet1302(0x82,timevalue);delay1ms(1);display_Minute(minute);Write_Address(0x4c);delay1ms(5);if(minute>=60) minute=0;delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void secondset(void) //秒归零{unsigned char second;delay1ms(500);WriteSet1302(0x8e,0x00);while(1){if(ADD==0){delay1ms(10);if(ADD==0){second=0;WriteSet1302(0x80,0x00);while(ADD==0);}delay1ms(1);display_Second(second);Write_Address(0x4f);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void yearset(void) //调年{unsigned char datevalue,year;delay1ms(500);WriteSet1302(0x8e,0x00);datevalue=ReadSet1302(0x8d);year=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){year++;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){year--;delay1ms(300);while(RED==0);}}datevalue=((year/10)<<4|(year%10));WriteSet1302(0x8c,datevalue);delay1ms(5);display_Year(year);delay1ms(5);Write_Address(0x09);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void monthset(void) //调月{unsigned char datevalue,month;delay1ms(500);WriteSet1302(0x8e,0x00);datevalue=ReadSet1302(0x89);month=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){month++;if(month>12) month=1;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){month--;delay1ms(300);if(month==0) month=12;while(RED==0);}}datevalue=((month/10)<<4|(month%10));WriteSet1302(0x88,datevalue);delay1ms(1);display_Month(month);Write_Address(0x0c);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void dayset(void) //调日{unsigned char datevalue,day;delay1ms(500);WriteSet1302(0x8e,0x00);datevalue=ReadSet1302(0x87);day=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));while(1){if(ADD==0){delay1ms(10);if(ADD==0){day++;if(day>31) day=1;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){day--;delay1ms(300);if(day==0) day=31;while(RED==0);}}datevalue=((day/10)<<4|(day%10));WriteSet1302(0x86,datevalue);delay1ms(1);display_Day(day);Write_Address(0x0f);delay1ms(5);if(CANL==0){mode=0;Write_com(0x0c);break;}if(SET==0) break;}WriteSet1302(0x8e,0x80);}void alarmhourset(void) //闹钟小时调整{unsigned char value;value=houra;while(1){if(ADD==0){delay1ms(20);if(ADD==0){Write_com(0x0c);delay1ms(5);value++;if(value>23) value=0;while(ADD==0);}}if(RED==0){delay1ms(20);if(RED==0){value--;if(value==0) value=23;while(RED==0);}}houra=value;display_houra(houra);gbdisplay(0x45);if(CANL==0){delay1ms(500);break;}}}void alarmminuteset(void) //闹钟分钟调整{while(1){if(ADD==0){delay1ms(20);if(ADD==0){minutea++;if(minutea>59) minutea=0;while(ADD==0) ;}}if(RED==0){delay1ms(20);if(RED==0){minutea--;if(minutea==0) minutea=59;while(RED==0) ;}}display_minutea(minutea);gbdisplay(0x48);if(CANL==0){delay1ms(500);break;}}}void TimeSet(void) //时间调整函数{display_Time();if(SET==0){delay1ms(10);if(SET==0){mode++;delay1ms(20);switch(mode){case(1):{gbdisplay(0x48);hourset(); delay1ms(500);} break;case(2):{gbdisplay(0x4c);minuteset();delay1ms(500);} break;case(3):{gbdisplay(0x4f);secondset();delay1ms(500);} break;case(4):{gbdisplay(0x09);yearset();delay1ms(500);} break;case(5):{gbdisplay(0x0c);monthset();delay1ms(500);} break;case(6):{gbdisplay(0x0f);dayset();delay1ms(500);} break;}if(mode==7) mode=0;}}}/***************显示温度模块********************/unsigned char code displayexplain[]={"Digit thermometer"};unsigned char code displayerror[]={"DS18B20 ERROR"};unsigned char code displayerror1[]={"PLEASE CHECK "};unsigned char code displaypart1[]={"WenDu:"};unsigned char code displaypart2[]={"Cent"};unsigned char flag;/**********************操作DS18B20模块***************************/ sbit DQ=P3^3;unsigned char time;/**********************DS18B20初始化****************************/bit IntDS18B20(void){bit temp;DQ=1;for(time=0;time<2;time++);DQ=0;for(time=0;time<200;time++);DQ=1;for(time=0;time<10;time++);temp=DQ;for(time=0;time<200;time++);return temp;}/**************************读DS18B20**********************/unsigned char ReadDS18B20(void){unsigned char i;unsigned char dat;for(i=0;i<8;i++){DQ=1;_nop_();DQ=0;_nop_();DQ=1;for(time=0;time<2;time++);dat>>=1;if(DQ==1)dat=dat|0x80;elsedat=dat|0x00;for(time=0;time<10;time++);}return dat;}/*********************向DS18B20写数据**************************/ void WriteDS18B20(unsigned char date){unsigned char i;for(i=0;i<8;i++){DQ=1;_nop_();DQ=0;DQ=date&0x01;for(time=0;time<10;time++);DQ=1;for(time=0;time<1;time++);date>>=1;}for(time=0;time<4;time++);}/*******************为读取温度做好准备************************/ void ReadyreadDS18B20(void){IntDS18B20();WriteDS18B20(0XCC);WriteDS18B20(0X44);delay1ms(200);IntDS18B20();WriteDS18B20(0XCC);WriteDS18B20(0XBE);}/*************在1602液晶上显示相关信息模块**********************/ /****************当未检测到DS18B20时显示信息模块****************/ void Display_Error(void){unsigned char i=0;Write_Address(0x00);while(displayerror[i]!='\0'){Write_Date(displayerror[i]);i++;delay1ms(100);}delay1ms(5);i=0;Write_Address(0x40);while(displayerror1[i]!='\0'){Write_Date(displayerror1[i]);i++;delay1ms(100);}delay1ms(3000) ;}/*************温度显示模块****************/void Display_Explain(void){unsigned char i=0;Write_Address(0x00);delay1ms(5);while(displayexplain[i]!='\0'){Write_Date(displayexplain[i]);i++;delay1ms(5);}i=0;Write_Address(0x40);while(displaypart1[i]!='\0'){Write_Date(displaypart1[i]);i++;delay1ms(5);}i=0;Write_Address(0x4C);while(displaypart2[i]!='\0'){Write_Date(displaypart2[i]);i++;delay1ms(5);}Write_Address(0x49);Write_Date('.');delay1ms(5);}/*********************显示温度整数部分*************************/ void Display_Integer(unsigned char x){unsigned char i,j,k;i=x/100;j=x%100/10;k=x%10;Write_Address(0x46);if(flag==1) Write_Date('-');Write_Date(digit[i]);Write_Date(digit[j]);Write_Date(digit[k]);delay1ms(50) ;}/************显示温度小数部分*********************/ void Display_Decimal(unsigned char x){Write_Address(0x4A);delay1ms(5);Write_Date(digit[x]);delay1ms(50);}void displayovert(void){unsigned char i;Write_com(0x01);delay1ms(10);Write_Address(0x03);delay1ms(10);Write_Date('W');Write_Date('A');Write_Date('R');Write_Date('N');Write_Date('I');Write_Date('N');Write_Date('G');Write_Date('!');Write_Address(0x40);while(overtemperature[i]!='\0'){Write_Date(overtemperature[i]);i++;delay1ms(10);}}/*******************温度主函数********************/ void Temperature(void){unsigned char i;delay1ms(5);if(IntDS18B20()!=0) Display_Error();else{delay1ms(1);Write_com(0x01);delay1ms(5);Display_Explain();for(length=50;length>0;length--){for(i=0;i<6;i++){flag=0;ReadyreadDS18B20();TL=ReadDS18B20();TH=ReadDS18B20();if((TH&0XF8)!=0X00){flag=1;TL=~TL;TH=~TH;TL+=1;if(TL>255) TH+=1;TN=TH*16+TL/16;TD=(TL%16)*10/16;}TN=TH*16+TL/16;TD=(TL%16)*10/16;Display_Integer(TN);Display_Decimal(TD);delay1ms(10);}if(tempswitch==1){if(TN>=Maxtemp){Write_Address(0x03);delay1ms(5);displayovert();while(1){baojing(1);if(CANL==0){Write_com(0x01);delay1ms(5);break;}}}}if(CANL==0) break;}}}void tempset(void){if(ADD==0){delay1ms(10);if(ADD==0){Maxtemp++;while(ADD==0);}}if(RED==0){delay1ms(10);if(RED==0){Maxtemp--;while(RED==0);}}}void Maxtempset(void) //高温报警设置{Write_com(0x01);delay1ms(10);Write_Address(0x03);delay1ms(10);Write_Date('T');Write_Date('E');Write_Date('M');Write_Date('P');Write_Address(0x09);Write_Date('S');Write_Date('E');Write_Date('T');Write_Address(0x40);Write_Date('M');Write_Date('A');Write_Date('X');Write_Date(':');while(1){if(SET==0){delay1ms(20);if(SET==0){Write_Address(0x4e);Write_com(0x0f);while(1){if(ADD==0){delay1ms(20);if(ADD==0){Write_Address(0x4b);delay1ms(5);Write_Date('O');Write_Date('N');Write_Date(' ');tempswitch=1;}}if(RED==0){delay1ms(20);if(RED==0){Write_Address(0x4b);Write_Date('O');Write_Date('F');Write_Date('F');tempswitch=0;}}if(CANL==0){delay1ms(500);break;}}}}if(tempswitch==1){Write_Address(0x4b);Write_Date('O');Write_Date('N');Write_Date(' ');tempset();displaymaxt(Maxtemp);if(CANL==0){Write_com(0x01);displaymainpart();break;}}if(tempswitch==0){Write_com(0x0c);Write_Address(0x4b);Write_Date('O');Write_Date('F');Write_Date('F');}if(CANL==0){Write_com(0x01); displaymainpart();break;}}}void alarmswitchset(void){while(1){if(ADD==0){delay1ms(100);if(ADD==0){alarmmode=1;Write_Address(0x4c);Write_Date('O');Write_Date('N');Write_Date(' ');}}if(RED==0){alarmmode=0;Write_Address(0x4c);Write_Date('O');Write_Date('F');Write_Date('F');}if(CANL==0){delay1ms(500);break;}}}/**************** 设置模块 ******************/ void Set(void){if(SET==0){delay1ms(10);if(SET==0){Write_Address(0x40);Write_Date('T');Write_com(0x0f);delay1ms(1000);Write_com(0x0c);while(1){TimeSet();if(ADD==0){delay1ms(20);if(ADD==0){Write_com(0x01);Write_Address(0x00);delay1ms(5);Write_Date('S');delay1ms(10);Maxtempset();}}if(RED==0){delay1ms(20);if(RED==0){Write_com(0x01);delay1ms(5);Write_Address(0x02);Write_Date('A');Write_Date('L');Write_Date('A');Write_Date('R');Write_Date('M');Write_Address(0x09);Write_Date('S');Write_Date('E');Write_Date('T');Write_Address(0x46);Write_Date('-');display_houra(houra);display_minutea(minutea);if(alarmmode==1){Write_Address(0x4c);Write_Date('O');Write_Date('N');Write_Date(' ');}if(alarmmode==0){Write_Address(0x4c);Write_Date('O');Write_Date('F');Write_Date('F');}while(1){if(SET==0){delay1ms(100);if(SET==0){amode++;switch(amode){case(1):{gbdisplay(0x45);alarmhourset();Write_com(0x0c);delay1ms( 500);} break;case(2):{gbdisplay(0x48);alarmminuteset();Write_com(0x0c);delay1m s(500);} break;case(3):{gbdisplay(0x4c);alarmswitchset();Write_com(0x0c);delay1m s(500);} break;}if(amode==4) amode=0;}}if(CANL==0){Write_com(0x01);delay1ms(5);displaymainpart();break;}}}}if(CANL==0){Write_Address(0x40);Write_Date(' ');Write_com(0x0c);break;}}}}}/***************主函数**********************/void main(){IE=0X82; //打开定时中断TMOD=0X01; //选择定时器0工作在方式1TR0=0; //启动定时器0IntDS1302(); //初始化DS1302delay1ms(1);Lcd_Int(); //1602液晶初始化delay1ms(2);displaystar(); //显示欢迎界面displaymainpart(); //显示主要部分(不变化)while(1){display_Time( );Set( );if(ADD==0){Write_com(0x01);delay1ms(5);Temperature();Write_com(0x01);displaymainpart();}if((hours==houra)&&(minutes==minutea)&&(seconds==0)) {if(alarmmode==1){Write_com(0x01);delay1ms(5);Write_Address(0x03);Write_Date('T');Write_Date('I');Write_Date('M');Write_Date('E');Write_Address(0x08);Write_Date('U');Write_Date('P');Write_Date('!');delay1ms(5);while(1){baojing(1);if(CANL==0){Write_com(0x01);delay1ms(5);displaymainpart();break;}}}}}}。

单片机实训--万年历程序

单片机实训--万年历程序

#include<reg52.h>typedef unsigned char uint8; #define clrdp 0x01#define enmode 0x06#define curback 0x02#define ondp 0x0f#define offdp 0x08#define movd 0x14#define funset 0x38#define bus P1#define contw 0x8e#define secw 0x80#define secr 0x81#define minw 0x82#define minr 0x83#define hourw 0x84#define hourr 0x85#define datw 0x86#define datr 0x87#define monthw 0x88#define monthr 0x89#define dayw 0x8a#define dayr 0x8b#define yearw 0x8c#define yearr 0x8dsbit K1=P2^7;sbit K2=P2^5;sbit K3=P2^4;sbit RS=P3^7;sbit RW=P3^6;sbit EN=P3^5;sbit DIO=P2^1;sbit RST=P2^2;sbit SCK=P2^0;bit flag=0;uint8 sel;uint8 time[7]={0};uint8 time_disp[32]={0}; void init_1602(void);void write_com(uint8 comd); void write_dat(uint8 wdata); void delay(void);void long_dleay(void);void init_1302(void);uint8 read_1302(uint8 addr);void write_1302(uint8 addr,uint8 wdata); void disp(void);void read_time(void);void sepr_time(void);void chk_key(void);uint8 bcd_dec(uint8 wdata);uint8 dec_bcd(uint8 wdata);void main(void){init_1302();init_1602();while(1){chk_key();read_time();sepr_time();disp();}}//1302初始化void init_1302(void){write_1302(contw,0);write_1302(secw,0);}//写1302寄存器void write_1302(uint8 addr,uint8 wdata) {uint8 i;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){DIO=wdata&0x01;SCK=1;SCK=0;wdata>>=1;}RST=0;}//读1302寄存器uint8 read_1302(uint8 addr) {uint8 i;uint8 temp;SCK=0;RST=1;for(i=0;i<8;i++){DIO=addr&0x01;SCK=1;SCK=0;addr>>=1;}for(i=0;i<8;i++){temp>>=1;if(! DIO){temp&=0x7f;}else{temp|=0x80;}SCK=1;SCK=0;}RST=0;return temp;}//读1302时间寄存器void read_time(void){uint8 i;uint8 temp;for(i=0;i<7;i++){temp=read_1302(0x81+i*2);time[i]=temp;}time[0]&=0x7f;}//显示时间void disp(void){uint8 i;write_com(0x80);for(i=0;i<16;i++){write_dat(time_disp[i]);}write_com(0xc0);for(i=16;i<32;i++){write_dat(time_disp[i]);}}//分离时间void sepr_time(void){time_disp[0]='-';time_disp[1]='-';time_disp[2]='2';time_disp[3]='0';time_disp[4]=(time[6]>>4)+0x30;time_disp[5]=(time[6]&0x0f)+0x30;time_disp[6]='-';time_disp[7]=(time[4]>>4)+0x30;time_disp[8]=(time[4]&0x0f)+0x30;time_disp[9]='-';time_disp[10]=(time[3]>>4)+0x30;time_disp[11]=(time[3]&0x0f)+0x30;time_disp[12]='-';time_disp[13]='-';time_disp[14]=(time[5]>>4)+0x30;time_disp[15]=(time[5]&0x0f)+0x30;time_disp[16]='-';time_disp[17]='-';time_disp[18]='-';time_disp[19]='-';time_disp[20]=(time[2]>>4)+0x30;time_disp[21]=(time[2]&0x0f)+0x30;time_disp[22]='-';time_disp[23]=(time[1]>>4)+0x30;time_disp[24]=(time[1]&0x0f)+0x30;time_disp[25]='-';time_disp[26]=(time[0]>>4)+0x30;time_disp[27]=(time[0]&0x0f)+0x30;time_disp[28]='-';time_disp[29]='-';time_disp[30]='-';time_disp[31]='-';}void init_1602(void){write_com(offdp);write_com(funset);write_com(enmode);write_com(movd);write_com(ondp);write_com(clrdp);}void write_com(uint8 comd){RS=0;RW=0;bus=comd;EN=1;EN=0;delay();}void write_dat(uint8 wdata){RS=1;RW=0;bus=wdata;EN=1;EN=0;delay();}void delay(void){uint8 t;for(t=0;t<250;t++);}void long_delay(void){uint8 t;for(t=0;t<200;t++){delay();}}void chk_key(void){uint8 buf;uint8 temp;if(!K1){long_delay();if(!K1){if(flag){if((sel>=0)&&(sel<8)){sel++;}else if(sel==8){temp=read_1302(secr);temp&=0x7f;write_1302(secw,temp);sel=0;flag=0;}else{sel=8;}}else{temp=read_1302(secr);temp|=0x80;write_1302(secw,temp);flag=1;}}}else if(!K2){long_delay();if(!K2){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf<99){buf++;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf<7){buf++;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf<12){buf++;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf<31){buf++;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf<23){buf++;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf<59){buf++;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}else if(!K3){long_delay();if(!K3){switch(sel){case 1:temp=read_1302(yearr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(yearw,temp);}break;case 2:temp=read_1302(dayr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(dayw,temp);}break;case 3:temp=read_1302(monthr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(monthw,temp);}break;case 4:temp=read_1302(datr);buf=bcd_dec(temp);if(buf>1){buf--;temp=dec_bcd(buf);write_1302(datw,temp);}break;case 5:temp=read_1302(hourr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(hourw,temp);}break;case 6:temp=read_1302(minr);buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);write_1302(minw,temp);}break;case 7:temp=read_1302(secr);temp&=0x7f;buf=bcd_dec(temp);if(buf>0){buf--;temp=dec_bcd(buf);temp|=0x80;write_1302(secw,temp);}break;default:break;}}}}uint8 dec_bcd(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata/10;j=wdata%10;temp=i;temp=temp<<4;temp=temp|j;return temp;}uint8 bcd_dec(uint8 wdata){uint8 temp;uint8 i;uint8 j;i=wdata>>4;j=wdata&0x0f;temp=i*10+j;return temp;。

基于51单片机的万年历设计流程

基于51单片机的万年历设计流程

基于51单片机的万年历设计流程
基于51单片机的万年历设计流程可以分为以下几个步骤:
1. 确定需求:首先,你需要明确你的万年历需要有哪些功能。

例如,是否需要显示日期、时间、星期,是否需要闹钟功能,是否需要手动或自动校准等。

2. 选择硬件:选择合适的单片机作为主控制器。

常用的单片机有8051系列,如AT89C51、AT89S52等。

此外,还需要选择适当的显示模块、按键模块、实时时钟模块等。

3. 设计硬件电路:根据选择的硬件设备,设计电路原理图和PCB图。

需要
考虑单片机的引脚连接、电源供给、时钟源、外部扩展等问题。

4. 编写软件程序:根据硬件电路和需求,编写相应的软件程序。

这包括初始化程序、主程序、中断服务程序等。

5. 调试和测试:将编写好的程序下载到单片机中,进行实际测试。

根据测试结果,对程序进行调试和修改,直到满足设计要求。

6. 生产:完成调试后,就可以进行批量生产了。

在生产过程中,还需要对产品进行质量检测,确保每个产品都能正常工作。

7. 后期维护:在产品上市后,可能需要对产品进行维护或升级。

例如,如果用户在使用过程中发现了问题,或者有新的需求,就需要对产品进行改进或升级。

以上是基于51单片机的万年历设计的基本流程,但具体的步骤可能会根据具体的需求和硬件设备有所不同。

单片机课程设计--基于51单片机的万年历

单片机课程设计--基于51单片机的万年历

单片机课程设计--基于51单片机的万年历单片机课程设计基于 51 单片机的万年历一、引言在现代生活中,时间的准确记录和显示对于我们的日常生活和工作具有重要意义。

万年历作为一种能够同时显示年、月、日、星期、时、分、秒等信息的设备,给人们带来了极大的便利。

本次课程设计旨在利用 51 单片机实现一个简单实用的万年历系统。

二、系统设计方案(一)硬件设计1、单片机选型选择经典的 51 单片机,如 STC89C52 单片机,其具有性能稳定、价格低廉、资源丰富等优点,能够满足本设计的需求。

2、显示模块采用液晶显示屏(LCD1602)作为显示设备,能够清晰地显示数字和字符信息。

3、时钟芯片选用DS1302 时钟芯片,它可以提供精确的实时时钟数据,包括年、月、日、星期、时、分、秒等。

4、按键模块设置三个按键,分别用于调整时间、选择调整项(年、月、日、时、分、秒等)以及切换显示模式(正常显示和设置模式)。

(二)软件设计1、主程序流程系统初始化后,首先读取 DS1302 中的时间数据,并将其显示在LCD1602 上。

然后进入循环,不断检测按键状态,根据按键操作进行相应的时间调整和显示模式切换。

2、时间读取与显示程序通过与 DS1302 进行通信,读取实时时间数据,并将其转换为适合LCD1602 显示的格式进行显示。

3、按键处理程序检测按键的按下状态,根据不同的按键执行相应的操作,如调整时间、切换显示模式等。

三、硬件电路设计(一)单片机最小系统单片机最小系统包括单片机芯片、晶振电路和复位电路。

晶振电路为单片机提供时钟信号,复位电路用于系统初始化时将单片机的状态恢复到初始值。

(二)显示电路LCD1602 显示屏通过数据总线和控制总线与单片机相连。

数据总线用于传输要显示的数据,控制总线用于控制显示屏的读写操作和显示模式。

(三)时钟电路DS1302 时钟芯片通过串行通信接口与单片机进行通信。

单片机通过发送特定的指令和数据,对 DS1302 进行读写操作,获取或设置时间信息。

c51单片机万年历的c程序

c51单片机万年历的c程序

c51单片机万年历的c程序c51单片机万年历的c程序求一个单片机可以显示阳历的年、月、日、星期、小时.分.秒。

阴历也能显示,标明是否为闰年。

用LCD液晶显示,可以调整时间。

这有个数码管显示的程序以前做的,。

你可以在我这个程序上修改修改包括键盘扫描,还有动态显示。

【。

】#include <REG51.H>//常量参数#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();};}。

单片机万年历 现成程序

单片机万年历  现成程序
64138,64291,64426,64489,64603,64704,64795, //低音1~7
64837,64913,64981,65012,65070,65120,65166, //中音1~7
65186,65225,65259,65274,65303,65328,65351, //高音1~7
2+7,0x02,5+0,0x02,1+7,0x04,
//------------
0,0x00
};
//-----------------------秒表用:-----------------------
struct clkms_type
{uchar s;
uchar ms;
else {if(riqi[4]==0&&riqi[5]==2&&riqi[6]==2&&riqi[7]>8) {riqi[5]++;riqi[6]=0;riqi[7]=1;}}}
{ if(riqi[4]==0&&riqi[5]>9) {riqi[4]=1;riqi[5]=0;}; //month
struct backclk_type
{char h;
char m;
char s;
}backclk={0,0,5};
void back_clk() //时钟-1s处理
{backclk.s--;
if(backclk.s==-1&&backclk.m)
{backclk.s=59;
if(riqi[4]==1&&riqi[5]>2) {riqi[4]=0;riqi[5]=1;};

单片机万年历程序代码

单片机万年历程序代码

单片机万年历程序代码以下是一个示例单片机万年历的程序代码:```c#include <reg51.h>typedef unsigned char uchar;typedef unsigned int uint;sbit K1 = P2^0; // 显示年份sbit K2 = P2^1; // 显示月份sbit K3 = P2^2; // 显示日期uchar code year_tab[] = {31,28,31,30,31,30,31,31,30,31,30,31}; uchar code week_tab[] = {0x06,0x07,0x01,0x02,0x03,0x04,0x05}; uchar year, month, day, week;void delay(uint ms){uint i, j;for(i=ms;i>0;i--)for(j=110;j>0;j--);}uchar getKey(){if(K1==0) {delay(5);if(K1==0)return 1;while(!K1);}if(K2==0) {delay(5);if(K2==0)return 2;while(!K2);}if(K3==0) {delay(5);if(K3==0)return 3;while(!K3);}return 0;}void display(uchar num) {P0 = num;delay(1);P0 = 0x00;}void init(){TMOD=0x01;TH0=0xFC;TL0=0x67;EA=1;ET0=1;TR0=1;}void main(){init();while(1) {uchar key = getKey();if(key == 1) {year++;if(year == 100)year = 0;}else if(key == 2) {month++;if(month == 13)month = 1;}else if(key == 3) {day++;if(day > year_tab[month-1]) { day = 1;}}display(year / 10);display(year % 10);display(month / 10);display(month % 10);display(day / 10);display(day % 10);display(week);}}void timer0() interrupt 1{TH0=0xFC;TL0=0x67;week++;if(week == 7)week = 0;}```该代码的主要思路是通过外部三个按键模拟年、月和日的调节,通过一个定时器不断更新星期的计数,然后将年、月、日和星期分别在数码管上显示出来。

单片机万年历程序(能自动调整误差)

单片机万年历程序(能自动调整误差)

#include<reg52.h> //52单片机头文件#include <intrins.h> //包含左右循环移位子函数的库#include <gzn.h>#include<ds18b20.h>/************************宏定义**************************/#define uchar 无符号字符#define uint 无符号整数/************************参数定义************************/char 苗,shi,fen,fge,fshi,sge,sshi,mge,mshi,count,a,b,c,flag,num,keynum,count1; 叉年,行奇,月,日;/************定义时间调整按钮********************/sbit resget=P3^6;sbit key_mode=P3^3;sbit key_up=P3^4;sbit key_down=P3^5;/************发光DS1302的控制************************/位 yi=P1^0;位 er=P1^1;sbit san=P1^2;位 si=P1^3;/************定义数码管的驱动位********************/位 fg=P2^0;位 fs=P2^1;sbit sg=P2^2;sbit ss=P2^3;/************数码管显示表****************************/uchar码表[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0 xc0,0x86,0x8e,0xff,0x9c};/******************设置初始化时间******************************/无效 set1302();无效du1302();无效集(uchar sel,uchar sel_1);无效的显示字();void qiehuan();无效调正();无效先世();/************************************************* *** ****************///函数名:delay()//函数:延时//参数:z(设置z的值可以设置延迟z MS)/************************************************* *** ****************/无效延迟(单位 z){单位 x,y;对于(x=z;x>0;x--)for(y=175;y>0;y--);}/*******************用于显示函数的延迟************************ ** ********/ 无效延迟1(无效){无符号字符 i=30;当我 - );}/************************************************* *** ****************///函数名:display()//功能:数码管显示子功能//参数:输入每一位的显示//调用函数:delay()/************************************************* *** ****************/无效显示(uchar aa、uchar bb、uchar cc、uchar dd){ss=0;P0=表[aa];延迟(2);P0=表[16];ss=1;SG=0;P0=表[bb];延迟(2);P0=表[16];SG=1;fs=0;P0=表[cc];延迟(2);P0=表[16];fs=1;fg=0;P0=表[dd];延迟(2);P0=表[16];fg=1;}/************************************************* *** ****************/ //函数名:keyscan()//功能:键盘扫描,调整时间//参数:1为时间设置功能键,2为时间加法操作,3为分钟加法操作//调用函数:delay(), display()/************************************************* *** ****************/ uchar id;无效键扫描(){如果(resget==0){延迟(5);如果(resget==0){set1302();身份证=0;显示(shi/16,shi%16,fshi,fge);}}杜1302();如果(键模式==0){延迟(5);如果(键模式==0){如果 (id++==6){身份证=0;}而(key_mode==0){显示字();}}}如果(id!=0){开关(ID){案例一:{display(16,16,fen/16,fen%16);yi=er=0;}break;案例2:{display(shi/16,shi%16,16,16);yi=er=0;}break;案例3:{display(16,16,ri/16,ri%16);san=0;yi=er=1;}break;案例4:{display(yue/16,yue%16,16,16);san=0;}break;案例5:{display(16,16,16,xingqi%16);san=1;}break;案例6:{display(2,0,nian/16,nian%16);}break;}}如果(id!=0){如果(key_up==0){延迟(5);如果(key_up==0){而(key_up==0){show_word();}set(id,0);//调用时间函数}}如果(key_down==0){延迟(5);如果(key_down==0){而(key_down==0){show_word();}set(id,1);//调用时间函数}}}}无效的显示字(){开关(ID){案例一:display(16,16,fen/16,fen%16);break;案例2:显示(shi/16,shi%16,16,16);break;案例3:显示(16,16,ri/16,ri%16);break;案例4:显示(yue/16,yue%16,16,16);break;案例5:display(16,16,16,xingqi%16);break;案例6:{display(2,0,nian/16,nian%16);}break;}}/************************************************* **** /位 SCLK=P1^7;位 SDA=P1^6;位 RST=P1^5;/************************************************* **** **** / 无效延迟(uchar z){ucharx,y;对于(x=z;x>0;x--)for(y=1;y>0;y--);}/************************************************* **** **** /无效 DS1302WriteByte(uchar 数据){uchar我;SCLK=0;延迟(1);对于(i=0;i<8;i++){SDA=dat&0x01;延迟(1);SCLK=1;延迟(1);SCLK = 0 ;日期 >>=1 ;} }}}/ ******************************************************* /飞 DS1302ReadByte(){uchar 我,那个;延误(1);对于 (i = 0 ; i < 8 ; i ++ ){>> = 1;如果(SDA == 1)那=那| 0x80;SCLK = 1;延误(1);SCLK = 0;延误(1);}}归期 ;}}/ ******************************************************* /飞DS1302Read(飞cmd)。

单片机c语言电子万年历完整程序

单片机c语言电子万年历完整程序

该程序为51单片机c语言电子万年历#include"reg52.h"//#include<stdio.h>#define uchar unsigned char#define uint unsigned intsbit lcden=P3^4;sbit lcdrs=P3^5;sbit DATA=P0^7;sbit RST=P0^5;sbit SCLK=P0^6;sbit menu=P3^0; //菜单sbit add=P3^1; //加一sbit dec=P3^7; //减一sbit led0=P1^0;sbit led1=P1^1;sbit led2=P1^2;sbit led3=P1^3;sbit ds=P3^2;//sbit beep=P3^3;uint temp;float f_temp;uint warn_l1=270;uint warn_l2=250;uint warn_h1=300;uint warn_h2=320;uint get_temp();void delayms(uint x);void write_com(uchar com);void write_data(uchar date);void init();void dis_temp(uint t);void Write1302(uchar dat);void WriteSet1302(uchar Cmd,uchar dat);uchar Read1302(void);uchar ReadSet1302(uchar Cmd);void Init_DS1302(void);void DisplaySecond(uchar x);void DisplayMinute(uchar x);void DisplayHour(uchar x);void DisplayDay(uchar x);void DisplayMonth(uchar x);void DisplayYear(uchar x);void DisplayWeek(uchar x);void dis_temp(uint t);void read_date(void);void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num);void key_scan(void);uchar code table2[]= "TUEWESTHUFRISATSUNMON"; uchar second,minute,hour,day,month,year,week,count=0; uchar ReadValue,num,time;void delayms(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}////////////////////////////////////////////////////////////void write_com(uchar com){lcdrs=0;P2=com;delayms(5);lcden=1;delayms(5);lcden=0;}void write_data(uchar date){lcdrs=1;P2=date;delayms(5);lcden=1;delayms(5);lcden=0;}void init(){lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}/////////////////////////////////////////////////////////////////void Write1302(uchar dat){uchar i;SCLK=0; //拉低SCLK,为脉冲上升沿写入数据做好准备delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续写8个二进制位数据{DATA=dat&0x01; //取出dat的第0位数据写入1302delayms(2); //稍微等待,使硬件做好准备SCLK=1; //上升沿写入数据delayms(2); //稍微等待,使硬件做好准备SCLK=0; //重新拉低SCLK,形成脉冲dat>>=1; //将dat的各数据位右移1位,准备写入下一个数据位 }}void WriteSet1302(uchar Cmd,uchar dat){RST=0; //禁止数据传递SCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输delayms(2); //稍微等待,使硬件做好准备Write1302(Cmd); //写入命令字Write1302(dat); //写数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递}uchar Read1302(void){uchar i,dat;delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续读8个二进制位数据{dat>>=1; //将dat的各数据位右移1位,因为先读出的是字节的最低位 if(DATA==1) //如果读出的数据是1dat|=0x80; //将1取出,写在dat的最高位SCLK=1; //将SCLK置于高电平,为下降沿读出delayms(2); //稍微等待SCLK=0; //拉低SCLK,形成脉冲下降沿delayms(2); //稍微等待}return dat; //将读出的数据返回}uchar ReadSet1302(uchar Cmd){uchar dat;RST=0; //拉低RSTSCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输Write1302(Cmd); //写入命令字dat=Read1302(); //读出数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递return dat; //将读出的数据返回}void Init_DS1302(void){WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)<<4|(59%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)<<4|(23%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((28/10)<<4|(28%10))); //根据写日寄存器命令字,写入日的初始值WriteSet1302(0x88,((2/10)<<4|(2%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((14/10)<<4|(14%10))); //nian//WriteSet1302(0x8a,((4/10)<<4|(4%10)));}/////////////////////////////////////////////////////////////////void DisplaySecond(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);write_com(0x80+0x48);write_data(' ');dis_temp(get_temp());}void DisSecond(uchar x){uchar i,j;ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);}void DisplayMinute(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x43);write_data(i+0x30);write_com(0x80+0x44);write_data(j+0x30);write_com(0x80+0x45);write_data(':');}void DisplayHour(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x40);write_data(i+0x30);write_com(0x80+0x41);write_data(j+0x30);write_com(0x80+0x42);write_data(':');}void DisplayDay(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x89);write_data(i+0x30);write_com(0x8a);write_data(j+0x30); }void DisplayMonth(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x86);write_data(i+0x30);write_com(0x87);write_data(j+0x30);write_com(0x88);write_data('/');}void DisplayYear(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x81);write_data(2+0x30);write_com(0x82);write_data(0+0x30);write_com(0x83);write_data(i+0x30);write_com(0x84);write_data(j+0x30);write_com(0x85);write_data('/');}void DisplayWeek(uchar x){ uchar i;x=x*3;// write_com(0x8c);write_data(table2[x]);// write_com(0x8d);write_data(table2[x+1]);// write_com(0x8e);write_data(table2[x+2]);write_com(0x8c);for(i=0;i<3;i++){write_data(table2[x]);x++;}}void read_date(void){ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x83);minute=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x85);hour=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x87);day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x89);month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x8d);year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue=ReadSet1302(0x8b); //读星期week=ReadValue&0x07;DisplayYear(year);DisplayMonth(month);DisplayDay(day);DisplayWeek(week);DisplayHour(hour);DisplayMinute(minute);DisplaySecond(second);dis_temp(get_temp()); ///温度显示key_scan(); ///按键检测}void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num){newval=ReadSet1302(newaddr); //读取当前时间newval=((newval&0x70)>>4)*10+(newval&0x0f); //将bcd码转换成十进制if(flag) //判断是加一还是减一{newval++;switch(s1num){ case 1: if(newval>99) newval=0;DisplayYear(newval);break;case 2: if(newval>12) newval=1;DisplayMonth(newval);break;case 3: if(newval>31) newval=1;DisplayDay(newval);break;case 4: if(newval>6) newval=0;DisplayWeek(newval);break;case 5: if(newval>23) newval=0;DisplayHour(newval);break;case 6: if(newval>59) newval=0;DisplayMinute(newval);break;case 7: if(newval>59) newval=0;DisplaySecond(newval);break;default:break;}}else{newval--;switch(s1num){ case 1: if(newval==0) newval=99;DisplayYear(newval);break;case 2: if(newval==0) newval=12;DisplayMonth(newval);break;case 3: if(newval==0) newval=31;DisplayDay(newval);break;case 4: if(newval<0) newval=6;DisplayWeek(newval);break;case 5: if(newval<0) newval=23;DisplayHour(newval);break;case 6: if(newval<0) newval=59;DisplayMinute(newval);break;case 7: if(newval<0) newval=59;DisplaySecond(newval);break;default:break;}}WriteSet1302((newaddr-1),((newval/10)<<4)|(newval%10)); //将新数据写入寄存器}//////////////////////////////////////void dsreset(void){uint i;ds=0;i=103;while(i>0)i--;ds=1;i=4;while(i>0)i--;}bit tempreadbit(void){uint i;bit dat;ds=0;i++;ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return(dat);}uchar tempread(void){uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1);}return(dat);}void tempwritebyte(uchar dat) {uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb){ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0;i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void){dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0x44);key_scan(); //////按键函数}uint get_temp(){uchar a,b;tempchange(); //////温度函数 dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread();b=tempread();temp=b;temp<<=8;temp=temp|a;f_temp=temp*0.0625;temp=f_temp*10+0.5;f_temp=f_temp+0.05;return temp;}//////void dis_temp(uint t){uchar n1,n2,n3;n1=t/100;n2=t%100/10;n3=t%100%10;DisSecond(second); ///秒显示write_com(0x80+0x49);write_data(table[n1]);//delayms(5);write_com(0x80+0x4a);write_data(table[n2]);//delayms(5);write_com(0x80+0x4b);write_data('.');// delayms(5);write_com(0x80+0x4c);write_data(table[n3]);//delayms(5);write_com(0x80+0x4d);write_data('^');//delayms(5);write_com(0x80+0x4e);write_data('C');//delayms(5);DisSecond(second); ////秒显示}/*********************液晶显示*****************/ void warn(uint s,uchar led){uchar i;i=s;// beep=0;P1=~(led);while(i--){dis_temp(get_temp());}// beep=1;P1=0xff;i=s;while(i--){dis_temp(get_temp());}}void deal(uint t){uchar i;if((t>warn_l2)&&(t<=warn_l1)){warn(40,0x01);}else if(t<=warn_l2){warn(10,0x03);}else if((t<warn_h2)&&(t>=warn_h1)) {warn(40,0x04);}else if(t>=warn_h2){warn(10,0x0c);}else{i=40;while(i--){dis_temp(get_temp());DisSecond(second);}}}///////////////////////////////////////void main(){init();Init_DS1302();while(1){tempchange();read_date();deal(temp);key_scan();}}////******************************************* void key_scan(void){// uchar miao,s1num=0;uchar s1num=0;if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;while(1){if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;}}// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,miao|0x80);write_com(0x0f);//光标闪射if(s1num==1){ //year=ReadSet1302(0x8d);write_com(0x80+4); //年光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(year,1,0x8d,1);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(year,0,0x8d,1);}}}if(s1num==2){//month=ReadSet1302(0x89);write_com(0x80+7); //月光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(month,1,0x89,2);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(month,0,0x89,2);}}}if(s1num==3){ //day=ReadSet1302(0x87);write_com(0x80+10);//日光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(day,1,0x87,3);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(day,0,0x87,3); //写入日寄存器}}}if(s1num==4){ //week=ReadSet1302(0x8b);write_com(0x80+14); //星期光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(week,1,0x8b,4);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(week,0,0x8b,4);}}}if(s1num==5){// hour=ReadSet1302(0x85)write_com(0x80+0x40+1); //时光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(hour,1,0x85,5);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(hour,0,0x85,5);}}}if(s1num==6)//调时间分{ // minute=ReadSet1302(0x83);write_com(0x80+0x40+4);if(add==0){delayms(5);if(add==0){ while(!add);turn_val(minute,1,0x83,6); //写入分寄存器}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(minute,0,0x83,6); //写入分寄存器}}}if(s1num==7)//调时间秒{// second=ReadSet1302(0x81);write_com(0x80+0x40+7);//秒光标if(add==0){delayms(3);if(add==0){ while(!add);if(second==0x60)second=0x00;turn_val(second,1,0x81,7);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(second,0,0x81,7);}}}if(s1num==8){// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,second&0x7f);s1num=0;//s1num清零//write_com(0x0c);//光标不闪烁//break;}}} } }。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
bflag=l;
prt_set_clk();
delay_nus(2);
prt_clear_clk();
delay_nus(2);
bpm=0;
if((pinp&(1<<io)))
bpm=1;
prt_set_clk();
TmpByte=0;
if((pinp&(1<<io)))
{
TmpByte=1;
TmpByte=0x80;
}
//TmpByte<<=7;
R_Byte= (R-Byte>>1);//R_Byte>>=1;
R_Byte |=TmpByte;
void write(unsigned char W_Byte)
{
unsigned char i;
DDRC=0xFF;
for(i=0;i<8;i++)
{
Prt_clear_io();
if(W_Byte &0x01)
delay_nus(2);
prt_clear_clk();
delay_nus(2);
prt_clear_rst();
prt_clear_clk();
R_Byte >>=4;
return R_Byte;
}
unsigned char readbyte(unsigned char w_byte)
R_Byte2 |=TmpByte;
Prt_set_clk();
delay_nus(1)://delay_nus(2);
prt_clear_clk();
delay_nus(1);//delay_nus(2);
}
{
unsigned char temp;
reset();
write(w_byte);
temp=read();
prt_clear_rst();
prt_clear_clk();
return temp;
DELAY.C
#define DELAY_C
#include”includes.h”
#define XTAL 12
void delay_lus(void)
{
asm(”nop”):
}
void delay_nus(unsigned int n)
{
unsigned int i=0;
}
void icd_send(unsigned char data)
{
Lcd_wait();
dataport=data;
lcd_pulse();
}
void clrscr(void)
{
lcd_clear_rs();
}
void writebyte(unsigned char w_byte,unsigned char w_2_byte) {
reset();
write(w_byte);
write(w_2_byte);
prt_clear_rst();
void lcd_pulse(void)
{
Icd—set—e()
delay_nms(1);
Icd_clear_e();
delay_nms(1);
}
void icd_wait(void)
{
asm(”nop”);
TmpByte<<=7;
R_Byte>>=1;
R_Byte I=TmpByte;
Prt_set_clk();
delay_nus(2);
prt_clear_clk();
delay_nus(2);
}
bflag=0;
if((pinp&(1<<io)))
}
void gotoxy(unsigned char x,unsigned char y)
{
gotoz((x)|((y)<(6));
}
void put_char(char c)
{
lcd_clear_rw();
lcd_set_rs();
{
prt_set_io();
}
Prt_set_clk();
delay_nus(1);//delay_nus(2);
prt_clear_clk();
delay_nus(1);//delay_nus(2);
W_Byte>>=1;
ddr_set_io();
R_Byte=0x00;
R_Byte2=0x00;
prt_clear_io();
ddr_clear_io();
for(i=0;i<4;i++)//get the first 4 bits
{
{
dirport_data:0xFF;
dirport_con |=(LCD_E | LCD_RS | LCD_RW);
lcd_clear_rs();
lcd_clear_rw();
lcd_send(0x3C);
lcd_send(0x3C);
{
TmpByte=1;
TmpByte=0x80;
}
//TmpByte<<=7;
R_Byte2=(R_Byte2>>1);//R_Byte2>>=l;
lcd_send(0x3C);
lcd_send(0x06);
lcd_send(0x0C);
}
#define LCD_C
#include”includes.h”
void LCD_INIT(void)
{
LCD_DIR_PORT=0xff;
lcd_send(c);
}
void outtext(unsigned char*text)
{
unsigned char i;
for(i=0; text[i]&&i<16;i++)
put_char(text[i]);
}
void initgraph(void)
{
unsigned int i=0;
for(i=0;i<n;i++)
{
delay_ims();
}
}
DSl302.C
#include”includes.h”
#define DSl302_C
unsigned char bflag;
unsigned char bpm;
unsigned char get_hours(void)
{
unsigned char i;
unsigned char R_Byte;
unsigned char TmpByte;
reset();
write(0x85);
ddr_set_io();
icd_clear_rs();
lcd__set_e();
asm(”nop”);
asm(”nop”);
icd_clear_e();
delay_nus(40);
icd_set_e();
asm(”nop”):
asm(”nop”);
lcd_clear_e();
unsigned char temp,high;
unsigned char low;
LCD_DIR_PORT=0x0f;
do
{
teap=LCD_OP_PORT;
temp=temp&BIT3;
LCD_OP_PORT=temp;
Lcd_set_rw(); //Set LCD to READ
LCD_0Pl_PORT=0x30;
lcd_clear_rw();
lcd_clear_rs();
lcd_set_e();
asm(”nop”);
asm(”nop”);
icd_clear_e();
delay_nus(40);
icd_clear_rw();
lcd_clear_rs();
R_Byte=0x00;
Prt_clear_io();
ddr_clear_io();
for(i:0;i<4;i++)
}
TmpByte=0;
if((pinp&(1<<io)))
TmpByte=1;
}
}
unslgned char read(void)
{
unsigned char i;
unsigned char R_Byte;
unsigned char R_Byte2;
unsigned char TmpByte;
prt_clear_clk();
}
void reset(void)
{
ddr_set_rst();
prt_clear_clk();
prt_clear_rst();
prt_set_rst();
}
lcd_clear_rw();
icd_send(0x01);
lcd_wait();
}
Байду номын сангаас void gotoz(unsigned char z)
相关文档
最新文档