#include
#include
#define DIG P2
#define uint unsigned int
#define uchar unsigned char
***it RST=P0^0;
***it CLK=P0^2;
***it IO=P0^1; //1302 Èý¸ö¿Ú
***it SEC1=P3^4;
***it SEC2=P3^3;
***it DU1=P3^5;
***it MIN1=P3^7;
***it MIN2=P3^6;
***it DU2=P4^1;
***it HOU1=P4^4;
***it HOU2=P4^2; //ÊýÂë¹Ü¶ÔÓ¦Òý½Å
uchar code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //¶ÁÈ¡µØÖ·
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //дµØÖ·
uchar tiME[7] = {0x49, 0x59, 0x23, 0x07, 0x05, 0x06, 0x16}; //³õʼֵ
uchar LED[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //ÊýÂë¹ÜÖÃÁã
uint i = 0;
void Ds1302Write(unsigned char addr, unsigned char dat);
void Ds1302Init();
void Ds1302ReadTime();
void datapros();
void display();
void main()
{
Ds1302Init();
while(1)
{
datapros();
display();
}
}
void datapros()
{
Ds1302ReadTime();
LED[0]=DIG_CODE[TIME[0]/16];
LED[1]=DIG_CODE[TIME[0]&0x0f];
LED[3]=DIG_CODE[TIME[1]/16];
LED[4]=DIG_CODE[TIME[1]&0x0f];
LED[6]=DIG_CODE[TIME[2]/16];
LED[7]=DIG_CODE[TIME[2]&0x0f];
}
void display()
{
DIG=0x00;
if (i==0)
{
SEC1=0;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[0];
}
else if(i==1)
{
SEC1=1;SEC2=0;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[1];
}
else if(i==2)
{
SEC1=1;SEC2=1;DU1=0;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=0x40;
}
else if(i==3)
{
SEC1=1;SEC2=1;DU1=1;MIN1=0;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[3];
}
else if(i==4)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=0;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[4];
}
else if(i==5)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=0;HOU1=1;HOU2=1;
i++;
DIG=0x40;
}
else if(i==6)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=0;HOU2=1;
i++;
DIG=LED[6];
}
else if(i==7)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=0;
i=0;
DIG=LED[7];
}
}
void Ds1302Write(unsigned char addr, unsigned char dat)
{
unsigned char n;
RST = 0;
_nop_();
CLK = 0; //½«SCLKÖõ͵çƽ¡£
_nop_();
RST = 1; //½«RSTÖøߵçƽ¡£
_nop_();
for (n=0; n<8; n++) //¿ªÊ¼´«ËÍ°ËλµØÖ·ÃüÁî
{
IO = addr & 0x01; //Êý¾Ý´ÓµÍλ¿ªÊ¼´«ËÍ
addr >>= 1;
CLK = 1; //Êý¾ÝÔÚÉÏÉýÑØʱ£¬DS1302¶ÁÈ¡Êý¾Ý
_nop_();
CLK = 0;
_nop_();
}
for (n=0; n<8; n++) //дÈë8λÊý¾Ý
{
IO = dat & 0x01;
dat >>= 1;
CLK = 1; //Êý¾ÝÔÚÉÏÉýÑÓʱ£¬DS1302¶ÁÈ¡Êý¾Ý
_nop_();
CLK = 0;
_nop_();
}
RST = 0; //½áÊø´«ËÍÊý¾Ý
_nop_();
}
unsigned char Ds1302Read(unsigned char addr)
{
unsigned char n,dat,dat1;
RST = 0;
_nop_();
CLK = 0; //½«SCLKÖõ͵çƽ¡£
_nop_();
RST = 1; //½«RSTÖøߵçƽ¡£
_nop_();
for(n=0; n<8; n++) //¿ªÊ¼´«Ë͵ØÖ·ÃüÁî
{
IO = addr & 0x01; //Êý¾Ý´«ËÍ
addr >>= 1;
CLK = 1; //Êý¾ÝÔÚÉÏÉýÑØʱ£¬DS1302¶ÁÈ¡Êý¾Ý
_nop_();
CLK = 0; //¼Ç¼Êý¾Ý
_nop_();
}
_nop_();
for(n=0; n<8; n++) //¶ÁÈ¡Êý¾Ý
{
dat1 = IO; //´Ó×îµÍλ¿ªÊ¼½ÓÊÕ
dat = (dat>>1) | (dat1<<7);
CLK = 1;
_nop_();
CLK = 0; //дÊý¾Ý
_nop_();
}
RST = 0;
_nop_(); //DS1302¸´Î»µÄÎȶ¨Ê±¼ä
CLK = 1;
_nop_();
IO = 0;
_nop_();
IO = 1;
_nop_();
return dat;
}
void Ds1302Init()
{
unsigned char i;
Ds1302Write(0x8E,0X00); //È¥³ýд±£»¤
for (i=0; i<7; i++) //дÈëʱÖÓÐźţº·ÖÃëʱÈÕÔÂÖÜÄê
{
Ds1302Write(WRITE_RTC_ADDR[i],TIME[i]);
}
Ds1302Write(0x8E,0x80); //´ò¿ªÐ´±£»¤
}
void Ds1302ReadTime()
{
unsigned char i;
for (i=0; i<7; i++) //¶ÁÈ¡7¸ö×Ö½ÚµÄʱÖÓÐźţº·ÖÃëʱÈÕÔÂÖÜÄê
{
TIME[i] = Ds1302Read(READ_RTC_ADDR[i]);
}
}
|