|
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar data_7seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};
uchar hour,min,sec;
sbit shi=P1^0;
sbit fen=P1^1;
sbit miao=P1^2;
sbit rst=P1^4;
sbit sck=P1^5;
sbit io=P1^6;
sbit fm=P1^7;
/*函数声明:*/
void write_ds1302_byte(uchar dat);
void write_ds1302(uchar add,uchar dat);
uchar read_ds1302(uchar add);
void read_rtc();
void set_rtc();
void display();
void delay(int n);
void show();
void fmzz();
/*DS1302单字节写入:*/
void write_ds1302_byte(uchar dat)
{
uchar i;
for (i=0;i<8;i++)
{
sck=0;
io=dat&0x01;
dat=dat>>1;
sck=1;
}
}
/*DS1302多字节写入:*/
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;
}
/*DS1302读取:*/
uchar read_ds1302(uchar add)? ??? ??? ??? ??? ??? ??? ??? ?
{
uchar i,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()
{
if (shi==0) delay(100);
if (shi==0)
{
hour=(hour>>4)*10+(hour&0x0f);
hour++;
if (hour==24) hour=0;
hour=((hour/10)<<4)+(hour%10);
write_ds1302(0x84,hour);
}
if (fen==0) delay(100);
if (fen==0)
{
min=(min>>4)*10+(min&0x0f);
min++;
if (min==60) min=0;
min=((min/10)<<4)+(min%10);
write_ds1302(0x82,min);
}
if (miao==0) delay(100);
if (miao==0)
{
sec=(sec>>4)*10+(sec&0x0f);
sec++;
if (sec==60) sec=0;
sec=((sec/10)<<4)+(sec%10);
write_ds1302(0x80,sec);
}
}
/*读取时间:*/
void read_rtc()
{
hour=read_ds1302(0x85);
min=read_ds1302(0x83);
sec=read_ds1302(0x81);
}
/*显示时间:*/
void display()
{
unsigned int a=1;
P2=0x01;P0=data_7seg[hour/16];delay(a);
P2=0x02;P0=data_7seg[hour%16];delay(a);
P2=0x04;P0=data_7seg[min/16];delay(a);
P2=0x08;P0=data_7seg[min%16];delay(a);
P2=0x10;P0=data_7seg[sec/16];delay(a);
P2=0x20;P0=data_7seg[sec%16];delay(a);??? ??? ??? ?
}
/*蜂鸣:*/
void fmzz()
{
uint i;
for (i=0;i<100;i++)
{
fm=!fm;
delay(2);
}
}
/*延时程序:*/
void delay(int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<121;j++)
{;}
}
}
/*显示学号:*/
void show()
{
unsigned int m;
for(m=0;m<20;m++)
{
uint a=10;
P2=0x01;P0=data_7seg[7];delay(a);
P2=0x02;P0=data_7seg[2];delay(a);
P2=0x04;P0=data_7seg[4];delay(a);
P2=0x08;P0=data_7seg[1];delay(a);
P2=0x10;P0=data_7seg[1];delay(a);
P2=0x20;P0=data_7seg[8];delay(a);
}
} /*主程序:*/
void main()
{
show();
write_ds1302(0x84,0x12);? ? //初始化
write_ds1302(0x82,0x00);
write_ds1302(0x80,0x00);
while (1)
{ set_rtc();
read_rtc();
display();
if(min==0)
{ if(sec==0)
{fmzz();}
}
}
}
|
|