PCF8563实时时钟 程序只实现在数码管上显示时分秒。麻烦帮我看看哪不对呀!
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P2^1;
sbit sda=P2^0;
//uchar temp[7];
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数码管编码表,存储在程序存储器中
struct TIME //定义一个结构体类型,包含秒分时等时间成员
{
uchar minute;
uchar second;
uchar hour;
uchar day;
uchar week;
uchar month;
uchar year;
};
struct TIME time;
void delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display()//显示
{
P0=tab[time.second%10]; //秒
P1=0xdf;
delay1(7);
P0=tab[time.second/10];
P1=0xef;
delay1(7);
P0=tab[time.minute%10]; //分
P1=0xf7;
delay1(7);
P0=tab[time.minute/10];
P1=0xfb;
delay1(7);
P0=tab[time.hour%10]; //时
P1=0xfd;
delay1(7);
P0=tab[time.hour/10];
P1=0xfe;
delay1(2);
}
void init()//初始化
{
scl=1;
delay();
sda=1;
delay();
}
void start() //起始信号
{
sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}
void stop() //结束信号
{
sda=0;
scl=1;
delay();
sda=1;
delay();
scl=0;
}
void respond()//从机应答
{
uint i;
scl=1;
delay();
while((sda==1)&&(i<250)) i++;
scl=0;
delay();
}
void respondmaster()//主机应答??????主机在读取一个数据后,需要继续读取数据,
//此时需要主机给从机发送个低电平的应答信号。
{
sda=0;
delay();
scl=1;
delay();
scl=0;
delay();
}
void norespondmaster()//主机无应答??????主机在读取一个数据后,需要继续读取数据,
//此时需要主机给从机发送个高电平的应答信号。
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void write(uchar date)//写一个字节
{
uchar i,datee;
datee=date;
for(i=8;i>0;i--)
{
datee<<=1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read() //读一个字节
{
uchar i,s=0;
scl=0;
delay();
sda=1;
delay();
for(i=8;i>0;i++)
{
scl=1;
delay();
s=(s<<1)|sda;
scl=0;
delay();
}
return s;
}
void write_(uchar add, uchar length,uchar *pbuf)//写入多个字节???????????
{ //add 寄存器地址;length 要写入的字节数;pbuf 指向数据缓冲区的指针
uchar i;
start();
write(0xa2);
respond();
write(add);
respond();
for(i=0;i<length;i++)
{
write(pbuf[i]);
respond();
}
stop();
}
void read_(uchar add,uchar length,uchar *pbuf)//读取多个字节????????????????
{
uchar i;
start();
write(0xa2);
respond();
write(add);
respond();
start();
write(0xa3);
respond();
for(i=0;i<length-1;i++)
{
pbuf[i]=read();
respondmaster();
}
pbuf[i]=read();
norespondmaster();
stop();
}
uchar bcd2val(uchar x)//bcd码转换为十进制
{
uchar timeshi;
timeshi=(x>>4)*10+(x&0x0f);
return timeshi;
}
uchar val2bcd(uchar x)//十进制数转换为BCD码
{
uchar timeer;
timeer=((x/10)<<4)|(x%10);
return timeer;
}
void timeset() //设定时间
{
uchar temp[7];
uchar i;
for(i=0;i<7;i++)
{
temp[i]=val2bcd(((uchar *)(&time))[i]);//?????????
}
write_(0x02,7,temp);
}
void qidong()//启动时钟芯片
{
start();
write(0xa2);
respond();
write(0x00);
respond();
write(0x00);
respond();
stop();
}
void readtime()//读取时间
{
uchar temp[7];
read_(0x02,7,temp);
time.second=bcd2val(temp[0]&0x7f);//秒
time.minute=bcd2val(temp[1]&0x7f);//分
time.hour=bcd2val(temp[2]&0x3f); //时
time.day=bcd2val(temp[3]&0x3f); //日
time.week=bcd2val(temp[4]&0x07); //星期
time.month=bcd2val(temp[5]&0x1f); //月
time.year=bcd2val(temp[6]); //年
}
void main()
{
time.year=10;
time.month=12;
time.day=22;
time.minute=0;
time.second=0;
time.hour=0;
time.week=5;
timeset();
qidong();
while(1)
{
readtime();
display();
}
} |