市面上出售的數碼管一般都很小,本人用led發光管自己制作了一種個頭很非常大的數碼管,掛在家里顯示效果非常牛逼下面是實物圖:
下面是c51程序源代碼:
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define pos P0 //設置數碼顯示的位選
#define segs P1 //設置數碼顯示的段選
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段選,共陰數碼管 0-9 a-f - 表
unsigned char code w[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//P1位選,直接使用P1的8個端口進行8位選擇(此演示中未用到)
//ds1302連線設置
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];//進行數據位的拆分處理用
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);//數字的顯示函數;lp為指向數組的地址,lc為顯示的個數
void Write_Ds1302_byte(unsigned char temp); //字節寫入函數
void Write_Ds1302( unsigned char address,unsigned char dat ); //調用字節定入函數在特定的地址寫入特定的數據
unsigned char Read_Ds1302 ( unsigned char address ); //調用指定的地址的數據
void Read_RTC(void);//read RTC //讀取時間日期值到nowtoshow
void Set_RTC(void);//set RTC //設定時間日期值,這里用于賦初值
void keydelay(unsigned char t);////鍵盤消抖延時
delay();
void main()
{
unsigned int k=0; //使k在0到4000之間循環,不同的時間段顯示不同的內容,以較多的時間顯示時間,以很少的時間顯示日期和星期
//Set_RTC(); //設初值,第一次使用,以后就可以注釋掉,以免再次調校時間,當然必須給1302接上2~5.5V的備用電源或電池
Read_RTC();
nowtoshow[0]=now[2]/16; //數據的轉換,因我們采用數碼管0~9的顯示,將數據分開
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) //循環1000次后進行一次數據更新
{
Read_RTC();
if (flash==0) //象征性的閃秒,并不準確,也沒有必要準確
{ flash=1;}
else
{flash=0;}
nowtoshow[0]=now[2]/16; //數據的轉換,因我們采用數碼管0~9的顯示,將數據分開
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; //數據的轉換,因我們采用數碼管0~9的顯示,將數據分開
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; //數據的轉換,因我們采用數碼管0~9的顯示,將數據分開
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++) //循環8次 寫入數據
{
SCL2=0;
SDA2=temp&0x01; //每次傳輸低字節
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); //發送地址
Write_Ds1302_Byte(dat); //發送數據
RST=0; //恢復
}
/****************************************************************************/
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++) //循環8次 讀取數據
{
if(SDA2)
temp|=0x80; //每次傳輸低字節
SCL2=0;
temp>>=1; //右移一位
SCL2=1;
}
RST=0;
_nop_(); //以下為DS1302復位的穩定時間
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) //設定 日歷
{
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++);
}