| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2750 人关注过本帖
标题:80C51单片机电子钟程序
取消只看楼主 加入收藏
六翅
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-12-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
80C51单片机电子钟程序
遇到了一个重要的51单片机程序,但是看不大懂。有哪位高手能讲解一下吗?
void datadispProc(void);
uchar disp_data[8]={0};
uchar TimeIntCnt;
uchar Timehms[3]={20,59,50};
uchar Timerhm[2]={21,5};
bit secflag = 0;
uchar func = 0;

void main()
{
    uchar i,j,tmpdata;
   
    P2 = 0xff;                        //关闭所有数码管
    P1 = 0x00;
    //数码管测试   
    for( i = 0; i < 1; i++)
    {
        tmpdata = 0xfe;
        for( j = 0; j < 6; j++)
        {
            P2 = tmpdata;
            P1 = 0xff;
               delayms(100);
            tmpdata = _crol_(tmpdata, 1);
        }
   
    }        
    for( i = 0; i < 1; i++)
    {
        tmpdata = 0xdf;
        for( j = 0; j < 6; j++)
        {
            P2 = tmpdata;
            P1 = 0xff;
               delayms(100);
            tmpdata = _cror_(tmpdata, 1);
        }
   
    }
    //定时/计数器初始化
    TMOD = 0X10;
    TH1 = 0X3C;
    TL1 = 0XB0;
    EA = 1;
    ET1 = 1;
    TR1 = 1;
    datadispProc();           
    TimeIntCnt = 0;
    while(1)
    {   
        //按键处理程序
        if(key1 == 0)
        {
            delayms(10);
            if(key1 == 0)
            {
                 if(func++ >= 4) func = 0;
                while(key1 == 0);
            }
        }
        //按键处理程序
        else if(key2 == 0)
        {
            delayms(10);
            if(key2 == 0)
            {
                 switch(func)
                {
                    case 1:  Timehms[0]++; break;
                    case 2:  Timehms[1]++; break;
                  case 3:  Timerhm[0]++; break;
                    case 4:  Timerhm[1]++; break;
                    default: func = func;
                }
                while(key2 == 0);
            }
        }
        //按键处理程序
           else if(key3 == 0)
        {
            delayms(10);
            if(key3 == 0)
            {
                 switch(func)
                {
                    case 1:  Timehms[0]--; break;
                    case 2:  Timehms[1]--; break;
                    case 3:  Timerhm[0]--; break;
                    case 4:  Timerhm[1]--; break;
                    case 5:  func =0 ; break;
                    default: func = func;
                }
                while(key3 == 0);
            }
        }
        //显示程序设计调用
        display();
    }
}
//显示数据处理程序
void datadispProc(void)
{

    if(func == 1 || func ==2 || func ==0)
    {
         disp_data[0] = Timehms[0]/10;
         disp_data[1] = Timehms[0]%10;
         disp_data[3] = Timehms[1]/10;
         disp_data[4] = Timehms[1]%10;
         disp_data[6] = Timehms[2]/10;
         disp_data[7] = Timehms[2]%10;
        disp_data[2] = 10;
        disp_data[5] = 10;
        if(func != 0)
        {
            if(secflag)
            {
                 switch(func)
                {
                     case 1:
                    {   
                        disp_data[0]=10;
                        disp_data[1]=10;
                    }break;
                    case 2:
                    {
                        disp_data[3]=10;
                        disp_data[4]=10;
                    }break;
                    
                }
            }   
        }
    }
    else if(func == 3 || func ==4 )
    {
         disp_data[0] = 11;
         disp_data[1] = 11;
         disp_data[3] = Timerhm[0]/10;
         disp_data[4] = Timerhm[0]%10;
         disp_data[6] = Timerhm[1]/10;
         disp_data[7] = Timerhm[1]%10;
        
        disp_data[5] = 10;
        if(func != 0)
        {
            if(secflag)
            {
                 switch(func)
                {
                     case 3:
                    {   
                        disp_data[3]=10;
                        disp_data[4]=10;
                    }break;
                    case 4:
                    {
                        disp_data[6]=10;
                        disp_data[7]=10;
                    }break;
                    
                }
            }   
        }
    }                        

}
//显示程序
void display(void)
{
    uchar  i;
    uchar  dis_digit;
    dis_digit = 0x01;
    for( i = 0; i < 8; i++)
    {
        P2 = dis_code[disp_data[i]];        //段码送P2口
        P1 = dis_digit;                 //位码送P1口
        delayms(1);
        P1 = 0x00;
        dis_digit = _crol_(dis_digit, 1);    //位选通左移,下次选通下一位         
    }
}

//延时程序
void delayms(unsigned int ms)            //延时子程序(晶振12M)
{                        
    uchar i;
    while(ms--)
    {
        for(i = 0; i < 120; i++);
    }
}
//定时器中断函数
void Time1Ser(void) interrupt 3
{
     TH1 = 0X3C;
    TL1 = 0XB0;
    if( TimeIntCnt++ >= 15)
    {
        secflag = ~secflag;
         TimeIntCnt = 0;
        if(Timehms[2]++ >=59)
        {
             Timehms[2] = 0;
            if(Timehms[1]++ >=59)
            {
                Timehms[1] = 0;
               if(Timehms[0]++ >=23)
               {
                    Timehms[0] = 0;
               }
            }            
        }
        //定时控制按键
        if(key4 == 0)
        {
            if((Timehms[0] == Timerhm[0]) && (Timehms[1] == Timerhm[1]))
            {
                 led = 0;
            }
            else
            {
                 led = 1;
            }
        }
        else
        {
             led = 1;   
        }
        //调用显示数据处理程序        
        datadispProc();         
    }  
}
搜索更多相关主题的帖子: void for while case break 
2019-03-03 22:14
六翅
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-12-6
收藏
得分:0 
回复 3楼 Mr_doge
谢谢你了
2019-03-27 23:15
快速回复:80C51单片机电子钟程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031467 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved