51单片机ds1302时钟掉电保护
单片机源程序如下:
- /*************************************************************
- 不借助24c02存储数据,利用DS1302内部RAM写入数据,如果数据掉电丢失
- 则初始化,如果数据不丢失,则不初始化。
- **************************************************************/
- #include<reg51.h>
- #include<intrins.h>
- #include"lcd1602.h"
- #include"ds1302.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char second,minute,hour,day,month,year,week,count=0;
- unsigned char ReadValue,num,time;
-
- sbit menu=P3^2; //菜单
- sbit add=P3^3; //加一
- sbit dec=P3^4; //减一
- sbit ok = P3^5; //确认
- void delay(unsigned int z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- 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;
- DisplaySecond(second);
- DisplayMinute(minute);
- DisplayHour(hour);
- DisplayDay(day);
- DisplayMonth(month);
- DisplayYear(year);
- DisplayWeek(week);
- }
- 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 key_scan(void)
- { uchar miao,s1num=0;
-
- if(menu==0)
- {
- delay(5);
- if(menu==0)
- {
- while(!menu);
- s1num++;
- while(1)
- {
-
- if(menu==0)
- {
- delay(5);
- if(menu==0)
- {
- while(!menu);
- s1num++;
- }
- }
- rd=0;
- 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)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(year,1,0x8d,1);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(year,0,0x8d,1);
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//年
- //月
- if(s1num==2)
- {
- month=ReadSet1302(0x89);
- write_com(0x80+7); //月光标
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(month,1,0x89,2);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(month,0,0x89,2);
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//月
- //日
- if(s1num==3)
- {
- day=ReadSet1302(0x87);
- write_com(0x80+10);//日光标
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(day,1,0x87,3);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(day,0,0x87,3); //写入日寄存器
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//日
- //星期
- if(s1num==4)
- {
- week=ReadSet1302(0x8b);
- write_com(0x80+14); //星期光标
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(week,1,0x8b,4);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(week,0,0x8b,4);
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//星期
- //时
- if(s1num==5)
- {
- hour=ReadSet1302(0x85);
- write_com(0x80+0x40+3); //时光标
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- turn_val(hour,1,0x85,5);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(hour,0,0x85,5);
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//时
- //分
- if(s1num==6)//调时间分
- {
- minute=ReadSet1302(0x83);
- write_com(0x80+0x40+6);
- if(add==0)
- {
- delay(5);
- if(add==0)
- {
- while(!add);
- turn_val(minute,1,0x83,6); //写入分寄存器
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(minute,0,0x83,6); //写入分寄存器
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//分
- //秒
- if(s1num==7)//调时间秒
- {
- second=ReadSet1302(0x81);
- write_com(0x80+0x40+9);//秒光标
- if(add==0)
- {
- delay(3);
- if(add==0)
- {
- while(!add);
- if(second==0x60)
- second=0x00;
- turn_val(second,1,0x81,7);
- }
- }
- if(dec==0)
- {
- delay(3);
- if(dec==0)
- {
- while(!dec);
- turn_val(second,0,0x81,7);
- }
- }
- if(ok == 0) //确认键
- {
- delay(3);
- if(ok==0)
- {
- while(!ok);
- miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }//秒
- if(s1num==8)
- { miao=ReadSet1302(0x81);
- second=miao;
- WriteSet1302(0x80,second&0x7f);
- s1num=0;//s1num清零//
- write_com(0x0c);//光标不闪烁//
- break;
- }
- }
- }
- }
- }
- void main()
- {
- init();
- Init_DS1302(); //将1302初始化
- while(1)
- {
- read_date();
- key_scan();
- }
- }
复制代码
所有资料51hei提供下载:
程序1-无24c02,掉电保护.zip
(61.27 KB, 下载次数: 66)
|