1、单片机阳历转阴历程序经过本人对函数的优化修改,编译后代码比原来少了近300 字节。/*/* 获取当前日期的农历及星期*/*/*=*/*公历年对应的农历数据,每年三字节, 格式: */*第一字节BIT7-4 位表示闰月月份,值为0 为无闰月, */*BIT3-0 对应农历第1-4 月的大小*/*第二字节BIT7-0 对应农历第5-12 月大小, */*第三字节BIT7 表示农历第13 个月大小月份对应的位*/*为1 表示本农历月大(30 天),为0 表示小(29 天) */*第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期*/*类型:Byte:unsigned char
2、 Word:unsigned int */*本代码中Tim0/Tim1/Tim2分别为当前年月日数据*/*其中Tim0Bit7 表示世纪,1 为20 世纪19xx,0 为21 世纪20xx*/*Tim10为周数据/Tim7/Tim8/Tim9为农历数据,根据需要自行更换*/*=*/#include Byte code year_code597=0x04,0xAe,0x53,0x0A,0x57,0x48,0x55,0x26,0xBd, / 1901-19030x0d,0x26,0x50,0x0d,0x95,0x44,0x46,0xAA,0xB9, / 1904-19060x05,0x6A,0x4
3、d,0x09,0xAd,0x42,0x24,0xAe,0xB6, / 1907-19090x04,0xAe,0x4A,0x6A,0x4d,0xBe,0x0A,0x4d,0x52, / 1910-19120x0d,0x25,0x46,0x5d,0x52,0xBA,0x0B,0x54,0x4e, / 1913-19150x0d,0x6A,0x43,0x29,0x6d,0x37,0x09,0x5B,0x4B, / 1916-19180x74,0x9B,0xC1,0x04,0x97,0x54,0x0A,0x4B,0x48, / 1919-19210x5B,0x25,0xBC,0x06,0xA5,0x5
4、0,0x06,0xd4,0x45, / 1922-19240x4A,0xdA,0xB8,0x02,0xB6,0x4d,0x09,0x57,0x42, / 1925-19270x24,0x97,0xB7,0x04,0x97,0x4A,0x66,0x4B,0x3e, / 1928-19300x0d,0x4A,0x51,0x0e,0xA5,0x46,0x56,0xd4,0xBA, / 1931-19330x05,0xAd,0x4e,0x02,0xB6,0x44,0x39,0x37,0x38, / 1934-19360x09,0x2e,0x4B,0x7C,0x96,0xBf,0x0C,0x95,0x5
5、3, / 1937-19390x0d,0x4A,0x48,0x6d,0xA5,0x3B,0x0B,0x55,0x4f, / 1940-19420x05,0x6A,0x45,0x4A,0xAd,0xB9,0x02,0x5d,0x4d, / 1943-19450x09,0x2d,0x42,0x2C,0x95,0xB6,0x0A,0x95,0x4A, / 1946-19480x7B,0x4A,0xBd,0x06,0xCA,0x51,0x0B,0x55,0x46, / 1949-19510x55,0x5A,0xBB,0x04,0xdA,0x4e,0x0A,0x5B,0x43, / 1952-19540
6、x35,0x2B,0xB8,0x05,0x2B,0x4C,0x8A,0x95,0x3f, / 1955-19570x0e,0x95,0x52,0x06,0xAA,0x48,0x7A,0xd5,0x3C, / 1958-19600x0A,0xB5,0x4f,0x04,0xB6,0x45,0x4A,0x57,0x39, / 1961-19630x0A,0x57,0x4d,0x05,0x26,0x42,0x3e,0x93,0x35, / 1964-19660x0d,0x95,0x49,0x75,0xAA,0xBe,0x05,0x6A,0x51, / 1967-19690x09,0x6d,0x46,0
7、x54,0xAe,0xBB,0x04,0xAd,0x4f, / 1970-19720x0A,0x4d,0x43,0x4d,0x26,0xB7,0x0d,0x25,0x4B, / 1973-19750x8d,0x52,0xBf,0x0B,0x54,0x52,0x0B,0x6A,0x47, / 1976-19780x69,0x6d,0x3C,0x09,0x5B,0x50,0x04,0x9B,0x45, / 1979-19810x4A,0x4B,0xB9,0x0A,0x4B,0x4d,0xAB,0x25,0xC2, / 1982-19840x06,0xA5,0x54,0x06,0xd4,0x49,0
8、x6A,0xdA,0x3d, / 1985-19870x0A,0xB6,0x51,0x09,0x37,0x46,0x54,0x97,0xBB, / 1988-19900x04,0x97,0x4f,0x06,0x4B,0x44,0x36,0xA5,0x37, / 1991-19930x0e,0xA5,0x4A,0x86,0xB2,0xBf,0x05,0xAC,0x53, / 1994-19960x0A,0xB6,0x47,0x59,0x36,0xBC,0x09,0x2e,0x50, / 1997-19990x0C,0x96,0x45,0x4d,0x4A,0xB8,0x0d,0x4A,0x4C,
9、/ 2000-20020x0d,0xA5,0x41,0x25,0xAA,0xB6,0x05,0x6A,0x49, / 2003-20050x7A,0xAd,0xBd,0x02,0x5d,0x52,0x09,0x2d,0x47, / 2006-20080x5C,0x95,0xBA,0x0A,0x95,0x4e,0x0B,0x4A,0x43, / 2009-20110x4B,0x55,0x37,0x0A,0xd5,0x4A,0x95,0x5A,0xBf, / 2012-20140x04,0xBA,0x53,0x0A,0x5B,0x48,0x65,0x2B,0xBC, / 2015-20170x05
10、,0x2B,0x50,0x0A,0x93,0x45,0x47,0x4A,0xB9, / 2018-20200x06,0xAA,0x4C,0x0A,0xd5,0x41,0x24,0xdA,0xB6, / 2021-20230x04,0xB6,0x4A,0x69,0x57,0x3d,0x0A,0x4e,0x51, / 2024-20260x0d,0x26,0x46,0x5e,0x93,0x3A,0x0d,0x53,0x4d, / 2027-20290x05,0xAA,0x43,0x36,0xB5,0x37,0x09,0x6d,0x4B, / 2030-20320xB4,0xAe,0xBf,0x04
11、,0xAd,0x53,0x0A,0x4d,0x48, / 2033-20350x6d,0x25,0xBC,0x0d,0x25,0x4f,0x0d,0x52,0x44, / 2036-20380x5d,0xAA,0x38,0x0B,0x5A,0x4C,0x05,0x6d,0x41, / 2039-20410x24,0xAd,0xB6,0x04,0x9B,0x4A,0x7A,0x4B,0xBe, / 2042-20440x0A,0x4B,0x51,0x0A,0xA5,0x46,0x5B,0x52,0xBA, / 2045-20470x06,0xd2,0x4e,0x0A,0xdA,0x42,0x35
12、,0x5B,0x37, / 2048-20500x09,0x37,0x4B,0x84,0x97,0xC1,0x04,0x97,0x53, / 2051-20530x06,0x4B,0x48,0x66,0xA5,0x3C,0x0e,0xA5,0x4f, / 2054-20560x06,0xB2,0x44,0x4A,0xB6,0x38,0x0A,0xAe,0x4C, / 2057-20590x09,0x2e,0x42,0x3C,0x97,0x35,0x0C,0x96,0x49, / 2060-20620x7d,0x4A,0xBd,0x0d,0x4A,0x51,0x0d,0xA5,0x45, / 2
13、063-20650x55,0xAA,0xBA,0x05,0x6A,0x4e,0x0A,0x6d,0x43, / 2066-20680x45,0x2e,0xB7,0x05,0x2d,0x4B,0x8A,0x95,0xBf, / 2069-20710x0A,0x95,0x53,0x0B,0x4A,0x47,0x6B,0x55,0x3B, / 2072-20740x0A,0xd5,0x4f,0x05,0x5A,0x45,0x4A,0x5d,0x38, / 2075-20770x0A,0x5B,0x4C,0x05,0x2B,0x42,0x3A,0x93,0xB6, / 2078-20800x06,0x
14、93,0x49,0x77,0x29,0xBd,0x06,0xAA,0x51, / 2081-20830x0A,0xd5,0x46,0x54,0xdA,0xBA,0x04,0xB6,0x4e, / 2084-20860x0A,0x57,0x43,0x45,0x27,0x38,0x0d,0x26,0x4A, / 2087-20890x8e,0x93,0x3e,0x0d,0x52,0x52,0x0d,0xAA,0x47, / 2090-20920x66,0xB5,0x3B,0x05,0x6d,0x4f,0x04,0xAe,0x45, / 2093-20950x4A,0x4e,0xB9,0x0A,0x
15、4d,0x4C,0x0d,0x15,0x41, / 2096-20980x2d,0x92,0xB5, / 2099;/月份天数数据表Byte code day_code19=0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3;Word code day_code23=0x111,0x130,0x14e;/月修正数据表Byte code week_code12=0,3,3,6,1,4,6,2,5,0,3,5;void Conver_week(void)/*=获取当前日期的星期=*/Byte temp1,temp2,year,month,day;year =Ti
16、m0&0x7f; /加载年月日数据,如为BCD 则需转为十进制month=Tim1;day =Tim2;if(Tim07=0)year+=0x64; /如果为21 世纪,年份数加100temp1=year/0x4; /所过闰年数只算1900 年之后的temp2=(year+temp1)%0x7; /为节省资源,先进行一次取余,避免数大于0xfftemp2=temp2+day+week_codemonth-1;if(year%0x4=0&month(month_p+3)%8);temp=year_codecode_addr+temp1&temp;if(temp=0)return(0);elser
17、eturn(1);void Conversion(void)/*=转换当前农历信息=*/Byte year,month,day;Byte temp1,temp2,temp3,month_p;Word temp4,code_addr;bit flag2,flag_y;year =Tim0&0x7f; /加载年月日数据,如为BCD 则需转为十进制month=Tim1;day =Tim2;code_addr=year-1; /定位数据表地址if(Tim07=0)code_addr+=0x64;code_addr*=3;temp1=year_codecode_addr+2&0x60; /取当年春节所在
18、的公历月份temp1=_cror_(temp1,5);temp2=year_codecode_addr+2&0x1f; /取当年春节所在的公历日temp3=temp2-1; /计算当年春节离当年元旦的天数,春节只会在公历1 月或2 月if(temp1!=1)temp3+=0x1f;if(month10)temp4=day_code1month-1+day;elsetemp4=day_code2month-10+day;if(month=temp3) /判断公历日在春节前还是春节后/公历日在春节后或就是春节当日使用下面代码进行运算temp4 -=temp3;month = 1;flag_y =
19、0;month_p= 1; /month_p 为月份指向,公历日在春节前或就是春节当日month_p 指向首月flag2=get_moon_day(month_p,code_addr); /检查该农历月为大小还是小月,大月返回1,小月返回0if(flag2=0)temp1=29;elsetemp1=30; /小月29 天、大月30 天temp2=year_codecode_addr&0xf0;temp2=_cror_(temp2,4); /从数据表中取该年的闰月月份,如为0 则该年无闰月while(temp4=temp1)temp4-=temp1;month_p+=1;if(month=tem
20、p2)flag_y=flag_y;if(flag_y=0)month+=1;else month+=1;flag2=get_moon_day(month_p,code_addr);if(flag2=0)temp1=0x1d;else temp1=0x1e;day=temp4+1;else/公历日在春节前使用下面代码进行运算temp3-=temp4;if(year=0)year=0xe3;elseyear-=1;code_addr-=3;month = 0xc;temp2 = year_codecode_addr&0xf0;temp2 = _cror_(temp2,4);flag_y = 0;i
21、f(temp2=0)month_p=12;elsemonth_p=13; /如果当年有闰月,一年有十三个月,月指向13,无闰月指向12flag2=get_moon_day(month_p,code_addr);if(flag2=0)temp1=29;elsetemp1=30;while(temp3temp1)temp3-=temp1;month_p-=1;if(flag_y=0)month-=1;if(month=temp2)flag_y=flag_y;flag2=get_moon_day(month_p,code_addr);if(flag2=0)temp1=0x1d;elsetemp1=0x1e;day=temp1-temp3+1;Tim7 = year|(Tim0&0x80); /将农历信息写进指定变量Tim8 = month;Tim9 = day;Conver_week(); /最后进行星期转换(根据需要自行选用)此主题相关图片如下:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1