找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6057|回复: 3
打印 上一主题 下一主题
收起左侧

51单片机数码管显示万年历proteus仿真

[复制链接]
跳转到指定楼层
楼主
51单片机数码管显示万年历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. //读一个字节
  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. //写一个字节
  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;            //设置模式为定时器T0的模式2 (8位自动重装计数初值的计数值)        
  119.     TH0=0x06;             //设置计数器初值,靠TH0存储重装的计数值X0=256-250=6
  120.     TL0=0x06;
  121.     TR0=1;                //启动T0
  122.     ET0=1;                //开启定时器T0中断允许
  123.     EA=1;                 //开启中断总控制
  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)//定时器的定时计数,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. }
复制代码


下载:
51单片机数码管显示万年历proteus仿真.zip (883.16 KB, 下载次数: 96)

评分

参与人数 1黑币 +5 收起 理由
luo22 + 5 赞一个!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:194844 发表于 2017-5-1 23:30 | 只看该作者
very good
回复

使用道具 举报

板凳
ID:256877 发表于 2017-12-3 21:38 | 只看该作者
感觉这个挺好的就是没法下,
回复

使用道具 举报

地板
ID:256877 发表于 2017-12-3 21:39 | 只看该作者
我想知道那个51下面的那个元件是什么有没有人能告诉我
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表