之前要做一个万年历,从论坛上辗转多处,最终发现以下这个阳历转换阴历的程序比较简单,数据带入可以直接使用,不过测试发现转换有问题,后面进行仿真调试找到问题所在,从而进而修改。不知道是我复制出错,还是自己不经意间修改才导致问题出现,已经修复,需要阳历转阴历的可以拿去。找不到具体来源,因此无法标志引用链接地址,如有侵权,联系删除。
问题所在:在SPDATE GetSpringDay(uchar GreYear,uchar GreMon,uchar GreDay)函数里面的参数定义,把Offset1定义为uchar类型,后面计算时Offset1数值溢出,将uchar改为uint后,数据不在溢出,进行上机测试,转换成功。
//公曆轉農曆(1901-2100)
//亦木明 2008.1.11
//2018.12.30清枫城主测试,发现存在问题,通过仿真一步步走找出问题所在,并作出修正
//原文在哪忘了,只知道作者有“亦木明”的标注,如有侵权联系删除
#include "12c5a60s2.h"//可修改为51的头文件
#define TRUE 1
//公历年对应的农历数据,每年三字节,
//格式第一字节BIT7-4 位表示闰月月份,值为0 为无闰月,BIT3-0 对应农历第1-4 月的大小
//第二字节BIT7-0 对应农历第5-12 月大小,第三字节BIT7 表示农历第13 个月大小
//月份对应的位为1 表示本农历月大(30 天),为0 表示小(29 天).
//第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期
//每年的數據對應在數組中的位置庫 (200-(2100-year)-1)*3+Offset1
//0x09,0x2d,0x47, //2008 0x47 01000111
//0x4d,0x4A,0xB8, //2001
typedef struct spdate
{
char cYear;
char cMon;
char cDay;
}SPDATE;
uchar code Data[]={
0x04,0xAe,0x53, //1901 0 (200-(2100-1901)-1)*3=0
0x0A,0x57,0x48, //1902 3 (200-(2100-1902)-1)*3=3
0x55,0x26,0xBd, //1903 6 (200-(2100-1903)-1)*3=3
0x0d,0x26,0x50, //1904 9 (200-(2100-year)-1)*3
0x0d,0x95,0x44, //1905 12
0x46,0xAA,0xB9, //1906 15
0x05,0x6A,0x4d, //1907
0x09,0xAd,0x42, //1908
0x24,0xAe,0xB6, //1909
0x04,0xAe,0x4A, //1910
0x6A,0x4d,0xBe, //1911
0x0A,0x4d,0x52, //1912
0x0d,0x25,0x46, //1913
0x5d,0x52,0xBA, //1914
0x0B,0x54,0x4e, //1915
0x0d,0x6A,0x43, //1916
0x29,0x6d,0x37, //1917
0x09,0x5B,0x4B, //1918
0x74,0x9B,0xC1, //1919
0x04,0x97,0x54, //1920
0x0A,0x4B,0x48, //1921
0x5B,0x25,0xBC, //1922
0x06,0xA5,0x50, //1923
0x06,0xd4,0x45, //1924
0x4A,0xdA,0xB8, //1925
0x02,0xB6,0x4d, //1926
0x09,0x57,0x42, //1927
0x24,0x97,0xB7, //1928
0x04,0x97,0x4A, //1929
0x66,0x4B,0x3e, //1930
0x0d,0x4A,0x51, //1931
0x0e,0xA5,0x46, //1932
0x56,0xd4,0xBA, //1933
0x05,0xAd,0x4e, //1934
0x02,0xB6,0x44, //1935
0x39,0x37,0x38, //1936
0x09,0x2e,0x4B, //1937
0x7C,0x96,0xBf, //1938
0x0C,0x95,0x53, //1939
0x0d,0x4A,0x48, //1940
0x6d,0xA5,0x3B, //1941
0x0B,0x55,0x4f, //1942
0x05,0x6A,0x45, //1943
0x4A,0xAd,0xB9, //1944
0x02,0x5d,0x4d, //1945
0x09,0x2d,0x42, //1946
0x2C,0x95,0xB6, //1947
0x0A,0x95,0x4A, //1948
0x7B,0x4A,0xBd, //1949
0x06,0xCA,0x51, //1950
0x0B,0x55,0x46, //1951
0x55,0x5A,0xBB, //1952
0x04,0xdA,0x4e, //1953
0x0A,0x5B,0x43, //1954
0x35,0x2B,0xB8, //1955
0x05,0x2B,0x4C, //1956
0x8A,0x95,0x3f, //1957
0x0e,0x95,0x52, //1958
0x06,0xAA,0x48, //1959
0x7A,0xd5,0x3C, //1960
0x0A,0xB5,0x4f, //1961
0x04,0xB6,0x45, //1962
0x4A,0x57,0x39, //1963
0x0A,0x57,0x4d, //1964
0x05,0x26,0x42, //1965
0x3e,0x93,0x35, //1966
0x0d,0x95,0x49, //1967
0x75,0xAA,0xBe, //1968
0x05,0x6A,0x51, //1969
0x09,0x6d,0x46, //1970
0x54,0xAe,0xBB, //1971
0x04,0xAd,0x4f, //1972
0x0A,0x4d,0x43, //1973
0x4d,0x26,0xB7, //1974
0x0d,0x25,0x4B, //1975
0x8d,0x52,0xBf, //1976
0x0B,0x54,0x52, //1977
0x0B,0x6A,0x47, //1978
0x69,0x6d,0x3C, //1979
0x09,0x5B,0x50, //1980
0x04,0x9B,0x45, //1981
0x4A,0x4B,0xB9, //1982
0x0A,0x4B,0x4d, //1983
0xAB,0x25,0xC2, //1984
0x06,0xA5,0x54, //1985
0x06,0xd4,0x49, //1986
0x6A,0xdA,0x3d, //1987
0x0A,0xB6,0x51, //1988
0x09,0x37,0x46, //1989
0x54,0x97,0xBB, //1990
0x04,0x97,0x4f, //1991
0x06,0x4B,0x44, //1992
0x36,0xA5,0x37, //1993
0x0e,0xA5,0x4A, //1994
0x86,0xB2,0xBf, //1995
0x05,0xAC,0x53, //1996
0x0A,0xB6,0x47, //1997
0x59,0x36,0xBC, //1998
0x09,0x2e,0x50, //1999
0x0C,0x96,0x45, //2000
0x4d,0x4A,0xB8, //2001
0x0d,0x4A,0x4C, //2002
0x0d,0xA5,0x41, //2003
0x25,0xAA,0xB6, //2004
0x05,0x6A,0x49, //2005
0x7A,0xAd,0xBd, //2006
0x02,0x5d,0x52, //2007
0x09,0x2d,0x47, //2008
0x5C,0x95,0xBA, //2009
0x0A,0x95,0x4e, //2010
0x0B,0x4A,0x43, //2011
0x4B,0x55,0x37, //2012
0x0A,0xd5,0x4A, //2013
0x95,0x5A,0xBf, //2014
0x04,0xBA,0x53, //2015
0x0A,0x5B,0x48, //2016
0x65,0x2B,0xBC, //2017
0x05,0x2B,0x50, //2018
0x0A,0x93,0x45, //2019
0x47,0x4A,0xB9, //2020
0x06,0xAA,0x4C, //2021
0x0A,0xd5,0x41, //2022
0x24,0xdA,0xB6, //2023
0x04,0xB6,0x4A, //2024
0x69,0x57,0x3d, //2025
0x0A,0x4e,0x51, //2026
0x0d,0x26,0x46, //2027
0x5e,0x93,0x3A, //2028
0x0d,0x53,0x4d, //2029
0x05,0xAA,0x43, //2030
0x36,0xB5,0x37, //2031
0x09,0x6d,0x4B, //2032
0xB4,0xAe,0xBf, //2033
0x04,0xAd,0x53, //2034
0x0A,0x4d,0x48, //2035
0x6d,0x25,0xBC, //2036
0x0d,0x25,0x4f, //2037
0x0d,0x52,0x44, //2038
0x5d,0xAA,0x38, //2039
0x0B,0x5A,0x4C, //2040
0x05,0x6d,0x41, //2041
0x24,0xAd,0xB6, //2042
0x04,0x9B,0x4A, //2043
0x7A,0x4B,0xBe, //2044
0x0A,0x4B,0x51, //2045
0x0A,0xA5,0x46, //2046
0x5B,0x52,0xBA, //2047
0x06,0xd2,0x4e, //2048
0x0A,0xdA,0x42, //2049
0x35,0x5B,0x37, //2050
0x09,0x37,0x4B, //2051
0x84,0x97,0xC1, //2052
0x04,0x97,0x53, //2053
0x06,0x4B,0x48, //2054
0x66,0xA5,0x3C, //2055
0x0e,0xA5,0x4f, //2056
0x06,0xB2,0x44, //2057
0x4A,0xB6,0x38, //2058
0x0A,0xAe,0x4C, //2059
0x09,0x2e,0x42, //2060
0x3C,0x97,0x35, //2061
0x0C,0x96,0x49, //2062
0x7d,0x4A,0xBd, //2063
0x0d,0x4A,0x51, //2064
0x0d,0xA5,0x45, //2065
0x55,0xAA,0xBA, //2066
0x05,0x6A,0x4e, //2067
0x0A,0x6d,0x43, //2068
0x45,0x2e,0xB7, //2069
0x05,0x2d,0x4B, //2070
0x8A,0x95,0xBf, //2071
0x0A,0x95,0x53, //2072
0x0B,0x4A,0x47, //2073
0x6B,0x55,0x3B, //2074
0x0A,0xd5,0x4f, //2075
0x05,0x5A,0x45, //2076
0x4A,0x5d,0x38, //2077
0x0A,0x5B,0x4C, //2078
0x05,0x2B,0x42, //2079
0x3A,0x93,0xB6, //2080
0x06,0x93,0x49, //2081
0x77,0x29,0xBd, //2082
0x06,0xAA,0x51, //2083
0x0A,0xd5,0x46, //2084
0x54,0xdA,0xBA, //2085
0x04,0xB6,0x4e, //2086
0x0A,0x57,0x43, //2087
0x45,0x27,0x38, //2088
0x0d,0x26,0x4A, //2089
0x8e,0x93,0x3e, //2090
0x0d,0x52,0x52, //2091
0x0d,0xAA,0x47, //2092
0x66,0xB5,0x3B, //2093
0x05,0x6d,0x4f, //2094
0x04,0xAe,0x45, //2095
0x4A,0x4e,0xB9, //2096
0x0A,0x4d,0x4C, //2097
0x0d,0x15,0x41, //2098
0x2d,0x92,0xB5, //2099
0x0d,0x53,0x49, //2100
};
uchar code Mon1[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,
0,31,29,31,30,31,30,31,31,30,31,30,31};
SPDATE Spdate;
//獲得當年春節的公曆日期
SPDATE GetSpringDay(uchar GreYear,uchar GreMon,uchar GreDay)
{
//0x09,0x2d,0x47, //2008 0x47 01000111
int day;
uchar i,Flag,F;
uint Offset1;//出现问题在这里,原本定义为uchar类型,下面计算时超出范围导致错误,进行修改为uint类型后,验证正常
unsigned char L=0x01,Flag1=1;
unsigned int Temp16,L1=0x0800;
//第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期
Spdate.cYear=GreYear ;
Spdate.cMon=(Data[(200-(100-GreYear)-1)*3+2]&0x60)>>5; //计算春节公历月份
Spdate.cDay=(Data[(200-(100-GreYear)-1)*3+2])&0x1f; //计算春节公历日期
//判断平年,闰年
if( (!(GreYear%4) && (GreYear%100)) || !(GreYear%400) ) Flag=1;
else Flag=0;
if(Spdate.cMon>GreMon) //春节离公历日期的天数
{
day=Mon1[Flag][GreMon]-GreDay;
for(i=GreMon+1;i<=Spdate.cMon-1;i++)
day+=Mon1[Flag][i];
day+=Spdate.cDay;
F=1;
}
else if(Spdate.cMon day=Mon1[Flag][Spdate.cMon]-Spdate.cDay; for(i=Spdate.cMon+1;i<=GreMon-1;i++) day+=Mon1[Flag][i]; day+=GreDay; // 000000000000000000000000000000000000000000000000000000000000000000 F=0; } else { if(Spdate.cDay>GreDay) { day=Spdate.cDay-GreDay; F=1; } else if(Spdate.cDay day=GreDay-Spdate.cDay; F=0; } else day=0; } Spdate.cYear=Spdate.cYear; Spdate.cMon=1; Spdate.cDay=1; if(!day) return Spdate ; if(F) //春节在公历日期后 { Spdate.cYear--; Spdate.cMon=12; //这边Offset1计算会出现超范围,因此把uchar类型改为uint后正常
上一篇:51单片机温控风扇仿真原理图+C语言程序与实物制作
下一篇:STC15F2系列单片机UART1使用定时器1做波特率发生器
推荐阅读最新更新时间:2024-11-10 19:14
设计资源 培训 开发板 精华推荐
- NUCLEO-F207ZG,STM32 Nucleo-144开发板,带STM32F207ZGT6 MCU,支持Arduino、ST Zio和Morpho
- LR645N3-G大电流SMPS启动电路典型应用
- IRU3034 Pentium Core 电源应用的典型应用(IRU3034 和 IRU3033 双布局)低成本 4 位 VID
- 使用 Analog Devices 的 LT1182CS 的参考设计
- 矩阵按键、独立按键模块
- LT8609HMSE 12V 降压型稳压器的典型应用电路
- TTL-UART to RS485 Convertor
- LT3595 16 通道降压模式 LED 驱动器的典型应用
- 3D3PowerBoard:电源分配板
- LF18ABDT-TR 1.8V 低压降稳压器的典型应用