找回密码
 立即注册

QQ登录

只需一步,快速开始

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

带秒表的单片机时钟LCD显示,如何修改才能实现秒表和时钟功能 求帮助

[复制链接]
跳转到指定楼层
楼主
ID:652868 发表于 2020-4-17 15:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
40黑币
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P3^5;
sbit en=P3^4;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^3;
sbit s4=P3^6;
sbit s5=P1^7;
sbit rd=P3^7;
sbit s6=P3^2;
sbit led=P2^7;
sbit beep=P1^5;
uchar count, count1,s1num,s4num,s6num;
char shi,fen,miao,pshi,mm;
uchar code table[]="    00:00:00 ";
uchar code table1[]="    23:59:57 ";
void delay(uint z)
{
    uint x,y;
    for(x=z;x>0;x--)
        for(y=110;y>0;y--);
}
void zhuanhuan()
{
    if(shi==0)
        pshi=12;
    if(shi<=12&&shi>=1)
        pshi=shi;
    if(shi>12)
        pshi=shi-12;
}
void write_com(uchar com)
{
    rs=0;
    en=0;
    P0=com;
    delay(5);
    en=1;
    delay(5);
    en=0;
}
void write_data(uchar date)
{
    rs=1;
    en=0;
    P0=date;
    delay(5);
    en=1;
    delay(5);
    en=0;
}
void _shi()
{
    if(shi>=12)
    {
        write_com(0x80+0x40+0);
        write_data('P');
        write_data('M');
    }
    else
    {
        write_com(0x80+0x40+0);
        write_data('A');
        write_data('M');  
    }
}
void init()
{
    uchar num;
    en=0;
    shi=23;
    fen=59;
    miao=57;
    write_com(0x38);
    write_com(0x0c);
    write_com(0x06);
    write_com(0x01);
    for(num=0;num<12;num++)
    {
        write_data(table[num]);
        delay(1);
    }
    write_com(0x80+0x40);
    for(num=0;num<12;num++)
    {
        write_data(table1[num]);
        delay(1);
    }
    TMOD=0x11;
    TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
    TH1=(65536-10000)/256;
    TL1=(65536-10000)%256;
    EA=1;
    ET0=1;
    TR0=1;
    ET1=1;
    TR1=0;
}
void write_sfm(uchar add,uchar date)
{
    uchar shi,ge;
    shi=date/10;
    ge=date%10;
    write_com(0x80+0x40+add);
    write_data(0x30+shi);
    write_data(0x30+ge);
}
void write_jishi(uchar add,uchar date)
{
    uchar shi,ge;
    shi=date/10;
    ge=date%10;
    write_com(0x80+add);
    write_data(0x30+shi);
    write_data(0x30+ge);
}
void keyscan()
{
    rd=0;
    if(s1==0)
    {
        delay(1);
        if(s1==0)
        {
            s1num++;
            while(!s1);
            if(s1num==1)
            {   
                write_com(0x80+0x40+11);
                TR0=0;
                write_com(0x0f);
            }
            if(s1num==2)
            {
                write_com(0x80+0x40+8);
            }
            if(s1num==3)
            {
                write_com(0x80+0x40+5);
            }
            if(s1num==4)
            {
                s1num=0;
                write_com(0x0c);
                TR0=1;
            }
        }
    }
        if(s1num!=0)
        {
            if(s2==0)
            {
                delay(1);
                if(s2==0)
                {
                    while(!s2);
                    if(s1num==1)
                    {
                        miao++;
                        if(miao==60)
                            miao=0;
                        write_sfm(10,miao);
                        write_com(0x80+0x40+11);
                    }
                    if(s1num==2)
                    {
                        fen++;
                        if(fen==60)
                            fen=0;
                        write_sfm(7,fen);
                        write_com(0x80+0x40+8);
                    }
                    if(s1num==3)
                    {
                        shi++;
                        if(shi==24)
                            shi=0;
                        if(s4num==0)
                        {
                            write_sfm(4,shi);
                            write_com(0x80+0x40+5);
                        }
                        if(s4num==1)
                        {
                            zhuanhuan();
                            _shi();
                            write_sfm(4,pshi);
                            write_com(0x80+0x40+5);
                        }
                 }
                }
            }
        }
        if(s3==0)
        {
            delay(1);
            if(s3==0)
            {
                while(!s3);
                if(s1num==1)
                {
                    miao--;
                    if(miao==-1)
                        miao=59;
                    write_sfm(10,miao);
                    write_com(0x80+0x40+11);
                }
                if(s1num==2)
                {
                    fen--;
                    if(fen==-1)
                        fen=59;
                    write_sfm(7,fen);
                    write_com(0x80+0x40+8);               
                }
                if(s1num==3)
                {
                    shi--;
                    if(shi==-1)
                        shi=23;
                    if(s4num==0)
                        {
                            write_sfm(4,shi);
                            write_com(0x80+0x40+5);
                        }
                        if(s4num==1)
                        {
                            zhuanhuan();
                            _shi();
                            write_sfm(4,pshi);
                            write_com(0x80+0x40+5);
                        }   
                }  
            }
        }
        if(s1num==0)
        {
            if(s4==0)
                delay(1);
            if(s4==0)
            {
                s4num++;
                while(!s4);
                if(s4num==1)
                {
                    zhuanhuan();
                    _shi();
                    write_sfm(4,pshi);
                }
                if(s4num==2)
                {
                    s4num=0;
                    write_com(0x80+0x40+0);
                    write_data(' ');
                    write_sfm(4,shi);
                }
            }
        }
}
void main()
{
    init();
    //it0_init();
     while(1)
    {            
        keyscan();
        if(s6==0)
    {
        delay(1);
        if(s6==0)
        {
            s6num++;
            while(!s5);
            if(s6num==1)
            {
                TR1=1;
                mm++;
                if(mm==100)
                {
                    mm=0;
                    miao++;
                    if(miao==60)
                    {
                        miao=0;
                        fen++;
                        if(fen==60)
                        {
                            TR1=0;
                        }
                            write_jishi(4,fen);
                            write_com(0x80+5);
                    }
                    write_jishi(7,miao);
                    write_com(0x80+8);   
                }
                write_jishi(10,mm);
                write_com(0x80+11);
            }
            if(s6num==2)
            {
                delay(5);
                TR1=0;
            }
            if(s6num==3)
            {
                s6num=0;
                init();
            }
        }
    }
        if(s5==0)
        {
            led=0;
        delay(3000);
        }
        else
            led=1;
        if(fen==0&&miao==0)
    {
        beep=0;
        delay(100);
    }
        else
            beep=1;
        
    }
}
void timer0() interrupt 1//¶¨Ê±Æ÷
{   
    TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
    count++;
    if(count==20)
        {
            count=0;
            miao++;
            if(miao==60)
            {
                miao=0;
                fen++;
                if(fen==60)
                {
                    fen=0;
                    shi++;
                    if(shi==24)
                    {
                        shi=0;
                    }
                    write_sfm(4,shi);
                }
                write_sfm(7,fen);
            }
            write_sfm(10,miao);
        }  
}
void timer1() interrupt 3//¶¨Ê±º¯Êý
{
    TH1=(65536-10000)/256;
    TL1=(65536-10000)%256;
    count1++;
    if(count1!=0)
    {
        mm++;
        if(mm==100)
        {
            mm=0;
            miao++;
            if(miao==60)
            {
                miao=0;
                fen++;
                if(fen==60)
                {
                    TR1=0;
                }
                write_jishi(4,fen);
            }
            write_jishi(7,miao);
        }
        write_jishi(10,mm);
    }        
}

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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