数据在数码管上显示
问题:1.打开初始ds1302写保护时,数据可以设置,但是每次关掉
电源数据都会重新设置一次,起不了时钟的功能
2.关闭初始ds1302写保护时 ,数据设置不了,每次掉电都能保持原有数据
(下方标示处为问题所在,各位帮帮忙看下,我QQ:410294170)
#include <REGX52.H>
#define uchar unsigned char
unsigned int second10,second1,minute10,minute1,hour10,hour1,date10,date1,month10,month1,year10,year1,day;
int num;
sbit RST=P1^2;
sbit SCLK=P1^0;
sbit SDA=P1^1;
sbit key1=P1^4;
void delay(int hm) //延时函数
{
int i,j;
for(i=hm;i>0;i--)
for(j=110;j>0;j--);
}
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define READ_DATE 0x87 //几号
#define READ_MONTH 0x89
#define READ_DAY 0x8B //星期几
#define READ_YEAR 0x8D
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define WRITE_DATE 0x86 //几号
#define WRITE_MONTH 0x88
#define WRITE_DAY 0x8A //星期几
#define WRITE_YEAR 0x8C
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf}; //定义共阳极字型码0123456789-
void delayum(int hm)
{
int i,j;
for(i=hm;i>0;i--)
for(j=110;j>0;j--);
}
void ds1302writebyte(uchar dat)
{
uchar i;
SCLK=0;
for(i=0;i<8;i++)
{
SDA=dat&0x01;
SCLK=1;
SCLK=0;
dat>>=1;
}
}
uchar ds1302readbyte() //
{
uchar i,dat;
for(i=0;i<8;i++)
{
dat>>=1;
if(SDA==1)
dat|=0x80;
SDA=dat&0x01;
SCLK=1;
SCLK=0;
}
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 display(second10,second1,minute10,minute1,hour10,hour1) //显示子程序
{
P0=0;
P2=0xfe;
P0=dispcode[second10];//显示秒的十位
delayum(1);P0=0;
P2=0xfd;
P0=dispcode[second1]; //显示秒的个位
delayum(1);P0=0;
P2=0xfb;
P0=dispcode[10]; //显示"-"
delayum(1);P0=0;
P2=0xf7;
P0=dispcode[minute10];//显示分的十位
delayum(1);P0=0;
P2=0xef;
P0=dispcode[minute1]; //显示分的个位
delayum(1);P0=0;
P2=0xdf;
P0=dispcode[10]; //显示"-"
delayum(1);P0=0;
P2=0xbf;
P0=dispcode[hour10]; //显示时的十位
delayum(1);P0=0;
P2=0x7f;
P0=dispcode[hour1]; //显示时的个位
delayum(1);
}
void init_ds1302()
{
// ds1302write(0x8e,0x00);// 写保护关
ds1302write(WRITE_SECOND ,0x55);// 初始秒值为 0 0 0 0
ds1302write(WRITE_MINUTE,0x55); // 初始分钟值为 0 0 0 0
ds1302write(WRITE_HOUR,0x23);// 初始为 24242424 小时模式 初始时间为
ds1302write(WRITE_DAY,0x07);//2011 年 1 月 1 日 星期 6
ds1302write(WRITE_MONTH,0x12);
ds1302write(WRITE_YEAR,0x13);
ds1302write(WRITE_DATE,0x31);
ds1302write(0x90,0x01);// 充电
ds1302write(0xc0,0xf0); // 初始化一次标示
// ds1302write(0x8e,0x80);
}
void init_dat()
{
second10=ds1302read(READ_SECOND)/16;
second1=ds1302read(READ_SECOND)%16;
minute10=ds1302read(READ_MINUTE)/16;
minute1=ds1302read(READ_MINUTE)%16+num;
hour10=ds1302read(READ_HOUR)/16;
hour1=ds1302read(READ_HOUR)%16;
date10=ds1302read(READ_DATE)/16;
date1=ds1302read(READ_DATE)%16;
month10=ds1302read(READ_MONTH)/16;
month1=ds1302read(READ_MONTH)%16;
year10=ds1302read(READ_YEAR)/16;
year1=ds1302read(READ_YEAR)%16;
day=ds1302read(READ_DAY);
}
main()
{
init_ds1302();
while(1)
{
init_dat();
display(minute10,minute1,hour10,hour1,second10,second1);
}
}
秒字节的第一个比特,为1时,时间不会工作。
一周热门 更多>