找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2942|回复: 0
打印 上一主题 下一主题
收起左侧

单片机阳历转阴历程序 LCD12864电子钟制作

[复制链接]
跳转到指定楼层
楼主
12864电子钟制作资料,共享给大家


单片机源程序如下:
  1. 经过本人对函数的优化修改,编译后代码比原来少了近300 字节。
  2. /****************************************************/
  3. /* 获取当前日期的农历及星期*/
  4. /****************************************************/
  5. /*==================================================*/
  6. /*公历年对应的农历数据,每年三字节, 格式: */
  7. /*第一字节BIT7-4 位表示闰月月份,值为0 为无闰月, */
  8. /*BIT3-0 对应农历第1-4 月的大小*/
  9. /*第二字节BIT7-0 对应农历第5-12 月大小, */
  10. /*第三字节BIT7 表示农历第13 个月大小月份对应的位*/
  11. /*为1 表示本农历月大(30 天),为0 表示小(29 天) */
  12. /*第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期*/
  13. /*类型:Byte:unsigned char Word:unsigned int */
  14. /*本代码中Tim[0]/Tim[1]/Tim[2]分别为当前年月日数据*/
  15. /*其中Tim[0]Bit7 表示世纪,1 为20 世纪19xx,0 为21 世纪20xx*/
  16. /*Tim[10]为周数据/Tim[7]/Tim[8]/Tim[9]为农历数据,根据需要自行更换*/
  17. /*==================================================*/
  18. #include <intrins.h>
  19. Byte code year_code[597]={
  20. 0x04,0xAe,0x53,0x0A,0x57,0x48,0x55,0x26,0xBd, // 1901-1903
  21. 0x0d,0x26,0x50,0x0d,0x95,0x44,0x46,0xAA,0xB9, // 1904-1906
  22. 0x05,0x6A,0x4d,0x09,0xAd,0x42,0x24,0xAe,0xB6, // 1907-1909
  23. 0x04,0xAe,0x4A,0x6A,0x4d,0xBe,0x0A,0x4d,0x52, // 1910-1912
  24. 0x0d,0x25,0x46,0x5d,0x52,0xBA,0x0B,0x54,0x4e, // 1913-1915
  25. 0x0d,0x6A,0x43,0x29,0x6d,0x37,0x09,0x5B,0x4B, // 1916-1918
  26. 0x74,0x9B,0xC1,0x04,0x97,0x54,0x0A,0x4B,0x48, // 1919-1921
  27. 0x5B,0x25,0xBC,0x06,0xA5,0x50,0x06,0xd4,0x45, // 1922-1924
  28. 0x4A,0xdA,0xB8,0x02,0xB6,0x4d,0x09,0x57,0x42, // 1925-1927
  29. 0x24,0x97,0xB7,0x04,0x97,0x4A,0x66,0x4B,0x3e, // 1928-1930
  30. 0x0d,0x4A,0x51,0x0e,0xA5,0x46,0x56,0xd4,0xBA, // 1931-1933
  31. 0x05,0xAd,0x4e,0x02,0xB6,0x44,0x39,0x37,0x38, // 1934-1936
  32. 0x09,0x2e,0x4B,0x7C,0x96,0xBf,0x0C,0x95,0x53, // 1937-1939
  33. 0x0d,0x4A,0x48,0x6d,0xA5,0x3B,0x0B,0x55,0x4f, // 1940-1942
  34. 0x05,0x6A,0x45,0x4A,0xAd,0xB9,0x02,0x5d,0x4d, // 1943-1945
  35. 0x09,0x2d,0x42,0x2C,0x95,0xB6,0x0A,0x95,0x4A, // 1946-1948
  36. 0x7B,0x4A,0xBd,0x06,0xCA,0x51,0x0B,0x55,0x46, // 1949-1951
  37. 0x55,0x5A,0xBB,0x04,0xdA,0x4e,0x0A,0x5B,0x43, // 1952-1954
  38. 0x35,0x2B,0xB8,0x05,0x2B,0x4C,0x8A,0x95,0x3f, // 1955-1957
  39. 0x0e,0x95,0x52,0x06,0xAA,0x48,0x7A,0xd5,0x3C, // 1958-1960
  40. 0x0A,0xB5,0x4f,0x04,0xB6,0x45,0x4A,0x57,0x39, // 1961-1963
  41. 0x0A,0x57,0x4d,0x05,0x26,0x42,0x3e,0x93,0x35, // 1964-1966
  42. 0x0d,0x95,0x49,0x75,0xAA,0xBe,0x05,0x6A,0x51, // 1967-1969
  43. 0x09,0x6d,0x46,0x54,0xAe,0xBB,0x04,0xAd,0x4f, // 1970-1972
  44. 0x0A,0x4d,0x43,0x4d,0x26,0xB7,0x0d,0x25,0x4B, // 1973-1975
  45. 0x8d,0x52,0xBf,0x0B,0x54,0x52,0x0B,0x6A,0x47, // 1976-1978
  46. 0x69,0x6d,0x3C,0x09,0x5B,0x50,0x04,0x9B,0x45, // 1979-1981
  47. 0x4A,0x4B,0xB9,0x0A,0x4B,0x4d,0xAB,0x25,0xC2, // 1982-1984
  48. 0x06,0xA5,0x54,0x06,0xd4,0x49,0x6A,0xdA,0x3d, // 1985-1987
  49. 0x0A,0xB6,0x51,0x09,0x37,0x46,0x54,0x97,0xBB, // 1988-1990
  50. 0x04,0x97,0x4f,0x06,0x4B,0x44,0x36,0xA5,0x37, // 1991-1993
  51. 0x0e,0xA5,0x4A,0x86,0xB2,0xBf,0x05,0xAC,0x53, // 1994-1996
  52. 0x0A,0xB6,0x47,0x59,0x36,0xBC,0x09,0x2e,0x50, // 1997-1999
  53. 0x0C,0x96,0x45,0x4d,0x4A,0xB8,0x0d,0x4A,0x4C, // 2000-2002
  54. 0x0d,0xA5,0x41,0x25,0xAA,0xB6,0x05,0x6A,0x49, // 2003-2005
  55. 0x7A,0xAd,0xBd,0x02,0x5d,0x52,0x09,0x2d,0x47, // 2006-2008
  56. 0x5C,0x95,0xBA,0x0A,0x95,0x4e,0x0B,0x4A,0x43, // 2009-2011
  57. 0x4B,0x55,0x37,0x0A,0xd5,0x4A,0x95,0x5A,0xBf, // 2012-2014
  58. 0x04,0xBA,0x53,0x0A,0x5B,0x48,0x65,0x2B,0xBC, // 2015-2017
  59. 0x05,0x2B,0x50,0x0A,0x93,0x45,0x47,0x4A,0xB9, // 2018-2020
  60. 0x06,0xAA,0x4C,0x0A,0xd5,0x41,0x24,0xdA,0xB6, // 2021-2023
  61. 0x04,0xB6,0x4A,0x69,0x57,0x3d,0x0A,0x4e,0x51, // 2024-2026
  62. 0x0d,0x26,0x46,0x5e,0x93,0x3A,0x0d,0x53,0x4d, // 2027-2029
  63. 0x05,0xAA,0x43,0x36,0xB5,0x37,0x09,0x6d,0x4B, // 2030-2032
  64. 0xB4,0xAe,0xBf,0x04,0xAd,0x53,0x0A,0x4d,0x48, // 2033-2035
  65. 0x6d,0x25,0xBC,0x0d,0x25,0x4f,0x0d,0x52,0x44, // 2036-2038
  66. 0x5d,0xAA,0x38,0x0B,0x5A,0x4C,0x05,0x6d,0x41, // 2039-2041
  67. 0x24,0xAd,0xB6,0x04,0x9B,0x4A,0x7A,0x4B,0xBe, // 2042-2044
  68. 0x0A,0x4B,0x51,0x0A,0xA5,0x46,0x5B,0x52,0xBA, // 2045-2047
  69. 0x06,0xd2,0x4e,0x0A,0xdA,0x42,0x35,0x5B,0x37, // 2048-2050
  70. 0x09,0x37,0x4B,0x84,0x97,0xC1,0x04,0x97,0x53, // 2051-2053
  71. 0x06,0x4B,0x48,0x66,0xA5,0x3C,0x0e,0xA5,0x4f, // 2054-2056
  72. 0x06,0xB2,0x44,0x4A,0xB6,0x38,0x0A,0xAe,0x4C, // 2057-2059
  73. 0x09,0x2e,0x42,0x3C,0x97,0x35,0x0C,0x96,0x49, // 2060-2062
  74. 0x7d,0x4A,0xBd,0x0d,0x4A,0x51,0x0d,0xA5,0x45, // 2063-2065
  75. 0x55,0xAA,0xBA,0x05,0x6A,0x4e,0x0A,0x6d,0x43, // 2066-2068
  76. 0x45,0x2e,0xB7,0x05,0x2d,0x4B,0x8A,0x95,0xBf, // 2069-2071
  77. 0x0A,0x95,0x53,0x0B,0x4A,0x47,0x6B,0x55,0x3B, // 2072-2074
  78. 0x0A,0xd5,0x4f,0x05,0x5A,0x45,0x4A,0x5d,0x38, // 2075-2077
  79. 0x0A,0x5B,0x4C,0x05,0x2B,0x42,0x3A,0x93,0xB6, // 2078-2080
  80. 0x06,0x93,0x49,0x77,0x29,0xBd,0x06,0xAA,0x51, // 2081-2083
  81. 0x0A,0xd5,0x46,0x54,0xdA,0xBA,0x04,0xB6,0x4e, // 2084-2086
  82. 0x0A,0x57,0x43,0x45,0x27,0x38,0x0d,0x26,0x4A, // 2087-2089
  83. 0x8e,0x93,0x3e,0x0d,0x52,0x52,0x0d,0xAA,0x47, // 2090-2092
  84. 0x66,0xB5,0x3B,0x05,0x6d,0x4f,0x04,0xAe,0x45, // 2093-2095
  85. 0x4A,0x4e,0xB9,0x0A,0x4d,0x4C,0x0d,0x15,0x41, // 2096-2098
  86. 0x2d,0x92,0xB5, // 2099
  87. };
  88. //月份天数数据表
  89. Byte code day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};
  90. Word code day_code2[3]={0x111,0x130,0x14e};
  91. //月修正数据表
  92. Byte code week_code[12]={0,3,3,6,1,4,6,2,5,0,3,5};
  93. void Conver_week(void)
  94. /*=======获取当前日期的星期====================================*/
  95. {
  96. Byte temp1,temp2,year,month,day;
  97. year =Tim[0]&0x7f; //加载年月日数据,如为BCD 则需转为十进制
  98. month=Tim[1];
  99. day =Tim[2];
  100. if(Tim[0]>>7==0){year+=0x64;} //如果为21 世纪,年份数加100
  101. temp1=year/0x4; //所过闰年数只算1900 年之后的
  102. temp2=(year+temp1)%0x7; //为节省资源,先进行一次取余,避免数
  103. 大于0xff
  104. temp2=temp2+day+week_code[month-1];
  105. if(year%0x4==0&&month<3)temp2-=1; //如果是闰年但不到3 月则减一天
  106. Tim[10]=temp2%0x7; //将星期数据写到指定星期变量Tim[9]
  107. }
  108. bit get_moon_day(Byte month_p,Word code_addr)
  109. /*读取数据表中农历月的大月或小月,如果该月大返回1,小返回0*/
  110. {
  111. Byte temp,temp1;
  112. temp1=(month_p+3)/8;
  113. temp=0x80>>((month_p+3)%8);
  114. temp=year_code[code_addr+temp1]&temp;
  115. if(temp==0){return(0);}else{return(1);}
  116. }
  117. void Conversion(void)
  118. /*==转换当前农历信息====================================*/
  119. {
  120. Byte year,month,day;
  121. Byte temp1,temp2,temp3,month_p;
  122. Word temp4,code_addr;
  123. bit flag2,flag_y;
  124. year =Tim[0]&0x7f; //加载年月日数据,如为BCD 则需转为十进制
  125. month=Tim[1];
  126. day =Tim[2];
  127. code_addr=year-1; //定位数据表地址
  128. if(Tim[0]>>7==0)code_addr+=0x64;
  129. code_addr*=3;
  130. temp1=year_code[code_addr+2]&0x60; //取当年春节所在的公历月

  131. temp1=_cror_(temp1,5);
  132. temp2=year_code[code_addr+2]&0x1f; //取当年春节所在的公历日
  133. temp3=temp2-1; //计算当年春节离当年元旦的天数,春节只会在公
  134. 历1 月或2 月
  135. if(temp1!=1)temp3+=0x1f;
  136. if(month<10){temp4=day_code1[month-1]+day;}
  137. else{temp4=day_code2[month-10]+day;}
  138. if((month<2)||(year%0x04!=0))temp4-=1; //如果公历月小于2 月或者该
  139. 年的2 月非闰月,天数减1
  140. if (temp4>=temp3) //判断公历日在春节前还是春节后
  141. {
  142. //公历日在春节后或就是春节当日使用下面代码进行运算
  143. temp4 -=temp3;
  144. month = 1;
  145. flag_y = 0;
  146. month_p= 1; //month_p 为月份指向,公历日在春节前或就是春节当
  147. 日month_p 指向首月
  148. flag2=get_moon_day(month_p,code_addr); //检查该农历月为大
  149. 小还是小月,大月返回1,小月返回0
  150. if(flag2==0){temp1=29;}else{temp1=30;} //小月29 天、大月30 天
  151. temp2=year_code[code_addr]&0xf0;
  152. temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0 则
  153. 该年无闰月
  154. while(temp4>=temp1)
  155. {
  156. temp4-=temp1;
  157. month_p+=1;
  158. if(month==temp2)
  159. {
  160. flag_y=~flag_y;
  161. if(flag_y==0)month+=1;
  162. }
  163. else month+=1;
  164. flag2=get_moon_day(month_p,code_addr);
  165. if(flag2==0)temp1=0x1d;
  166. else temp1=0x1e;
  167. }
  168. day=temp4+1;
  169. }
  170. else
  171. {
  172. //公历日在春节前使用下面代码进行运算
  173. temp3-=temp4;
  174. if(year==0){year=0xe3;}else{year-=1;}
  175. code_addr-=3;
  176. month = 0xc;
  177. temp2 = year_code[code_addr]&0xf0;
  178. temp2 = _cror_(temp2,4);
  179. flag_y = 0;
  180. if(temp2==0){month_p=12;}else{month_p=13;} //如果当年有闰月,
  181. 一年有十三个月,月指向13,无闰月指向12
  182. flag2=get_moon_day(month_p,code_addr);
  183. if(flag2==0){temp1=29;}else{temp1=30;}
  184. while(temp3>temp1)
  185. {
  186. temp3-=temp1;
  187. month_p-=1;
  188. if(flag_y==0)month-=1;
  189. if(month==temp2)flag_y=~flag_y;
  190. flag2=get_moon_day(month_p,code_addr);
  191. if(flag2==0){temp1=0x1d;}else{temp1=0x1e;}
  192. }
  193. day=temp1-temp3+1;
  194. }
  195. Tim[7] = year|(Tim[0]&0x80);; //将农历信息写进指定变量
  196. Tim[8] = month;
  197. Tim[9] = day;
  198. Conver_week(); //最后进行星期转换(根据需要自行选用)
  199. }
复制代码

所有资料51hei提供下载:
12864资料.zip (194.46 KB, 下载次数: 35)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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