找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

C51语言“公历转农历及星期”

[复制链接]
admin 发表于 2010-5-3 16:43:02 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
现在发一份C51的公历转农历及星期源代码,来源于网上下载,作者不详。经过本人对函数的优化修改,编译后代码比原来少了近300字节。
  1. /****************************************************/
  2. /* 获取当前日期的农历及星期 */
  3. /****************************************************/

  4. /*==================================================*/
  5. /*公历年对应的农历数据,每年三字节, 格式: */
  6. /*第一字节BIT7-4 位表示闰月月份,值为0 为无闰月, */
  7. /*BIT3-0 对应农历第1-4 月的大小 */
  8. /*第二字节BIT7-0 对应农历第5-12 月大小, */
  9. /*第三字节BIT7 表示农历第13个月大小月份对应的位 */
  10. /*为1表示本农历月大(30 天),为0 表示小(29 天) */
  11. /*第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期 */
  12. /*类型:Byte:unsigned char Word:unsigned int */
  13. /*本代码中Tim[0]/Tim[1]/Tim[2]分别为当前年月日数据 */
  14. /*其中Tim[0]Bit7表示世纪,1为20世纪19xx,0为21世纪20xx*/
  15. /*Tim[10]为周数据/Tim[7]/Tim[8]/Tim[9]为农历数据,根据需要自行更换*/
  16. /*==================================================*/

  17. #include <intrins.h>

  18. Byte code year_code[597]={
  19. 0x04,0xAe,0x53,0x0A,0x57,0x48,0x55,0x26,0xBd, // 1901-1903
  20. 0x0d,0x26,0x50,0x0d,0x95,0x44,0x46,0xAA,0xB9, // 1904-1906
  21. 0x05,0x6A,0x4d,0x09,0xAd,0x42,0x24,0xAe,0xB6, // 1907-1909
  22. 0x04,0xAe,0x4A,0x6A,0x4d,0xBe,0x0A,0x4d,0x52, // 1910-1912
  23. 0x0d,0x25,0x46,0x5d,0x52,0xBA,0x0B,0x54,0x4e, // 1913-1915
  24. 0x0d,0x6A,0x43,0x29,0x6d,0x37,0x09,0x5B,0x4B, // 1916-1918
  25. 0x74,0x9B,0xC1,0x04,0x97,0x54,0x0A,0x4B,0x48, // 1919-1921
  26. 0x5B,0x25,0xBC,0x06,0xA5,0x50,0x06,0xd4,0x45, // 1922-1924
  27. 0x4A,0xdA,0xB8,0x02,0xB6,0x4d,0x09,0x57,0x42, // 1925-1927
  28. 0x24,0x97,0xB7,0x04,0x97,0x4A,0x66,0x4B,0x3e, // 1928-1930
  29. 0x0d,0x4A,0x51,0x0e,0xA5,0x46,0x56,0xd4,0xBA, // 1931-1933
  30. 0x05,0xAd,0x4e,0x02,0xB6,0x44,0x39,0x37,0x38, // 1934-1936
  31. 0x09,0x2e,0x4B,0x7C,0x96,0xBf,0x0C,0x95,0x53, // 1937-1939
  32. 0x0d,0x4A,0x48,0x6d,0xA5,0x3B,0x0B,0x55,0x4f, // 1940-1942
  33. 0x05,0x6A,0x45,0x4A,0xAd,0xB9,0x02,0x5d,0x4d, // 1943-1945
  34. 0x09,0x2d,0x42,0x2C,0x95,0xB6,0x0A,0x95,0x4A, // 1946-1948
  35. 0x7B,0x4A,0xBd,0x06,0xCA,0x51,0x0B,0x55,0x46, // 1949-1951
  36. 0x55,0x5A,0xBB,0x04,0xdA,0x4e,0x0A,0x5B,0x43, // 1952-1954
  37. 0x35,0x2B,0xB8,0x05,0x2B,0x4C,0x8A,0x95,0x3f, // 1955-1957
  38. 0x0e,0x95,0x52,0x06,0xAA,0x48,0x7A,0xd5,0x3C, // 1958-1960
  39. 0x0A,0xB5,0x4f,0x04,0xB6,0x45,0x4A,0x57,0x39, // 1961-1963
  40. 0x0A,0x57,0x4d,0x05,0x26,0x42,0x3e,0x93,0x35, // 1964-1966
  41. 0x0d,0x95,0x49,0x75,0xAA,0xBe,0x05,0x6A,0x51, // 1967-1969
  42. 0x09,0x6d,0x46,0x54,0xAe,0xBB,0x04,0xAd,0x4f, // 1970-1972
  43. 0x0A,0x4d,0x43,0x4d,0x26,0xB7,0x0d,0x25,0x4B, // 1973-1975
  44. 0x8d,0x52,0xBf,0x0B,0x54,0x52,0x0B,0x6A,0x47, // 1976-1978
  45. 0x69,0x6d,0x3C,0x09,0x5B,0x50,0x04,0x9B,0x45, // 1979-1981
  46. 0x4A,0x4B,0xB9,0x0A,0x4B,0x4d,0xAB,0x25,0xC2, // 1982-1984
  47. 0x06,0xA5,0x54,0x06,0xd4,0x49,0x6A,0xdA,0x3d, // 1985-1987
  48. 0x0A,0xB6,0x51,0x09,0x37,0x46,0x54,0x97,0xBB, // 1988-1990
  49. 0x04,0x97,0x4f,0x06,0x4B,0x44,0x36,0xA5,0x37, // 1991-1993
  50. 0x0e,0xA5,0x4A,0x86,0xB2,0xBf,0x05,0xAC,0x53, // 1994-1996
  51. 0x0A,0xB6,0x47,0x59,0x36,0xBC,0x09,0x2e,0x50, // 1997-1999
  52. 0x0C,0x96,0x45,0x4d,0x4A,0xB8,0x0d,0x4A,0x4C, // 2000-2002
  53. 0x0d,0xA5,0x41,0x25,0xAA,0xB6,0x05,0x6A,0x49, // 2003-2005
  54. 0x7A,0xAd,0xBd,0x02,0x5d,0x52,0x09,0x2d,0x47, // 2006-2008
  55. 0x5C,0x95,0xBA,0x0A,0x95,0x4e,0x0B,0x4A,0x43, // 2009-2011
  56. 0x4B,0x55,0x37,0x0A,0xd5,0x4A,0x95,0x5A,0xBf, // 2012-2014
  57. 0x04,0xBA,0x53,0x0A,0x5B,0x48,0x65,0x2B,0xBC, // 2015-2017
  58. 0x05,0x2B,0x50,0x0A,0x93,0x45,0x47,0x4A,0xB9, // 2018-2020
  59. 0x06,0xAA,0x4C,0x0A,0xd5,0x41,0x24,0xdA,0xB6, // 2021-2023
  60. 0x04,0xB6,0x4A,0x69,0x57,0x3d,0x0A,0x4e,0x51, // 2024-2026
  61. 0x0d,0x26,0x46,0x5e,0x93,0x3A,0x0d,0x53,0x4d, // 2027-2029
  62. 0x05,0xAA,0x43,0x36,0xB5,0x37,0x09,0x6d,0x4B, // 2030-2032
  63. 0xB4,0xAe,0xBf,0x04,0xAd,0x53,0x0A,0x4d,0x48, // 2033-2035
  64. 0x6d,0x25,0xBC,0x0d,0x25,0x4f,0x0d,0x52,0x44, // 2036-2038
  65. 0x5d,0xAA,0x38,0x0B,0x5A,0x4C,0x05,0x6d,0x41, // 2039-2041
  66. 0x24,0xAd,0xB6,0x04,0x9B,0x4A,0x7A,0x4B,0xBe, // 2042-2044
  67. 0x0A,0x4B,0x51,0x0A,0xA5,0x46,0x5B,0x52,0xBA, // 2045-2047
  68. 0x06,0xd2,0x4e,0x0A,0xdA,0x42,0x35,0x5B,0x37, // 2048-2050
  69. 0x09,0x37,0x4B,0x84,0x97,0xC1,0x04,0x97,0x53, // 2051-2053
  70. 0x06,0x4B,0x48,0x66,0xA5,0x3C,0x0e,0xA5,0x4f, // 2054-2056
  71. 0x06,0xB2,0x44,0x4A,0xB6,0x38,0x0A,0xAe,0x4C, // 2057-2059
  72. 0x09,0x2e,0x42,0x3C,0x97,0x35,0x0C,0x96,0x49, // 2060-2062
  73. 0x7d,0x4A,0xBd,0x0d,0x4A,0x51,0x0d,0xA5,0x45, // 2063-2065
  74. 0x55,0xAA,0xBA,0x05,0x6A,0x4e,0x0A,0x6d,0x43, // 2066-2068
  75. 0x45,0x2e,0xB7,0x05,0x2d,0x4B,0x8A,0x95,0xBf, // 2069-2071
  76. 0x0A,0x95,0x53,0x0B,0x4A,0x47,0x6B,0x55,0x3B, // 2072-2074
  77. 0x0A,0xd5,0x4f,0x05,0x5A,0x45,0x4A,0x5d,0x38, // 2075-2077
  78. 0x0A,0x5B,0x4C,0x05,0x2B,0x42,0x3A,0x93,0xB6, // 2078-2080
  79. 0x06,0x93,0x49,0x77,0x29,0xBd,0x06,0xAA,0x51, // 2081-2083
  80. 0x0A,0xd5,0x46,0x54,0xdA,0xBA,0x04,0xB6,0x4e, // 2084-2086
  81. 0x0A,0x57,0x43,0x45,0x27,0x38,0x0d,0x26,0x4A, // 2087-2089
  82. 0x8e,0x93,0x3e,0x0d,0x52,0x52,0x0d,0xAA,0x47, // 2090-2092
  83. 0x66,0xB5,0x3B,0x05,0x6d,0x4f,0x04,0xAe,0x45, // 2093-2095
  84. 0x4A,0x4e,0xB9,0x0A,0x4d,0x4C,0x0d,0x15,0x41, // 2096-2098
  85. 0x2d,0x92,0xB5, // 2099
  86. };
  87. //月份天数数据表
  88. Byte code day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};
  89. Word code day_code2[3]={0x111,0x130,0x14e};
  90. //月修正数据表
  91. Byte code week_code[12]={0,3,3,6,1,4,6,2,5,0,3,5};

  92. void Conver_week(void)
  93. /*=======获取当前日期的星期====================================*/
  94. {
  95. Byte temp1,temp2,year,month,day;

  96. year =Tim[0]&0x7f; //加载年月日数据,如为BCD则需转为十进制
  97. month=Tim[1];
  98. day =Tim[2];

  99. if(Tim[0]>>7==0){year+=0x64;} //如果为21世纪,年份数加100
  100. temp1=year/0x4; //所过闰年数只算1900年之后的
  101. temp2=(year+temp1)%0x7; //为节省资源,先进行一次取余,避免数大于0xff
  102. temp2=temp2+day+week_code[month-1];
  103. if(year%0x4==0&&month<3)temp2-=1; //如果是闰年但不到3月则减一天
  104. Tim[10]=temp2%0x7; //将星期数据写到指定星期变量Tim[9]
  105. }

  106. bit get_moon_day(Byte month_p,Word code_addr)
  107. /*读取数据表中农历月的大月或小月,如果该月大返回1,小返回0*/
  108. {
  109. Byte temp,temp1;
  110. temp1=(month_p+3)/8;
  111. temp=0x80>>((month_p+3)%8);
  112. temp=year_code[code_addr+temp1]&temp;
  113. if(temp==0){return(0);}else{return(1);}
  114. }

  115. void Conversion(void)
  116. /*==转换当前农历信息====================================*/
  117. {
  118. Byte year,month,day;
  119. Byte temp1,temp2,temp3,month_p;
  120. Word temp4,code_addr;
  121. bit flag2,flag_y;

  122. year =Tim[0]&0x7f; //加载年月日数据,如为BCD则需转为十进制
  123. month=Tim[1];
  124. day =Tim[2];

  125. code_addr=year-1; //定位数据表地址
  126. if(Tim[0]>>7==0)code_addr+=0x64;
  127. code_addr*=3;

  128. temp1=year_code[code_addr+2]&0x60; //取当年春节所在的公历月份
  129. temp1=_cror_(temp1,5);

  130. temp2=year_code[code_addr+2]&0x1f; //取当年春节所在的公历日

  131. temp3=temp2-1; //计算当年春节离当年元旦的天数,春节只会在公历1月或2月
  132. if(temp1!=1)temp3+=0x1f;
  133. if(month<10){temp4=day_code1[month-1]+day;}
  134. else{temp4=day_code2[month-10]+day;}
  135. if((month<2)||(year%0x04!=0))temp4-=1; //如果公历月小于2月或者该年的2月非闰月,天数减1

  136. if (temp4>=temp3) //判断公历日在春节前还是春节后
  137. {
  138. //公历日在春节后或就是春节当日使用下面代码进行运算
  139. temp4 -=temp3;
  140. month = 1;
  141. flag_y = 0;
  142. month_p= 1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月

  143. flag2=get_moon_day(month_p,code_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0
  144. if(flag2==0){temp1=29;}else{temp1=30;} //小月29天、大月30天

  145. temp2=year_code[code_addr]&0xf0;
  146. temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0则该年无闰月
  147. while(temp4>=temp1)
  148. {
  149. temp4-=temp1;
  150. month_p+=1;
  151. if(month==temp2)
  152. {
  153. flag_y=~flag_y;
  154. if(flag_y==0)month+=1;
  155. }
  156. else month+=1;
  157. flag2=get_moon_day(month_p,code_addr);
  158. if(flag2==0)temp1=0x1d;
  159. else temp1=0x1e;
  160. }
  161. day=temp4+1;
  162. }
  163. else
  164. {
  165. //公历日在春节前使用下面代码进行运算
  166. temp3-=temp4;
  167. if(year==0){year=0xe3;}else{year-=1;}
  168. code_addr-=3;
  169. month = 0xc;
  170. temp2 = year_code[code_addr]&0xf0;
  171. temp2 = _cror_(temp2,4);
  172. flag_y = 0;
  173. if(temp2==0){month_p=12;}else{month_p=13;} //如果当年有闰月,一年有十三个月,月指向13,无闰月指向12

  174. flag2=get_moon_day(month_p,code_addr);
  175. if(flag2==0){temp1=29;}else{temp1=30;}

  176. while(temp3>temp1)
  177. {
  178. temp3-=temp1;
  179. month_p-=1;
  180. if(flag_y==0)month-=1;
  181. if(month==temp2)flag_y=~flag_y;
  182. flag2=get_moon_day(month_p,code_addr);
  183. if(flag2==0){temp1=0x1d;}else{temp1=0x1e;}
  184. }
  185. day=temp1-temp3+1;
  186. }
  187. Tim[7] = year|(Tim[0]&0x80);; //将农历信息写进指定变量
  188. Tim[8] = month;
  189. Tim[9] = day;
  190. Conver_week(); //最后进行星期转换(根据需要自行选用)
  191. }

复制代码
再次优化,编译后代码又少了近50字节。同时修改一个Bug:原先计算天数在闰年并且月份大于2月(>2)时天数加1。反向条件后应该是非闰年或者月份小于等于2月(<=2)。结果少了等于(只<2)。这样造成闰年时且当前月为2月时农历会比较实际的多出一天,在此修正。
  1. /****************************************************/
  2. /* 获取当前日期的农历及星期 */
  3. /****************************************************/

  4. /*==================================================*/
  5. /*公历年对应的农历数据,每年三字节, 格式: */
  6. /*第一字节BIT7-4 位表示闰月月份,值为0 为无闰月, */
  7. /*BIT3-0 对应农历第1-4 月的大小 */
  8. /*第二字节BIT7-0 对应农历第5-12 月大小, */
  9. /*第三字节BIT7 表示农历第13个月大小月份对应的位 */
  10. /*为1表示本农历月大(30 天),为0 表示小(29 天) */
  11. /*第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期 */
  12. /*类型:Byte:unsigned char Word:unsigned int */
  13. /*本代码中Tim[0]/Tim[1]/Tim[2]分别为当前年月日数据 */
  14. /*其中Tim[0]Bit7表示世纪,1为20世纪19xx,0为21世纪20xx*/
  15. /*Tim[10]为周数据/Tim[7]/Tim[8]/Tim[9]为农历数据,根据需要自行更换*/
  16. /*==================================================*/

  17. Byte code year_code[597]={
  18. 0x04,0xAe,0x53,0x0A,0x57,0x48,0x55,0x26,0xBd, // 1901-1903 0
  19. 0x0d,0x26,0x50,0x0d,0x95,0x44,0x46,0xAA,0xB9, // 1904-1906 9
  20. 0x05,0x6A,0x4d,0x09,0xAd,0x42,0x24,0xAe,0xB6, // 1907-1909 18
  21. 0x04,0xAe,0x4A,0x6A,0x4d,0xBe,0x0A,0x4d,0x52, // 1910-1912 27
  22. 0x0d,0x25,0x46,0x5d,0x52,0xBA,0x0B,0x54,0x4e, // 1913-1915 36
  23. 0x0d,0x6A,0x43,0x29,0x6d,0x37,0x09,0x5B,0x4B, // 1916-1918 45
  24. 0x74,0x9B,0xC1,0x04,0x97,0x54,0x0A,0x4B,0x48, // 1919-1921 54
  25. 0x5B,0x25,0xBC,0x06,0xA5,0x50,0x06,0xd4,0x45, // 1922-1924 63
  26. 0x4A,0xdA,0xB8,0x02,0xB6,0x4d,0x09,0x57,0x42, // 1925-1927 72
  27. 0x24,0x97,0xB7,0x04,0x97,0x4A,0x66,0x4B,0x3e, // 1928-1930 81
  28. 0x0d,0x4A,0x51,0x0e,0xA5,0x46,0x56,0xd4,0xBA, // 1931-1933 90
  29. 0x05,0xAd,0x4e,0x02,0xB6,0x44,0x39,0x37,0x38, // 1934-1936 99
  30. 0x09,0x2e,0x4B,0x7C,0x96,0xBf,0x0C,0x95,0x53, // 1937-1939 108
  31. 0x0d,0x4A,0x48,0x6d,0xA5,0x3B,0x0B,0x55,0x4f, // 1940-1942 117
  32. 0x05,0x6A,0x45,0x4A,0xAd,0xB9,0x02,0x5d,0x4d, // 1943-1945 126
  33. 0x09,0x2d,0x42,0x2C,0x95,0xB6,0x0A,0x95,0x4A, // 1946-1948 135
  34. 0x7B,0x4A,0xBd,0x06,0xCA,0x51,0x0B,0x55,0x46, // 1949-1951 144
  35. 0x55,0x5A,0xBB,0x04,0xdA,0x4e,0x0A,0x5B,0x43, // 1952-1954 153
  36. 0x35,0x2B,0xB8,0x05,0x2B,0x4C,0x8A,0x95,0x3f, // 1955-1957 162
  37. 0x0e,0x95,0x52,0x06,0xAA,0x48,0x7A,0xd5,0x3C, // 1958-1960 171
  38. 0x0A,0xB5,0x4f,0x04,0xB6,0x45,0x4A,0x57,0x39, // 1961-1963 180
  39. 0x0A,0x57,0x4d,0x05,0x26,0x42,0x3e,0x93,0x35, // 1964-1966 189
  40. 0x0d,0x95,0x49,0x75,0xAA,0xBe,0x05,0x6A,0x51, // 1967-1969 195
  41. 0x09,0x6d,0x46,0x54,0xAe,0xBB,0x04,0xAd,0x4f, // 1970-1972 207
  42. 0x0A,0x4d,0x43,0x4d,0x26,0xB7,0x0d,0x25,0x4B, // 1973-1975 216
  43. 0x8d,0x52,0xBf,0x0B,0x54,0x52,0x0B,0x6A,0x47, // 1976-1978 225
  44. 0x69,0x6d,0x3C,0x09,0x5B,0x50,0x04,0x9B,0x45, // 1979-1981 234
  45. 0x4A,0x4B,0xB9,0x0A,0x4B,0x4d,0xAB,0x25,0xC2, // 1982-1984 243
  46. 0x06,0xA5,0x54,0x06,0xd4,0x49,0x6A,0xdA,0x3d, // 1985-1987 252
  47. 0x0A,0xB6,0x51,0x09,0x37,0x46,0x54,0x97,0xBB, // 1988-1990 261
  48. 0x04,0x97,0x4f,0x06,0x4B,0x44,0x36,0xA5,0x37, // 1991-1993 270
  49. 0x0e,0xA5,0x4A,0x86,0xB2,0xBf,0x05,0xAC,0x53, // 1994-1996 279
  50. 0x0A,0xB6,0x47,0x59,0x36,0xBC,0x09,0x2e,0x50, // 1997-1999 288
  51. 0x0C,0x96,0x45,0x4d,0x4A,0xB8,0x0d,0x4A,0x4C, // 2000-2002 297
  52. 0x0d,0xA5,0x41,0x25,0xAA,0xB6,0x05,0x6A,0x49, // 2003-2005 306
  53. 0x7A,0xAd,0xBd,0x02,0x5d,0x52,0x09,0x2d,0x47, // 2006-2008 315
  54. 0x5C,0x95,0xBA,0x0A,0x95,0x4e,0x0B,0x4A,0x43, // 2009-2011 324
  55. 0x4B,0x55,0x37,0x0A,0xd5,0x4A,0x95,0x5A,0xBf, // 2012-2014 333
  56. 0x04,0xBA,0x53,0x0A,0x5B,0x48,0x65,0x2B,0xBC, // 2015-2017 342
  57. 0x05,0x2B,0x50,0x0A,0x93,0x45,0x47,0x4A,0xB9, // 2018-2020 351
  58. 0x06,0xAA,0x4C,0x0A,0xd5,0x41,0x24,0xdA,0xB6, // 2021-2023 360
  59. 0x04,0xB6,0x4A,0x69,0x57,0x3d,0x0A,0x4e,0x51, // 2024-2026 369
  60. 0x0d,0x26,0x46,0x5e,0x93,0x3A,0x0d,0x53,0x4d, // 2027-2029 378
  61. 0x05,0xAA,0x43,0x36,0xB5,0x37,0x09,0x6d,0x4B, // 2030-2032 387
  62. 0xB4,0xAe,0xBf,0x04,0xAd,0x53,0x0A,0x4d,0x48, // 2033-2035 396
  63. 0x6d,0x25,0xBC,0x0d,0x25,0x4f,0x0d,0x52,0x44, // 2036-2038 405
  64. 0x5d,0xAA,0x38,0x0B,0x5A,0x4C,0x05,0x6d,0x41, // 2039-2041 414
  65. 0x24,0xAd,0xB6,0x04,0x9B,0x4A,0x7A,0x4B,0xBe, // 2042-2044 423
  66. 0x0A,0x4B,0x51,0x0A,0xA5,0x46,0x5B,0x52,0xBA, // 2045-2047 432
  67. 0x06,0xd2,0x4e,0x0A,0xdA,0x42,0x35,0x5B,0x37, // 2048-2050 441
  68. 0x09,0x37,0x4B,0x84,0x97,0xC1,0x04,0x97,0x53, // 2051-2053 450
  69. 0x06,0x4B,0x48,0x66,0xA5,0x3C,0x0e,0xA5,0x4f, // 2054-2056 459
  70. 0x06,0xB2,0x44,0x4A,0xB6,0x38,0x0A,0xAe,0x4C, // 2057-2059 468
  71. 0x09,0x2e,0x42,0x3C,0x97,0x35,0x0C,0x96,0x49, // 2060-2062 477
  72. 0x7d,0x4A,0xBd,0x0d,0x4A,0x51,0x0d,0xA5,0x45, // 2063-2065 486
  73. 0x55,0xAA,0xBA,0x05,0x6A,0x4e,0x0A,0x6d,0x43, // 2066-2068 495
  74. 0x45,0x2e,0xB7,0x05,0x2d,0x4B,0x8A,0x95,0xBf, // 2069-2071 504
  75. 0x0A,0x95,0x53,0x0B,0x4A,0x47,0x6B,0x55,0x3B, // 2072-2074 513
  76. 0x0A,0xd5,0x4f,0x05,0x5A,0x45,0x4A,0x5d,0x38, // 2075-2077 522
  77. 0x0A,0x5B,0x4C,0x05,0x2B,0x42,0x3A,0x93,0xB6, // 2078-2080 531
  78. 0x06,0x93,0x49,0x77,0x29,0xBd,0x06,0xAA,0x51, // 2081-2083 540
  79. 0x0A,0xd5,0x46,0x54,0xdA,0xBA,0x04,0xB6,0x4e, // 2084-2086 549
  80. 0x0A,0x57,0x43,0x45,0x27,0x38,0x0d,0x26,0x4A, // 2087-2089 558
  81. 0x8e,0x93,0x3e,0x0d,0x52,0x52,0x0d,0xAA,0x47, // 2090-2092 567
  82. 0x66,0xB5,0x3B,0x05,0x6d,0x4f,0x04,0xAe,0x45, // 2093-2095 576
  83. 0x4A,0x4e,0xB9,0x0A,0x4d,0x4C,0x0d,0x15,0x41, // 2096-2098 585
  84. 0x2d,0x92,0xB5, // 2099 594
  85. };
  86. //月份天数数据表
  87. Byte code day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};
  88. Word code day_code2[3]={0x111,0x130,0x14e};
  89. //月修正数据表
  90. Byte code week_code[12]={0,3,3,6,1,4,6,2,5,0,3,5};

  91. void Conver_week(void)
  92. /*=======获取当前日期的星期====================================*/
  93. {
  94. Byte temp1,temp2,year,month,day;

  95. year =Tim[0]&0x7f; //加载年月日数据,如为BCD则需转为十进制
  96. month=Tim[1];
  97. day =Tim[2];

  98. if(Tim[0]>>7==0){year+=0x64;} //如果为21世纪,年份数加100
  99. temp1=year/0x4; //所过闰年数只算1900年之后的
  100. temp2=(year+temp1)%0x7; //为节省资源,先进行一次取余,避免数大于0xff
  101. temp2=temp2+day+week_code[month-1];
  102. if(year%0x4==0&&month<3)temp2-=1; //如果是闰年但不到3月则减一天
  103. Tim[10]=temp2%0x7; //将星期数据写到指定星期变量Tim[9]
  104. }

  105. Byte get_moon_day(Byte month_p,Word code_addr)
  106. /*读取数据表中农历月的大月或小月,如果该月大返回1,小返回0*/
  107. {
  108. Byte temp,temp5;

  109. temp=0x80>>((month_p+3)%8);
  110. temp5=(month_p+3)/8;
  111. temp=year_code[code_addr+temp5]&temp;
  112. if(temp==0){return(29);}else{return(30);}
  113. }

  114. void Conversion(void)
  115. /*==转换当前农历信息====================================*/
  116. { Byte year,month,day;
  117. Byte temp1,temp2,temp3,month_p;
  118. Word temp4,code_addr;
  119. bit flag_y;

  120. year =Tim[0]&0x7f; //加载年月日数据,如为BCD则需转为十进制
  121. month=Tim[1];
  122. day =Tim[2];

  123. code_addr=year-1; //定位数据表地址
  124. if(Tim[0]>>7==0)code_addr+=100;
  125. code_addr*=3;

  126. temp1=(year_code[code_addr+2]&0x60)>>5; //取当年春节所在的公历月份
  127. temp2=year_code[code_addr+2]&0x1f; //取当年春节所在的公历日
  128. temp3=temp2-1; //计算当年春节离当年元旦的天数,春节只会在公历1月或2月

  129. if(temp1!=1)temp3+=31; //如果不在1月则天数加上31天(1月)
  130. if(month<10){temp4=day_code1[month-1]+day;}
  131. else{temp4=day_code2[month-10]+day;}
  132. if((month<=2)||(year%0x04!=0))temp4-=1; //如果公历月小于等于2月或者该年的2月非闰月,天数减1

  133. temp2=(year_code[code_addr]&0xf0)>>4; //从数据表中取该年的闰月月份,如为0则该年无闰月

  134. if (temp4>=temp3) //判断公历日在春节前还是春节后
  135. {
  136. //公历日在春节后或就是春节当日使用下面代码进行运算
  137. temp4 -=temp3;
  138. month = 1;
  139. flag_y = 0;
  140. month_p= 1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月
  141. temp1=get_moon_day(month_p,code_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0
  142. while(temp4>=temp1)
  143. {
  144. temp4-=temp1;
  145. month_p+=1;
  146. if(month==temp2)
  147. {
  148. flag_y=~flag_y;
  149. if(flag_y==0)month+=1;
  150. }else{month+=1;}
  151. temp1=get_moon_day(month_p,code_addr);
  152. }
  153. day=temp4+1;
  154. }
  155. else
  156. {
  157. //公历日在春节前使用下面代码进行运算
  158. temp3-=temp4;
  159. if(year==0){year=0xe3;}else{year-=1;}
  160. code_addr-=3;
  161. month = 12;
  162. flag_y = 0;
  163. if(temp2==0){month_p=12;}else{month_p=13;} //如果当年有闰月,一年有十三个月,月指向13,无闰月指向12
  164. temp1=get_moon_day(month_p,code_addr);
  165. while(temp3>temp1)
  166. {
  167. temp3-=temp1;
  168. month_p-=1;
  169. if(flag_y==0)month-=1;
  170. if(month==temp2)flag_y=~flag_y;
  171. temp1=get_moon_day(month_p,code_addr);
  172. }
  173. day=temp1-temp3+1;
  174. }
  175. Tim[7] = year|(Tim[0]&0x80);; //将农历信息写进指定变量
  176. Tim[8] = month;
  177. Tim[9] = day;
  178. Conver_week(); //最后进行星期转换(根据需要自行选用)
  179. }

  180. UID6380 帖子10 精华0 积分349 阅读权限50 在线时间0 小时 注册时间2005-9-23 最后登录2009-8-1 查看详细资料
  181. TOP

复制代码
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-23 06:50 , Processed in 0.054331 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表