• 电子钟程序


    //不同单片机数码管显示可能不一样,但是核心程序一样

    #include<reg52.h>     //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义

    #define uchar unsigned char
    #define uint  unsigned int
    static unsigned char count;
    code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 
            //共阴数码管 0-9
    uchar smg[8];                 //定义缓冲区
    uint minute=30,hour=12,second;  //定义并且初始化值 12:30:00
     
    void delay(unsigned int cnt)
    {
     while(--cnt);
    }
     
    void display( )
    {


               smg[0]=tab[hour/10];    //显示小时
          smg[1]=tab[hour%10];
     smg[2]=0x40;            //显示"-"
               smg[3]=tab[minute/10];  //显示分钟
                smg[4]=tab[minute%10];
          smg[5]=0x40;            //显示"-"
                     smg[6]=tab[second/10];  //显示秒
     smg[7]=tab[second%10];

    }


    void main()
    {
         uchar i;
         TMOD |=0x01;  //定时器0 10ms in 12M crystal 用于计时
    TH0=0xd8;     //初值
    TL0=0xf0;
    ET0=1;
    TR0=1;
         EA =1;


     display();


     while(1)
     {
     
    for(i=0;i<8;i++)//显示时间,不同单片机数码管显示可能不一样,以你的单片机为主
      {
         P0=smg[i];
    P2=i;
    delay(100);
      }

     
    if (count==100)
       {
       count=0;
       second++; display();            //秒加1
    if(second==60)
      {
      second=0;
      minute++;          //分加1
      if(minute==60)
     {
      minute=0;
      hour++;      //时加1
      if(hour==24)
         hour=0;
     }  
         
      }
       
      } 


    display();
      }
      
    }


    void timer() interrupt 1  
    {
       
       TH0=0xd8;                  //重新赋值
       TL0=0xf0;
       count++;


    }




     
     

  • 相关阅读:
    关于配置文件权衡,.config VS .xml
    Google不支持小于12px字体 终极办法
    两个表循环的复杂度分析 征集
    SQL 计算列
    5分钟上手写ECharts的第一个图表
    NGif, Animated GIF Encoder for .NET
    Chart系列(一):Chart的基本元素
    一张广告图片引起的思维DFS
    洛谷 P2580 于是他错误的点名开始了
    洛谷 P1496 火烧赤壁
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3000813.html
Copyright © 2020-2023  润新知