
程序:
#include #include #define uchar unsigned char  #define uint unsigned int sbit sclk=P3^0; sbit dio=P3^1; sbit ce=P3^2; sbit s0=P2^0; sbit s1=P2^1; uchar d[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; uchar a[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; uchar shi,fen,miao; uint x=1; void show(uchar shi,uchar fen,uchar miao); void delay(uint z) {     uint x,y; for(y=z;y>0;y--)         for(x=110;x>0;x--); } void display(uchar shu,uchar wei) {         s0=0;s1=0;     P1=0XFF;     P1=a[wei];     s1=1;     s1=0;     if(x%2==0)//小数点显示判定     {P1=d[shu]&0x7f;      x++;     }     else     {P1=d[shu];      x++;     }     if(x==6) x=0;     delay(2);     s0=0; } void write(uchar add,uchar date) {     uchar temp,t;     ce=0;     sclk=0;     ce=1;     temp=add; for(t=0;t<8;t++)//地址控制     {         if((temp&0x01)==1)         {             dio=1;         }         else             dio=0;         sclk=1;         delay(1);         sclk=0;         temp=temp>>1;     }     temp=date; for(t=8;t>0;t--) //写数据     {         if((temp&0x01)==1)         {             dio=1;         }         else             dio=0;         sclk=1;         delay(1);         sclk=0;         temp=temp>>1;     }     ce=0; } uchar read(uchar add) {     uchar temp,t;     ce=0;     sclk=0;     ce=1;     temp=add; for(t=0;t<8;t++)     {         if((temp&0x01)==1)             dio=1;         else             dio=0;         sclk=1;         sclk=0;         temp>>=1;     }     temp=0; for(t=0;t<7;t++)     {         if(dio==1)             temp=temp|0x80;         else             temp=temp&0x7f;         sclk=1;         sclk=0;         temp>>=1;     }     return temp; } void chu() {     write(0x8e,0x00);//写保护     write(0x80,0x00); //写秒     write(0x82,0x43);//写分     write(0x84,0x15); //写时     write(0x86,0x17);     write(0x88,0x7);     write(0x8c,0x11);     write(0x8e,0x80);         } void main() {         chu();//设定初始时间     while(1)     {              shi=read(0x85);//读出时间         fen=read(0x83);         miao=read(0x81);         show(shi,fen,miao);//显示时间     } }         void show(uchar shi,uchar fen,uchar miao) { display(shi>>4,0);     display(shi&0x0f,1); display(fen>>4,2);     display(fen&0x0f,3); display(miao>>4,4);     display(miao&0x0f,5); }
