| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1754 人关注过本帖
标题:89C52电子钟程序
只看楼主 加入收藏
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
结帖率:100%
收藏
 问题点数:0 回复次数:8 
89C52电子钟程序
#include<reg52.h>                              
#define uint    unsigned int        
#define uchar   unsigned char   
sbit wela=P2^7;                                             //键盘位选
sbit dula=P2^6;                                             //键盘段选
sbit lcdrs=P3^5;                                                //写数据与命令选择
sbit lcden=P3^4;                                        //脉冲
sbit s1=P3^1;                                               //调时选择调哪部分
sbit s2=P3^2;                                           //增加
sbit s3=P3^3;                                           //减少
sbit ss=P3^7;                                           //检测信号
sbit kai=P3^6;                                              //开始的确定
uchar code table[]="  2011-03-12 sat";                  //时间标准,开始的初始化
uchar code table1[]="   00:00:00";
uchar code table2[]="Welcome my watch";                 //欢迎界面
uchar code table3[]="     Thanks";
uchar code table4[]="Mon,Tue,Wen,Thu,Fri,Sat,Sun";//输出星期
uchar code table5[]="  Only    for";
uchar code table6[]="   2000-2099";
uchar shi,fen,miao,tt0,s1num,nian,yue,ri,week;
void delay(uint z)                                          //延时函数               
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=114;y>0;y--)
                ;
}
void write_com(uchar com)                                   //写命令函数
{
        lcdrs=0;
        P0=com;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
        delay(5);
}
void write_date(uchar date)                                 //写数据函数
{
        lcdrs=1;
        P0=date;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
        delay(5);
}
void write_sfm(uchar add,uchar date)                    //有地址的写数据
{
        uchar shi,ge;
        shi=date/10;
        ge=date%10;
        write_com(add);        
        write_date(shi+0x30);   
        write_date(ge+0x30);
}
void write_week()                               //写星期
{
        uchar num;
        num=week;
        write_com(0x80+13);
        for(;num<week+3;num++)
                write_date(table4[num]);
}
void keyscan()                                  //扫描键盘
{
        
        ss=0;
        if(s1==0)                                       //选择调试的部位
        {
                delay(5);
                if(s1==0)
            {
                    while(!s1);
                    TR0=0;
                    s1num++;
               
            }   
        }
        switch(s1num)                                   //调试的部位,光标闪烁
        {
                case 1:
                                write_com(0x0f);
                                    write_com(0x80+0x40+10);   
                            break;        
                case 2:
                                write_com(0x0f);
                                    write_com(0x80+0x40+7);   
                            break;        
                case 3:
                                write_com(0x0f);
                                    write_com(0x80+0x40+4);   
                            break;        
                case 4:
                                write_com(0x0f);
                                    write_com(0x80+15);   
                            break;
                case 5:
                                write_com(0x0f);
                                    write_com(0x80+11);   
                            break;        
                case 6:
                                write_com(0x0f);
                                write_com(0x80+8);   
                            break;        
                case 7:
                                write_com(0x0f);
                                write_com(0x80+5);   
                            break;
                case 8:
                            s1num=0;
                                write_com(0x0c);
                            TR0=1;
                            break;            
        }
        /*
        if(s1num==1)
        {
                write_com(0x0f);
                write_com(0x80+0x40+10);   
        }
        if(s1num==2)
        {
                write_com(0x0f);
                write_com(0x80+0x40+7);   
        }
        if(s1num==3)
        {
                write_com(0x0f);
                write_com(0x80+0x40+4);   
        }
        if(s1num==4)
        {
                s1num=0;
                write_com(0x0c);
            TR0=1;
        }
        */
        if(s2==0)                                           //增加选择的部位数据
        {
                delay(5);
                if(s2==0)
            {
                    while(!s2);
                    switch(s1num)
                {
                        case 1:
                                    miao++;
                                        if(miao==60)
                                        miao=0;
                                                write_sfm(0x80+0x40+9,miao);               
                                    break;        
                        case 2:
                                    fen++;
                                        if(fen==60)
                                        fen=0;
                                                write_sfm(0x80+0x40+6,fen);
                                        break;        
                        case 3:
                                    shi++;
                                        if(shi==24)
                                        shi=0;
                                                write_sfm(0x80+0x40+3,shi);   
                                    break;        
                        case 4:
                                    week+=4;
                                        if(week==28)
                                {
                                        week=0;
                                }
                                        write_week();
                                    break;
                        case 5:
                                    ri++;
                                        if(ri==32)
                                        ri=1;
                                                write_sfm(0x80+10,ri);                                
                                    break;        
                        case 6:
                                    yue++;
                                        if(yue==13)
                                        yue=1;
                                            write_sfm(0x80+7,yue);
                                                break;        
                        case 7:
                                    nian++;
                                        if(nian==100)
                                        nian=0;
                                            write_sfm(0x80+4,nian);
                                    break;
               
                }
                /*
                    if(s1num==1)
                {
                        miao++;
                        if(miao==60)
                            miao=0;
                                write_sfm(0x80+0x40+9,miao);               
                }
                    if(s1num==2)
                {
                        fen++;
                        if(fen==60)
                            fen=0;
                                write_sfm(0x80+0x40+6,fen);
                }
                    if(s1num==3)
                {
                        shi++;
                        if(shi==24)
                            shi=0;
                                write_sfm(0x80+0x40+3,shi);   
                }
                */
            }
        }
        if(s3==0)                                               //减少选择部位的数据
        {
                delay(5);
                if(s3==0)
            {
                    while(!s3);
                    switch(s1num)
                {
                        case 1:
                                        if(miao==0)
                                        miao=59;
                                    else
                                        miao--;
                                                write_sfm(0x80+0x40+9,miao);               
                                                break;        
                        case 2:
                                        if(fen==0)
                                        fen=59;
                                    else
                                        fen--;
                                                write_sfm(0x80+0x40+6,fen);
                                    break;        
                        case 3:
                                        if(shi==0)
                                        shi=23;
                                    else
                                        shi--;
                                                write_sfm(0x80+0x40+3,shi);   
                                    break;        
                        case 4:
                                
                                        if(week==0)
                                {
                                        week=24;
                                }
                                    else
                                        week-=4;
                                        write_week();
                                    break;
                        case 5:
                                    if(ri==1)
                                        ri=31;
                                    else
                                        ri--;
                                                write_sfm(0x80+10,ri);                                
                                    break;        
                        case 6:
                                        if(yue==1)
                                        yue=12;
                                    else
                                        yue--;
                                            write_sfm(0x80+7,yue);
                                                break;        
                        case 7:
                                        if(nian==0)
                                {
                                        nian=99;
                                }
                                    else
                                        nian--;
                                            write_sfm(0x80+4,nian);
                                    break;            
                }
                /*
                    if(s1num==1)
                {
                        if(miao==0)
                            miao=59;
                    else
                            miao--;
                                write_sfm(0x80+0x40+9,miao);               
                }
                    if(s1num==2)
                {
                    
                        if(fen==0)
                            fen=59;
                    else
                            fen--;
                                write_sfm(0x80+0x40+6,fen);
                }
                    if(s1num==3)
                {
                        if(shi==0)
                            shi=23;
                    else
                            shi--;
                                write_sfm(0x80+0x40+3,shi);   
                }
                */
            }
            
        }
        
}

void show()                                                     //时间运行后显示月,日,
{
                switch(yue)
            {
                    case 1:
                            ri++;
                                if(ri==32)            
                        {
                                ri=1;
                                yue++;
                        }
                            break;
                    case 2:
                            ri++;
                                        if(nian%400==0||(nian%4==0&&nian%100!=0))
                        {
                                if(ri==30)
                            {
                                    ri=1;
                                    yue++;
                            }
                        }
                            else
                        {
                                if(ri==29)
                            {
                                    ri=1;
                                    yue++;
                            }
                        }
                            break;            
                     case 3:
                             ri++;
                                 if(ri==32)            
                          {
                                 ri=1;
                                 yue++;
                               }
                              break;
                     case 4:
                             ri++;
                                  if(ri==31)            
                         {
                                ri=1;
                                yue++;
                        }
                            break;
                    case 5:
                            ri++;
                            if(ri==32)
                        {
                                ri=1;
                                yue++;
                            }               
                            break;            
                     case 6:
                             ri++;
                                 if(ri==31)            
                          {
                                 ri=1;
                                 yue++;
                               }
                              break;
                     case 7:
                             ri++;
                                  if(ri==32)            
                         {
                                ri=1;
                                yue++;
                        }
                            break;

                      case 8:
                            ri++;
                                if(ri==32)            
                        {
                                ri=1;
                                yue++;
                        }
                            break;
                      case 9:
                            ri++;
                                if(ri==31)            
                        {
                                ri=1;
                                yue++;
                        }
                            break;
                        case 10:
                            ri++;
                                if(ri==32)            
                        {
                                ri=1;
                                yue++;
                        }
                            break;
               
                    case 11:
                            ri++;
                                if(ri==31)            
                        {
                                ri=1;
                                yue++;
                        }
                            break;
                    case 12:
                            ri++;
                                if(ri==32)            
                        {
                                ri=1;
                                yue=1;
                                nian++;
                                    if(nian==100)
                                    nian=0;
                                        write_sfm(0x80+4,nian);
                        }
                                break;                        
            }
                write_sfm(0x80+7,yue);
                write_sfm(0x80+10,ri);                                
}
void shizhong()                                             //用单片机时钟作为时间运行的脉冲
{
        if(tt0==18)        
        {
            tt0=0;
                miao++;
                if(miao==60)   
            {
                    miao=0;
                    fen++;
                    if(fen==60)
                {
                        fen=0;
                        shi++;
                        if(shi==24)
                    {
                            shi=0;
                            show();
                            week+=4;
                                if(week==28)
                        {
                                week=0;
                        }
                                write_week();
                    }
                                write_sfm(0x80+0x40+3,shi);   
                }
                        write_sfm(0x80+0x40+6,fen);
            }
                write_sfm(0x80+0x40+9,miao);        
        }

}
void init1()                        //初始化欢迎界面
{
        uchar num;
        write_com(0x80);
        for(num=0;num<16;num++)
        {
                write_date(table2[num]);
        }
        write_com(0x80+0x40);
        for(num=0;num<11;num++)
        {
                write_date(table3[num]);        
        }
        kai=0;
        while(1)
        {   
                if(s1==0)   
            {
                    delay(5);
                    if(s1==0)
                {
                        while(!s1);
                            write_com(0x01);
                        break;               
                }   
            }
        }
        write_com(0x80);
        for(num=0;num<13;num++)
        {
                write_date(table5[num]);
        }
        write_com(0x80+0x40);
        for(num=0;num<12;num++)
        {
                write_date(table6[num]);
        }
        while(1)
        {   
                if(s1==0)   
            {
                    delay(5);
                    if(s1==0)
                {
                        while(!s1);
                            write_com(0x01);
                        break;               
                }   
            }
        }
        
}
void init()                                     //初始化1602,中断
{
        uchar num;
        s1num=0;
        shi=fen=miao=tt0=0;
        nian=yue=ri=week=0;
//      shi=23;
//      fen=58;
        TMOD=0x01;
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        EA=1;
        ET0=1;
        wela=0;   
        dula=0;
        lcden=0;   
        write_com(0x38);   
        write_com(0x0c);
        write_com(0x01);
        write_com(0x80);
        init1();
        write_com(0x80);
        for(num=0;num<16;num++)
        {
                write_date(table[num]);   
        }
        write_com(0x80+0x40);
        for(num=0;num<11;num++)
        {
                write_date(table1[num]);   
        }
        TR0=1;   
}
void main()                                 //主函数,调用子函数
{
        init();
        while(1)
        {
                keyscan();
                shizhong();
        }
}
void t0() interrupt  1                  //t0中断
{
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        tt0++;
}
用郭天祥的板子可以用一下
text9.rar (2.05 KB)

互相学习!指点指点。
搜索更多相关主题的帖子: 电子钟 键盘 
2011-03-19 14:23
风影无声
Rank: 2
等 级:论坛游民
帖 子:7
专家分:24
注 册:2011-3-2
收藏
得分:0 
不错,谢谢帮你顶下
2011-03-21 10:34
嗨,我们的C
Rank: 1
来 自:汉中
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-6
收藏
得分:0 
多谢分享!

执着,每个人都可以如优秀!
2011-04-06 21:27
软编小虫
Rank: 5Rank: 5
来 自:广西,南宁,马山
等 级:职业侠客
威 望:1
帖 子:160
专家分:324
注 册:2008-10-8
收藏
得分:0 
太长了,没时儿看,帮顶。。。
2011-04-07 21:25
离“炫”的心
Rank: 1
等 级:新手上路
帖 子:10
专家分:2
注 册:2011-4-9
收藏
得分:0 
一看程序的开头就有老郭的风格。
2011-04-17 00:36
niu91
Rank: 2
等 级:论坛游民
帖 子:64
专家分:44
注 册:2009-7-25
收藏
得分:0 
的确有老郭的风范 楼主也在看郭天祥的视频教程吧
写的挺好的 加油
2011-04-19 22:09
windleefstta
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-13
收藏
得分:0 
友情帮顶,不错的帖子。貌似老郭的学生!!!
2011-06-13 10:20
kidoheart
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-7-25
收藏
得分:0 
楼主顶你,但是我想问一下,用定时器做时钟的误差怎么尽可能减小?咱俩的这程序误差都蛮大额~~~
2011-07-28 16:21
失忆后诗意
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-12-3
收藏
得分:0 
必须顶啊  不过貌似超出了13K  。。。。。
2012-12-03 15:24
快速回复:89C52电子钟程序
数据加载中...
 
   



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

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