用51单片机做时钟,我没用1302,就是一个简单的时钟,可以用6个数码管显示时分秒,液晶上显示日期和时间,可以用3个按键实现功能选择和加减。这些在开发板上都可以实现,之后我想在此基础上我想用AT24C02实现断电保护功能,当我的程序写完之后,发现无法实现,这个只能实现的是用按键控制的数字,比如我用按键将时间调成000019,之后让它自己往上加,然后我再断电,等我在上电的时候数码管上显示的还是000019,这个现象是属于断电保护还是代码出问题了,我有点不太清楚,代码如下,请求大家帮助。谢谢。
单片机源程序如下:
- #include<reg52.h>
- sbit lsa=P2^2;
- sbit lsb=P2^3;
- sbit lsc=P2^4;
- sbit k1=P3^1;
- sbit k2=P3^0;
- sbit k3=P3^2;
- sbit SDA=P2^0;
- sbit SCL=P2^1;
- sbit e=P2^7;
- sbit rw=P2^5;
- sbit rs=P2^6;
- typedef unsigned int ui;
- typedef unsigned char uc;
- ui code shuju[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- ui disp[6];
- ui num,shi,fen,miao;
- ui ss=0;
- uc code table[]=" 2020-2-12 WED";
- void delay(ui i)
- {
- while(i--);
- }
- void Delay10us()
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=2;a>0;a--);[/backcolor][/color]
- [color=rgb(102, 102, 102)][backcolor=rgb(255, 255, 255)]}
- void yjdelay(ui c)
- {
- uc a,b;
- for (; c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
- }[/backcolor][/color]
- [color=rgb(102, 102, 102)][backcolor=rgb(255, 255, 255)]void I2cStart()
- {
- SDA=1;
- Delay10us();
- SCL=1;
- Delay10us();//?????????SDA???????>4.7us
- SDA=0;
- Delay10us();//?????????>4us
- SCL=0;
- Delay10us();
- }
- void I2cStop()
- {
- SDA=0;
- Delay10us();
- SCL=1;
- Delay10us();//??????????4.7us
- SDA=1;
- Delay10us();
- }
- void respons()
- {
- ui b;
- SCL=1;
- while(SDA)//????????????????豸??SDA????
- {
- b++;
- if(b>200) //???????2000us????????????????????????????????
- {
- SCL=0;
- Delay10us();
- }
- }
- SCL=0;
- Delay10us();
- }
- void I2cSendByte(unsigned char dat)
- {
- unsigned char a=0,b=0;//???255??????????????1us????????255us??
- for(a=0;a<8;a++)//?????8λ???????λ???
- {
- SDA=dat>>7; //?????????SCL=0??????????????SDA???
- dat=dat<<1;
- Delay10us();
- SCL=1;
- Delay10us();//???????>4.7us
- SCL=0;
- Delay10us();//??????4us
- }
- SDA=1;
- Delay10us();
- SCL=0;
- Delay10us();
- }
- unsigned char I2cReadByte()
- {
- unsigned char a=0,dat=0;
- SDA=1; //?????????????????SCL????0
- Delay10us();
- for(a=0;a<8;a++)//????8?????
- {
- SCL=1;
- Delay10us();
- dat<<=1;
- dat|=SDA;
- Delay10us();
- SCL=0;
- Delay10us();
- }
- return dat;
- }[/backcolor][/color]
- [color=rgb(102, 102, 102)][backcolor=rgb(255, 255, 255)]
- void write_add(unsigned char addr,unsigned char dat)
- {
- I2cStart();
- I2cSendByte(0xa0);//????д???????
- respons();
- I2cSendByte(addr);//?????д???????
- respons();
- I2cSendByte(dat); //????????
- respons();
- I2cStop();
- }
- unsigned char read_add(unsigned char addr)
- {
- unsigned char num;
- I2cStart();
- I2cSendByte(0xa0); //????д???????
- respons();
- I2cSendByte(addr); //????????????
- respons();
- I2cStart();
- I2cSendByte(0xa1); //????????????
- respons();
- num=I2cReadByte(); //???????
- I2cStop();
- return num;
- }
- void init24c02()
- {
- SDA=1;
- Delay10us();
- SCL=1;
- Delay10us();
- miao=read_add(1);
- fen=read_add(2);
- shi=read_add(3);
- }
- void write_com(uc com)
- {
- rs=0;
- rw=0;
- e=0;
- P0=com;
- yjdelay(1);
- e=1;
- yjdelay(5);
- e=0;
- }
- void lcdwrite_data(uc dat)
- {
- e=0;
- rs=1;
- rw=0;
- P0=dat;
- yjdelay(1);
- e=1;
- yjdelay(5);
- e=0;
- }
- void write_sfm(uc add,uc date)
- {
- uc shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+0x40+add);
- lcdwrite_data(0x30+shi);
- lcdwrite_data(0x30+ge);
- }
- void lcdinit()
- {
- write_com(0x38);
- write_com(0x0c); //?????????????
- write_com(0x06); //д???????1
- write_com(0x01); //????
- }
- void yejing()
- {
- uc i;
- lcdinit();
- for(i=0;i<15;i++)
- {
- lcdwrite_data(table[i]);
- delay(10);
- }
- write_com(0x80+0x40+6);
- lcdwrite_data(':');
- delay(10);
- write_com(0x80+0x40+9);
- lcdwrite_data(':');
- delay(10);
- write_sfm(4,shi);
- write_sfm(7,fen);
- write_sfm(10,miao);
- }
- void datapros()
- {
- disp[0]=shuju[miao%10];
- disp[1]=shuju[miao/10];
- disp[2]=shuju[fen%10];
- disp[3]=shuju[fen/10];
- disp[4]=shuju[shi%10];
- disp[5]=shuju[shi/10];
- }
- void display()
- {
- ui i;
- for(i=0;i<6;i++)
- {
- switch(i)
- {
- case(0): lsa=0;lsb=0;lsc=0;break;
- case(1): lsa=1;lsb=0;lsc=0;break;
- case(2): lsa=0;lsb=1;lsc=0;break;
- case(3): lsa=1;lsb=1;lsc=0;break;
- case(4): lsa=0;lsb=0;lsc=1;break;
- case(5): lsa=1;lsb=0;lsc=1;break;
- }
- P0=disp[i];
- delay(100);
- P0=0x00;
- }
- }
- void init()
- {
- TMOD=0x01;
- TH0=0xfc;
- TL0=0x18; //1ms
- EA=1;
- TR0=1;
- ET0=1;
- }
- void key()
- {
- if(k1==0)
- {
- delay(100);
- {
- if(k1==0)
- {
- ss=ss+2;
- while(!k1);
- switch(ss)
- {
- case(2): lsa=0;lsb=0;lsc=0;TR0=0;write_com(0x80+0x40+10);write_com(0x0f);break;
-
- case(4): lsa=0;lsb=1;lsc=0;write_com(0x80+0x40+7);break;
-
- case(6): lsa=0;lsb=0;lsc=1;write_com(0x80+0x40+4);break;
-
- case(8): ss=0;write_com(0x0c);TR0=1;break;
- }
- }
- }
- }
- if(ss!=0)
- {
- if(k2==0)
- {
- delay(100);
- {
- if(k2==0)
- {
- while(!k2);
- if(ss==2)
- {
- miao++;
- if(miao==60)
- miao=0;
- write_sfm(10,miao);
- write_com(0x80+0x40+10);
- write_add(1,miao);
- }
- if(ss==4)
- {
- fen++;
- if(fen==60)
- fen=0;
- write_sfm(7,fen);
- write_com(0x80+0x40+7);
- write_add(2,fen);
- }
- if(ss==6)
- {
- shi++;
- if(shi==24)
- shi=0;
- write_sfm(4,shi);
- write_com(0x80+0x40+4);
- write_add(3,shi);
- }
- }
- }
- }
- if(k3==0)
- {
- delay(100);
- if(k3==0)
- {
- while(!k3);
- if(ss==2)
- {
- miao--;
- if(miao==-1)
- miao=59;
- write_sfm(10,miao);
- write_com(0x80+0x40+10);
- write_add(1,miao);
- }
- if(ss==4)
- {
- fen--;
- if(fen==-1)
- fen=59;
- write_sfm(7,fen);
- write_com(0x80+0x40+7);
- write_add(2,fen);
- }
- if(ss==6)
- {
- shi--;
- if(shi==-1)
- shi=23;
- write_sfm(4,shi);
- write_com(0x80+0x40+4);
- write_add(3,shi);
- }
- }
- }
- }
-
- }
- void main()
- {
- init();
- init24c02();
- yejing();
- while(1)
- {
- key();
- datapros();
- display();
- }
- }
- void init0() interrupt 1
- {
- TH0=0xfc;
- TL0=0x18;
- num++;
- if(num==1000)
- {
- miao++;
- write_sfm(10,miao);
- num=0;
- if(miao==60)
- {
- miao=0;
- fen++;
- write_sfm(7,fen);
- if(fen==60)
- {
- fen=0;
- shi++;
- write_sfm(4,shi);
- if(shi==24)
- {
- shi=0;
- write_sfm(4,shi);
- write_add(3,shi);
- }
- write_sfm(7,fen);
- write_add(2,fen);
- }
- write_sfm(10,miao);
- write_add(1,miao);
- }
- }
-
- }
复制代码
|