我做了一个关于ds1302的实时时钟,但电源掉电后实时时钟又会复位,即是掉电后时钟就不会走时,,, 50
下面是我写的源代码:#include<reg51.h>#include"ds1302.h"#defineGPIO_DIGP0sbitLSA=P2^2;sbitLSB=P2...
下面是我写的源代码:
#include<reg51.h>#include"ds1302.h"#define GPIO_DIG P0sbit LSA=P2^2;sbit LSB=P2^3;sbit LSC=P2^4;sbit k1=P1^2;sbit k2=P1^3;sbit k3=P1^4;sbit k4=P1^5;bit k1_flag=0;unsigned char code DIG_CODE[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char DisplayData[8];void DigDisplay();void delay(uint x){ uint i,j;for(i=x;i>0;i--) for(j=110;j>0;j--);}void main(){ while(1) { Ds1302Init(); Ds1302ReadTime(); DisplayData[0] = DIG_CODE[TIME[2]/16];DisplayData[1] = DIG_CODE[TIME[2]&0x0f];DisplayData[2] = 0x40;DisplayData[3] = DIG_CODE[TIME[1]/16]; DisplayData[4] = DIG_CODE[TIME[1]&0x0f]; DisplayData[5] = 0x40;DisplayData[6] = DIG_CODE[TIME[0]/16]; DisplayData[7] = DIG_CODE[TIME[0]&0x0f]; DigDisplay(); }}void DigDisplay(){unsigned char i;unsigned int j;for(i=0;i<8;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; case(6): LSA=0;LSB=1;LSC=1; break;case(7): LSA=1;LSB=1;LSC=1; break; } GPIO_DIG=DisplayData[i]; j=50; while(j--); GPIO_DIG=0x00;}}1302.h程序中的初始化程序
void Ds1302t()
{
uchar n;
Ds1302Write(0x8E,0X00); //禁止写保护,就是关闭写保护功能
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
}
void Ds1302Init()
{ uchar flag;
flag=Ds1302Read(0x81);
if(flag&0x80)
{ Ds1302t();
}
} 展开
#include<reg51.h>#include"ds1302.h"#define GPIO_DIG P0sbit LSA=P2^2;sbit LSB=P2^3;sbit LSC=P2^4;sbit k1=P1^2;sbit k2=P1^3;sbit k3=P1^4;sbit k4=P1^5;bit k1_flag=0;unsigned char code DIG_CODE[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char DisplayData[8];void DigDisplay();void delay(uint x){ uint i,j;for(i=x;i>0;i--) for(j=110;j>0;j--);}void main(){ while(1) { Ds1302Init(); Ds1302ReadTime(); DisplayData[0] = DIG_CODE[TIME[2]/16];DisplayData[1] = DIG_CODE[TIME[2]&0x0f];DisplayData[2] = 0x40;DisplayData[3] = DIG_CODE[TIME[1]/16]; DisplayData[4] = DIG_CODE[TIME[1]&0x0f]; DisplayData[5] = 0x40;DisplayData[6] = DIG_CODE[TIME[0]/16]; DisplayData[7] = DIG_CODE[TIME[0]&0x0f]; DigDisplay(); }}void DigDisplay(){unsigned char i;unsigned int j;for(i=0;i<8;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; case(6): LSA=0;LSB=1;LSC=1; break;case(7): LSA=1;LSB=1;LSC=1; break; } GPIO_DIG=DisplayData[i]; j=50; while(j--); GPIO_DIG=0x00;}}1302.h程序中的初始化程序
void Ds1302t()
{
uchar n;
Ds1302Write(0x8E,0X00); //禁止写保护,就是关闭写保护功能
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
}
void Ds1302Init()
{ uchar flag;
flag=Ds1302Read(0x81);
if(flag&0x80)
{ Ds1302t();
}
} 展开
3个回答
引用tony_sgt的回答:
跟代码没关系,跟硬件设计有关系,需要有个电池加在DS1302上
跟代码没关系,跟硬件设计有关系,需要有个电池加在DS1302上
展开全部
Ds1302Write(0x8E,0X00); //禁止写保护,就是关闭写保护功能 (这里得修改为 Ds1302Write(0x8E,0X80);) 记得在ds1302处加上电池,就可以长时间掉电都可以走
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改if(flag&0x80)
if ((flag&0x80)!=0)
if ((flag&0x80)!=0)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询