#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rest=P3^5;//ds1302-5
sbit dates=P3^6;//ds1302-6
sbit sclk=P3^7;//ds1302-7
sbit acc0=ACC^0;
sbit acc7=ACC^7;
//595端口
sbit SD=P2^2;
sbit SCK=P2^1;
sbit RCK=P2^3;
uchar nian,yue,ri,shi,fen,miao,xingqi;
uint day1;
bit b1,b2,b3,b4,b5,
time_bz,power_bz;
uchar disp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//595串入并出
void WR1_595(uchar num1)
{
uchar date;
uchar b;
date=num1;
for(b=0;b<=7;b++)
{
if((date&0x80)==0x80) //最高位为1,则向SDATA_595发送1
SD=1; //发出数据的最高位
else
SD=0;
date<<=1; //左移位
SCK=0;
_nop_();
_nop_();
SCK=1; //产生上升沿
}
}
/*写操作*/
void delay()
{
uchar n;
for(n=0;n<10;n++);
}
void write(uchar da)
{
uchar i;
ACC=da;
for(i=8;i>0;i--)
{
dates=acc0;
sclk=0;
sclk=1;
ACC>>=1;
}
}
/*读操作*/
uchar read()
{
uchar i;
for(i=0;i<8;i++)
{
ACC>>=1;
sclk=1;
sclk=0;
acc7=dates;
}
return(ACC);
}
/*写数据*/
void write_time(uchar address,uchar date)
{
rest=0;
sclk=0;
rest=1;
write(address);
write(date);
rest=0;
sclk=1;
}
/*读数据*/
uchar read_time(uchar address)
{
uchar temp;
rest=0;
sclk=0;
rest=1;
write(address);
temp=read();
rest=0;
sclk=1;
dates=0; //必须加上
return (temp);
}
/*=======================================*/
void write_tt1(uchar address,uchar time)//写时间
{
uchar temp1,temp2;
temp1=time/10;
temp2=time%10;
time=(temp1<<4)|temp2;
write_time(address,time);
}
/*==============================*/
uchar read_tt1(uchar addr)
{
uchar temp;
temp=read_time(addr);
temp=temp/16*10+temp%16;
return temp;
}
void write_tt()
{
write_time(0x8e,0x00); //WP=0 写操作
write_tt1(0x80,0x01); //0x80是写秒数据
write_tt1(0x82,0x01); //0x82是写分数据
write_tt1(0x84,0x01); //0x82是写时数据
write_tt1(0x86,0x01); //0x82是写日数据
write_tt1(0x88,0x01); //0x82是写月数据
write_tt1(0x8a,0x01); //0x8a是写星期数据
write_tt1(0x8c,0x01); //0x82是写年数据
write_time(0x8e,0x80); //WP=1 写保护
}
void read_tt() //读时间
{
fen=read_tt1(0x83);
shi=read_tt1(0x85);
ri=read_tt1(0x87);
yue=read_tt1(0x89);
nian=read_tt1(0x8d);
xingqi=read_tt1(0x8b);
miao=read_tt1(0x81);
if(miao>59)
{
if((miao&0x80)&&(miao&0x7f)<60)
{
write_time(0x8e,0x00); //WP=0 写操作
write_tt1(0x80,miao&0x7f);//0x80是写秒数据
write_time(0x8e,0x80); //WP=1 写保护
}
else
{
if(fen<59)
fen+=1;
write_time(0x8e,0x00); //WP=0 写操作
write_tt1(0x80,0x00);//0x80是写秒数据
write_tt1(0x82,fen);//0x80是写分数据
write_time(0x8e,0x80); //WP=1 写保护
}
}
if(shi==0&&fen==0&&miao==0&&time_bz==0)
{
time_bz=1;
day1=day1+1;
}
if(shi!=0&&fen!=0&&miao!=0)
time_bz=0;
}
void display()
{
RCK=0;
WR1_595(disp_code[2]);
WR1_595(disp_code[0]);
//WR1_595(disp_code[nian]);
WR1_595(disp_code[nian]);
//WR1_595(disp_code[yue]);
WR1_595(disp_code[yue]);
//WR1_595(disp_code[ri]);
WR1_595(disp_code[ri]);
//WR1_595(disp_code[shi]);
WR1_595(disp_code[shi]);
//WR1_595(disp_code[fen]);
WR1_595(disp_code[fen]);
WR1_595(disp_code[miao]);
RCK=1;
}
void main()
{
while(1)
{
write_tt();
read_tt();
display();
delay();
}
}
哦哦怎么就不行呢
一周热门 更多>