找回密码
 立即注册

QQ登录

只需一步,快速开始

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

通过单片机定时器实现时钟的51程序

[复制链接]
跳转到指定楼层
楼主
ID:383209 发表于 2018-8-5 20:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
可以通过定时器编写万年历中的1时钟程序,十分简单

单片机源程序如下:
  1. #include<reg52.h>
  2. #include<intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. uchar count0,count1;
  6. uchar temp0,temp1 = 0x7f,temp2,temp3;
  7. uchar code leddata[] = {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
  8. void Delay100us()                //@11.0592MHz
  9. {
  10.         unsigned char i,j;

  11.         _nop_();
  12.         _nop_();
  13.         i = 2;
  14.         j = 15;
  15.         do
  16.         {
  17.                 while (--j);
  18.         } while (--i);
  19. }
  20. void Delay500ms()                //@11.0592MHz
  21. {
  22.         unsigned char i, j, k;

  23.         _nop_();
  24.         _nop_();
  25.         i = 22;
  26.         j = 3;
  27.         k = 227;
  28.         do
  29.         {
  30.                 do
  31.                 {
  32.                         while (--k);
  33.                 } while (--j);
  34.         } while (--i);
  35. }

  36. void display(uchar t)
  37. {
  38.   uchar ge,shi;
  39.   shi = t/10;
  40.   ge = t%10;
  41.   P2 = 0xe0;P0 =leddata[ge];P2 = 0x00;
  42.   P2 = 0xc0;P0 = 0x80;P2 = 0x00;
  43.   Delay100us();
  44.   P2 = 0xe0;P0 =leddata[shi];P2 = 0x00;
  45.   P2 = 0xc0;P0 = 0x40;P2 = 0x00;
  46.   Delay100us();
  47.   P2 = 0xc0;P0 = 0x00;P2 = 0x00;
  48.   P2 = 0xe0;P0 =0xbf;P2 = 0x00;
  49.   P2 = 0xc0;P0 = 0x20;P2 = 0x00;
  50.   Delay100us();
  51. }
  52. void display1(uchar a)
  53. {
  54.   uchar ge,shi;
  55.   ge = a%10;
  56.   shi = a/10;
  57.   P2 = 0xe0;P0 = leddata[ge];P2 = 0x00;
  58.   P2 = 0xc0;P0 = 0x10;P2 = 0x00;
  59.   Delay100us();
  60.   P2 = 0xe0;P0 =leddata[shi];P2 = 0x00;
  61.   P2 = 0xc0;P0 = 0x08;P2 = 0x00;
  62.   Delay100us();
  63.   P2 = 0xc0;P0 = 0x00;P2 = 0x00;
  64.   P2 = 0xe0;P0 = 0xbf;P2 = 0x00;
  65.   P2 = 0xc0;P0 = 0x04;P2 = 0x00;
  66.   Delay100us();
  67. }
  68. void display2(uchar c)
  69. {
  70.   uchar ge,shi;
  71.   ge = c%10;
  72.   shi = c/10;
  73.   P2 = 0xe0;P0 =leddata[ge];P2 = 0x00;
  74.   P2 = 0xc0;P0 = 0x02;P2 = 0x00;
  75.   Delay100us();
  76.   P2 = 0xe0;P0 =leddata[shi];P2 = 0x00;
  77.   P2 = 0xc0;P0 = 0x01;P2 = 0x00;
  78.   Delay100us();
  79. }
  80. /*void init()                          //定时器内部中断0
  81. {
  82.   TMOD = 0x11;
  83.   TH1 = TH0 = 0X4B;
  84.   TL1 = TH0 = 0XFC;
  85.   ET1 = ET0 = 1;
  86.   TR1 = TR0 = 1;
  87.   EX0 = 1;                  //开启外部中断0
  88.   IT0 = 0;                  //设置外部中断启动方式为低电平启动
  89.   EA = 1;
  90. }*/
  91. #define KEYPORT P3
  92. uchar Trg;
  93. uchar Cont,d;
  94. void Key_Read( void )
  95. {
  96.     unsigned char ReadData = KEYPORT^0xff;
  97.     Trg = ReadData & (ReadData ^ Cont);      
  98.     Cont = ReadData;                                        
  99. }
  100. void shijianshezhi()
  101. {
  102.   Key_Read();
  103.   if(Trg&0x08) //S4
  104.   {
  105.      TR0 = 0;
  106.   }
  107.   if(Trg&0x04) //S5
  108.   {
  109.      TR0 = 1;
  110.   }
  111. }
  112. void shijianhanshu()
  113. {
  114.           if(TF0 == 1)
  115.           {
  116.             count0++;
  117.                 TF0 = 0;
  118.                 TH0 = 0x4b;
  119.             TL0 = 0Xfc;
  120.           }
  121.           if(count0 == 20)
  122.           {
  123.             count0 = 0;
  124.                 temp0++;
  125.           }
  126.           if(temp0 == 60)
  127.           {
  128.             temp2++;
  129.                 P2=0xa0;P0=0xff;P2=0X00;
  130.                 Delay500ms();
  131.                 P2=0xa0;P0=0x00;P2=0X00;                                             
  132.                 temp0 = 0;
  133.           }
  134.           if(temp2 == 60)
  135.           {
  136.             temp3++;
  137.                 temp2 = 0;
  138.           }
  139.           display(temp0);
  140.           display1(temp2);
  141.           display2(temp3);
  142. }
  143. void main()
  144. {
  145.    TMOD = 0X01;
  146.    TH0 = 0x4b;
  147.    TL0 = 0Xfc;
  148.    while(1)
  149.    {
  150.       shijianhanshu();
  151.           shijianshezhi();
  152.    }
  153. }

复制代码

所有资料51hei提供下载:
定时器实现时钟的51程序.docx (12.71 KB, 下载次数: 8)



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

使用道具 举报

沙发
ID:382322 发表于 2018-8-11 17:21 | 只看该作者
定时器做的时钟,误差会很大,不是很好哦!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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