新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 單片機電子時鐘程序

        單片機電子時鐘程序

        作者: 時間:2016-11-29 來源:網絡 收藏

        參數配置
        *******************************************************/
        #define length_whack_ms 100//按鍵按下大于1000ms為長擊
        #define auto_regulator_ms 10//自動調整參數100ms一次
        #define flash_cymometer_ms 40//調參數閃爍頻率400ms一次
        #define Alarm_switch_on_off_ms 250//按鍵按下大于2500ms鬧鐘開關切換一次
        #define cymometer_1s 100
        TIME time;
        BETY_BIT BIT_FLAG,BIT_FLAG1;
        unsigned char scanf_dat[2];
        //code P_leab_led_7[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
        //code N_leab_led_7[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
        code P_leab_led_7[10] = {0x0a,0xfa,0x4c,0x68,0xb8,0x29,0x09,0x7a,0x08,0x28};
        code unsigned char leab_day[12]={31,29,31,30,31,30,31,31,30,31,30,31};
        void Time_Count0_Init(void)
        {
        TMOD =0x51;
        TH0=0Xd8;
        TL0=0Xf0;
        ET0=1;
        TR0=1;
        IT0=1;
        EX0=1;
        //IT1=1;
        //EX1=1;
        //TH1=0X00;
        TL1=0X05;
        EA=1;
        }
        void time_add(void)
        {
        switch(time.mode)
        {
        case 2: time.min++;if(time.min>59)time.min=0;break;
        case 3: time.hou++;if(time.hou>23)time.hou=0;break;
        case 4: time.day++;if(time.mon==2&&(time.day>time.February_day))time.day=1;
        else if(time.day>leab_day[time.mon-1])time.day=1;break;
        case 5: time.mon++;if(time.mon>59)time.mon=0;break;
        case 6: time.year[0]++;if(time.year[0]>99)time.year[0]=0;break;
        case 7: time.year[1]++;if(time.year[0]>99)time.year[1]=0;break;
        case 8: time.AlarmMin++;if(time.AlarmMin>59)time.AlarmMin=0;break;
        case 9: time.AlarmHou++;if(time.AlarmHou>59)time.AlarmHou=0;break;
        }
        }
        void time_sub(void)
        {
        switch(time.mode)
        {
        case 2:if(time.min>0) time.min--;else time.min=59;break;
        case 3: if(time.hou>0)time.hou--;else time.hou=23;break;
        case 4: time.day--;if(time.day<1)
        {
        if(time.mon==2)time.day = time.February_day;
        else time.day=leab_day[time.mon-1];
        } break;
        case 5: time.mon--;if(time.mon<1)time.mon=12;break;
        case 6: if(time.year[0]>0)time.year[0]--;else time.year[0]=99;break;
        case 7: if(time.year[0]>0)time.year[1]--;else time.year[1]=99;break;
        case 8: if(time.AlarmMin>0)time.AlarmMin--;else time.AlarmMin=59;break;
        case 9: if(time.AlarmHou>0)time.AlarmHou--;else time.AlarmHou=23;break;
        }
        }
        /***************************************************
        產生十毫秒時基
        ****************************************************/
        void timer0(void) interrupt 1 using 1
        {
        unsigned int temp;
        TH0=0Xd8;
        TL0=0Xf0;
        RCLK=~RCLK;
        temp = time.year[1];
        temp = temp*100;
        temp +=time.year[0];
        if(temp%4)
        {
        time.February_day = 28;
        //leap_year_flag=1;
        }
        else
        {
        time.February_day = 29;
        }
        /************************************
        按鍵一長按處理(可改為函數 在這調用)
        *************************************/
        isr_button1_proces();
        /*************************************
        按鍵2長擊判斷 (可改為函數 在這調用)
        *************************************/
        isr_button2_proces();
        /*************************************
        按鍵3長擊判斷(可改為函數 在這調用)
        *************************************/
        isr_button3_proces();
        /***********************************
        秒表功能 (可改為函數 在這調用)
        ************************************/
        if((time.mode ==1)&&(start_stopwatch==1))
        {
        stopwatch_clear_flag = 0;
        time.stopwatch[0]++;
        if(time.stopwatch[0]>=100)
        {
        time.stopwatch[0] = 0;
        time.stopwatch[1]++;
        }
        if(time.stopwatch[1]>=60)
        {
        time.stopwatch[1] = 0;
        time.stopwatch[2]++;
        }
        if(time.stopwatch[2]>=60)
        time.stopwatch[2]=0;
        }
        if(stopwatch_clear_flag==1)
        {
        time.stopwatch[0] = 0;
        time.stopwatch[1] = 0;
        time.stopwatch[2] = 0;
        }
        //*計數器 (可改為函數 在這調用)
        if(cymometer_start_flag)
        {
        TR1 = 1;
        time.cymometer[1]=TH1;
        time.cymometer[0]=TL1;
        }
        //頻率計 (可改為函數 在這調用)
        if(cymometer1_start_flag)
        {
        TR1 = 1;
        time.cymometer_count--;
        if(time.cymometer_count==0)
        {
        TR1 = 0;
        time.cymometer[1]=TH1;
        time.cymometer[0]=TL1;
        time.cymometer_count=cymometer_1s;
        TH1=TL1=0;
        }
        }

        /*******************************
        調參數閃爍處理 (可改為函數 在這調用)
        *******************************/
        if((time.mode>1)&&(time.mode<10))
        {
        time.flashcount++;
        if(time.flashcount>flash_cymometer_ms)
        {
        time.flashcount = 0;
        flash_control = ~flash_control;
        }
        }
        else flash_control = 0;
        /***********************************
        時間產生 (可改為函數 在這調用)
        ************************************/
        time.count++;
        if( time.count>=100)
        {
        time.count = 0;
        time.sec ++;
        }
        if(time.sec>=60)
        {
        time.sec = 0;
        time.min ++;
        }
        if(time.min>=60)
        {
        time.min = 0;
        time.hou ++;
        }
        if(time.hou>23)
        {
        time.hou = 0;
        time.day ++;
        }
        if(time.mon==2)
        {
        if((time.day>time.February_day))
        {
        time.day = 1;
        time.mon ++;
        }
        }
        else
        {
        if((time.day>leab_day[time.mon-1]))
        {
        time.day = 1;
        time.mon ++;
        }
        }
        if(time.mon>12)
        {
        time.mon = 1;
        time.year[0]++;
        }
        if(time.year[0]>99)
        time.year[1]++;
        }
        /************************************************
        按鍵一中斷處理函數
        *************************************************/
        void ext_inter0(void) interrupt 0 using 3
        {
        //if(time.mode==0)
        time.ditheringcount = 100;
        menu_button1_down = 1;
        /* if(time.mode==1){time.mode =0;flash_flag=0;}
        if ((time.mode<10)&&(time.mode>1))
        {
        time.mode++;
        time.flashcount = 0;
        }
        else
        {
        time.mode = 0;
        flash_flag = 0;
        }
        menu_button2_down = 0;*/
        }
        /**
        /************************************
        按鍵一長按處理
        *************************************/
        void isr_button1_proces(void)
        {
        if(menu_button1_down_1 ==1)
        {
        if(button1==0)
        {
        time.delaycount--;
        if(time.delaycount==0)
        {
        time.delaycount = 0;
        time.mode = 2;
        menu_button1_down_1 = 0;
        }
        }
        else
        {
        time.delaycount = 0;
        time.mode = 1;
        menu_button1_down_1 = 0;

        }
        }
        /************************************************
        消抖部分 (10~20ms)
        ************************************************/
        if(menu_button1_down==1)
        {
        time.ditheringcount--;
        if(time.ditheringcount==98)
        { if(button1==0)
        {
        if(time.mode==0){menu_button1_down_1=1;time.delaycount = length_whack_ms;}
        else if(time.mode==1){time.mode =0;}
        else if ((time.mode<10))
        {
        time.mode++;
        time.flashcount = 0;
        menu_button1_down_1=0;
        }
        else
        {
        time.mode = 0;
        flash_flag = 0;
        }
        menu_button2_down = 0;
        menu_button1_down=0;
        }
        }
        }
        }
        /************************************************
        按鍵二中斷處理函數
        ************************************************/
        /*void ext_inter1(void) interrupt 2 using 2
        {
        menu_button2_down = 1;
        time.delaycount =length_whack_ms; */
        /*if(time.mode==1) start_stopwatch = ~start_stopwatch ;
        if(time.mode==10)cymometer_start_flag =~cymometer_start_flag;
        time_add();*/
        //}
        /************************************************
        處理函數
        ************************************************/
        void button2_proces(void)
        {
        if((menu_button2_down==0)&&(button2==0))
        {
        menu_button2_down = 1;
        time.delaycount =length_whack_ms;
        }
        }
        /************************************
        按鍵二長按處理
        *************************************/
        void isr_button2_proces(void)
        {

        if(menu_button2_down==1)
        {
        if(button2==0)
        {
        time.delaycount--;
        if(time.delaycount==length_whack_ms-2)//消抖部分 (10~20ms)
        {
        if(time.mode==1) start_stopwatch = ~start_stopwatch ;
        if(time.mode==10)
        {
        cymometer_start_flag =~cymometer_start_flag;
        TL1 = 0;
        TH1 = 0;
        cymometer1_start_flag=0;
        }
        time_add();
        }
        if(time.delaycount==0)
        {
        time.delaycount = auto_regulator_ms;
        button2_length_whack_flag = 1;
        // menu_button2_down = 0;
        }
        }
        else
        {
        menu_button2_down = 0;
        time.delaycount = auto_regulator_ms;
        button2_length_whack_flag = 0;
        }

        }
        if(button2_length_whack_flag)
        {
        if(time.mode>1)
        {
        button2_length_whack_flag = 0;
        time_add();
        TR1 =0;
        time.cymometer[0]=0;
        TH1=0;
        time.cymometer[1]=0;
        TL1=0;
        cymometer_start_flag =0;
        }
        }

        }
        /************************************************
        按鍵三處理函數
        ************************************************/
        void button3_proces(void)
        {
        if((menu_button3_down==0)&&(button3==0))
        {
        menu_button3_down = 1;
        time.delaycount =length_whack_ms;
        }
        }
        /************************************
        按鍵三長按處理
        *************************************/
        void isr_button3_proces(void)
        {
        if(menu_button3_down==1)
        {
        if(button3==0)
        {
        time.delaycount--;
        if(time.delaycount==length_whack_ms-2) //消抖部分 (10~20ms)
        {
        if(time.mode==1) stopwatch_clear_flag = 1;
        if(time.mode==10){cymometer1_start_flag=1;time.cymometer_count =cymometer_1s;
        TL1 = 0;
        TH1 = 0;
        start_stopwatch = 0;
        }
        time_sub();
        }
        if(time.delaycount==0)
        {
        time.delaycount = auto_regulator_ms;
        button3_length_whack_flag = 1;
        // menu_button2_down = 0;
        }
        }
        else
        {
        menu_button3_down = 0;
        time.delaycount = auto_regulator_ms;
        button3_length_whack_flag = 0;
        }

        }
        if(button3_length_whack_flag)
        {
        if(time.mode>1)
        {
        button3_length_whack_flag = 0;
        time_sub();
        TR1 =0;
        time.cymometer[0]=0;
        TH1=0;
        time.cymometer[1]=0;
        TL1=0;
        cymometer1_start_flag =0;
        }
        }

        }

        關鍵詞: 單片機電子時

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 大洼县| 汪清县| 海盐县| 大石桥市| 达尔| 南澳县| 东乡族自治县| 玉溪市| 墨脱县| 泊头市| 阜宁县| 金乡县| 渭源县| 沐川县| 镇安县| 贵港市| 隆回县| 金门县| 梅河口市| 武穴市| 顺平县| 宜都市| 北宁市| 精河县| 庆云县| 宽城| 雷山县| 克山县| 炎陵县| 柏乡县| 陇川县| 阿尔山市| 盐池县| 五寨县| 分宜县| 隆昌县| 泾源县| 板桥市| 鄯善县| 关岭| 泸西县|