关于DS1302时钟芯片问题。帮忙看看程序哪错了?
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0xC0,0xF9,0xA4,0xB0,
0x99,0x92,0x82,0xF8,
0x80,0x90};
uchar shi,fen,miao;
sbit ce=P1^2;
sbit sclk=P1^1;
sbit sda=P1^0;
void delay(uint z) //延迟
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_bit(uchar date) //写一位
{
uchar i;
for(i=0;i<8;i++)
{
sclk=0;
sda=date&0x01;
date<<=1;
sclk=1;
}
}
uchar read_bit() //读一位
{
uchar i,temp;
for(i=0;i<8;i++)
{
temp=temp>>1;
sclk=0;
if(sda)
temp=temp&0x80;
sclk=1;
}
return temp;
}
void write_1302(uchar add,uchar dat) //写进1302地址,数据
{
ce=0;
sclk=1;
ce=1;
write_bit(add);
write_bit(dat);
sclk=0;
ce=0;
}
uchar read_1302(uchar dats) //读1302数据
{
uchar dat;
ce=0;
sclk=0;
ce=1;
write_bit(dats);
dat=read_bit();
sclk=1;
ce=0;
return dat;
}
void write() //对内部进行初始化
{
write_1302(0x8e,0x00);
write_1302(0x80,0x00);
write_1302(0x82,0x43);
write_1302(0x84,0x15);
write_1302(0x86,0x17);
write_1302(0x88,0x07);
write_1302(0x8a,0x01);
write_1302(0x8c,0x11);
write_1302(0x8e,0x80);
}
uchar BCD(uchar bcd) //转换成BCD码
{
uchar num;
num=bcd>>4;
return(num=num*10+(bcd&=0x0F));
}
void dispaly(uchar shi,uchar fen,uchar miao) //显示
{
uchar ad1,ad2,ad3,ad4,ad5,ad6;
ad1=miao/10;
ad2=miao%10;
ad3=fen/10;
ad4=fen%10;
ad5=shi/10;
ad6=shi%10;
P0=0Xff;
P0=table[ad1];
P2=0x01;
delay(1);
P0=0Xff;
P0=table[ad2];
P2=0x02;
delay(1);
P0=0Xff;
P0=table[ad3];
P2=0x04;
delay(1);
P0=0Xff;
P0=table[ad4];
P2=0x08;
delay(1);
P0=0Xff;
P0=table[ad5];
P2=0x10;
delay(1);
P0=0Xff;
P0=table[ad6];
P2=0x20;
delay(1);
}
void init() //中断初始化
{
TMOD=0x01;
TH0=(65535-8000)/256;
TL0=(65535-8000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main() //主函数
{
init();
write();
while(1)
{
miao=BCD(read_1302(0x81));
fen=BCD(read_1302(0x83));
shi=BCD(read_1302(0x85));
}
}
void to() interrupt 1 //中断子函数
{
TH0=(65535-8000)/256;
TL0=(65535-8000)%256;
dispaly(shi,fen,miao);
}
显示不正常
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0xC0,0xF9,0xA4,0xB0,
0x99,0x92,0x82,0xF8,
0x80,0x90};
uchar shi,fen,miao;
sbit ce=P1^2;
sbit sclk=P1^1;
sbit sda=P1^0;
void delay(uint z) //延迟
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_bit(uchar date) //写一位
{
uchar i;
for(i=0;i<8;i++)
{
sclk=0;
sda=date&0x01;
date<<=1;
sclk=1;
}
}
uchar read_bit() //读一位
{
uchar i,temp;
for(i=0;i<8;i++)
{
temp=temp>>1;
sclk=0;
if(sda)
temp=temp&0x80;
sclk=1;
}
return temp;
}
void write_1302(uchar add,uchar dat) //写进1302地址,数据
{
ce=0;
sclk=1;
ce=1;
write_bit(add);
write_bit(dat);
sclk=0;
ce=0;
}
uchar read_1302(uchar dats) //读1302数据
{
uchar dat;
ce=0;
sclk=0;
ce=1;
write_bit(dats);
dat=read_bit();
sclk=1;
ce=0;
return dat;
}
void write() //对内部进行初始化
{
write_1302(0x8e,0x00);
write_1302(0x80,0x00);
write_1302(0x82,0x43);
write_1302(0x84,0x15);
write_1302(0x86,0x17);
write_1302(0x88,0x07);
write_1302(0x8a,0x01);
write_1302(0x8c,0x11);
write_1302(0x8e,0x80);
}
uchar BCD(uchar bcd) //转换成BCD码
{
uchar num;
num=bcd>>4;
return(num=num*10+(bcd&=0x0F));
}
void dispaly(uchar shi,uchar fen,uchar miao) //显示
{
uchar ad1,ad2,ad3,ad4,ad5,ad6;
ad1=miao/10;
ad2=miao%10;
ad3=fen/10;
ad4=fen%10;
ad5=shi/10;
ad6=shi%10;
P0=0Xff;
P0=table[ad1];
P2=0x01;
delay(1);
P0=0Xff;
P0=table[ad2];
P2=0x02;
delay(1);
P0=0Xff;
P0=table[ad3];
P2=0x04;
delay(1);
P0=0Xff;
P0=table[ad4];
P2=0x08;
delay(1);
P0=0Xff;
P0=table[ad5];
P2=0x10;
delay(1);
P0=0Xff;
P0=table[ad6];
P2=0x20;
delay(1);
}
void init() //中断初始化
{
TMOD=0x01;
TH0=(65535-8000)/256;
TL0=(65535-8000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main() //主函数
{
init();
write();
while(1)
{
miao=BCD(read_1302(0x81));
fen=BCD(read_1302(0x83));
shi=BCD(read_1302(0x85));
}
}
void to() interrupt 1 //中断子函数
{
TH0=(65535-8000)/256;
TL0=(65535-8000)%256;
dispaly(shi,fen,miao);
}
显示不正常
自顶一个啊 高手看看啊
学习 学习!
程序是错的