<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

        單片機C語言時鐘源碼

        作者:佚名   來源:本站原創   點擊數:  更新時間:2014年05月02日   【字體:

         此程序是在51hei單片機開發板上面做的,如需要移植到自己的電路上,修改相應的端口即可,開發板完整的電路圖下載:  點這里 (注意:只需要看相關部分即可,其他部分可以忽略)

        //****80c52***時鐘程序
        //***手把手教你學單片開發板***
        //**2012/9/7***23:02***
        # include<reg52.h>

        typedef unsigned char uint8;
        typedef unsigned int  uint16;
        typedef unsigned long uint32;
         
        uint8 block[]={0xc0,0xf9,0xa4,0xb0,0x99,
          0x92,0x82,0xf8,0x80,0x90};//顯示數組

        uint8 a[6];  
        uint16 counter=0;  
        uint32 sec=0;  
        #define tr 40  //發聲值
        bit MS    = 1;    //狀態字;
        sbit ADDR0 = P1^0; //A0****
        sbit ADDR1 = P1^1; //A1*****
        sbit ADDR2 = P1^2; //A2*****
        sbit ADDR3 = P1^3; //E1|E2**
        sbit ENLED = P1^4; //E3****38譯碼器
        sbit BUZZ  = P1^6; //蜂鳴器
        sbit    keyout1 = P2^3; //按鍵公共端
        sbit keyin1  = P2^4; //k1 長安0.1S進入時間調節;
        sbit keyin2  = P2^5; //k2 時間調節斷碼選擇
        sbit keyin3  = P2^6; //k3 調節時間加
        sbit keyin4  = P2^7; //k4 調節時間減

        void refresh_LS(); //數碼管掃描函數
        void timer1_int(); //定時器1中斷函數
        void keying(); //按鍵讀取函數
        void display(); //數碼管顯示函數
        void buzzer(); //蜂鳴器發聲函數
        void delay(uint16 n); //延時函數

        main()
        {
        keyout1 = 0; 
        keyin1 = 1; keyin2 = 1; keyin3 = 1; keyin4 = 1;
        ENLED = 0; ADDR3 = 1;
        timer1_int();
        display(); 
            while(1)
        {
        if(keyin1 == 0)
        {
        delay(100000);
        if(keyin1 == 0)
        {
        MS = ~MS;
        buzzer();
        while(!keyin1);
        }
        }
        if(MS == 0)keying();   

        }
        void buzzer()
        {
        static  uint8 i=0;
        for(i=0;i<tr;i++)
        {
        BUZZ =~BUZZ;
        delay(100);
        }
            BUZZ = 1;
        }
        void delay(uint16 n)
        {
        while(n--);
        }
        void timer1_int()
        {
        TMOD |= 0X10;
        TMOD &= 0xdf;
        TH1   = 0xfc;
        TL1   = 0x67;
        TR1   = 1;  
        EA    = 1;
        ET1   = 1;
         
        }
        void interrupt_tiner1() interrupt 3   
        {
        TH1 = 0xfc;
        TL1 = 0x67; 
        counter++;
        if(counter == 1000)
        display();
        refresh_LS();
        }
        void display()
        {
        counter = 0;
        if(sec>=86400)sec=0;

        a[0] = 0x7f&block[sec%10];
        a[1] = block[sec/10%6];
        a[2] = 0x7f&block[sec/60%10];
        a[3] = block[sec/600%6];
        a[4] = 0x7f&block[sec/3600%10];
        a[5] = block[sec/36000%3];
        if(MS ==1)sec++;

        }

        void keying()
        {  
        static uint8 i=0,y,y1;
           if(keyin2 == 0)
        {
        i+=2;
        if(i == 6)i=0;
        delay(500);
        }
        else if(keyin3 == 0)
        {
         
        if(i == 0)sec++;

        else
        if(i == 2)sec+=60;  

        else
        if(i == 4)sec+=3600;

        delay(1000);  
        }  
        else if(keyin4 == 0)
        {
         
        if(i == 0)sec--; 
        else
        if(i == 2)sec-=60;

        else
        if(i == 4)sec-=3600; 
        delay(500);
         
        }
        display();
        if((keyin1 == 0)||(keyin2 == 0)||(keyin3 == 0)||(keyin4 == 0))buzzer();
        while((keyin1 == 0)||(keyin2 == 0)||(keyin3 == 0)||(keyin4 == 0));
        y    = a[i];
        y1   = a[i+1];
        a[i] = 0xff;
        a[i+1]=0xff;
        delay(5000);
        a[i]  = y;
        a[i+1]=y1;
        delay(5000);
        }
        void refresh_LS()
        {
        static uint8 j=0;
        switch(j)
        {
        case 0: ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0=a[0]; j++; break;
        case 1: ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0=a[1]; j++; break;
        case 2: ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0=a[2]; j++; break;
        case 3: ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0=a[3]; j++; break;
        case 4: ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0=a[4]; j++; break;
        case 5: ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; P0=a[5]; j=0; break;
        default: break;
        }

        }
        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮