单片机万年历 时钟 闹钟 温度程序..

合集下载

单片机万年历温度检测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; //模式键是否动作标志。

单片机制作数字钟(含万年历、秒表功能)

单片机制作数字钟(含万年历、秒表功能)

数字钟、万年历制作(基于单片机)电路原理图:程序://********************20131206****数字钟程序#pragma SMALL#include <reg51.h>#include <absacc.h>#include <intrins.h>//********************************************************* *********编译预处理void display(unsigned char *p); //显示函数,P为显示数据首地址unsigned char keytest(); //按键检测函数unsigned char search(); //按键识别函数void alarm(); //闹钟判断启动函数void ftion0(); //始终修改函数void ftion1(); //闹钟修改函数void ftion3(); //日期修改函数void cum(); //加1修改函数void minus(); //减1修改函数void jinzhi(); //进制修改函数void riqi(); //日期void stopwatch(); //秒表函数//********************************************************* *******函数声明sbit P2_7=P2^7;//********************************************************* *******端口定义unsigned char clockbuf[3]={0,0,0};unsigned char bellbuf[3]={0,0,0};unsigned char date[3]={1,1,1}; //日期存放数组unsigned char stop[3]={0,0,0};unsigned char msec1,msec2;unsigned char timdata,rtimdata,dtimdata;unsigned char count;unsigned char *dis_p;unsigned char or; //12进制控制标志unsigned char ri; //日期显示控制标志位unsigned char mb; //秒表控制标志位bit arm,rtim,rhour,rmin,hour,min,sec,day,mon,year; //定义位变量//********************************************************* *****全局变量定义void main(){unsigned char a;or=0; //12进制修改标志清零ri=0;mb=0;P2_7=0;arm=0;msec1=0;msec2=0;timdata=0;rtimdata=0;count=0;TMOD=0x12;TL0=0x06;TH0=0x06;TH1=(65536-10000)/256;TL1=(65536-10000)%256;EA=1;ET0=1;ET1=1;TR0=1;TR1=0;dis_p=clockbuf;while(1){a=keytest();if(a==0x78) //判断是否有键按下{display(dis_p);if(arm==1) alarm();}else{display(dis_p);a=keytest();if(a!=0x78){a=search();switch(a){case 0x00:ftion0();break;case 0x01:ftion1();break;case 0x02:cum();break;case 0x06:jinzhi();break;case 0x03:riqi();break;case 0x04:ftion3();break;case 0x05:minus();break;case 0x07:stopwatch();break;case 0x09:TR1=1;break;case 0x0a:TR1=0;break;case 0x0b:stop[0]=0;stop[1]=0;stop[2]=0;break;default:break;}}}}}//********************************************主函数【完】void display(unsigned char *p){unsigned char buffer[]={0,0,0,0,0,0};unsigned char k,i,j,m,temp;unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};buffer[0]=p[0]/10;buffer[1]=p[0]%10;buffer[2]=p[1]/10;buffer[3]=p[1]%10;buffer[4]=p[2]/10;buffer[5]=p[2]%10;if((sec==0)&&(min==0)&&(hour==0)&&(rmin==0)&&(rhour==0)&&( day==0)&&(mon==0)&&(year==0)) //没有修改标志,正常显示{for(k=0;k<3;k++){temp=0x01;for(i=0;i<6;i++){P0=0x00; //段选端口j=buffer[i];P0=led[j];P1=~temp; //位选端口temp<<=1;for(m=0;m<200;m++);}}}else //若有修改标志,则按以下标志分别显示{if(sec==1||day==1){P1=0x1f;i=buffer[5];P0=led[i];for(m=0;m<200;m++);P1=0x2f;j=buffer[4];P0=led[j];for(m=0;m<200;m++);}if(min==1||rmin==1||mon==1){P1=0x3b;i=buffer[2];P0=led[i];for(m=0;m<200;m++);P1=0x37;j=buffer[3];P0=led[j];for(m=0;m<200;m++);}if(hour==1||rhour==1||year==1) {P1=0x3e;i=buffer[0];P0=led[i];for(m=0;m<200;m++);P1=0x3d;j=buffer[1];P0=led[j];for(m=0;m<200;m++);}}}//**********************************LED显示函数【完】unsigned char keytest(){unsigned char c;P2=0x78; //检测是否有键按下c=P2;c=c&0x78;return(c);}//******************************************键盘检测函数【完】unsigned char search(){unsigned char a,b,c,d,e;c=0x3f;a=0; //行号while(1){P2=c;d=P2;d=d&0x07;if(d==0x03){b=0;break;} //列号else if(d==0x05){b=1;break;}else if(d==0x06){b=2;break;}a++;c>>=1;if(a==5){a=0;c=0x3f;}}e=a*3+b;do{display(dis_p);}while((d=keytest())!=0x78);return(e);}//***********************************************查键值函数【完】void alarm(){if((clockbuf[0]==bellbuf[0])&&(clockbuf[1]==bellbuf[1])){P2_7=1;rtim=1;if(count==10){count=0;P2_7=0;arm=0;rtim=0;}}}//****************************************闹钟判断启动函数【完】void ftion0(){TR0=0;rhour=0;rmin=0;dis_p=clockbuf;rtimdata=0;timdata++;switch(timdata){case 0x01:sec=1;break;case 0x02:sec=0;min=1;break;case 0x03:min=0;hour=1;break;case 0x04:timdata=0;hour=0;TR0=1;break;default:break;}}//*********************************************时钟设置函数【完】void ftion1(){if(TR0==0) TR0=1;sec=0;min=0;hour=0;dis_p=bellbuf;timdata=0;rtimdata++;switch(rtimdata){case 0x01:rmin=1;break;case 0x02:rmin=0;rhour=1;break;case 0x03:rtimdata=0;rhour=0;arm=1;dis_p=clockbuf;break;default:break;}}//*********************************************闹钟设置函数【完】void ftion3(){if(TR0==0) TR0=1;day=0;mon=0;year=0;dis_p=date;timdata=0;rtimdata=0;dtimdata++;switch(dtimdata){case 0x01:day=1;break;case 0x02:day=0;mon=1;break;case 0x03:mon=0;year=1;break;case 0x04:dtimdata=0;year=0;dis_p=clockbuf;break;default:break;}}//*************************************************日期修改函数【完】void minus(){if(sec==1){if(0==clockbuf[2]) clockbuf[2]=59;else clockbuf[2]--;}else if(min==1){if(0==clockbuf[1]) clockbuf[1]=59;else clockbuf[1]--;}else if(hour==1){if(or==0) //判断进制{if(0==clockbuf[0]) clockbuf[0]=23;else clockbuf[0]--;}if(or==1){if(1==clockbuf[0]) clockbuf[0]=12;else clockbuf[0]--;}}else if(rmin==1){if(bellbuf[1]==0) bellbuf[1]=59;else bellbuf[1]--;}else if(rhour==1){if(or==0){if(bellbuf[0]==0) bellbuf[0]=23;else bellbuf[0]--;}if(or==1){if(bellbuf[0]==1) bellbuf[0]=12;else bellbuf[0]--;}}else if(day==1){if(date[2]==1) date[2]=31;else date[2]--;}else if(mon==1){if(date[1]==1) date[1]=12;else date[1]--;}else if(year==1){if(date[0]==1) date[0]=99;else date[0]--;}}//*************************************减1修改功能函数【完】void cum(){if(sec==1){if(59==clockbuf[2]) clockbuf[2]=0;else clockbuf[2]++;}else if(min==1){if(59==clockbuf[1]) clockbuf[1]=0;else clockbuf[1]++;}else if(hour==1){if(or==0) //判断进制{if(23==clockbuf[0]) clockbuf[0]=0;else clockbuf[0]++;}if(or==1){if(12==clockbuf[0]) clockbuf[0]=1;else clockbuf[0]++;}}else if(rmin==1){if(bellbuf[1]==59) bellbuf[1]=0;else bellbuf[1]++;}else if(rhour==1){if(or==0){if(bellbuf[0]==23) bellbuf[0]=0;else bellbuf[0]++;}if(or==1){if(bellbuf[0]==12) bellbuf[0]=1;else bellbuf[0]++;}}else if(day==1){if(date[2]==31) date[2]=1;else date[2]++;}else if(mon==1){if(date[1]==12) date[1]=1;else date[1]++;}else if(year==1){if(date[0]==99) date[0]=0;else date[0]++;}}//*************************************加1修改功能函数【完】void jinzhi(){if(or==0) or=1;else or=0;}//***********************************进制修改控制函数【完】void riqi(){if(ri==0){dis_p=date;}if(ri==1){dis_p=clockbuf;}ri++;if(ri==2) ri=0;}//********************************日期控显示函数【完】void stopwatch(){if(mb==0){dis_p=stop;mb=1;}else{mb=0;dis_p=clockbuf;}}//************秒表**********秒表**********秒表函数【完】void clock() interrupt 1{EA=0;if(msec1!=0x14) msec1++; //6MHz晶振定时10mselse{msec1=0;if(msec2!=100) msec2++; //定时1selse{if(rtim==1) count++; //闹钟启动标志计时10smsec2=0;if(clockbuf[2]!=59) clockbuf[2]++;else{clockbuf[2]=0;if(clockbuf[1]!=59) clockbuf[1]++;else{clockbuf[1]=0;if(or==0){if(clockbuf[0]!=23) clockbuf[0]++;else{clockbuf[0]=0;if((date[1]==1)||(date[1]==1)||(date[1]==1)||(date[1]==3)||(date[ 1]==5)||(date[1]==7)||(date[1]==8)||(date[1]==10)||(date[1]==12)){if(date[2]!=30) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}if((date[1]==4)||(date[1]==6)||(date[1]==9)||(date[1]==11)){if(date[2]!=29) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}if(date[1]==2){if((((date[0]%4==0)&&(date[0]%100!=0))||(date[0]%400==0))){if(date[2]!=28) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}else{if(date[2]!=27) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}}}}if(or==1){if(clockbuf[0]!=12) clockbuf[0]++;else{clockbuf[0]=0;if((date[1]==1)||(date[1]==1)||(date[1]==1)||(date[1]==3)||(date[ 1]==5)||(date[1]==7)||(date[1]==8)||(date[1]==10)||(date[1]==12)){if(date[2]!=30) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}if((date[1]==4)||(date[1]==6)||(date[1]==9)||(date[1]==11)){if(date[2]!=29) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}if(date[1]==2){if((((date[0]%4==0)&&(date[0]%100!=0))||(date[0]%400==0))){if(date[2]!=28) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}else{if(date[2]!=27) date[2]++;else{date[2]=1;if(date[1]!=11) date[1]++;else{date[1]=1;date[0]++;}}}}}}}}}}EA=1;}//*******************************定时器0中断函数【完】void miaobiao() interrupt 3{TH1=(65536-10000)/256;TL1=(65536-10000)%256;if(stop[2]!=99) stop[2]++;else{stop[2]=0;if(stop[1]!=59) stop[1]++;else{stop[1]=0;if(stop[0]!=59) stop[0]++;else stop[0]=0;}}}//***********************************定时器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("简洁界面");}。

单片机万年历程序带农历和闹钟

单片机万年历程序带农历和闹钟

#include < character.h >#include < lcd.h >#include < clock.h >#include < sensor.h>#include < calendar.h >#include < key.h >/*****************************预概念**************************************/#define uchar unsigned char#define uint unsigned int/****************************************************************************/ sbit bell = P2 ^ 0; //概念蜂鸣器端口sbit in = P2 ^ 7; //概念红外检测端口/***************************************************************************** * 名称: Timer0_Service() inturrupt 1* 功能: 中断效劳程序整点报时3声嘟嘟的声音* 入口参数:* 出口参数:*****************************************************************************/ void Timer0_Service() interrupt 1{static uchar count = 0;static uchar flag = 0; //记录鸣叫的次数count = 0;TR0 = 0; //关闭Timer0TH0 = 0x3c;TL0 = 0XB0; //延时50 msTR0 = 1 ; //启动Timer0count ++;if( count == 20 ) //鸣叫1 秒{bell = ~ bell;count = 0;flag ++;}if( flag == 6 ){flag = 0;TR0 = 0; //关闭Timer0}}/****************************************************************************** 名称: Timer2_Servie() interrupt 5* 功能: 中断效劳程序整点报时一分钟* 入口参数:* 出口参数:*****************************************************************************/ void Timer3_Service() interrupt 5{static uchar count;TF2 = 0; //软件清除中断标志count ++;if( in == 1 ){count = 0; //计算清0TR2 = 0; //关闭Timer2bell = 1; //关闭蜂鸣器}if( count == 120 ) // 一分钟后关闭报警{count = 0; //计算清0TR2 = 0; //关闭Timer2bell = 1; //关闭蜂鸣器}}/****************************************************************************** * 函数名称:main()* 功能:* 入口参数:* 出口参数:******************************************************************************* */void main( void ){uchar clock_time[7] = { 0x00, 0x17, 0x12, 0x14, 0x12, 0x08 }; //概念时刻变量秒分时日月年uchar alarm_time[2] = { 0, 0}; //闹钟设置alarm_time[0]: 分钟alarm_time[1] :小时uchar temperature[2]; //概念温度变量temperature[0] 低8位temperature[1] 高8位Lcd_Initial(); //LCD初始化Clock_Initial( clock_time ); //时钟初试化/***********************中断初始化***************************/EA = 1; //开总中断ET0 = 1; //Timer0 开中断ET2 = 1; //Timer2 开中断TMOD = 0x01 ; //Timer0 工作方式1RCAP2H = 0x3c;RCAP2L = 0xb0; //Timer2 延时50 mswhile( 1 ){switch( Key_Scan() ){case up_array: //{Key_Idle();}break;case down_array:{Key_Idle();}break;case clear_array:{Key_Idle();}break;case function_array:{Key_Function( clock_time, alarm_time );}case null:{Clock_Fresh( clock_time ); //时刻刷新Lcd_Clock( clock_time ); //时刻显示Sensor_Fresh( temperature ); //温度更新Lcd_Temperture( temperature ); //温度显示Calendar_Convert( 0 , clock_time );Week_Convert( 0, clock_time );//整点报时if( ( * clock_time == 0x59 ) && ( * ( clock_time + 1 ) == 0x59 ) ){bell = 0;TR2 = 1; //启动Timer2}//闹钟报警if( * alarm_time == * ( clock_time + 1 ) ) //分钟相吻合if( * ( alarm_time + 1 ) == *( clock_time + 2 ) ) //小时相吻合{bell = 0;TR2 = 1; //启动Timer2}}break;}}}#ifndef _SUN_MOON#define _SUN_MOON/*************************************************************************/#define uchar unsigned char#define uint unsigned int/****************************************************************************** *** 名称: get_moon_day(uchar month_p,uint table_addr)* 功能: 读取数据表中农历的大月或小月,若是大月返回1, 小月返回0* 入口参数:* 出口参数:******************************************************************************* **/bit get_moon_day( uchar month_p,uint calendar_address ){uchar temp;switch(month_p){case 1: { temp = year_code[calendar_address] & 0x08; if(temp==0) return(0); else return(1); }case 2: { temp = year_code[calendar_address] & 0x04; if(temp==0) return(0); else return(1); }case 3: { temp = year_code[calendar_address] & 0x02; if(temp==0) return(0); else return(1); }case 4: { temp = year_code[calendar_address] & 0x01; if(temp==0) return(0); else return(1); }case 5: { temp = year_code[calendar_address + 1] & 0x80; if(temp==0) return(0); else return(1); }case 6: { temp = year_code[calendar_address + 1] & 0x40; if(temp==0) return(0); else return(1); }case 7: { temp = year_code[calendar_address + 1] & 0x20; if(temp==0) return(0); else return(1); }case 8: { temp = year_code[calendar_address + 1] & 0x10; if(temp==0) return(0); else return(1); }case 9: { temp = year_code[calendar_address + 1] & 0x08; if(temp==0) return(0); else return(1); }case 10: { temp = year_code[calendar_address + 1] & 0x04; if(temp==0) return(0); else return(1); }case 11: { temp = year_code[calendar_address + 1] & 0x02; if(temp==0) return(0); else return(1); }case 12: { temp = year_code[calendar_address + 1] & 0x01; if(temp==0) return(0); else return(1); }case 13: { temp = year_code[calendar_address + 2] & 0x80; if(temp==0) return(0); else return(1); }}}/*************************************************************************** 名称: void Calendar_Convert( uchar * clock_time )* 功能: 输入BCD的阳历数据,输出BCD阴历数据( 1901 - 2099 )* 入口参数: c_flag:阳历的世纪标志clock_time: 时钟地址* 出口参数: 无* 说明: c_flag = 0 :21世纪c_flag = 1 :19世纪*****************************************************************************/ void Calendar_Convert( uchar c_flag, uchar * clock_time ){bit flag_month, flag_year;uchar year, month, day, month_point; //概念年月天uchar temp1, temp2, temp3;uint calendar_address; //概念农历地址uint day_number;uchar clock_moon[3]; //概念阴历clock_time += 3; //指向日day = ( * clock_time >> 4 ) * 10 + ( *clock_time & 0x0f ); //BCD转换十进制clock_time ++; //指向月month = ( * clock_time >> 4 ) * 10 + ( * clock_time & 0x0f ); //BCD转换十进制clock_time ++; //指向年year = ( * clock_time >> 4 ) * 10 + ( * clock_time & 0x0f ); //BCD转换十进制//定位日历地址if( c_flag == 0 )calendar_address = ( year + 99 ) * 3;elsecalendar_address = ( year - 1 ) * 3;//春节(正月初一)所在的阳历月份temp1 = year_code[ calendar_address + 2 ] & 0x60; //Bit6~~Bit5:春节所在的阳历月份temp1 >>= 5 ;//春节(正月初一)所在的阳历日期temp2 = year_code[ calendar_address + 2 ] & 0x1f; //Bit4~~Bit0:春节所在的阳历日期//计算春节(正月初一)离昔时元旦{ 1月1日(阳历) }的天数;春节只会在阳历的1月或2月if( temp1 == 1 )temp3 = temp2 - 1;elsetemp3 = temp2 + 31 - 1;//计算阳历月离昔时元旦{ 1月1日(阳历) }的天数if( month < 10 )day_number = day_code1[ month - 1 ] + day - 1;elseday_number = day_code2[ month - 10 ] + day - 1;//若是阳历的月大于2 且该年的2月为闰月,天数加1//闰年指的确实是阳历有闰日或阴历有闰月的一年;//阳历四年一闰,在二月加一天,这一天叫做闰日://农历三年一闰,五年两闰,十九年七闰,每逢闰年所加的一个月叫做闰月。

单片机万年历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>#include<math.h>#include<INTRINS.H>#define uchar unsigned char#define uint unsigned int//各个IO口的初始化sbit dscs=P2^0;sbit dsas=P2^1;sbit dsrw=P2^2;sbit dsds=P2^3;sbit irq=P3^2;sbit s1=P3^0;sbit s2=P3^1;sbit s3=P3^2;sbit s4=P3^3;sbit beep=P1^0; sbit beep1=P1^1;sbit rs=P2^4;sbit rw=P2^5;sbit e=P2^6;sbit DQ=P1^7; int i,tt=0;uchar table0[]={"WELCOME !"};uchar table1[]={"HAVE A NICE DAY!"};uchar table2[]={"SET ALARM"};int miao,fen,shi,nian,yue,ri,week,s1num=0,num=0,num2=1,flag,flag_di,flag1,amiao,afen,ashi;uchar flag_get,num1,TZ=0;TX=0;keyxuan=1;uchar baiwei,shiwei,gewei,shifen,baifen,qianfen,wanfen;uint wd;void write_temperture(uchar TZ,uchar TX);void ReadTemperature();void DS18B20();unsigned char ReadOneChar();void WriteOneChar(unsigned char dat);void ddelay(unsigned int i);void dididi();void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}//液晶写数据与命令函数void write_com(uchar com){rs=0;rw=0;e=0;P0=com;delay(5);e=1;delay(5);e=0;}void write_date(uchar date){rs=1;rw=0;e=0;P0=date;delay(5);e=1;delay(5);e=0;}void init(){rw=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01); write_com(0x80);EA=1;EX0=1;IT0=1;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;ET0=1;TR0=1;flag1=0;flag=0;flag_di=1;}DS12C887//读写数据与地址命令void write_ds(uchar add,uchar date){dscs=0;dsas=1;dsds=1;dsrw=1;P0=add;dsas=0;dsrw=0;P0=date;dsrw=1;dsas=1;dscs=1;}uchar read_ds(uchar add){uchards_date;dsas=1;dsds=1;dsrw=1;dscs=0;P0=add;dsas=0;dsds=0;P0=0xff;ds_date=P0;dsds=1;dsas=1;dscs=1;return ds_date; }void write_sfm(uchar add,uchar date)显示时分秒函数{ucharshi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_date(0x3 0+shi);write_date(0x30+ge);}void write_nyr(uchar add,uchar date)显示年月日函数{uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+add);write_date(0x30+shi);write_date(0x30+ge);}void didi()//闹钟报警程序{beep=0;delay(100);beep=1;delay(100);beep=0;delay(100);beep=1;}void dididi()//温度报警程序{beep1=0;delay(150);beep1=1;delay(150);beep1=0;delay(150);beep1=1;} void init_ds12c887()//DS12C887的初始化{write_ds(0x0a,0x20);write_ds(0x0b,0x26);}void read_alarm()//读取闹钟寄存器地址到相应的变量中{amiao=read_ds(1);afen=read_ds(3);ashi=read_ds(5);}void set_alarm(ashi,afen,amiao)//设置闹钟{write_ds(1,amiao);write_ds(3,afen);write_ds(5,ashi);}void showsign()//显示其他字符{ write_com(0x80);write_date('2');write_com(0x80+1);write_date('0'); write_com(0x80+4);write_date('-');write_com(0x80+7);write_date('-'); write_com(0x80+0x40+2);write_date(':');write_com(0x80+0x40+5);write_date(':');}void show_week()//显示星期函数{switch(week){case 1:write_com(0x80+11);write_date('M');write_date('O');write_date('N');write_date('D');write_date('A');break;case 2:write_com(0x80+11);write_date('T');write_date('U');write_date('E');write_date('S');write_date('D');break;case 3:write_com(0x80+11);write_date('W');write_date('E');write_date('N');write_date('D');write_date('N');break;case 4:write_com(0x80+11);write_date('T');write_date('H');write_date('U');write_date('R');write_date('S');break;case 5:write_com(0x80+11);write_date('F');write_date('R');write_date('I');write_date('D');write_date('A');break;case 6:write_com(0x80+11);write_date('S');write_date('A');write_date('T');write_date('T');write_date('U');break;case 7:write_com(0x80+11);write_date('S');write_date('U'); write_date('N');write_date('D');write_date('A');break;}}void keyscan()//键盘扫描{if(s1==0){delay(4);if(s1==0){s1num++;if(flag1==1){if(s1num==8){s1num=1;}}keyxuan=0; flag=1;// 温度扫描和时钟显示标志位设为无效while(!s1);switch(s1num)//选择液晶光标位置{case 1:write_com(0x80+0x40+7);write_com(0x0f);break;case 2:write_com(0x80+0x40+4);write_com(0x0f);break;case 3:write_com(0x80+0x40+1);write_com(0x0f);break;case 4:write_com(0x80+15);write_com(0x0f);break;case 5:write_com(0x80+9);write_com(0x0f);break;case 6:write_com(0x80+6);write_com(0x0f);break;case 7:write_com(0x80+3);write_com(0x0f);break;case 8:write_ds(0x00,miao);delay(5);//最后一次将修改后的数据读取到相应的寄存器中write_ds(0x02,fen);delay(5);write_ds(0x04,shi);delay(5);write_ds(0x06,week);delay(5);write_ds(0x07,ri);delay(5);write_ds(0x08,yue);delay(5);write_ds(0x09,nian);delay(5);flag=0;keyxuan=1;write_com(0x0c);s1num=0;break;}}}//在推出键盘扫描程序前将相应标志位设置为有效,并发出清屏命令if(s1num!=0){if(s2==0){delay(4);if(s2==0){while(!s2);switch(s1num){case 1:miao++;if(miao==60)miao=0;write_sfm(6,miao);write_com(0x80+0x40+7);break;case 2:fen++;if(fen==60)fen=0;write_sfm(3,fen);write_com(0x80+0x40+4);break;case 3:shi++;if(shi==24)shi=0;write_sfm(0,shi);write_com(0x80+0x40+1);break;case 4:week++;if(week==8)week=1;show_week();break;case 5:ri++;if(ri==32)ri=1;write_nyr(8,ri);write_com(0x80+9);break;case 6:yue++;if(yue==13)yue=1;write_nyr(5,yue);write_com(0x80+6);break;case 7:nian++;if(nian==100)nian=0;write_nyr(2,nian);write_com(0x80+3);break;}}}}if(s1num!=0){if(s3==0){delay(4);if(s3==0){while(!s3);switch(s1num){case 1:miao--;if(miao==-1)miao=59;write_sfm(6,miao);write_com(0x80+0x40+7);break;case 2:fen--;if(fen==-1)fen=59;write_sfm(3,fen);write_com(0x80+0x40+4);break;case3:shi--;if(shi==-1)shi=23;write_sfm(0,shi);write_com(0x80+0x40+1);break;case 4:week--;if(week==0)week=7;show_week();break;case 5:ri--;if(ri==-1)ri=31;write_nyr(8,ri);write_com(0x80+9);break; case 6:yue--;if(yue==0)yue=12;write_nyr(5,yue);write_com(0x80+6);break;case7:nian--;if(nian==-1)nian=99;write_nyr(2,nian);write_com(0x80+3); break;}}}}if(s1num!=0){if(s4==0){delay(4);if(s4==0)//进入闹钟设置程序{num++;while(!s4);keyxuan=0;if(num==1){flag1=1;write_com(0x80);for(i=0;i<16;i++){write_date(' ');delay(5);}write_com(0x0f);write_com(0x80+3);for(i=0;i<10;i++){write_date(table2[i]);delay(2);} read_alarm();miao=amiao;fen=afen;shi=ashi;write_sfm(0,ashi);write_com(0x80+0x40+1);write_sfm(3,afen);write_com(0x80+0x40+4);write_sfm(6,amiao);write_com(0x80+0x40+7);}if(num==2){num=0;s1num=0;write_com(0x80+3);for(i=0;i<10;i++){write_date(' ');delay(5);}amiao=miao;afen=fen;ashi=shi;set_alarm(ashi,afen,amiao);flag=0;flag1=0;write_com(0x0c);keyxuan=1;}}}}}//推出闹钟设置程序,将修改后的数据读取到相应寄存器中,并将相应标志位设置为有效,发出清屏命令。

DS1302 12864lcd单片机智能闹钟日期温度程序

DS1302 12864lcd单片机智能闹钟日期温度程序

/****************************************************************************/ //头文件#include <AT89X52.h>#include <string.h>/*****************************************************************************/ //灯与蜂鸣器定义sbit led_b = P2^7;sbit led_r = P1^4;sbit led_g1 = P1^3;sbit led_g2 = P1^2;sbit led_g3 = P1^0;sbit led_g4 = P1^1;sbit beep = P3^1;/*****************************************************************************/ //温度传感器定义sbit DQ = P3 ^ 0;//ds18B20/*****************************************************************************/ //键盘引脚定义sbit KEY_1 = P2^3; //左上sbit KEY_2 = P2^4; //左下sbit KEY_3 = P2^6; //右上sbit KEY_4 = P2^5; //右下sbit KEY_I = P3^2; //常0/****************************************************************************/ //引脚定义sbit SID = P2^1; //数据sbit SCLK = P2^2; //时钟/*****************************************************************************/ //定义DS1302时钟接口sbit clock_clk = P3 ^ 5;//ds1302_clk(时钟线)sbit clock_dat = P3 ^ 4;//ds1302_dat(数据线)sbit clock_Rst = P3 ^ 3;//ds1302_Rst(复位线)/*****************************************************************************/ //定义累加器A中的各位sbit a0 = ACC ^ 0;sbit a1 = ACC ^ 1;sbit a2 = ACC ^ 2;sbit a3 = ACC ^ 3;sbit a4 = ACC ^ 4;sbit a5 = ACC ^ 5;sbit a6 = ACC ^ 6;sbit a7 = ACC ^ 7;/****************************************************************************/ //定义全局变量unsigned char yy,mo,dd,xq,hh,mm,ss;//定义时间映射全局变量(专用寄存器)bit w = 0; //调时标志位static unsigned char menu = 0;//定义静态小时更新用数据变量static unsigned char keys = 0;//定义静态小时更新用数据变量static unsigned char timecount = 0;//定义静态软件计数器变量/****************************************************************************/ void DelayM(unsigned int a){//延时函数1MS/次unsigned char i;while( --a != 0){for(i = 0; i < 125; i++); //一个; 表示空语句,CPU空转。

单片机课程设计之万年历、闹钟与温度显示设计

单片机课程设计之万年历、闹钟与温度显示设计
根据方案的选择,数码管时钟温度系统由51单片机、时钟芯片DS1302、测温芯片DS1802、数码管显示电路、键盘电路组成,如图3-1所示。
本系统采用C语言编写,控制器采用单片机STC89C52,温度检测部分采用DS18B20温度传感器,时钟系统用时钟芯片DS1302,用八位数码管作为显示器。单片机通过时钟芯片DS1302获取时间数据;通过温度传感器DS18B20采集温度信号送给单片机处理,并在数码管中显示出来,键盘可以切换界面,调时和温度查询与设置闹钟的功能。
由于DS18B20是在一根I/O线上读写数据,因此,对读写的数据位有着严格的时序要求。DS18B20有严格的通信协议来保证各位数据传输的正确性和完整性。该协议定义了几种信号的时序:初始化时序、读时序、写时序。所有时序都是将主机作为主设备,单总线器件作为从设备。而每一次命令和数据的传输都是从主机主动启动写时序开始,如果要求单总线器件回送数据,在进行写命令后,主机需启动读时序完成数据接收。数据和命令的传输都是低位在先。电路图如图3-4所示。
6、数码管显示电路设计
数码管内部是由7个条形发光二极管与一个小圆点发光二极管组成,根据这八个二极管的亮暗组成不同的字符。本系统采用八位数码管作为显示模块,数码管相对其他LCD类的显示器来说,成本比较低廉,省电。两个四位共阴0.56英寸的数码管作为显示屏。如图3-6所示。
7、按键控制电路设计
本系统采用四个按键作为键盘控制实现界面切换的功能,如图3-7所示。能够实现界面切换、时间校正、闹钟设置等功能。机械式按键再按下或释放时,由于机械弹性作用的影响,通常伴随有一定时间的触点机械抖动,然后其触点才稳定下来。其抖动过程如图3-8所示。抖动时间的长短与开关的机械特性有关,一般为5-10 ms。故软件编程时需要注意消除抖动。

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

51单片机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);char code table[]="0123456789" ;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=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) {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);//日光标{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);{ 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;}}}}}。

单片机万年历 时钟 闹钟 温度程序

单片机万年历  时钟 闹钟 温度程序
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^3;
sbit L=P1^4;
sbit DZ=P3^5;//这是用于控制数码管关闭,这个数码管和液晶同时亮所需电流大,单片机难驱动
sbit rs=P2^5;
sbit rw=P2^6;
}
if(s1num==8)
{
write_com(0x80+12);
}
if(s1num==9)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s1num!=0)
{
if(w2==0)
{
delay(5);
if(w2==0)
{
while(!w2);
if(s1num==1)
{
miao++;
void write_com(uchar com)
{
rs=0;
lcden=0;
rw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//写指令
//***********************************************
void write_date(uchar dat)
}
}
}
if(w4==0)
{
delay(5);
if(w4==0)
{
while(!w4);
if(s1num==1)
{
miao1--;
if(miao1==-1)

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

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

#include<reg52.h> //52单片机头文件#include <intrins.h> //包含有左右循环移位子函数的库#include <gzn.h>#include<ds18b20.h>/*******************宏定义*************************/#define uchar unsigned char#define uint unsigned int/******************参数定义*************************/charmiao,shi,fen,fge,fshi,sge,sshi,mge,mshi,count,a,b,c,flag,num,keynum,count1; char nian,xingqi,yue,ri;/***********对时间调节按钮进行定义*******************/sbit resget=P3^6;sbit key_mode=P3^3;sbit key_up=P3^4;sbit key_down=P3^5;/***********对发光ds1302的控制************************/sbit yi=P1^0;sbit er=P1^1;sbit san=P1^2;sbit si=P1^3;/**********对数码管驱动位进行定义********************/sbit fg=P2^0;sbit fs=P2^1;sbit sg=P2^2;sbit ss=P2^3;/************数码管显示表***************************/uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0, 0x86,0x8e,0xff,0x9c};/*************设置初始化时间**************************/void set1302();void du1302();void set(uchar sel,uchar sel_1);void show_word();void qiehuan();void tiaozheng();void xianshi();/*****************************************************************///函数名:delay()//功能:延时//参数:z(设置z的值即可设置延时 z MS)/*****************************************************************/ void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=175;y>0;y--);}/*****************用于显示函数的延时*********************************/ void delay1(void){unsigned char i=30;while(i--);}/*****************************************************************///函数名:display()//功能:数码管显示子函数//参数:输入各位的显示//调用函数:delay()/*****************************************************************/ void display(uchar aa,uchar bb,uchar cc,uchar dd){ss=0;P0=table[aa];delay(2);P0=table[16];ss=1;sg=0;P0=table[bb];delay(2);P0=table[16];sg=1;fs=0;P0=table[cc];delay(2);P0=table[16];fs=1;fg=0;P0=table[dd];delay(2);P0=table[16];fg=1;}/*****************************************************************/ //函数名:keyscan()//功能:键盘扫描,对时间进行调节//参数:1为时间设置功能键,2对时进行加操作,3对分钟进行加操作//调用函数:delay(),display()/*****************************************************************/ uchar id;void keyscan(){if(resget==0){delay(5);if(resget==0){set1302();id=0;display(shi/16,shi%16,fshi,fge);}}du1302();if(key_mode==0){delay(5);if(key_mode==0){if (id++==6){id=0;}while (key_mode==0){show_word();}}}if(id!=0){switch(id){case 1: {display(16,16,fen/16,fen%16);yi=er=0;}break;case 2: {display(shi/16,shi%16,16,16);yi=er=0;}break;case 3: {display(16,16,ri/16,ri%16);san=0;yi=er=1;}break;case 4: {display(yue/16,yue%16,16,16);san=0;}break;case 5: {display(16,16,16,xingqi%16);san=1;}break;case 6: {display(2,0,nian/16,nian%16);}break;}}if(id!=0){if(key_up==0){delay(5);if(key_up==0){while(key_up==0){show_word();}set(id,0);//调用时间函数}}if(key_down==0){delay(5);if(key_down==0){while(key_down==0){show_word();}set(id,1);//调用时间函数}}}}void show_word(){switch(id){case 1: display(16,16,fen/16,fen%16);break;case 2: display(shi/16,shi%16,16,16);break;case 3: display(16,16,ri/16,ri%16);break;case 4: display(yue/16,yue%16,16,16);break;case 5: display(16,16,16,xingqi%16);break;case 6: {display(2,0,nian/16,nian%16);}break;}}/*************************************************/ sbit SCLK=P1^7;sbit SDA=P1^6;sbit RST=P1^5;/*************************************************/ void delayus(uchar z){uchar x,y;for(x=z;x>0;x--)for(y=1;y>0;y--);}/*************************************************/void DS1302WriteByte(uchar dat){uchar i;SCLK=0;delayus(1);for(i=0;i<8;i++){SDA=dat&0x01;delayus(1);SCLK=1;delayus(1);SCLK=0;dat>>=1;}}/*************************************************/ uchar DS1302ReadByte(){uchar i,dat;delayus(1);for(i=0;i<8;i++){dat>>=1;if(SDA==1)dat=dat|0x80;SCLK=1;delayus(1);SCLK=0;delayus(1);}return dat;}/*************************************************/uchar DS1302Read(uchar cmd){uchar dat;RST=0;SCLK=0;RST=1;DS1302WriteByte(cmd);dat=DS1302ReadByte();SCLK=1;RST=0;return dat;}/*************************************************/void DS1302Write(uchar cmd, uchar dat){RST=0;SCLK=0;RST=1;DS1302WriteByte(cmd);DS1302WriteByte(dat);SCLK=1;RST=0;}/***********根据按键选择调整相应的时间*******************************/ void set(char sel,char sel_1){char address,time;char max,min;if(sel==1){address=0x82;max=59;min=0;}//分if(sel==2){address=0x84;max=23;min=0;}//小时if(sel==3){address=0x86;max=31;min=1;}//日if(sel==4){address=0x88;max=12;min=1;}//月if(sel==5){address=0x8a;max=7;min=1;}//星期if(sel==6){address=0x8c;max=99;min=0;}//年time=DS1302Read(address+1)/16*10+DS1302Read(address+1)%16;if(sel_1==0) time++;else time--;if(time>max) time=min;if(time<min) time=max;DS1302Write(0x8e,0x00);DS1302Write(address,time/10*16+time%10);DS1302Write(0x8e,0x80);}/*************设置初始化时间**************************/void set1302(){DS1302Write(0x8e,0x00);//控制命令,wp=0,写操作DS1302Write(0x80,0x00);//秒DS1302Write(0x82,0x08);//分DS1302Write(0x84,0x08);//时DS1302Write(0x86,0x11);//日DS1302Write(0x88,0x12);//月DS1302Write(0x8a,0x07);//星期DS1302Write(0x8c,0x11);//年DS1302Write(0x90,0xf5);//涓流充电器,现在不充电;0xa5为打开充电二极管,一个二极管串联一个2K电阻DS1302Write(0x8e,0x80);//控制命令,wp=1,禁止写操作}/*************读取ds1302的数据**************************/void du1302(){miao=DS1302Read(0x81);a=miao/16*10+miao%16;fen=DS1302Read(0x83);b=fen/16*10+fen%16;shi=DS1302Read(0x85);c=shi/16*10+shi%16;xingqi=DS1302Read(0x8b);ri=DS1302Read(0x87);yue=DS1302Read(0x89);nian=DS1302Read(0x8d);}/*****************************************************************///函数名:init()//功能:初始化定时以及参数/*****************************************************************/void init(){flag=0;yi=er=1;san=si=1;TMOD=0x11;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}/*****************************************************************///函数名:qiehuan()//功能:固定时间与自动循环显示的切换//调用函数:init(),delay(),display()/*****************************************************************/void qiehuan(){if(id==0&&key_down==0){delay(5);if(id==0&&key_down==0){if(keynum++==1){keynum=0;}while(key_down==0){display(shi/16,shi%16,fen/16,fen%16);yi=er=0;san=si=1;}}}}/*****************************************************************///函数名:tiaozheng()//功能:用于调整由于晶振误差引起的时间误差,分别在00时,06时,12时,18时自动调整//调用函数:init(),delay(),display()/*****************************************************************/ void tiaozheng(){if((b==0)&&(c==0)&&(a==30)&&(flag==0)){DS1302Write(0x8e,0x00);//控制命令,wp=0,写操作DS1302Write(0x80,0x00);flag++;DS1302Write(0x8e,0x80);//控制命令,wp=1,禁止写操作}if((b==0)&&(c==0)&&(a==40)&&(flag!=0)){flag=0;}if((b==0)&&(c==6)&&(a==30)&&(flag==0)){DS1302Write(0x8e,0x00);//控制命令,wp=0,写操作DS1302Write(0x80,0x00);flag++;DS1302Write(0x8e,0x80);//控制命令,wp=1,禁止写操作}if((b==0)&&(c==6)&&(a==40)&&(flag!=0)){flag=0;}if((b==0)&&(c==12)&&(a==30)&&(flag==0)){DS1302Write(0x8e,0x00);//控制命令,wp=0,写操作DS1302Write(0x80,0x00);flag++;DS1302Write(0x8e,0x80);//控制命令,wp=1,禁止写操作}if((b==0)&&(c==12)&&(a==40)&&(flag!=0)){flag=0;}if((b==0)&&(c==18)&&(a==30)&&(flag==0)){DS1302Write(0x8e,0x00);//控制命令,wp=0,写操作DS1302Write(0x80,0x00);flag++;DS1302Write(0x8e,0x80);//控制命令,wp=1,禁止写操作}if((b==0)&&(c==18)&&(a==40)&&(flag!=0)){flag=0;}}/*****************************************************************///函数名:xianshi()//功能:用于显示循环的显示和固定显示//调用函数:init(),delay(),display()/*****************************************************************/void xianshi(){if(id==0&&keynum==0){if(num>=0&&num<10)//时间{display(shi/16,shi%16,fen/16,fen%16);yi=er=0;}if(num>=10&&num<18)//公历{display(yue/16,yue%16,ri/16,ri%16);yi=er=1;san=0;}if(num>=18&&num<26)//时间{display(shi/16,shi%16,fen/16,fen%16);yi=er=0;san=1;}if(num>=26&&num<34)//农历{c_sun=0;year_sun=DS1302Read(0x8d);month_sun=yue ;day_sun=ri;Conver_week(c_sun,year_sun,month_sun,day_sun);Conversion(c_sun,year_sun,month_sun,day_sun);display(month_moon/16,month_moon%16,day_moon/16,day_moon%16);yi=er=1;si=0;}if(num>=34&&num<42)//时间{display(shi/16,shi%16,fen/16,fen%16);yi=er=0;si=1;}if(num>=42&&num<50)//温度{wendu();display(wq,wb,17,12);san=yi=1;er=1;}if(num>=50&&num<55)//时间{display(shi/16,shi%16,fen/16,fen%16);yi=er=0;}if(num>=55&&num<=60)//星期{display(16,16,16,xingqi%16);yi=er=1;}}if(id==0&&keynum==1){display(shi/16,shi%16,fen/16,fen%16);}}/*****************************************************************/ //函数名:main()//功能:主函数//调用函数:init(),delay(),display()/*****************************************************************/ void main() //主函数{init();while (1){keyscan();//用于对时间的调整tiaozheng();//用于时间的自动调整,分别得零点,六点,十二点,十八点自动调整qiehuan();//用于自动显示和固定显示的切换xianshi();//用于数码管的显示}}/*****************************************************************///函数名:timer()//功能:中断函数//功能介绍:每50MS产生中断一次,并对时间进行处理/*****************************************************************/void timer() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;if(count++==40){count=0;if(num++==60){num=0;}}if(count1++==10){count1=0;if(keynum==1){yi=~yi;er=~er;}}}。

51单片机实现万年历程序

51单片机实现万年历程序

51单片机实现万年历利用AT89S52单片机的P0口来和另外几个口来控制1602液晶的显示和P1口还有其它口来控制ds12887时钟芯片。

设置四个按键,1个定义为时间设置功能键,一个定义为闹钟设置功能键,另外两个用来调节时间的增减。

原理图:pcb图:源程序:#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit beep=P2^0;//蜂鸣器控制口sbit timeadd=P2^1;//调节时间加sbit timesub=P2^2;//调节时间减sbit timeclk=P2^3;//闹钟设定键sbit timefun=P2^4;//时间设定键sbit lcdrs=P2^5;//液晶的命令和数据控制口sbit ledrw=P2^6;//液晶的写数据口sbit lcden=P2^7;//液晶显示模块的使能端sbit timeds=P3^3;//时钟芯片地址闸sbit timerw=P3^4;//时钟芯片读写sbit timeas=P3^5;//时钟芯片地址闸sbit timeen=P3^6;//时钟芯片片选uchar code table[]="20 - - week ";uchar code table1[]=" : : ";//要显示的字符串char num,shi,fen,miao,nian=2000,yue,ri,week,numfun,anumfun,flag,flag1; uchar leap;void delay(uint x){uchar i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}void didi(){beep=0;delay(1000);beep=1;delay(1000);}void write_com(uchar com){lcdrs=0;P0=com;delay(5);lcden=1;delay(20);lcden=0;}void write_date(uchar date){lcdrs=1;P0=date;delay(5);lcden=1;delay(20);lcden=0;}void write_sfm(uchar add,uchar date){ uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_date(0x30+shi);write_date(0x30+ge);}void write_nyr(uchar add,uchar date){ uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+add);write_date(0x30+shi);write_date(0x30+ge);}void write_zhou(uchar add,uchar date) {write_com(0x80+add);write_date(0x30+date);}void write_ds(uchar add,uchar date){ timeen=0;timeas=1;timeds=1;timerw=1;P1=add;timeas=0;timerw=0;P1=date;timerw=1;timeas=1;timeen=1;}uchar read_ds(uchar add){uchar ds_date;timeas=1;timeds=1;timerw=1;timeen=0;P1=add;timeas=0;timeds=0;P1=0xff;ds_date=P1;timeds=1;timeas=1;timeen=1;return ds_date;}void keyscan(){if(flag1==1)//用来关闭闹钟铃声{if(timeadd==0){delay(5);if(timeadd==0){while(!timeadd==0);flag1=0;}}if(timesub==0){delay(5);if(timesub==0){while(!timesub==0);flag1=0;}}}if(timefun==0){delay(5);if(timefun==0){while(!timefun);flag=1;numfun++;if(numfun==1){write_com(0x80+0x40+0x0a);write_com(0x0f);}if(numfun==2){write_com(0x80+0x40+0x07);write_com(0x0f);}if(numfun==3){write_com(0x80+0x40+0x04);write_com(0x0f);}if(numfun==4){write_com(0x80+0x0f);write_com(0x0f);}if(numfun==5){write_com(0x80+0x09);write_com(0x0f);}if(numfun==6){write_com(0x80+0x06);write_com(0x0f);}if(numfun==7){write_com(0x80+0x03);write_com(0x0f);}if(numfun==8){numfun=0;flag=0;}}}if(numfun!=0){if(timeadd==0){delay(5);while(!timeadd);if(numfun==1){miao++;if(miao==60)miao=0;write_sfm(9,miao);write_com(0x80+0x40+9);write_ds(0,miao);}if(numfun==2){fen++;if(fen==60)fen=0;write_sfm(6,fen);write_com(0x80+0x40+6);write_ds(2,fen);}if(numfun==3){shi++;if(shi==24)shi=0;write_sfm(3,shi);write_com(0x80+0x40+3);write_ds(4,shi);}if(numfun==4){week++;if(week==8)week=1;write_zhou(0x0f,week);write_com(0x80+0x0f);write_ds(6,week);}if(numfun==5){if(nian==0){switch(yue){case 1:ri++;if(ri==32) ri=1;break;case 2:ri++;if(ri==30) ri=1;break;case 3:ri++;if(ri==32) ri=1;break;case 4:ri++;if(ri==31) ri=1;break;case 5:ri++;if(ri==32) ri=1;break;case 6:ri++;if(ri==31) ri=1;break;case 7:ri++;if(ri==32) ri=1;break;case 8:ri++;if(ri==32) ri=1;break;case 9:ri++;if(ri==31)ri=1;break;case 10:ri++;if(ri==32)ri=1;break;case 11:ri++;if(ri==31)ri=1;break;case 12:ri++;if(ri==32)ri=1;break;}}else if(leap&&yue==2){ri++;if(ri==30)ri=1;}else if(yue==2&&leap==0){ri++;if(ri==29)ri=1;}elseswitch(yue){case 1:ri++;if(ri==32) ri=1;break;case 3:ri++;if(ri==32) ri=1;break;case 4:ri++;if(ri==31) ri=1;break;case 5:ri++;if(ri==32) ri=1;break;case 6:ri++;if(ri==31) ri=1;break;case 7:ri++;if(ri==32) ri=1;break;case 8:ri++;if(ri==32) ri=1;break;case 9:ri++;if(ri==31) ri=1;break;case 10:ri++;if(ri==32) ri=1;break;case 11:ri++;if(ri==31) ri=1;break;case 12:ri++;if(ri==32) ri=1;break;}write_nyr(8,ri);write_com(0x80+0x08);write_ds(7,ri);}if(numfun==6){yue++;if(yue==13)yue=0;write_nyr(5,yue);write_com(0x80+0x05);write_ds(8,yue);}if(numfun==7){nian++;if(nian==100)nian=0;leap=nian%4==0&&nian%100!=0;write_nyr(2,nian);write_com(0x80+2);write_ds(9,nian);}}if(timesub==0){delay(5);while(!timesub);if(numfun==1){miao--;if(miao==-1)miao=59;write_sfm(9,miao);write_com(0x80+0x40+9);write_ds(0,miao);}if(numfun==2){fen--;if(fen==-1)fen=59;write_sfm(6,fen);write_com(0x80+0x40+6);write_ds(2,fen);}if(numfun==3){shi--;if(shi==-1)shi=23;write_sfm(3,shi);write_com(0x80+0x40+3);write_ds(4,shi);}if(numfun==4){week--;if(week==0)week=7;write_zhou(0x0f,week);write_com(0x80+0x0f);write_ds(6,week);}if(numfun==5){if(nian==0){switch(yue){case 1:ri--;if(ri==0) ri=31;break;case 2:ri--;if(ri==0) ri=29;break;case 3:ri--;if(ri==0) ri=31;break;case 4:ri--;if(ri==0) ri=30;break;case 5:ri--;if(ri==0) ri=31;break;case 6:ri--;if(ri==0) ri=30;break;case 7:ri--;if(ri==0) ri=31;break;case 8:ri--;if(ri==0) ri=31;break;case 9:ri--;if(ri==0) ri=30;break;case 10:ri--;if(ri==0) ri=31;break;case 11:ri--;if(ri==0) ri=30;break;case 12:ri--;if(ri==0) ri=31;break;}}else if(leap&&yue==2){ri--;if(ri==0)ri=29;}else if(yue==2&&leap==0){ri--;if(ri==0)ri=28;}elseswitch(yue){case 1:ri--;if(ri==0) ri=31;break;case 3:ri--;if(ri==0) ri=31;break;case 4:ri--;if(ri==0) ri=30;break;case 5:ri--;if(ri==0) ri=31;break;case 6:ri--;if(ri==0) ri=30;break;case 7:ri--;if(ri==0) ri=31;break;case 8:ri--;if(ri==0) ri=31;break;case 9:ri--;if(ri==0) ri=30;break;case 10:ri--;if(ri==0) ri=31;break;case 11:ri--;if(ri==0) ri=30;break;case 12:ri--;if(ri==0) ri=31;break;}write_nyr(8,ri);write_com(0x80+0x08);write_ds(7,ri);}if(numfun==6){yue--;if(yue==-1)yue=12;write_nyr(5,yue);write_com(0x80+0x05);write_ds(8,yue);}if(numfun==7){nian--;if(nian==-1)nian=99;leap=nian%4==0&&nian%100!=0;write_nyr(2,nian);write_com(0x80+2);write_ds(9,nian);}}}}void set_alarm(){uchar ashi,afen,amiao;if(timeclk==0)//判断是否按下闹钟设定键{delay(5);//延时消抖if(timeclk==0){while(!timeclk);anumfun++;//按键次数加1if(anumfun==1){flag=1;write_com(0x80+0x40+0x0a);write_com(0x0f);}if(anumfun==2){flag=1;write_com(0x80+0x40+0x07);write_com(0x0f);}if(anumfun==3){flag=1;write_com(0x80+0x40+0x04);write_com(0x0f);}if(anumfun==4){flag=0;anumfun=0;}}}if(anumfun!=0){if(timeadd==0)//如果加功能键按下{delay(5);while(!timeadd);if(anumfun==1) //设定秒{amiao++;if(amiao==60)amiao=0;write_sfm(9,amiao);write_com(0x80+0x40+9);write_ds(1,amiao);}if(anumfun==2){afen++;if(afen==60)afen=0;write_sfm(6,afen);write_com(0x80+0x40+6);write_ds(3,afen);}if(anumfun==3){ashi++;if(ashi==24)ashi=0;write_sfm(3,ashi);write_com(0x80+0x40+3);write_ds(5,ashi);}}}if(anumfun!=0){if(timesub==0){delay(5);while(!timesub);if(anumfun==1){amiao--;if(amiao==-1)amiao=59;write_sfm(9,amiao);write_com(0x80+0x40+9);write_ds(1,amiao);}if(anumfun==2){afen--;if(afen==-1)afen=59;write_sfm(6,afen);write_com(0x80+0x40+6);write_ds(3,afen);}if(anumfun==3){ashi--;if(ashi==-1)ashi=23;write_sfm(3,ashi);write_com(0x80+0x40+3);write_ds(5,ashi);}}}}void init(){EA=1;//开总中断EX0=1;//开外部中断0IT0=1;//设定触发方式为负跳变沿有效lcden=0;//选通液晶显示屏ledrw=0;//设定液晶显示屏的读写方式为读write_com(0x38);//设置液晶显示模式为16*2显示,5*7点阵,8位数据接口 write_com(0x0c);//打开显示,并使光标不显示也不闪烁write_com(0x06);//光标指针每次自动加1write_com(1);//清屏write_com(0x80);//送地址指针for(num=0;num<16;num++){write_date(table[num]);delay(5);}write_com(0x80+0x40);//写到第二行for(num=0;num<11;num++){write_date(table1[num]);delay(5);}write_ds(0x0a,0x20);//开启时钟芯片振荡器write_ds(0x0b,0x26);//开启闹钟read_ds(0x0c);//读取时钟芯片c寄存器}void main(){init();while(1){keyscan();set_alarm();if(flag1==1)didi();if(flag==0){//读取时间miao=read_ds(0x00);fen=read_ds(0x02);shi=read_ds(0x04);yue=read_ds(0x08);ri=read_ds(0x07);nian=read_ds(0x09);week=read_ds(0x06);//写入液晶显示屏write_sfm(9,miao);write_sfm(6,fen);write_sfm(3,shi);write_nyr(2,nian);write_nyr(5,yue);write_nyr(8,ri);write_zhou(0x0f,week);}}}void exter() interrupt 0{flag1=1;read_ds(0x0c);//读时钟芯片c寄存器。

12864万年历带闹钟和温度

12864万年历带闹钟和温度

基于51单片机的设计:源代码调试通过主函数部分:包含头文件,液晶18b20和DS1302的初始化#include<reg52.h>#include"12864.h"#include"ds1302.h"#include"18b20.h"#define uchar unsigned char#define uint unsigned intvoid main(){uchar i;lcd_init();b20_init();ds1302_init();ds1302_write_time();while(1){write_com(0x80);dis("★★mage心寒★★");get_temperature();b20_display();ds1302_read_time();for(i=1;i<8;i++)tempdata[i]=time_buf1[i];ds1302_display();}}12864控制部分:#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit rs=P2^4;sbit e=P2^6;sbit rw=P2^5;uchar i,j,num,aa;void delayms(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void write_com(uchar com) {rs=0;P0=com;delayms(5);e=1;delayms(5);e=0;}void write_data(uchar date) {rs=1;P0=date;delayms(5);e=1;delayms(5);e=0;}void dis(unsigned char code *s) { while(*s>0){ write_data(*s);s++;delayms(5);}}void lcd_init(){rw=0;e=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);}/*lcd_display(){write_com(0x80);dis("12864 液晶,,,,");delayms(100);write_com(0x90);dis("安徽建筑工业学院");delayms(100);write_com(0x88);dis("电子创新实验室");delayms(100);write_com(0x98);dis("mage心寒");delayms(1000);write_com(0x01);}*/DS1302部分:#include<reg52.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned intuchar time_buf2[16];uchar tempdata[8];uchar num;sbit sck=P3^6;sbit sda=P3^4;sbit rst=P3^5;#define ds1302_sec_add 0x80 //秒数据地址#define ds1302_min_add 0x82 //分数据地址#define ds1302_hr_add 0x84 //时数据地址#define ds1302_date_add 0x86 //日数据地址#define ds1302_month_add 0x88 //月数据地址#define ds1302_day_add 0x8a //星期数据地址#define ds1302_year_add 0x8c //年数据地址#define ds1302_control_add 0x8e //控制数据地址#define ds1302_charger_add 0x90#define ds1302_clkburst_add 0xbeunsigned char time_buf1[] = {20,10,8,26,15,57,56,4};//年月日时分秒周unsigned char time_buf[16] ;//空年月日时分秒周/*向DS1302写入一字节数据*/void ds1302_write_byte(uchar add, uchar d) {uchar i;rst=1; /*启动DS1302总线*//*写入目标地址:addr*/add= add & 0xfe;/*最低位置零*/for (i = 0; i < 8; i ++) {if (add & 0x01) {sda=1;}else {sda=0;}sck=1;sck=0;add = add >> 1;}/*写入数据:d*/for (i = 0; i < 8; i ++) {if (d & 0x01) {sda=1;}else {sda=0;}sck=1;sck=0;d = d >> 1;}rst=0; /*停止DS1302总线*/ }/*从DS1302读出一字节数据*/uchar ds1302_read_byte(uchar add){uchar i;uchar temp;rst=1; /*启动DS1302总线*//*写入目标地址:addr*/add = add | 0x01;/*最低位置高*/for (i = 0; i < 8; i ++) {if (add & 0x01) {sda=1;;}else {sda=0;}sck=1;sck=0;add = add >> 1;}/*输出数据:temp*/for (i = 0; i < 8; i ++) {temp = temp >> 1;if (sda==1) {temp |= 0x80;}else {temp &= 0x7f;}sck=1;sck=0;}rst=0; /*停止DS1302总线*/return temp;}/*向DS302写入时钟数据*/void ds1302_write_time(){unsigned char i,tmp;for(i=1;i<8;i++){ //BCD处理tmp=time_buf1[i]/10;time_buf[i]=time_buf1[i]%10;time_buf[i]=time_buf[i]+tmp*16;}ds1302_write_byte(ds1302_control_add,0x00); //关闭写保护ds1302_write_byte(ds1302_sec_add,0x80); //暂停ds1302_write_byte(ds1302_charger_add,0xa9); //涓流充电ds1302_write_byte(ds1302_year_add,time_buf[1]); //年ds1302_write_byte(ds1302_month_add,time_buf[2]); //月ds1302_write_byte(ds1302_date_add,time_buf[3]); //日ds1302_write_byte(ds1302_hr_add,time_buf[4]); //时ds1302_write_byte(ds1302_min_add,time_buf[5]); //分ds1302_write_byte(ds1302_sec_add,time_buf[6]); //秒ds1302_write_byte(ds1302_day_add,time_buf[7]); //周ds1302_write_byte(ds1302_control_add,0x80); //打开写保护}/*从DS302读出时钟数据*/void ds1302_read_time(){uchar i,tmp;time_buf[1]=ds1302_read_byte(ds1302_year_add); //年time_buf[2]=ds1302_read_byte(ds1302_month_add); //月time_buf[3]=ds1302_read_byte(ds1302_date_add); //日time_buf[4]=ds1302_read_byte(ds1302_hr_add); //时time_buf[5]=ds1302_read_byte(ds1302_min_add); //分time_buf[6]=ds1302_read_byte(ds1302_sec_add); //秒time_buf[7]=ds1302_read_byte(ds1302_day_add); //周for(i=1;i<8;i++){ //BCD处理tmp=time_buf[i]/16;time_buf1[i]=time_buf[i]%16;time_buf1[i]=time_buf1[i]+tmp*10;}}/*DS302初始化函数*/void ds1302_init(){rst=0; /*RST脚置低*/sck=0; /*SCK脚置低*/}void write_sfm(uchar date){uchar shi,ge;shi=date/10;ge=date%10;//write_com(0x90+add);write_data(0x30+shi);write_data(0x30+ge);}void write_nyr(uchar date){uchar shi,ge;shi=date/10;ge=date%10;//write_com(0x90+add);write_data(0x30+shi);write_data(0x30+ge);}void write_xq(uchar date){//write_com(0x90+add);switch(date){case 1:write_data('M');break;case 2:write_data('T');break;case 3:write_data('W');break;case 4:write_data('T');break;case 5:write_data('F');break;case 6:write_data('S');break;case 7:write_data('s');break;}}void ds1302_display(){write_com(0x88);//write_nyr(tempdata[1]);write_nyr(tempdata[2]);write_data('-');write_nyr(tempdata[3]);write_data(' ');write_sfm(tempdata[4]);write_data(':');write_sfm(tempdata[5]);write_data(':');write_sfm(tempdata[6]);write_data(' ');write_xq(tempdata[7]);//if((tempdata[4]==0x13)&&(tempdata[5]==0x38)&&(tempdata[6]==0x00))//beez=1;//if((tempdata[4]==0x13)&&(tempdata[5]==0x38)&&(tempdata[6]==0x05))//beez=0;write_com(0x98);if ( tempdata[2] == 0x01 && tempdata[3] == 0x01 ){ dis("Happy New Y ear!!"); }//1月if ( tempdata[2] == 0x02 && tempdata[3] == 0x02 ){ dis(" 世界湿地日"); }//2月if ( tempdata[2] == 0x02 && tempdata[3] == 0x0d ){ dis(" 明天情人节了"); }if ( tempdata[2] == 0x02 && tempdata[3] == 0x0e ){ dis("今天是情人节"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x01 ){ dis(" 国际海豹日"); }//3月if ( tempdata[2] == 0x03 && tempdata[3] == 0x03 ){ dis(" 全国爱耳日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x08 ){ dis(" 3.8妇女节"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x0c ){ dis("植树节"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x0e){ dis(" 国际警察日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x0f ){ dis(" 消费者权益日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x11 ){ dis(" 国际航海日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x15 ){ dis(" 世界森林日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x16 ){ dis(" 世界水日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x17 ){ dis(" 世界气象日"); }if ( tempdata[2] == 0x03 && tempdata[3] == 0x18 ){ dis("世界防治结核病日"); }if ( tempdata[2] == 0x04 && tempdata[3] == 0x01 ){ dis("愚人节小心上当"); }//4if ( tempdata[2] == 0x04 && tempdata[3] == 0x07 ){ dis(" 世界卫生日"); }if ( tempdata[2] == 0x04 && tempdata[3] == 0x08 ){ dis(" 复活节"); }if ( tempdata[2] == 0x04 && tempdata[3] == 0x0d ){ dis(" 黑色星期五"); }if ( tempdata[2] == 0x05 && tempdata[3] == 0x01 ){ dis(" 劳动节放假"); }//5if ( tempdata[2] == 0x05 && tempdata[3] == 0x04 ){ dis(" 青年节"); }if ( tempdata[2] == 0x05 && tempdata[3] == 0x08 ){ dis(" 世界红十字日"); }if ( tempdata[2] == 0x05 && tempdata[3] == 0x05 ){ dis("近日注意母亲节"); }if ( tempdata[2] == 0x05 && tempdata[3] == 0x15 ){ dis(" 国际家庭日"); }if ( tempdata[2] == 0x05 && tempdata[3] == 0x1f ){ dis(" 世界无烟日"); }if ( tempdata[2] == 0x06 && tempdata[3] == 0x01 ){ dis(" 国际儿童节"); }//6 if ( tempdata[2] == 0x06 && tempdata[3] == 0x05 ){ dis(" 世界环境日"); }if ( tempdata[2] == 0x06 && tempdata[3] == 0x1a ){ dis("国际禁毒日"); }if ( tempdata[2] == 0x06 && tempdata[3] == 0x06 ){ dis(" 全国爱眼日"); }if ( tempdata[2] == 0x06 && tempdata[3] == 0x0d ){ dis("近日注意父亲节"); }if ( tempdata[2] == 0x06 && tempdata[3] == 0x0f ){ dis("近日注意父亲节"); }if ( tempdata[2] == 0x07 && tempdata[3] == 0x01 ){ dis("香港回归记念日"); }//7 if ( tempdata[2] == 0x07 && tempdata[3] == 0x07 ){ dis("抗日战争记念日"); }if ( tempdata[2] == 0x07 && tempdata[3] == 0x0b ){ dis(" 世界人口日"); }if ( tempdata[2] == 0x08 && tempdata[3] == 0x01 ){ dis(" 八一建军节"); }//8 if ( tempdata[2] == 0x08 && tempdata[3] == 0x08 ){ dis(" 中国男子节"); }if ( tempdata[2] == 0x08 && tempdata[3] == 0x0f ){ dis("抗战胜利记念日"); }if ( tempdata[2] == 0x09 && tempdata[3] == 0x10 ){ dis(" 中国教师节"); }//9 if ( tempdata[2] == 0x09 && tempdata[3] == 0x12 ){ dis("九·一八事变记念"); }if ( tempdata[2] == 0x09 && tempdata[3] == 0x14 ){ dis(" 国际爱牙日"); }if ( tempdata[2] == 0x09 && tempdata[3] == 0x1b ){ dis(" 世界旅游日"); }if ( tempdata[2] == 0x0a && tempdata[3] == 0x01 ){ dis("中国国庆节"); }//10 if ( tempdata[2] == 0x0a && tempdata[3] == 0x04 ){ dis("世界动物日"); }if ( tempdata[2] == 0x0a && tempdata[3] == 0x18 ){ dis("联合国日"); }if ( tempdata[2] == 0x0a && tempdata[3] == 0x0c ){ dis("明天国际教师节"); }if ( tempdata[2] == 0x0a && tempdata[3] == 0x0d ){ dis("国际教师节"); }if ( tempdata[2] == 0x0b && tempdata[3] == 0x0a ){ dis("世界青年节"); }//11 if ( tempdata[2] == 0x0b && tempdata[3] == 0x11 ){ dis(" 世界学生节"); }if ( tempdata[2] == 0x0c && tempdata[3] == 0x01 ){ dis("世界艾滋病日"); }//12 if ( tempdata[2] == 0x0c && tempdata[3] == 0x17 ){ dis("明晚平安夜"); }if ( tempdata[2] == 0x0c && tempdata[3] == 0x18 ){ dis("今晚平安夜"); }if ( tempdata[2] == 0x0c && tempdata[3] == 0x19 ){ dis("圣诞快乐"); }else{if ( tempdata[4] >= 0x04 && tempdata[4] <0x06 ){ dis("★★__▲▲__凌晨"); }if ( tempdata[4]>=0x06&&tempdata[4] <=0x08 ){ dis("☆○__▲△__早晨"); }if ( tempdata[4] >= 0x08 && tempdata[4] < 0x0c){ dis("__●__▲▲__上午"); }if ( tempdata[4] >= 0x0c&&tempdata[4]<=0x0d) { dis("____▲●▲__中午"); }if (tempdata[4] >= 0x0d&&tempdata[4] <0x12){ dis("__▲▲__●__下午"); }if (tempdata[4] >= 0x12&&tempdata[4] <0x16){ dis("△▲__●☆__晚上"); }if ( tempdata[4] >= 0x16&&tempdata[4] <=0x17){ dis("△▲__★☆__夜里"); }if ( tempdata[4] >= 0x00 && tempdata[4] <=0x04 ){ dis("__★▲▲★__深夜"); }}}DS18B20部分:#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include<math.h>#include<intrins.H>#define uchar unsigned char#define uint unsigned intsbit DQ=P1^3;//ds18b20 端口uint temp;uchar flag_get,count,num,minute,second,aa,bb,cc,dd,ee;uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //7段数码管段码表共阳void delay1(uchar MS);unsigned int ReadTemperature(void);void Init_DS18B20(void);unsigned char ReadOneChar(void);void WriteOneChar(unsigned char dat);void delay(unsigned int i);void b20_display(){write_com(0x90);dis("温度:");write_data(aa);//write_com(0x80+0x01);write_data(0x30+bb);//write_com(0x80+0x02);write_data(0x30+cc);//write_com(0x80+0x03);write_data(0x30+dd);//write_com(0x80+0x04);write_data('.');//write_com(0x80+0x05);write_data(0x30+ee);//write_com(0x80+0x06);write_data('C');//write_com(0x80+0x06);}void b20_init(){TMOD|=0x01;//定时器设置TH0=0xef;TL0=0xf0;IE=0x82;TR0=1;}void get_temperature(){unsigned char TempH,TempL;bb=TempH/100; //百位温度cc=(TempH%100)/10; //十位温度dd=(TempH%100)%10; //个位温度,带小数点ee=TempL;// display();if(flag_get==1) //定时读取当前温度{temp=ReadTemperature();if(temp&0x8000){aa='-';//负号标志temp=~temp; // 取反加1temp +=1;}elseaa='+';TempH=temp>>4;TempL=temp&0x0F;TempL=TempL*6/10;//小数近似处理flag_get=0;}}void tim(void) interrupt 1 using 1//中断,用于数码管扫描和温度检测间隔{TH0=0xef;//定时器重装值TL0=0xf0;num++;if (num==50){num=0;flag_get=1;//标志位有效second++;if(second>=60){second=0;minute++;}}}void delay(unsigned int i)//延时函数{while(i--); //延迟1us}void Init_DS18B20(void){unsigned char x=0;DQ = 1; //DQ复位delay(8); //稍做延时DQ = 0; //单片机将DQ拉低delay(80); //精确延时大于480usDQ = 1; //拉高总线delay(10);x=DQ; //稍做延时后如果x=0则初始化成功x=1则初始化失败delay(5);}/******************************************************************/ /* 读一个字节*/ /******************************************************************/ unsigned char ReadOneChar(void){unsigned char i=0;unsigned char dat = 0;for (i=8;i>0;i--){DQ = 0; // 给脉冲信号dat>>=1;DQ = 1; // 给脉冲信号if(DQ)dat|=0x80;delay(5);}return(dat);}/******************************************************************//* 写一个字节*//******************************************************************/ void WriteOneChar(unsigned char dat){unsigned char i=0;for (i=8; i>0; i--){DQ = 0;DQ = dat&0x01;delay(5);DQ = 1;dat>>=1;}delay(5);}/******************************************************************//* 读取温度*//******************************************************************/ unsigned int ReadTemperature(void){unsigned char a=0;unsigned int b=0;unsigned int t=0;Init_DS18B20();WriteOneChar(0xCC); // 跳过读序号列号的操作WriteOneChar(0x44); // 启动温度转换delay(200);Init_DS18B20();WriteOneChar(0xCC); //跳过读序号列号的操作WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器)前两个就是温度a=ReadOneChar(); //低位b=ReadOneChar(); //高位b<<=8; t=a+b;return(t); }。

单片机万年历程序(带闹钟)

单片机万年历程序(带闹钟)

第一章设计方案1.1.硬件设计1.1.1设计要求实现年月日、时分秒、星期的显示功能,用两个按键来实现日期和时间的调整功能,调整要求星期能自动更新,且能实现位调。

显示格式:四位年,两位月,两位日,星期字母前三位,两位时,两位分,两位秒。

比如:YYYY-MM-DD WEEKHH:MM:SS1.1.2硬件框图图1-1-硬件框图日期时间由硬件软件结合产生;按键之间相互配合完成当前日期时间的调整功能。

过程与结果由控制芯片分别输出到显示器上。

本设计附加了播放音乐的功能。

音乐从蜂鸣器产生。

1.1.3硬件选择1).显示模块本设计具体选用与实际应用与消耗资源相关。

万年历用到的地方大多都在室内。

现市场上与许多显示器,在日常生活中最常见的有数码管,led点阵屏,lcd液晶显示器等。

八段数码管的优点:亮度高,显示大。

驱动部份的软件简单;缺点:与液晶相比,耗电及体积大。

数码管只能单纯的显示数字,不能生动的表达各个参数的信息,消耗电力也比led液晶显示器大。

LCD液晶显示器它是一种专门用来显示字母、数字、符号等的点阵型液晶模块,在单片机系统中应用晶液显示器作为输出器件有以下几个优点:显示质量高,由于液晶显示器每一个点在收到信号后就一直保持那种色彩和亮度,恒定发光,而不像阴极射线管显示器(CRT)那样需要不断刷新新亮点。

因此,液晶显示器画质高且不会闪烁。

数字式接口,液晶显示器都是数字式的,和单片机系统的接口更加简单可靠,操作更加方便。

体积小、重量轻,液晶显示器通过显示屏上的电极控制液晶分子状态来达到显示的目的,在重量上比相同显示面积的传统显示器要轻得多。

功耗低,相对而言,液晶显示器的功耗主要消耗在其内部的电极和驱动IC上,因而耗电量比其它显示器要少得多。

Lcd液晶能一屏把本设计所要显示的信息表达完整。

而且lcd耗电小,体积小很适合与室内使用。

本设计的要求日期时间以数字表示、星期用3位英文字符表示共21个字符,lcd1602能显示32个字表 1-1 lcd1602引脚及功能2).按键与蜂鸣器本设计选用轻触按钮,型号为XDJT1102S。

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;};

51单片机实现万年历程序文件

51单片机实现万年历程序文件

51单片机实现万年历利用AT89S52单片机的P0口来和另外几个口来控制1602液晶的显示和P1口还有其它口来控制ds12887时钟芯片。

设置四个按键,1个定义为时间设置功能键,一个定义为闹钟设置功能键,另外两个用来调节时间的增减。

原理图:pcb图:源程序:#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit beep=P2^0;//蜂鸣器控制口sbit timeadd=P2^1;//调节时间加sbit timesub=P2^2;//调节时间减sbit timeclk=P2^3;//闹钟设定键sbit timefun=P2^4;//时间设定键sbit lcdrs=P2^5;//液晶的命令和数据控制口sbit ledrw=P2^6;//液晶的写数据口sbit lcden=P2^7;//液晶显示模块的使能端sbit timeds=P3^3;//时钟芯片地址闸sbit timerw=P3^4;//时钟芯片读写sbit timeas=P3^5;//时钟芯片地址闸sbit timeen=P3^6;//时钟芯片片选uchar code table[]="20 - - week ";uchar code table1[]=" : : ";//要显示的字符串char num,shi,fen,miao,nian=2000,yue,ri,week,numfun,anumfun,flag,flag1; uchar leap;void delay(uint x){uchar i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}void didi(){beep=0;delay(1000);beep=1;delay(1000);}void write_com(uchar com){lcdrs=0;P0=com;delay(5);lcden=1;delay(20);lcden=0;}void write_date(uchar date){lcdrs=1;P0=date;delay(5);lcden=1;delay(20);lcden=0;}void write_sfm(uchar add,uchar date){ uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_date(0x30+shi);write_date(0x30+ge);}void write_nyr(uchar add,uchar date){ uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+add);write_date(0x30+shi);write_date(0x30+ge);}void write_zhou(uchar add,uchar date) {write_com(0x80+add);write_date(0x30+date);}void write_ds(uchar add,uchar date){timeas=1;timeds=1;timerw=1;P1=add;timeas=0;timerw=0;P1=date;timerw=1;timeas=1;timeen=1;}uchar read_ds(uchar add){ uchar ds_date;timeas=1;timeds=1;timerw=1;timeen=0;P1=add;timeas=0;timeds=0;ds_date=P1;timeds=1;timeas=1;timeen=1;return ds_date;}void keyscan(){if(flag1==1)//用来关闭闹钟铃声{if(timeadd==0){delay(5);if(timeadd==0){while(!timeadd==0); flag1=0;}}if(timesub==0){delay(5);if(timesub==0){while(!timesub==0);flag1=0;}}}if(timefun==0){delay(5);if(timefun==0){while(!timefun);flag=1;numfun++;if(numfun==1){write_com(0x80+0x40+0x0a); write_com(0x0f);}if(numfun==2){write_com(0x80+0x40+0x07); write_com(0x0f);}if(numfun==3){write_com(0x80+0x40+0x04); write_com(0x0f);}if(numfun==4){write_com(0x80+0x0f);write_com(0x0f);}if(numfun==5){write_com(0x80+0x09);write_com(0x0f);}if(numfun==6){write_com(0x80+0x06);write_com(0x0f);}if(numfun==7){write_com(0x80+0x03); write_com(0x0f);}if(numfun==8){numfun=0;flag=0;}}}if(numfun!=0){if(timeadd==0){delay(5);while(!timeadd);if(numfun==1){miao++;if(miao==60)miao=0;write_sfm(9,miao);write_com(0x80+0x40+9); write_ds(0,miao);}if(numfun==2){fen++;if(fen==60)fen=0;write_sfm(6,fen);write_com(0x80+0x40+6); write_ds(2,fen);}if(numfun==3){shi++;if(shi==24)shi=0;write_sfm(3,shi);write_com(0x80+0x40+3);write_ds(4,shi);}if(numfun==4){week++;if(week==8)week=1;write_zhou(0x0f,week);write_com(0x80+0x0f);write_ds(6,week);}if(numfun==5){if(nian==0){switch(yue){case 1:ri++;if(ri==32) ri=1;break; case 2:ri++;if(ri==30) ri=1;break; case 3:ri++;if(ri==32) ri=1;break; case 4:ri++;if(ri==31) ri=1;break; case 5:ri++;if(ri==32) ri=1;break;case 6:ri++;if(ri==31) ri=1;break; case 7:ri++;if(ri==32) ri=1;break; case 8:ri++;if(ri==32) ri=1;break; case 9:ri++;if(ri==31) ri=1;break; case 10:ri++;if(ri==32) ri=1;break; case 11:ri++;if(ri==31) ri=1;break; case 12:ri++;if(ri==32) ri=1;break; }}else if(leap&&yue==2){ri++;if(ri==30)ri=1;}else if(yue==2&&leap==0){ri++;if(ri==29)ri=1;}elseswitch(yue){case 1:ri++;if(ri==32) ri=1;break; case 3:ri++;if(ri==32) ri=1;break; case 4:ri++;if(ri==31) ri=1;break; case 5:ri++;if(ri==32) ri=1;break; case 6:ri++;if(ri==31) ri=1;break; case 7:ri++;if(ri==32) ri=1;break; case 8:ri++;if(ri==32) ri=1;break; case 9:ri++;if(ri==31) ri=1;break; case 10:ri++;if(ri==32) ri=1;break; case 11:ri++;if(ri==31) ri=1;break; case 12:ri++;if(ri==32) ri=1;break; }write_nyr(8,ri);write_com(0x80+0x08);write_ds(7,ri);}if(numfun==6){yue++;if(yue==13)yue=0;write_nyr(5,yue);write_com(0x80+0x05);write_ds(8,yue);}if(numfun==7){nian++;if(nian==100)nian=0;leap=nian%4==0&&nian%100!=0; write_nyr(2,nian);write_com(0x80+2);write_ds(9,nian);}}if(timesub==0){delay(5);while(!timesub);if(numfun==1){miao--;if(miao==-1)miao=59;write_sfm(9,miao);write_com(0x80+0x40+9); write_ds(0,miao);}if(numfun==2){fen--;if(fen==-1)fen=59;write_sfm(6,fen);write_com(0x80+0x40+6); write_ds(2,fen);}if(numfun==3){shi--;if(shi==-1)shi=23;write_sfm(3,shi);write_com(0x80+0x40+3);write_ds(4,shi);}if(numfun==4){week--;if(week==0)week=7;write_zhou(0x0f,week);write_com(0x80+0x0f);write_ds(6,week);}if(numfun==5){if(nian==0){switch(yue){case 1:ri--;if(ri==0) ri=31;break; case 2:ri--;if(ri==0) ri=29;break; case 3:ri--;if(ri==0) ri=31;break; case 4:ri--;if(ri==0) ri=30;break; case 5:ri--;if(ri==0) ri=31;break; case 6:ri--;if(ri==0) ri=30;break;case 7:ri--;if(ri==0) ri=31;break; case 8:ri--;if(ri==0) ri=31;break; case 9:ri--;if(ri==0) ri=30;break; case 10:ri--;if(ri==0) ri=31;break; case 11:ri--;if(ri==0) ri=30;break; case 12:ri--;if(ri==0) ri=31;break; }}else if(leap&&yue==2){ri--;if(ri==0)ri=29;}else if(yue==2&&leap==0){ri--;if(ri==0)ri=28;}elseswitch(yue){case 1:ri--;if(ri==0) ri=31;break; case 3:ri--;if(ri==0) ri=31;break; case 4:ri--;if(ri==0) ri=30;break; case 5:ri--;if(ri==0) ri=31;break; case 6:ri--;if(ri==0) ri=30;break; case 7:ri--;if(ri==0) ri=31;break; case 8:ri--;if(ri==0) ri=31;break; case 9:ri--;if(ri==0) ri=30;break; case 10:ri--;if(ri==0) ri=31;break; case 11:ri--;if(ri==0) ri=30;break; case 12:ri--;if(ri==0) ri=31;break; }write_nyr(8,ri);write_com(0x80+0x08);write_ds(7,ri);}if(numfun==6){yue--;if(yue==-1)yue=12;write_nyr(5,yue);write_com(0x80+0x05);write_ds(8,yue);}if(numfun==7){nian--;if(nian==-1)nian=99;leap=nian%4==0&&nian%100!=0; write_nyr(2,nian);write_com(0x80+2);write_ds(9,nian);}}}}void set_alarm(){uchar ashi,afen,amiao;if(timeclk==0)//判断是否按下闹钟设定键{delay(5);//延时消抖if(timeclk==0){while(!timeclk);anumfun++;//按键次数加1if(anumfun==1){flag=1;write_com(0x80+0x40+0x0a); write_com(0x0f);}if(anumfun==2){flag=1;write_com(0x80+0x40+0x07); write_com(0x0f);}if(anumfun==3){flag=1;write_com(0x80+0x40+0x04); write_com(0x0f);}{flag=0;anumfun=0;}}}if(anumfun!=0){if(timeadd==0)//如果加功能键按下{delay(5);while(!timeadd);if(anumfun==1) //设定秒{amiao++;if(amiao==60)amiao=0;write_sfm(9,amiao);write_com(0x80+0x40+9); write_ds(1,amiao);}{afen++;if(afen==60)afen=0;write_sfm(6,afen);write_com(0x80+0x40+6); write_ds(3,afen);}if(anumfun==3){ashi++;if(ashi==24)ashi=0;write_sfm(3,ashi);write_com(0x80+0x40+3); write_ds(5,ashi);}}}if(anumfun!=0){if(timesub==0){delay(5);while(!timesub);if(anumfun==1){amiao--;if(amiao==-1)amiao=59;write_sfm(9,amiao);write_com(0x80+0x40+9); write_ds(1,amiao);}if(anumfun==2){afen--;if(afen==-1)afen=59;write_sfm(6,afen);write_com(0x80+0x40+6); write_ds(3,afen);}if(anumfun==3){ashi--;if(ashi==-1)ashi=23;write_sfm(3,ashi);write_com(0x80+0x40+3);write_ds(5,ashi);}}}}void init(){EA=1;//开总中断EX0=1;//开外部中断0IT0=1;//设定触发方式为负跳变沿有效lcden=0;//选通液晶显示屏ledrw=0;//设定液晶显示屏的读写方式为读write_com(0x38);//设置液晶显示模式为16*2显示,5*7点阵,8位数据接口 write_com(0x0c);//打开显示,并使光标不显示也不闪烁write_com(0x06);//光标指针每次自动加1write_com(1);//清屏write_com(0x80);//送地址指针for(num=0;num<16;num++){write_date(table[num]);delay(5);}write_com(0x80+0x40);//写到第二行for(num=0;num<11;num++){write_date(table1[num]);delay(5);}write_ds(0x0a,0x20);//开启时钟芯片振荡器 write_ds(0x0b,0x26);//开启闹钟read_ds(0x0c);//读取时钟芯片c寄存器}void main(){init();while(1){keyscan();set_alarm();if(flag1==1)didi();if(flag==0){//读取时间miao=read_ds(0x00);fen=read_ds(0x02);shi=read_ds(0x04);yue=read_ds(0x08);ri=read_ds(0x07);nian=read_ds(0x09);week=read_ds(0x06);//写入液晶显示屏write_sfm(9,miao);write_sfm(6,fen);write_sfm(3,shi);write_nyr(2,nian);write_nyr(5,yue);write_nyr(8,ri);write_zhou(0x0f,week);}}}void exter() interrupt 0{flag1=1;read_ds(0x0c);//读时钟芯片c寄存器。

单片机万年历汇编闹钟时间可调程序(LCD1602,DS18B20,DS1302,MAX810L)

单片机万年历汇编闹钟时间可调程序(LCD1602,DS18B20,DS1302,MAX810L)

片机万年历汇编闹钟时间可调程序(LCD1602,DS18B20,DS1302,MAX810L)说明:0该程序全部是用汇编语言编写1.该程序附带有温度显示,并且带有闹铃功能2.正常时间和闹钟时间都可以用键盘设置3.该程序是使用LCD1602显示年月日,格式为TIME XX-XX-XX XX,由于屏幕的关系,所以就不显示周的数据,其中闹铃调整的上界面是“BELLADJUST”4.该程序的时间是由时钟芯片DS1302提供的,据说很精准,目前看起来确实是这样的,刚开始我是使用定时器计时的,但是不怎么精准,可能是我的汇编技术有限才导致,所以本人就把这个版本呈上,希望诸位多多斧正5.该程序的温度是通过DS18B20提取的,这个模块是我从别人那里COPY过来的,O(∩_∩)O~,显示是肯定没问题的6.该程序也有用MAX810l的复位功能,但是好像这一点还没被体现出来7.烧程序我使用的是MAX232外加几个极性电容,还有一个RS232,不幸的是我的PCB板的RS232的封装画错啦!!~~~~(>_<)~~~~8.该万年历是由五个按键控制的,SW5复位,SW1调时间,调年月日时分秒,除秒可以加减调节之外其余的调节都是只能加不能减(自我觉得如果增加减的功能会觉得程序复杂,所以免!), SW4 是用来调节闹钟的时分秒的,和SW1按键不同的是时分秒都能加减,当闹钟设置的时间和正常显示时间的时分秒相同时,闹铃十秒钟,期间你可以按SW4键终止响铃,SW2 和SW3 分别是用来加减时间的!9.说了这么多,最后说说自我认为的缺陷,行家应该都一眼就看出一些破绽了,那就是按键消抖的问题,这个我觉得有点麻烦所以本人就把这个问题留给阅览资料的你吧!10.废话说了这么多,最后把我的程序,ORCAD电路原理图,PADS软件的PCB图,以及一张实物图献上,希望你能喜欢,页脚有我的Email(prettyboy2009@),欢迎随时交流!程序:;*************************************************************** ;***************************************************************RS BIT P2.5RW BIT P2.6E BIT P2.7SU BIT P1.6 ;1302串行口数据端RST BIT P1.5CLK BIT P1.7ALARM BIT P2.3JIAN BIT P2.2 ;减JIA BIT P2.1 ;加MOSHI BIT P2.0 ;模式DQ BIT P1.0SOUND BIT P1.3NIAN EQU 7FHYUE EQU 7DHDAY EQU 7CHHHH EQU 7EHSHI EQU 7BHFEN EQU 7AHMIAO EQU 79HASHI EQU 78HAFEN EQU 77HAMIAO EQU 76HGEW EQU 75HA_BIT EQU 70H ;18B20B_BIT EQU 6FHFLAG EQU 6EHDS1302_ADDR EQU 6DHDS1302_DATA EQU 6CHORG 0000HLJMP STARTORG 0030H ;***************************************************************; 初始化开机时间 &&&&&&& 初始化闹铃时间;***************************************************************START:MOV DPTR,#TABMOV SHI,#16HMOV ASHI,#17MOV FEN,#58HMOV AFEN,#30MOV MIAO,#00HMOV AMIAO,#00MOV NIAN,#11HMOV YUE,#12HMOV DAY,#13HMOV HHH,#00H;***************************************************************; 初始化LCD1602 &&&&&&& 初始化DS1302 ;***************************************************************MOV P0,#01H ; 显示清屏数据指针清零所有显示清零LCALL WR_CODEMOV P0,#02H ; 显示回车数据指针清零LCALL WR_CODEMOV P0,#38H ;设置16*2显示 5*7 点阵 8位数据接口LCALL WR_CODEMOV P0,#06H ; 当写或写一个数据时地址指针自动加一光标自动加一LCALL WR_CODEMOV P0,#0cH ; 开显示不显光标 0fh开显示开光标光标闪烁LCALL WR_CODELCALL XIAN1;***************************************************************; 主函数(这个大家都懂的);*************************************************************** MAIN:lcall RD_DS1302LCALL XIANSHIlcall keyLCALL AKEYLCALL RE_TEMPLCALL CONVERLCALL DISPLAY_INTLJMP BELLRING;***************************************************************; 读1302子程序;***************************************************************RD_DS1302:MOV DS1302_ADDR,#8DH ;读DS1302中的年LCALL READ ;调用读DS1302子程序MOV NIAN, DS1302_DATA ;把读出的年数据存入年寄存器中MOV DS1302_ADDR,#89H ;读DS1302中的月LCALL READMOV YUE, DS1302_DATA ;把读出的月数据存入月寄存器中MOV DS1302_ADDR,#87H ;读DS1302中的天LCALL READMOV DAY, DS1302_DATA ;把读出的天数据存入天寄存器中MOV DS1302_ADDR,#85H ;读DS1302中的小时LCALL READMOV SHI, DS1302_DATA ;把读出的天数据存入小时寄存器中MOV FEN, DS1302_DATA ;把读出的天数据存入分钟寄存器中MOV DS1302_ADDR,#81H ;读DS1302中的秒钟LCALL READMOV MIAO, DS1302_DATA ;把读出的天数据存入秒钟寄存器中RET ;***************************************************************; 写1302子程序;***************************************************************WR_DS1302:MOV DS1302_ADDR, #8EH ;写DS1302控制指令MOV DS1302_DATA, #00H ;写数据到DS1302的8E控制寄存器中,允许对其进行写操作LCALL WRITE ;调用写DS1302子程序MOV DS1302_ADDR,#8CH ;写DS1302中的年MOV DS1302_DATA , NIAN ;把写出的年数据存入年寄存器中LCALL WRITEMOV DS1302_ADDR,#88H ;写DS1302中的月MOV DS1302_DATA ,YUE ;把写出的月数据存入月寄存器中LCALL WRITEMOV DS1302_ADDR,#86H ;写DS1302中的天MOV DS1302_DATA ,DAY ;把写出的天数据存入天寄存器中LCALL WRITEMOV DS1302_ADDR,#84H ;写DS1302中的小时MOV DS1302_DATA , SHI ;把写出的天数据存入小时寄存器中LCALL WRITEMOV DS1302_ADDR,#82H ;写DS1302中的分钟MOV DS1302_DATA , FEN ;把写出的天数据存入分钟寄存器中LCALL WRITEMOV DS1302_ADDR,#80H ;写DS1302中的秒钟MOV DS1302_DATA , MIAO ;把写出的天数据存入秒钟寄存器中LCALL WRITEMOV DS1302_ADDR, #90H ;写DS1302控制指令MOV DS1302_DATA,#0A6H ;写数据到DS1302的90控制寄存器中,对电池涓流充电,充电电流为1.1mARET;***************************************************************; 写1302程序;***************************************************************WRITE:CLR CLKNOPSETB RSTNOPMOV A,DS1302_ADDRMOV R4,#8 WRITE1:RRC A ;送地址给1302NOPNOPCLR CLKNOPNOPNOPMOV SU,CSETB CLK ;送入地址在时钟上升沿有效NOPNOPNOPNOPDJNZ R4,WRITE1CLR CLKNOPMOV A,DS1302_DATAMOV R4,#8 WRITE2:RRC ANOP ;送数据给1302,时钟上升沿,数据输入CLR CLKNOPNOPMOV SU,CNOPNOPNOPSETB CLKDJNZ R4,WRITE2CLR RSTCLR ARET ;***************************************************************; 读1302程序;***************************************************************READ:CLR CLKNOPNOPSETB RSTNOPMOV A,DS1302_ADDRMOV R4,#8 READ1:RRC ANOPMOV SU,CNOPNOPNOPSETB CLK ;送入地址在时钟上升沿有效NOPNOPNOPCLR CLKNOPNOPDJNZ R4,READ1 ;判断8位数据是否传送完?MOV R4,#8 READ2:CLR CLKNOPNOPNOPMOV C,SUNOPNOPNOPNOPNOP ;从ds1302中读取数据,时钟下降沿有效NOPNOPNOPSETB CLKNOPDJNZ R4,READ2 ;判断8位数据是否传送完?MOV DS1302_DATA,ACLR RSTRET;***************************************************************; 固定显示显示TIME ,'-',:;***************************************************************XIAN1:MOV P0,#84HLCALL WR_CODEMOV A,#'-' ;1602有字库可以直接调用不用查表LCALL WR_DATAMOV P0,#87HLCALL WR_CODEMOV A,#'-'LCALL WR_DATAMOV P0,#80HLCALL WR_CODEMOV A,#'2' ;计时100年所以20XX 20是固定的LCALL WR_DATAMOV P0,#81HLCALL WR_CODEMOV A,#'0'LCALL WR_DATAMOV P0,#0C1HLCALL WR_CODEMOV A,#'T'LCALL WR_DATAMOV P0,#0C2HLCALL WR_CODEMOV A,#'I'LCALL WR_DATAMOV P0,#0C3HLCALL WR_CODEMOV A,#'M'LCALL WR_DATAMOV P0,#0C4HLCALL WR_DATAMOV P0,#0CCHLCALL WR_CODEMOV A,#':'LCALL WR_DATAMOV P0,#0C9HLCALL WR_CODEMOV A,#':'LCALL WR_DATAMOV P0,#08EHLCALL WR_CODEMOV A,#0DFHLCALL WR_DATAMOV P0,#08FHLCALL WR_CODEMOV A,#'C'LCALL WR_DATARET;*************************************************************** ; 显示数据时分秒年月日周地址数据都在变化没用循环体;***************************************************************XIANSHI:MOV HHH,MIAOLCALL HEXMOV P0,#0CDHLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV A,BMOV P0,#0CEHLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV HHH,FENLCALL HEXMOV P0,#0CAHLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATALCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATAMOV HHH,SHI LCALL HEXMOV P0,#0C7H LCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATA MOV A,BMOV P0,#0C8H LCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATAMOV HHH,DAY LCALL HEXMOV P0,#088H LCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATA MOV A,BMOV P0,#089H LCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATAMOV HHH,YUE LCALL HEXMOV P0,#085H LCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATA MOV A,BMOV P0,#086H LCALL WR_CODE MOVC A,@A+DPTR LCALL WR_DATAMOV HHH,NIAN LCALL HEXMOV P0,#082HMOVC A,@A+DPTRLCALL WR_DATAMOV A,BMOV P0,#083HLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATARET;***************************************************************; 判键子程按一次模式键后就一直判断下一次按键是P2.1还是P2.2;判断进入下一模式还是加1(按下模式键第一次加1需要按两次);MO1加分 MO2加时 MO3加月 MO4加日 MO5加年,除秒可以加减调节外,;时间只能通过加分时日月年调节;***************************************************************KEY:JNB MOSHI,MORET MO:JNB MOSHI,$MOV P0,#0DH ; 开显示不显光标 0fh开显示开光标光标闪烁LCALL WR_CODE MO1:MOV P0,#0CBHLCALL WR_CODEJNB JIA,JIAFFJNB JIAN,JIANFFJNB MOSHI,MO11LJMP MO1 MO11:JNB MOSHI,$;MO2:MOV P0,#0C8HLCALL WR_CODEJNB JIA,JIASSJNB MOSHI,MO21LJMP MO2 MO21:JNB MOSHI,$; ;MO4:MOV P0,#089HLCALL WR_CODELJMP MO4 MO41:JNB MOSHI,$MO3:MOV P0,#086HLCALL WR_CODEJNB JIA,JIAMMJNB MOSHI,MO31LJMP MO3 MO31:JNB moshi,$;MO5:MOV P0,#083HLCALL WR_CODEJNB JIA,JIAYYJNB MOSHI,MO51LJMP MO5 MO51:JNB MOSHI,$MOV P0,#0CH ; 开显示不显光标 0fh开显示开光标光标闪烁LCALL WR_CODElcall XIAN1RET;***************************************************************; 跳转出范围中转;***************************************************************JIAFF:LJMP JIAFJIANFF:LJMP JIANFJIADD:LJMP JIADJIAMM:LJMP JIAMJIAYY:LJMP JIAYJIASS:LJMP JIAS;***************************************************************;判断加1 以及跳转到另一模式(模式是指加分还是加时加月等);***************************************************************JIAF:MOV P0,#0CBHLCALL WR_CODEJNB JIAN,JIANFJB JIA,JIAF2JNB JIA,$mov A,FENADD A , #01HDA Amov FEN,ACJNE A , #60H ,JIAF3MOV FEN ,#00H JIAF3:LCALL WR_DS1302LCALL XIANSHILJMP JIAF JIAF1:JNB MOSHI,$LJMP MO11JIANF:MOV P0,#0CBHLCALL WR_CODEJNB MOSHI,JIANF1JNB JIA,JIAFJB JIAN,JIANFJNB JIAN,$MOV A,FENCJNE A,#00,JIANF2MOV FEN,#59HLJMP JIANF3 JIANF2:CLR CSUBB A,#01DA AMOV FEN,A JIANF3:LCALL WR_DS1302LCALL XIANSHILJMP JIANF JIANF1:JNB MOSHI,$LJMP MO11JIAS:MOV P0,#0C8HJIAS2:JNB MOSHI,JIAS1JB JIA,JIAS2JNB JIA,$MOV A,SHIADD A,#01HDA AMOV SHI,ACJNE A,#24H,JIAS3MOV SHI,#00 JIAS3:LCALL WR_DS1302LCALL XIANSHILJMP JIAS JIAS1:JNB MOSHI,$LJMP MO21JIAD:MOV P0,#089HLCALL WR_CODEJIAD2:JNB MOSHI,JIAD1JB JIA,JIAD2JNB JIA,$MOV A,DAYADD A,#01HDA AMOV DAY,AMOV A,YUECJNE A,#01H,SS1LJMP JIAD4 SS1:CJNE A,#03H,SS2 LJMP JIAD4 SS2:CJNE A,#05H,SS3LJMP JIAD4 SS3:CJNE A,#07H,SS4LJMP JIAD4 SS4:SS5:CJNE A,#10H,SS6LJMP JIAD4 SS6:CJNE A,#12H,SSS JIAD4:MOV A,DAYCJNE A,#32H,JIAD3MOV DAY,#01H JIAD3:LCALL WR_DS1302LCALL XIANSHILJMP JIAD JIAD1:JNB MOSHI,$LJMP MO41 SSS:CJNE A,#02H,S30MOV A,NIANANL A,#0FHMOV GEW,AMOV A,NIANANL A,#0F0HSWAP AMOV B,#10MUL ABADD A,GEWMOV B,#04HDIV ABMOV A,BCJNE A,#00H,S29LJMP S28 S30:MOV A,DAYCJNE A,#31H,JIAD3MOV DAY,#01HLJMP JIAD3 S29:MOV A,DAYCJNE A,#29H,JIAD3MOV DAY,#01HLJMP JIAD3 S28:MOV DAY,#01HLJMP JIAD3;***************************************************************;;判断月的天数判断闰年的方法先将年的数据有十六进制BCD码转换成正常的十六进制;再除以4 判断余数是否为0 为零则为闰年不为零不是;***************************************************************JIAM:MOV P0,#086HLCALL WR_CODE JIAM2:JNB MOSHI,JIAM1JB JIA,JIAM2JNB JIA,$MOV A,YUEADD A,#01HDA AMOV YUE,ACJNE A,#13H,JIAM3MOV YUE,#01 JIAM3:LCALL WR_DS1302LCALL XIANSHI JIAM1:JNB MOSHI,$LJMP MO31 JIAY:MOV P0,#083HLCALL WR_CODE JIAY2:JNB MOSHI,JIAY1JB JIA,JIAY2JNB JIA,$MOV A,NIANADD A,#01HDA AMOV NIAN,ACJNE A,#99H,JIAY3MOV NIAN,#00 JIAY3:LCALL WR_DS1302LCALL XIANSHILJMP JIAY;***************************************************************;温度读取;***************************************************************RE_TEMP:SETB DQACALL INIT_18B20JB FLAG, STRET ST:MOV A, #0CCHACALL WRITE_18B20MOV A, #44HACALL WRITE_18B20ACALL INIT_18B20MOV A, #0CCHACALL WRITE_18B20MOV A, #0BEHACALL WRITE_18B20ACALL READ_18B20RET;***************************************************************;初始化DS18B20;***************************************************************INIT_18B20:SETB DQNOPCLR DQMOV R1, #3 DLY:MOV R0, #107DJNZ R0, $DJNZ R1, DLYSETB DQNOPNOPNOPMOV R0, #25H T2:JNB DQ, T3DJNZ R0, T2JMP T4 T3:SETB FLAGJMP T5JMP T7 T5:MOV R0, #117 T6:DJNZ R0, T6 T7:SETB DQRET ;***************************************************************;写DS18B20;***************************************************************WRITE_18B20:MOV R2, #8CLR C WR1:CLR DQMOV R3, #6DJNZ R3, $RRC AMOV DQ, CMOV R3, #23DJNZ R3, $SETB DQNOPDJNZ R2, WR1SETB DQRET;***************************************************************;读DS18B20;***************************************************************READ_18B20:MOV R4, #2MOV R1, #21H RE0:MOV R2, #8 RE1:CLR CSETB DQNOPNOPCLR DQNOPNOPMOV R3, #9 RE2:DJNZ R3, RE2MOV C, DQMOV R3, #23 RE3:DJNZ R3, RE3RRC ADJNZ R2, RE1MOV @R1, ADEC R1DJNZ R4, RE0RET;***************************************************************;温度转换;***************************************************************CONVER:MOV A, 21HMOV C, 20H.0RRC AMOV C, 20H.1RRC AMOV C, 20H.2RRC AMOV C, 20H.3RRC AMOV 21H, ARET;***************************************************************;显示温度整数部分;***************************************************************DISPLAY_INT:MOV A, 21HMOV B, #10DIV ABMOV B_BIT, AMOV A_BIT, BMOV DPTR, #TABMOV P0,#08cHLCALL WR_CODELCALL WR_DATAMOV P0,#08DHLCALL WR_CODEMOV A, A_BITMOVC A, @A+DPTRLCALL WR_DATARET;***************************************************************; 1602的写数据读指令判忙程序;***************************************************************WR_DATA:MOV P0,ASETB RS ;RS=1选择数据寄存器CLR RW ;RW=0 ;准备CLR E ;E=0 ;执行显示命令LCALL BUSY ;判断液晶模块是否忙?SETB E ;E=1 ;显示完成,程序停车RET WR_CODE:CLR RS ;写入控制命令的子程序CLR RWCLR ELCALL BUSYSETB ERET BUSY:MOV P0,#0FFH ;判断液晶显示器是否忙的子程序CLR RSSETB RWCLR ENOP ;nop一定要有SETB EJB P0.7,BUSY ;如果P0.7为高电平表示忙就循环等待RET ;***************************************************************; 判键子程按一次模式键后就一直判断下一次按键是P3.4还是P3.5 判断进入下一模式还是加1(按下模式键第一次加1需要按两次);MO1加分 MO2加时 MO3加月 MO4加日 MO5加年AKEY:JNB ALARM, XIANGUANGRET XIANGUANG:JNB ALARM,$MOV P0,#01H ; 显示清屏数据指针清零所有显示清零LCALL WR_CODEMOV P0,#02H ; 显示回车数据指针清零LCALL WR_CODEMOV P0,#038H ;设置16*2显示 5*7 点阵 8位数据接口LCALL WR_CODEMOV P0,#04H ; 当读或写一个数据时地址指针自动加一光标自动加一LCALL WR_CODEMOV P0,#0DH ; 开显示不显光标 0fh开显示开光标光标闪烁LCALL WR_CODELCALL AXIANLCALL AXIANSHIMOMIAO: MOV P0,#0CBHLCALL WR_CODEJNB JIA,INCMJNB JIAN,DECMJNB ALARM ,BACKMIAOLJMP MOMIAO BACKMIAO:JNB ALARM,$LJMP MOFENINCM:MOV P0,#0CBHLCALL WR_CODE INCM2:JNB ALARM,INCM1JNB JIAN,DECMJB JIA,INCM2JNB JIA,$INC AMIAOMOV A,AMIAOCJNE A,#60,INCM3MOV AMIAO,#00 INCM3:LCALL AXIANSHILJMP INCMINCM1:JNB ALARM ,BACKMIAODECM:MOV P0,#0CBHLCALL WR_CODEJNB ALARM,DECM1JNB JIA,INCMJB JIAN,DECMJNB JIAN,$MOV A,AMIAOCJNE A,#00,DECM2MOV AMIAO,#59LJMP DECM3 DECM2:CLR CSUBB A,#01MOV AMIAO,A DECM3:LCALL AXIANSHILJMP DECM DECM1:JNB ALARM,BACKMIAOMOFEN:MOV P0,#0C8HLCALL WR_CODEJNB JIA,INCFJNB JIAN,DECFJNB ALARM ,BACKFENLJMP MOFEN BACKFEN:JNB ALARM,$LJMP AMOSHIINCF:MOV P0,#0C8HLCALL WR_CODEINCF2:JNB ALARM,INCF1JNB JIAN,DECFJB JIA,INCF2JNB JIA,$INC AFENMOV A,AFENCJNE A,#60,INCF3MOV AFEN,#00INCF3:LCALL AXIANSHILJMP INCFINCF1:JNB ALARM ,BACKFENDECF:MOV P0,#0C8HLCALL WR_CODEJNB ALARM,DECF1JNB JIA,INCFJB JIAN,DECFJNB JIAN,$MOV A,AFENCJNE A,#00,DECF2MOV AFEN,#59LJMP DECF3DECF2:CLR C;DEC ASUBB A,#01;DA AMOV AFEN,ADECF3:LCALL AXIANSHILJMP DECFDECF1:JNB ALARM,BACKFENAMOSHI:MOV P0,#0C5HLCALL WR_CODEJNB JIA,INCSJNB JIAN,DECSJNB ALARM ,BACKSHI LJMP AMOSHIINCS:MOV P0,#0C5HLCALL WR_CODEINCS2:JNB ALARM,INCS1JNB JIAN,DECSJB JIA,INCS2JNB JIA,$INC ASHIMOV A,ASHICJNE A,#24,INCS3MOV ASHI,#00INCS3:LCALL AXIANSHILJMP INCSINCS1:JNB ALARM ,BACKSHIBACKSHI:JNB ALARM,$MOV P0,#01HLCALL WR_CODEMOV P0,#0CHLCALL WR_CODELCALL XIAN1LJMP MAINDECS:MOV P0,#0C5HLCALL WR_CODEJNB ALARM,DECS1JNB JIA,INCSJB JIAN,DECSJNB JIAN,$MOV A,ASHICJNE A,#00,DECS2MOV ASHI,#23LJMP DECS3 DECS2:CLR CSUBB A,#01MOV ASHI,ADECS3:LCALL AXIANSHILJMP DECS DECS1:JNB ALARM,BACKSHI;***************************************************************; 闹钟部分固定显示显示TIME ADJUST;***************************************************************AXIAN:MOV P0,#82HLCALL WR_CODEMOV A,#'B'LCALL WR_DATAMOV P0,#83HLCALL WR_CODEMOV A,#'E'LCALL WR_DATAMOV P0,#84HLCALL WR_CODEMOV A,#'L' ;LCALL WR_DATAMOV P0,#85HLCALL WR_CODEMOV A,#'L'LCALL WR_DATAMOV P0,#088HLCALL WR_CODEMOV A,#'A'LCALL WR_DATAMOV P0,#089HLCALL WR_CODEMOV A,#'D'LCALL WR_DATAMOV P0,#08AHLCALL WR_CODEMOV A,#'J'LCALL WR_DATAMOV P0,#08BHLCALL WR_CODEMOV A,#'U'LCALL WR_DATAMOV P0,#08CHLCALL WR_CODEMOV A,#'S'LCALL WR_DATAMOV P0,#08DHLCALL WR_CODEMOV A,#'T'LCALL WR_DATAMOV P0,#0C6HLCALL WR_CODEMOV A,#':'LCALL WR_DATAMOV P0,#0C9HLCALL WR_CODEMOV A,#':'LCALL WR_DATARET;*************************************************************** ; 显示数据时分秒年月日周地址数据都在变化没用循环体;*************************************************************** AXIANSHI:MOV HHH,AMIAOLCALL AHEXMOV P0,#0CAHLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV A,BMOV P0,#0CBHLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV HHH,AFENLCALL AHEXMOV P0,#0C7HLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV A,BMOV P0,#0C8HLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV HHH,ASHILCALL AHEXMOV P0,#0C4HLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATAMOV A,BMOV P0,#0C5HLCALL WR_CODEMOVC A,@A+DPTRLCALL WR_DATARET;***************************************************************; 分离十进制BCD码中的十位和个位;***************************************************************AHEX: MOV A,HHHMOV B,#10DIV ABRET;***************************************************************; 闹钟模块;***************************************************************;==========================条件符合响铃1分钟============================ BELLRING:MOV HHH,SHILCALL HEXMOV R2,BMOV B,#10MUL ABADD A,R2CJNE A ,ASHI, AAAMOV HHH,FENLCALL HEXMOV R2,BMOV B,#10MUL ABADD A,R2CJNE A ,AFEN,AAAMOV HHH,MIAOLCALL HEXMOV R2,BMOV B,#10MUL ABADD A,R2CJNE A ,AMIAO,AAALCALL BELLRAAA:LJMP MAIN;==========================闹钟响铃10秒钟============================BELLR:MOV COUNT,#10ZZZ:SETB SOUNDLCALL DELAY500MSCLR SOUNDLCALL DELAY500MSlcall RD_DS1302LCALL XIANSHIJNB ALARM ,BBBDJNZ COUNT ,ZZZBBB:JNB ALARM ,$SETB SOUNDRET;==========================设置闹铃延时500MS响一次=================== DELAY500MS:MOV R7,#17HDL1:MOV R6,#98HDL0:MOV R5,#46HDJNZ R5,$DJNZ R6,DL0DJNZ R7,DL1RET;*************************************************************** ; 转换成BCD码;*************************************************************** ZHUAN:MOV A,HHHADD A,#00HDA ARET;*************************************************************** ; 分离BCD码中的十位和个位;*************************************************************** HEX:MOV A,HHHMOV B,#16DIV ABRET;*************************************************************** ; 数据查表;*************************************************************** TAB: DB 30H,31H,32H,33H,34H,35H,36H,37H,38H,39HENDORCAD电路原理图PADS软件的PCB图实物图。

LCD12864单片机电子万年历程序

LCD12864单片机电子万年历程序

*********************************#include<>#include<>#define LCD_Data P0#define uchar unsigned charunsigned charsec,min,hour,day,month,year,cen,week, next,aa,bb,cc,dd,mm,temp0,LunarMonth, LunarDay,LunarYear;int temp;uchar tt=1;bit c_moon;bit cenbit=1;bit w;sbit LCD_RS=P2^0;sbit LCD_RW=P2^1;sbit LCD_E=P2^2;sbit PSB=P2^3;sbit DS1302_CLK=P3^7;sbit DS1302_IO=P3^6;sbit DS1302_RST=P3^5;sbit DS18B20=P2^5;sbit speak=P2^7;sbit SetKey=P1^4; // 按键功能:设置sbit SureKey=P1^5; // 按键功能:确认sbit PlusKey=P1^6; // 按键功能:加sbit ReduceKey=P1^7; // 按键功能:减sbit scl=P1^1;sbit sda=P1^0;void DisplayShengXiao(void);void delay(unsigned int a)// 延时 1MS/ 次{unsigned char i;while(--a){for(i=0;i<125;i++) ;}}void delayb(unsigned int count){while(count--);}void delays(){ ; ;}void start(){sda=1;scl=1;delays();sda=0;delays();}void stop(){sda=0;scl=1;delays();sda=1;delays();}void response(){uchar i;while((sda==1)&&i<255)i++; //给应答信号 sda=0 ;错误( while (( sda==0 )|i<255 ) i++ ;)程序进入 while 死循环scl=1;delays();scl=0; // 没有这一步出错,如果没有 sda 将保持 0 状态,一直处于应答中。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^3;
sbit L=P1^4;
sbit DZ=P3^5;//这是用于控制数码管关闭,这个数码管和液晶同时亮所需电流大,单片机难驱动
sbit rs=P2^5;
sbit rw=P2^6;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
//第一行
void write_sfr(uchar ad,uchar da)
{
uchar x,y;
x=da/10;
y=da%10;
write_com(0x80+ad);
{
lcden=0;
rs=1;
rw=0;
P0=dat;
delay(5);lBiblioteka den=1;delay(5);
lcden=0;
}
//初始化函数
//**************************************************
void init()
{
L=1;
jd=0;
DZ=1;
P0=0XFF;
//延时函数,在12MHz的晶振频率下延时1ms
//************************************************
void delay(uint t)
{
uchar x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
//写数据
//************************************************
sbit lcden=P2^7;
sbit w1=P3^2;
sbit w2=P3^3;
sbit w4=P3^4;
sbit w6=P3^7;
sbit jd=P3^6;
uchar temp,f,k;
uchar count,s1num;
uchar miao,shi,fen,miao1,shi1,fen1,nian=14,nian2=20,yue=12,ri=12,ri1=12,r=0,k,num;
uchar code table[]=" 2014-12-12 FRI ";
uchar code table1[]=" 00:00:00";
uchar code table2[]="MONTUEWEDTHUFIRSATSUN";
//************************************************
write_date(0x30+x);
write_date(0x30+y);
}
//星期
void write_xq(uchar m)
{
uchar p,o,i;
p=table2[m+m+m];
o=table2[m+m+m+1];
i=table2[m+m+m+2];
write_com(0x80+12);
write_date(p);
void write_com(uchar com)
{
rs=0;
lcden=0;
rw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//写指令
//***********************************************
void write_date(uchar dat)
这个程序是在1602液晶上显示世界时钟,并且能设定闹钟,链接闹钟的引脚为p1^4,w1控制位选,w2控制加一,w4控制减一
w6为闹钟模式和时钟模式的切换键,按一下w6可设定那种时间,w2单独使用时为开关闹钟,中间屏蔽了一段18b20温度显示程序,把屏蔽去除后也能在液晶上显示温度
#include<reg52.h>
}
//将一个两位数的个位和十位分离,液晶只能一位一位的显示
//********************************************************
//第二行
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
void keyscan()
{
if(w1==0)
{
delay(5);
if(w1==0)
{s1num++;
while(!w1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
if(s1num==4)
{
write_com(0x80+9);
}
if(s1num==5)
{
write_com(0x80+6);
}
if(s1num==6)
{
write_com(0x80+3);
}
if(s1num==7)
{
write_com(0x80+1);
DZ=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_date(o);
write_date(i);
}
//w1控制小时分钟秒哪一位闪动,按下时定时器停止,一下是秒闪,两下是分闪,三下是小时闪,
//w2控制闪位的增加,按一下加一
//w3控制闪位的减一,按一下减一
//********************************************************
}
if(s1num==8)
{
write_com(0x80+12);
}
if(s1num==9)
{
s1num=0;
相关文档
最新文档