新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 調試通過51單片機的萬年歷

        調試通過51單片機的萬年歷

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

        bit get_moon_day(uchar month_p,uint table_addr)
        {
        unsigned char temp;
        switch (month_p){
        case 1:{if(year_code[table_addr]&0x08 == 0) return(0);
        else return(1);}
        case 2:{if(year_code[table_addr]&0x04 == 0) return(0);
        else return(1);}
        case 3:{if(year_code[table_addr]&0x02 == 0) return(0);
        else return(1);}
        case 4:{if(year_code[table_addr]&0x01 == 0) return(0);
        else return(1);}
        case 5:{if(year_code[table_addr+1]&0x80 == 0) return(0);
        else return(1);}
        case 6:{if(year_code[table_addr+1]&0x40 == 0) return(0);
        else return(1);}
        case 7:{if(year_code[table_addr+1]&0x20 == 0) return(0);
        else return(1);}
        case 8:{if(year_code[table_addr+1]&0x10 == 0) return(0);
        else return(1);}
        case 9:{if(year_code[table_addr+1]&0x08 == 0) return(0);
        else return(1);}
        case 10:{if(year_code[table_addr+1]&0x04 == 0) return(0);
        else return(1);}
        case 11:{if(year_code[table_addr+1]&0x02 == 0) return(0);
        else return(1);}
        case 12:{if(year_code[table_addr+1]&0x01 == 0) return(0);
        else return(1);}
        case 13:{temp=year_code[table_addr+2]&0x80;
        if (temp==0)return(0);else return(1);}
        }
        }
        void Conversion(bit c,uchar year,uchar month,uchar day)
        { //c=0 為21世紀,c=1 為19世紀 輸入輸出數據均為BCD數據
        uchar temp1,temp2,temp3,month_p;
        uint temp4,table_addr;
        bit flag2,flag_y;
        temp1=year/16; //BCD->hex 先把數據轉換為十六進制
        temp2=year;
        year=temp1*10+temp2;
        temp1=month/16;
        temp2=month;
        month=temp1*10+temp2;
        temp1=day/16;
        temp2=day;
        day=temp1*10+temp2;
        //定位數據表地址
        if(c==0){
        table_addr=(year+0x64-1)*0x3;
        }
        else {
        table_addr=(year-1)*0x3;
        }
        //定位數據表地址完成
        //取當年春節所在的公歷月份
        temp1=year_code[table_addr+2]&0x60;
        temp1=_cror_(temp1,5);
        //取當年春節所在的公歷月份完成
        //取當年春節所在的公歷日
        temp2=year_code[table_addr+2]&0x1f;
        //取當年春節所在的公歷日完成
        // 計算當年春年離當年元旦的天數,春節只會在公歷1月或2月
        if(temp1==0x1){
        temp3=temp2-1;
        }
        else{
        temp3=temp2+0x1f-1;
        }
        // 計算當年春年離當年元旦的天數完成
        //計算公歷日離當年元旦的天數,為了減少運算,用了兩個表
        //day_code1[9],day_code2[3]
        //如果公歷月在九月或前,天數會少于0xff,用表day_code1[9],
        //在九月后,天數大于0xff,用表day_code2[3]
        //如輸入公歷日為8月10日,則公歷日離元旦天數為day_code1[8-1]+10-1
        //如輸入公歷日為11月10日,則公歷日離元旦天數為day_code2[11-10]+10-1
        if (month<10){
        temp4=day_code1[month-1]+day-1;
        }
        else{
        temp4=day_code2[month-10]+day-1;
        }
        if ((month>0x2)&&(year%0x4==0)){ //如果公歷月大于2月并且該年的2月為閏月,天數加1
        temp4+=1;
        }
        //計算公歷日離當年元旦的天數完成
        //判斷公歷日在春節前還是春節后
        if (temp4>=temp3){ //公歷日在春節后或就是春節當日使用下面代碼進行運算
        temp4-=temp3;
        month=0x1;
        month_p=0x1; //month_p為月份指向,公歷日在春節前或就是春節當日month_p指向首月
        flag2=get_moon_day(month_p,table_addr); //檢查該農歷月為大小還是小月,大月返回1,小月返回0
        flag_y=0;
        if(flag2==0)temp1=0x1d; //小月29天
        else temp1=0x1e; //大小30天
        temp2=year_code[table_addr]&0xf0;
        temp2=_cror_(temp2,4); //從數據表中取該年的閏月月份,如為0則該年無閏月
        while(temp4>=temp1){
        temp4-=temp1;
        month_p+=1;
        if(month==temp2){
        flag_y=~flag_y;
        if(flag_y==0)month+=1;
        }
        else month+=1;
        flag2=get_moon_day(month_p,table_addr);
        if(flag2==0)temp1=0x1d;
        else temp1=0x1e;
        }
        day=temp4+1;
        }
        else{ //公歷日在春節前使用下面代碼進行運算
        temp3-=temp4;
        if (year==0x0){year=0x63;c=1;}
        else year-=1;
        table_addr-=0x3;
        month=0xc;
        temp2=year_code[table_addr]&0xf0;
        temp2=_cror_(temp2,4);
        if (temp2==0)month_p=0xc;
        else month_p=0xd; //
        flag_y=0;
        flag2=get_moon_day(month_p,table_addr);
        if(flag2==0)temp1=0x1d;
        else temp1=0x1e;
        while(temp3>temp1){
        temp3-=temp1;
        month_p-=1;
        if(flag_y==0)month-=1;
        if(month==temp2)flag_y=~flag_y;
        flag2=get_moon_day(month_p,table_addr);
        if(flag2==0)temp1=0x1d;
        else temp1=0x1e;
        }
        day=temp1-temp3+1;
        }
        c_moon=c; //HEX->BCD ,運算結束后,把數據轉換為BCD數據
        temp1=year/10;
        temp1=_crol_(temp1,4);
        temp2=year;
        year_moon=temp1|temp2;
        temp1=month/10;
        temp1=_crol_(temp1,4);
        temp2=month;
        month_moon=temp1|temp2;
        temp1=day/10;
        temp1=_crol_(temp1,4);
        temp2=day;
        day_moon=temp1|temp2;
        }
        code uchar table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正數據表
        void Conver_week(bit c,uchar year,uchar month,uchar day)
        {//c=0 為21世紀,c=1 為19世紀 輸入輸出數據均為BCD數據
        uchar temp1,temp2;
        temp1=year/16; //BCD->hex 先把數據轉換為十六進制
        temp2=year;
        year=temp1*10+temp2;
        temp1=month/16;
        temp2=month;
        month=temp1*10+temp2;
        temp1=day/16;
        temp2=day;
        day=temp1*10+temp2;
        if (c==0){year+=0x64;} //如果為21世紀,年份數加100
        temp1=year/0x4; //所過閏年數只算1900年之后的
        temp2=year+temp1;
        temp2=temp2%0x7; //為節省資源,先進行一次取余,避免數大于0xff,避免使用整型數據
        temp2=temp2+day+table_week[month-1];
        if (year%0x4==0&&month<3)temp2-=1;
        week=temp2%0x7;
        }
        //test
        uchar c_sun,year_sun,month_sun,day_sun;
        void main(){
        c_sun=1;
        year_sun=0x2;
        month_sun=0x11;
        day_sun=0x3;
        Conver_week(c_sun,year_sun,month_sun,day_sun);
        Conversion(c_sun,year_sun,month_sun,day_sun);
        while(1);
        }

        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 林甸县| 侯马市| 红河县| 龙门县| 津南区| 蛟河市| 上犹县| 南靖县| 隆昌县| 平度市| 深水埗区| 三门峡市| 金昌市| 平安县| 长海县| 连云港市| 波密县| 甘德县| 富蕴县| 济南市| 琼海市| 宣威市| 广河县| 临桂县| 莱芜市| 菏泽市| 巴林左旗| 鄂温| 耒阳市| 石屏县| 济阳县| 基隆市| 聊城市| 阿拉善盟| 湾仔区| 天全县| 关岭| 肥东县| 周至县| 西乌珠穆沁旗| 万盛区|