找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 6650|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

51單片機(jī)數(shù)碼管顯示萬(wàn)年歷proteus仿真

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
51單片機(jī)數(shù)碼管顯示萬(wàn)年歷proteus仿真原理圖如下:



源程序:
  1. #define uchar unsigned char
  2. #define uint  unsigned int
  3. #define DQ P3_7
  4. #include<AT89X51.H>

  5. uint sec;
  6. uint min=41;
  7. uint hour=18;
  8. uint day=14;
  9. uint month=9;
  10. uint yearl=6;
  11. uint yearh=20;
  12. uint tcnt;
  13. uint cursor=0;
  14. uchar a=0xff;
  15. uchar code Seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

  16. void delay(uint t)
  17. {
  18.     uint i;
  19.     while(t--)
  20.     {for (i=0;i<125;i++);}
  21. }
  22. void Tdelay(unsigned int i)
  23. {
  24. while(i--);
  25. }
  26. void Kdelay()
  27. {
  28.     uchar i,j;
  29.     for(i=100;i>0;i--)
  30.     for(j=248;j>0;j--);
  31. }
  32. Init_DS18B20(void)
  33. {
  34. unsigned char x=0;
  35. DQ = 1;   
  36. Tdelay(8);  
  37. DQ = 0;   
  38. Tdelay(80);
  39. DQ = 1;   
  40. Tdelay(14);
  41. Tdelay(20);
  42. }
  43. //讀一個(gè)字節(jié)
  44. ReadOneChar(void)
  45. {
  46. unsigned char i=0;
  47. unsigned char dat = 0;
  48. for (i=8;i>0;i--)
  49. {
  50.   DQ = 0;
  51.   dat>>=1;
  52.   DQ = 1;
  53.   if(DQ)
  54.    dat|=0x80;
  55.   Tdelay(4);
  56. }
  57. return(dat);
  58. }

  59. //寫(xiě)一個(gè)字節(jié)
  60. WriteOneChar(unsigned char dat)
  61. {
  62. unsigned char i=0;
  63. for (i=8; i>0; i--)
  64. {
  65.   DQ = 0;
  66.   DQ = dat&0x01;
  67.   Tdelay(5);
  68.   DQ = 1;
  69.   dat>>=1;
  70. }
  71. }

  72. //讀取溫度
  73. ReadTemperature(void)
  74. {
  75. unsigned char a=0;
  76. unsigned char b=0;
  77. unsigned int t=0;
  78. float tt=0;
  79. Init_DS18B20();
  80. WriteOneChar(0xCC);
  81. WriteOneChar(0x44);
  82. Init_DS18B20();
  83. WriteOneChar(0xCC);
  84. WriteOneChar(0xBE);
  85. a=ReadOneChar();
  86. b=ReadOneChar();
  87. t=b;
  88. t<<=8;
  89. t=t|a;
  90. tt=t*0.0625;        
  91. t= tt*10+0.5;      
  92. return(t);
  93. }

  94. void display(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7,uchar L8,uchar L9,uchar L10,uchar L11,uchar L12,uchar L13,uchar L14,uchar L15,uchar L16)
  95. {
  96.     P2=0x7F;P0=L1;delay(1);  //yearh
  97.     P2=0xBF;P0=L2;delay(1);  //yearh
  98.     if(cursor==6){P2=0xDF|a;P0=L3;delay(1);}else{P2=0xDF;P0=L3;delay(1);}   //yearl
  99.     if(cursor==6){P2=0xEF|a;P0=L4;delay(1);}else{P2=0xEF;P0=L4;delay(1);}   //yearl
  100.     if(cursor==5){P2=0xF7|a;P0=L5;delay(1);}else{P2=0xF7;P0=L5;delay(1);}   //month
  101.     if(cursor==5){P2=0xFB|a;P0=L6;delay(1);}else{P2=0xFB;P0=L6;delay(1);}   //month
  102.     if(cursor==4){P2=0xFD|a;P0=L7;delay(1);}else{P2=0xFD;P0=L7;delay(1);}   //day
  103.     if(cursor==4){P2=0xFE|a;P0=L8;delay(1);}else{P2=0xFE;P0=L8;delay(1);}   //day
  104.     P2=0xFF;
  105.     if(cursor==3){P1=0x7F|a;P0=L9;delay(1);}else{P1=0x7F;P0=L9;delay(1);}   //hour
  106.     if(cursor==3){P1=0xBF|a;P0=L10;delay(1);}else{P1=0xBF;P0=L10;delay(1);}  //hour
  107.     if(cursor==2){P1=0xDF|a;P0=L11;delay(1);}else{P1=0xDF;P0=L11;delay(1);} //min
  108.     if(cursor==2){P1=0xEF|a;P0=L12;delay(1);}else{P1=0xEF;P0=L12;delay(1);} //min
  109.     if(cursor==1){P1=0xF7|a;P0=L13;delay(1);}else{P1=0xF7;P0=L13;delay(1);} //sec
  110.     if(cursor==1){P1=0xFB|a;P0=L14;delay(1);}else{P1=0xFB;P0=L14;delay(1);} //sec
  111.     P1=0xFD;P0=L15;delay(1); //temp
  112.     P1=0xFE;P0=L16;delay(1); //temp
  113.     P1=0xFF;
  114. }

  115. main()
  116. {   
  117.     uint i;   
  118.     TMOD=0x02;            //設(shè)置模式為定時(shí)器T0的模式2 (8位自動(dòng)重裝計(jì)數(shù)初值的計(jì)數(shù)值)        
  119.     TH0=0x06;             //設(shè)置計(jì)數(shù)器初值,靠TH0存儲(chǔ)重裝的計(jì)數(shù)值X0=256-250=6
  120.     TL0=0x06;
  121.     TR0=1;                //啟動(dòng)T0
  122.     ET0=1;                //開(kāi)啟定時(shí)器T0中斷允許
  123.     EA=1;                 //開(kāi)啟中斷總控制
  124.     while(1)
  125.     {
  126.         if(P3_0==0)
  127.         {
  128.             Kdelay();
  129.             if(P3_0==0)
  130.             {
  131.                 cursor++;
  132.                 if(cursor>=7){cursor=0;}
  133.             }
  134.         }
  135.         if(P3_1==0)
  136.         {
  137.             Kdelay();
  138.             if(P3_1==0)
  139.             {
  140.                 if(cursor==1){sec++;if(sec==60)sec=0;}
  141.                 if(cursor==2){min++;if(min==60)min=0;}
  142.                 if(cursor==3){hour++;if(hour==24)hour=0;}
  143.                 if(cursor==4){day++;if(day==31)day=0;}
  144.                 if(cursor==5){month++;if(month==12)month=0;}
  145.                 if(cursor==6){yearl++;if(yearl==100)yearl=0;}
  146.                 if(cursor==7){yearh++;if(yearh==30)yearh=20;}
  147.             }
  148.         }
  149.         if(P3_2==0)
  150.         {
  151.             Kdelay();
  152.             if(P3_2==0)
  153.             {
  154.                 if(cursor==1){sec--;}
  155.                 if(cursor==2){min--;}
  156.                 if(cursor==3){hour--;}
  157.                 if(cursor==4){day--;}
  158.                 if(cursor==5){month--;}
  159.                 if(cursor==6){yearl--;}
  160.                 if(cursor==7){yearh--;}
  161.             }
  162.         }
  163.         i=ReadTemperature();
  164.         display(Seg[yearh/10],Seg[yearh%10],Seg[yearl/10],Seg[yearl%10],Seg[month/10],Seg[month%10],Seg[day/10],Seg[day%10],Seg[hour/10],Seg[hour%10],Seg[min/10],Seg[min%10],Seg[sec/10],Seg[sec%10],Seg[i/100],Seg[i/10%10]);     
  165.     }
  166. }

  167. void t0(void)interrupt 1 using 0   //t0的中斷程序
  168. {
  169.     tcnt++;
  170.     if(tcnt==4000)//定時(shí)器的定時(shí)計(jì)數(shù),4000次250us為1秒
  171.     {
  172.         tcnt=0;
  173.         P3_3=~P3_3;
  174.         a=~a;
  175.         sec++;
  176.         if(sec==60)
  177.         {     
  178.             sec=0;
  179.             min++;
  180.             if(min==60)
  181.             {
  182.                 min=0;
  183.                 hour++;
  184.                 if(hour==24)
  185.                 {
  186.                     hour=0;
  187.                     day++;
  188.                     if(month==2&&((yearl==0&&yearh%4==0)||(yearl!=0&&yearl%4==0))&&day==30)day=1;  
  189.                     else if(month==2&&day==29)day=1;
  190.                     else if((month==4||month==6||month==9||month==11)&&day==31)day=1;
  191.                     else if(day==32)day=1;
  192.                     if(day==1)
  193.                     {
  194.                     month++;
  195.                     if(month==13)
  196.                     {
  197.                         month=1;
  198.                         yearl++;
  199.                         if(yearl==100)
  200.                         {
  201.                             yearl=0;
  202.                             yearh++;
  203.                             if(yearh==100)
  204.                             {
  205.                                 yearh=20;
  206.                             }
  207.                         }
  208.                     }
  209.                 }
  210.                 }
  211.             }
  212.         }
  213.     }
  214. }
復(fù)制代碼


下載:
51單片機(jī)數(shù)碼管顯示萬(wàn)年歷proteus仿真.zip (883.16 KB, 下載次數(shù): 96)

評(píng)分

參與人數(shù) 1黑幣 +5 收起 理由
luo22 + 5 贊一個(gè)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:194844 發(fā)表于 2017-5-1 23:30 | 只看該作者
very good
回復(fù)

使用道具 舉報(bào)

板凳
ID:256877 發(fā)表于 2017-12-3 21:38 | 只看該作者
感覺(jué)這個(gè)挺好的就是沒(méi)法下,
回復(fù)

使用道具 舉報(bào)

地板
ID:256877 發(fā)表于 2017-12-3 21:39 | 只看該作者
我想知道那個(gè)51下面的那個(gè)元件是什么有沒(méi)有人能告訴我
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表