#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sck=P1^0;
sbit io=P1^1;
sbit rst=P3^6;
sbit smgen=P2^3;
unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x10};
unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78};
uchar
time_data[7]={10,6,4,17,11,58,30};
uchar time_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar disp[8];
void write_ds1302_byte(uchar dat);
void write_ds1302(uchar add,uchar dat);
uchar read_ds1302(uchar add);
void set_rtc(void);
void read_rtc(void);
void time_pros(void);
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6245;j>0;j--);
}
void write_ds1302_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
sck=0;
io=dat&0x01;
dat=dat>>1;
sck=1;
}
}
void write_ds1302(uchar add,uchar dat)
{
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
rst=0;
_nop_();
io=1;
sck=1;
}
uchar read_ds1302(uchar add)
{
uchar i; uchar value;
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
value=value>>1;
sck=0;
if(io)
value=value|0x80;
sck=1;
}
rst=0;
_nop_();
sck=0;
_nop_();
sck=1;
io=1;
return value;
}
void set_rtc(void)
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_data[i]/10;
time_data[i]=time_data[i]%10;
time_data[i]=time_data[i]+j*16;
}
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(time_add[i],time_data[i]);
}
write_ds1302(0x8e,0x80);
}
void read_rtc(void)
{
uchar i;
for(i=0;i<7;i++)
{
time_data[i]=read_ds1302(read_add[i]);
}
}
void time_pros(void)
{
disp[0]=time_data[6]%16;
disp[1]=time_data[6]/16;
disp[2]=17;
disp[3]=time_data[5]%16;
disp[4]=time_data[5]/16;
disp[5]=17;
disp[6]=time_data[4]%16;
disp[7]=time_data[4]/16;
}
void display(void)
{
uchar i;
for(i=0;i<8;i++)
{
P1=smg_du[disp[i]];
P2=smg_we[i];
delay_50ms(20);
}
}
void main(void)
{
set_rtc();
while(1)
{
smgen=0;
read_rtc();
smgen=1;
time_pros();
display();
}
}
-
-
-
ds1302数码管.rar
下载积分: 积分 -1 分
62.59 KB, 下载次数: 13, 下载积分: 积分 -1 分
一周热门 更多>