初级会员
- 积分
- 64
- 金钱
- 64
- 注册时间
- 2015-3-23
- 在线时间
- 4 小时
|
5金钱
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sck=P3^0;
sbit io=P3^1;
sbit rst=P3^2;
sbit smgen=P3^3;
unsigned char code smg_du[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned char code smg_we[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
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/10;
time_data=time_data%10;
time_data=time_data+j*16;
}
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(time_add,time_data);
}
write_ds1302(0x8e,0x80);
}
void read_rtc(void)
{
uchar i;
for(i=0;i<7;i++)
{
time_data=read_ds1302(read_add);
}
}
void time_pros(void)
{
disp[0]=time_data[6]%16;
disp[1]=time_data[6]/16;
disp[2]=10;
disp[3]=time_data[5]%16;
disp[4]=time_data[5]/16;
disp[5]=10;
disp[6]=time_data[4]%16;
disp[7]=time_data[4]/16;
}
void display(void)
{
uchar i;
for(i=0;i<8;i++)
{
P2=smg_we;
smgen=1;
P1=smg_du[disp];
smgen=0;
delay_50ms(20);
}
}
void main(void)
{
set_rtc();
while(1)
{
read_rtc();
time_pros();
display();
}
}
这是我参考网上自己改的,但是用proteus仿真就是不好用我也不知道出错在哪 |
|