📄 ds1302_2.c
字号:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
sbit P2_4=P2^4;
sbit P2_7=P2^7;
sbit DQ = P3^4;
sbit P1_1=P1^1;
sbit P1_4=P1^4;
sbit ab=P0^3;
sbit clk=P0^4;
sbit a=P0^0;
sbit b=P0^1;
sbit c=P0^2;
bit pin1;
bit pin2;
bit pin3;
bit pin4;
bit pin5;
sbit RST=P0^5;
sbit IO=P0^6;
sbit CLK=P0^7;
//sbit KY=P3^3;
void sendd(uchar c);
void dely5ms();
uchar count1=5;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
uchar disbuf[4]={1,2,3,4};
uchar tbuf[]={0x00,0x00,0x10,0x6,0x11,6,0x4};
void disp()
{
uchar temp;
temp=table[disbuf[0]];
if(pin1) temp=temp|0x80;
sendd(temp);
a=b=c=0;
dely5ms();
a=b=c=1;
temp=table[disbuf[1]];
if(pin2) temp=temp|0x80;
sendd(temp);
a=1;
b=0;
c=0;
dely5ms();
a=b=c=1;
temp=table[disbuf[2]];
if(pin3) temp=temp|0x80;
sendd(temp);
a=0;
b=1;
c=0;
dely5ms();
a=b=c=1;
temp=table[disbuf[3]];
if(pin4) temp=temp|0x80;
sendd(temp);
a=1;
b=1;
c=0;
dely5ms();
a=b=c=1;
temp=table[disbuf[4]];
if(pin5) temp=temp|0x80;
sendd(temp);
a=0;
b=0;
c=1;
dely5ms();
P0=P0|0x07;
}
void sendd(uchar c)
{
uchar i;
for(i=0;i<8;i++)
{
if((c<<i)&0x80) ab=1;
else ab=0;
clk=1;
clk=0;
};
}
void dely5ms()
{
unsigned int i,j,count=5;
for (i=0;i<count;i++)
for (j=0;j<10;j++);
}
/////////////////////////////////////////////////////////////
void write(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
IO=0;
if(dat&0x1)
IO=1; _nop_();
CLK=1;
_nop_(); _nop_();
CLK=0; _nop_();
dat=dat>>1;
}
}
uchar read()
{
uchar i,rbyte;
rbyte=0;
for(i=0;i<8;i++)
{
rbyte=rbyte>>1;
if(IO)
rbyte=rbyte|0x80;
CLK=1;
_nop_(); _nop_();
CLK=0;
}
return rbyte;
}
void rcwrite(uchar add,uchar da)
{
RST=0;
CLK=0;
RST=1;
write(add); _nop_(); _nop_();
write(da);
RST=0;
CLK=1;
}
uchar rcread(uchar add)
{
uchar val;
RST=0;
CLK=0;
RST=1;
write(add); _nop_(); _nop_(); _nop_();
val=read();
RST=0;
CLK=1;
return val;
}
void set_time(char data *pt)
{
uchar i,add;
add=0x80;
rcwrite(0x8e,0);
rcwrite(0x90,0);
for(i=0;i<7;i++)
{
rcwrite(add,*pt++);
add+=2;
}
//rcwrite(0x8e,0x80);
}
void get_time()
{
uchar i,address;
address=0x81;
for(i=0;i<7;i++)
{
tbuf[i]=rcread(address);
address+=2;
}
}
main()
{
P1_4=0;
RST=0;
CLK=0;
RST=1;
set_time(tbuf);
while(1)
{ _nop_(); _nop_();
get_time();
disbuf[4]=tbuf[0]&0x0f;
disbuf[3]=tbuf[1]&0x0f;
disbuf[2]=(tbuf[1]&0xf0)>>4;
disbuf[1]=tbuf[2]&0x0f;
disbuf[0]=(tbuf[2]&0xf0)>>4;
disp();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -