標(biāo)題: 電子時(shí)鐘編程 如有不對(duì)請(qǐng)指正 謝謝 [打印本頁]

作者: 無門無    時(shí)間: 2018-3-25 20:06
標(biāo)題: 電子時(shí)鐘編程 如有不對(duì)請(qǐng)指正 謝謝
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
uchar num=0,temp=0,count=0;
uchar aa;
uchar hour,min,sec;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void delay(uint z);
void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f,uchar aa);
void read_key();
void led();
void time_change();
void main()        //主函數(shù)
{
        P2=0xff;
        hour=12;
        min=0;
        sec=0;
        TMOD=0x11;
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        TH1=(65536-50000)/256;
        TL1=(65536-50000)%256;
        EA=1;
        ET0=1;
        ET1=1;
        TR0=1;
        TR1=1;
        aa=0xff;
        while(1)
        {
                time_change();
                display(table[hour/10],table[hour%10],table[min/10],table[min%10],table[sec/10],table[sec%10],0xff);
        }
}
void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f,uchar aa)        //顯示函數(shù)
{
         if(num==1){P1=0x01&aa;P0=a;delay(2);}else{P1=0x01;P0=a;delay(2);}   //hour  
            if(num==1){P1=0x02&aa;P0=b;delay(2);}else{P1=0x02;P0=b;delay(2);}  //hour  
     if(num==2){P1=0x04&aa;P0=c;delay(2);}else{P1=0x04;P0=c;delay(2);} //min   
     if(num==2){P1=0x08&aa;P0=d;delay(2);}else{P1=0x08;P0=d;delay(2);} //min     
     if(num==3){P1=0x10&aa;P0=e;delay(2);}else{P1=0x10;P0=e;delay(2);} //sec
     if(num==3){P1=0x20&aa;P0=f;delay(2);}else{P1=0x20;P0=f;delay(2);} //sec
}
void timer0() interrupt 1        //定時(shí)器0中斷函數(shù)
{
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        temp++;
}
void timer1() interrupt 3        //定時(shí)器1中斷函數(shù)
{
        TH1=(65536-50000)/256;
        TL1=(65536-50000)%256;
        count++;
        if(count>=20)
        count=0;
}
void delay(uint z)        //延時(shí)函數(shù)
{
        uint j,k;
        for(j=z;j>0;j--)
        for(k=120;k>0;k--);
}
void read_key()        //讀按鍵函數(shù)
{
        if(key1==0)
        {
                _led=0;
                delay(100);
                if(key1==0)
                {
                        delay(100);
                        _led=1;
                        num++;
                        if(num>3)
                        {
                                num=0;
                        }
                        while(1)
                        {
                                if(key1==0)
                                {
                                        _led=0;
                                        delay(10);
                                        if(key1==0)
                                        {
                                                num++;
                                                if(num>3)
                                                {
                                                        num=0;
                                                        break;
                                                }
                                        }
                                                while(!key1);
                                                delay(10);
                                                while(!key1);
                                                _led=1;

                                        }
                                        if(key2==0)
                                        {
                                                _led=0;
                                                delay(80);
                                                if(key2==0)
                                                {
                                                          if(num==1){hour++;if(hour==24)hour=0;}
                                                          if(num==2){min++;if(min==60)min=0;}
                                                          if(num==3){sec++;if(sec==60)sec=0;}
                                                }
                                                while(!key1);
                                                delay(10);
                                                while(!key1);
                                                _led=1;

                                        }
                                        if(key3==0)
                                        {
                                                _led=0;
                                                delay(80);
                                                if(key3==0)
                                                {
                                                        if(num==1)
                                                        {
                                                                hour--;
                                                                if(hour==0)
                                                                {
                                                                        hour=23;
                                                                }
                                                        }
                                                        if(num==2)
                                                        {
                                                                min--;
                                                                if(min==0)
                                                                {
                                                                        min=59;
                                                                }
                                                        }
                                                        if(num==3)
                                                        {
                                                                sec--;
                                                                if(sec==59)
                                                                {
                                                                        sec=59;
                                                                }
                                                        }
                                                        while(!key1);
                                                        delay(10);
                                                        while(!key1);
                                                        _led=1;
                                                }
                                                if(count<=15)
                                                display(table[hour/10],table[hour%10],table[min/10],table[min%10],table[sec/10],table[sec%10],0xff);
                                                if(count>15)
                                                display(table[hour/10],table[hour%10],table[min/10],table[min%10],table[sec/10],table[sec%10],0x00);
                                        }
                                }
                                while(!key1);
                                delay(10);
                                while(!key1);
                                _led=1;
                        }
                }
}
void time_change()        //時(shí)間調(diào)整函數(shù)
{
        read_key();
        if(temp>=20)
        {
        temp=0;
        sec++;
        if(sec>=60)
        {
                sec=0;
                min++;
                if(min>=60)
                {
                        min=0;
                        hour++;
                        if(hour>=24)
                        {
                                hour=0;
                         }
                 }
         }
        }
}



作者: admin    時(shí)間: 2018-3-25 20:17
電路圖呢?
作者: 無門無    時(shí)間: 2018-3-26 18:56
話說圖片怎么發(fā)  說是地址  但是地址搞不來
作者: 無門無    時(shí)間: 2018-3-26 18:59
admin 發(fā)表于 2018-3-25 20:17
電路圖呢?

電路圖

作者: 無門無    時(shí)間: 2018-3-27 16:15
admin 發(fā)表于 2018-3-25 20:17
電路圖呢?

嗯........   我想把 時(shí)間之間的0改成-    但是我編程能力還很差  這個(gè)編程還是參照別人的才搞出來的...    求大佬幫忙   謝謝!





歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1