專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計實例 >> 瀏覽文章

自制51單片機(jī)超大數(shù)碼管時鐘

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2012年04月03日   【字體:

市面上出售的數(shù)碼管一般都很小,本人用led發(fā)光管自己制作了一種個頭很非常大的數(shù)碼管,掛在家里顯示效果非常牛逼下面是實物圖:
   
 

        

下面是電路圖:
 

下面是c51程序源代碼:

#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define pos P0 //設(shè)置數(shù)碼顯示的位選
#define segs P1 //設(shè)置數(shù)碼顯示的段選

code unsigned char d[]=
    {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0};
// 0    1    2 3    4    5    6   7   8     9   a     b   c    d     e   f    -    熄滅
    //P0段選,共陰數(shù)碼管 0-9 a-f - 表

unsigned char code w[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
    //P1位選,直接使用P1的8個端口進(jìn)行8位選擇(此演示中未用到)

//ds1302連線設(shè)置
sbit SCL2=P2^0;   //SCL2定義為P2口的第2位腳,連接DS1302SCL
sbit SDA2=P2^1;   //SDA2定義為P2口的第1位腳,連接DS1302SCL
sbit RST = P2^2;   // DS1302片選腳

unsigned char now[8]= {0,   19, 9, 17, 1, 7 ,   10, 0};
                      // 秒 分 時   日 月   星期 年 寫入禁止(0允許寫入,1禁止寫入)
unsigned char nowtoshow[8];//進(jìn)行數(shù)據(jù)位的拆分處理用   
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //保存寫入寄存器位置
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //保存讀取寄存器位置

unsigned char flash=0; //閃秒
void display(unsigned char *lp);//數(shù)字的顯示函數(shù);lp為指向數(shù)組的地址,lc為顯示的個數(shù)
          
void Write_Ds1302_byte(unsigned char temp); //字節(jié)寫入函數(shù)
void Write_Ds1302( unsigned char address,unsigned char dat );    //調(diào)用字節(jié)定入函數(shù)在特定的地址寫入特定的數(shù)據(jù)
unsigned char Read_Ds1302 ( unsigned char address );    //調(diào)用指定的地址的數(shù)據(jù)

void Read_RTC(void);//read RTC     //讀取時間日期值到nowtoshow
void Set_RTC(void);//set RTC    //設(shè)定時間日期值,這里用于賦初值
void keydelay(unsigned char t);////鍵盤消抖延時
delay();


void main()
{
unsigned int k=0;    //使k在0到4000之間循環(huán),不同的時間段顯示不同的內(nèi)容,以較多的時間顯示時間,以很少的時間顯示日期和星期

//Set_RTC();    //設(shè)初值,第一次使用,以后就可以注釋掉,以免再次調(diào)校時間,當(dāng)然必須給1302接上2~5.5V的備用電源或電池

Read_RTC();
      nowtoshow[0]=now[2]/16;    //數(shù)據(jù)的轉(zhuǎn)換,因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
      nowtoshow[1]=now[2]&0x0f;
   nowtoshow[2]=now[1]/16;
   nowtoshow[3]=now[1]&0x0f;
display(nowtoshow);

while(1)
{
if (k<2000)   //顯示時間
{
   if (k%100==0)    //循環(huán)1000次后進(jìn)行一次數(shù)據(jù)更新
   {
    Read_RTC();
     if (flash==0)      //象征性的閃秒,并不準(zhǔn)確,也沒有必要準(zhǔn)確
     { flash=1;}
        else
     {flash=0;}
      nowtoshow[0]=now[2]/16;    //數(shù)據(jù)的轉(zhuǎn)換,因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
     if (nowtoshow[0]==0)nowtoshow[0]=17; //如果是0則不顯示
      nowtoshow[1]=now[2]&0x0f;
   nowtoshow[2]=now[1]/16;
   nowtoshow[3]=now[1]&0x0f;
   }
   }
   if (k>2000&&k<2500)   //顯示日期
   {
   flash=17;
      nowtoshow[0]=now[4]/16;    //數(shù)據(jù)的轉(zhuǎn)換,因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
        if (nowtoshow[0]==0)nowtoshow[0]=17; //如果是0則不顯示
      nowtoshow[1]=now[4]&0x0f;
   nowtoshow[2]=now[3]/16;
        if (nowtoshow[2]==0)nowtoshow[2]=17; //如果是0則不顯示

   nowtoshow[3]=now[3]&0x0f;

  
   }
   if (k>2500)      //顯示星期
   {
     flash=17;
      nowtoshow[0]=17;    //數(shù)據(jù)的轉(zhuǎn)換,因我們采用數(shù)碼管0~9的顯示,將數(shù)據(jù)分開
      nowtoshow[1]=17;
      nowtoshow[2]=17;
      nowtoshow[3]=now[5];
   }
k++;
if (k==3000)k=0;
display(nowtoshow);
   }
}

delay()
{
int j;
for (j=0;j<100;j++);
}

void display(unsigned char *lp)//顯示
{
uint k;
for (k=0;k<4;k++)
{
pos=w[k];
segs=d[lp[k]];
delay();
segs=d[17];
}

pos=w[4];
segs=d[flash];   //秒閃爍燈
delay();
segs=d[17];

}

void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)     //循環(huán)8次 寫入數(shù)據(jù)
{
   SCL2=0;
     SDA2=temp&0x01;     //每次傳輸?shù)妥止?jié)
     temp>>=1;    //右移一位
     SCL2=1;
   }
}

/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )    
{
   RST=0;
_nop_();
   SCL2=0;
_nop_();
   RST=1;
   _nop_();   //啟動
   Write_Ds1302_Byte(address); //發(fā)送地址
   Write_Ds1302_Byte(dat);   //發(fā)送數(shù)據(jù)
   RST=0;    //恢復(fù)
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
   unsigned char i,temp=0x00;
   RST=0;
_nop_();
   SCL2=0;
_nop_();
   RST=1;
_nop_();
   Write_Ds1302_Byte(address);
   for (i=0;i<8;i++)    //循環(huán)8次 讀取數(shù)據(jù)
   {
    if(SDA2)
    temp|=0x80;    //每次傳輸?shù)妥止?jié)
   SCL2=0;
   temp>>=1;    //右移一位
    SCL2=1;
}
   RST=0;
_nop_();   //以下為DS1302復(fù)位的穩(wěn)定時間
   RST=0;
SCL2=0;
_nop_();
SCL2=1;
_nop_();
SDA2=0;
_nop_();
SDA2=1;
_nop_();
return (temp);    //返回
}
/****************************************************************************/
void Read_RTC(void)   //讀取 日歷
{
unsigned char i,*p;
p=read_rtc_address; //地址傳遞
for(i=0;i<7;i++)   //分7次讀取 年月日時分秒星期
{
now[i]=Read_Ds1302(*p);
p++;
}
}
/***********************************************************************/
void Set_RTC(void)   //設(shè)定 日歷
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++){
   tmp=now[i]/10;
   now[i]=now[i]%10;
   now[i]=now[i]+tmp*16;
}
   Write_Ds1302(0x8E,0X00);

   p=write_rtc_address; //傳地址
   for(i=0;i<7;i++)   //7次寫入 年月日時分秒星期
   {
    Write_Ds1302(*p,now[i]);
    p++;
}
Write_Ds1302(0x8E,0x80);
}

void keydelay(unsigned char t)
{
unsigned char i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++);
}

關(guān)閉窗口

相關(guān)文章