万年历程序
万年历汇编程序
单片机报告任务:实现年、月、日、时间、星期的显示,通过单键转换实现日期与时间的调换调整,同时可通过按键来调整日期与时间。
硬件:STC89C52型单片机、一片频率为11.0592MHz的晶振片、三片74HC573锁存器、6块共阴极数码管、8个发光二极管、4个按键、一个压电式蜂鸣器等。
(1)发光二极管采用共阳极排列方式,一般普通发光二极管的工作电压为1.6~2.1V,硬件参数:工作电流为5~20mA。
在这个单片机系统上,发光二极管的工作电压约为1.8V,工作电流约为4.5mA。
(2)74HC573为8输入8输出锁存器,工作电压为5伏,OE为片选端,LE为锁存允许端。
OE为低电平,LE为高电平时锁存数据;当LE变为低电平时不允许新数据输入,输出不变;当OE为高电平时,不选中此芯片。
(3)压电式蜂鸣器是以压电陶瓷的压电效应,来带动金属片的振动而发声的,压电式蜂鸣器是以方波来驱动,所以在程序中改变P2.5引脚输出波形的频率,就可以调整控制蜂鸣器音调,产生各种不同音色、音调的声音。
另外,改变输出电平的高低电平占空比,则可以控制蜂鸣器的声音大小。
(4)LED 数码管中各段发光二极管的伏安特性和普通二极管类似,只是正向压降较大,正向电阻也较大。
在一定范围内,其正向电流与发光亮度成正比。
由于常规的数码管起辉电流只有1~2 mA,最大极限电流也只有10~30 mA,所以它的输入端在5 V电源的电路信号相接时,一定要串加限流电阻,以免损坏器件。
此板用的是共阴7段数码管,由公共端控制位。
操作说明:上电后6个数码管显示为时间12.00.00,8个数码管显示为星期一。
按下按键2不动,数码管转而显示日期00.01.01,前两位表示年份,中间两位表示月份,后面两位表示日,松开按键2回到时间显示;按下按键3不动,数码管转而显示设置闹钟的时间00.00. 0,前两位为闹钟的小时,中间为闹钟的分,后面一位为是否开启闹铃的标志,“0”表示关闹钟,“1”表示开闹钟,松开按键3回到时间显示;按键4供用户调整时间,按下按键4大约1秒钟,当听到“嘀”的一声说明已进入时间调整程序,此时按键1用来调整小时,按一下小时加1,按键2用来调整分钟,按一下分钟加1,按键3用来调整星期,按一下星期加1;继续按一下按键4,则进入调整日期程序,此时按键1用来调整年份,按键2用来调整月份,按键3用来调整日,按下其中一个按键相应的位加1;继续按一下按键4,则进入设置闹钟程序,按键1用来调整闹钟的小时,按键2用来调整闹钟的分钟,按键3用来开启或是关闭闹钟,按下其中一个按键相应的位加1;再按一下按键4,则跳出调整时间程序,在此情况下如果时间到达设定的闹铃时间,闹铃响起,要想关掉闹铃可按一下按键1即可停止闹铃。
万年历程序
•/*********************************************************************************** *************•C51写的公历转农历,并驱动LCM-12864汉字液晶显示。
•author: cth•本程序参考了网上部分程序,在这里感谢所有无私奉献的朋友!•全程注释,有耐心就慢慢看!•************************************************************************************ ************/••#include <reg52.h>•#include <intrins.h>•#include <stdio.h>•#define uint unsigned int•#define uchar unsigned char•char sec,min,hour,day,month,year,week; //定义全局变量,秒,分,时,日,月,年和星期•bit century; //定义世纪位:0为20世纪,1为19世纪。
•//此时初始日期为2006年8月13日,即此程序完成日期!••/**************************************C51写的公历转农历和星期•**************************************••公历是全世界通用的历法以地球绕太阳的一周为一年一年365 天分为12 个月1 3 5 7 8 10 12 月为•31 天2 月为28 天其余月份为30 天事实上地球绕太阳一周共365 天5 小时48 分46 秒比公历一年多出5 小时48•分46 秒为使年误差不累积公历年用闰年法来消除年误差由于每年多出5 小时48 分46 秒每4 年累计多出23•小时15 分4 秒接近1 天天文学家就规定每4 年有一个闰年把2 月由28 天改为29 天凡是公历年代能被4 整除•的那一年就是闰年但是这样一来每4 年又少了44 分56 秒为了更准确地计时天文学家又规定凡能被100 整除•的年份只有能被400 整除才是闰年即每400 年要减掉3 个闰年经过这样处理后实际上每400 年的误差只有2 小时•53 分20 秒已相当准确了•农历与公历不同农历把月亮绕地球一周作为一月因为月亮绕地球一周不是一整天所以农历把月分为大月和小•月大月30 天小月29 天通过设置大小月使农历日始终与月亮与地球的位置相对应为了使农历的年份与公历年•相对应农历通过设置闰月的办法使它的平均年长度与公历年相等农历是中国传统文化的代表之一并与农业生产联•系密切中国人民特别是广大农民十分熟悉并喜爱农历•公历与农历是我国目前并存的两种历法各有其固有的规律农历与月球的运行相对应其影响因素多它的大小•月和闰月与天体运行有关计算十分复杂且每年都不一致因此要用单片机实现公历与农历的转换用查表法是最方•便实用的办法•51 系列单片机因其在功能上能满足大部份对速度要求不高的应用场合的要求且价格低廉开发工具普及程度高•是目前应用最多的单片机之一本文介绍一种用51 单片机实现从1901 年到2099 年2199 年公历日到农历日及星期•的转换方法并向读者提供完整的51C程序•二基本原理•实现公历与农历的转换一般采用查表法按日查表是速度最快的方法但51 单片机寻址能力有限不可能采用•按日查表的方法除按日查外我们可以通过按月查表和按年查表的方法再通过适当的计算来确定公历日所对应的•农历日期本文采用的是按年查表法最大限度地减少表格所占的程序空间•对于农历月来说大月为30 天小月为29 天这是固定不变的这样我们就可用1 个BIT 位来表示大小月信•息农历一年如有闰月为13 个月否则是12 个月所以一年需要用13 个BIT 闰月在农历年中所在的月份并不固定•大部分闰月分布在农历2 8 月但也有少量年份在9 月以后所以要表示闰月的信息至少要4BIT 在这里我们用4BIT•的值来表示闰月的月份值为0 表示本年没有闰月有了以上信息还不足以判断公历日对应的农历日因为还需要一•个参照日我们选用农历正月初一所对应的公历日期作参照日公历日最大为31 日需要5BIT 来表示而春节所在的•月份不是1 月就是2 月用1BIT 就够了考虑到表达方便我们用2BIT 来表示春节月2BIT 的值直接表示月份这•样一年的农历信息只用3 个字节就全部包括了•计算公历日对应的农历日期的方法先计算出公历日离当年元旦的天数然后查表取得当年的春节日期计算出春•节离元旦的天数二者相减即可算出公历日离春节的天数以后只要根据大小月和闰月信息减一月天数调整一月农•历月份即可推算出公历日所对应的农历日期如公历日不到春节日期农历年要比公历年小一年农历大小月取前一•年的信息农历月从12 月向前推算•公历日是非常有规律的所以公历日所对应的星期天可以通过计算直接得到理论上公元0 年1 月1 日为星期日•只要求得公历日离公元0 年1 月1 日的日子数除7 后的余数就是星期天为了简化计算采用月校正法根据公历的•年月日可直接计算出星期天其算法是日期年份所过闰年数月校正数之和除7 的余数就是星期天但如果是在•闰年又不到3 月份上述之和要减一天再除7 其1 12 月的校正数据为6 2 2 5 0 3 5 1 4 6 2 4 在•本程序中采用1 个字节表示年份闰年数也只计算1900 年以后的闰年数所以实际校正数据也和上述数据不同••••公历年对应的农历数据,每年三字节,•格式第一字节BIT7-4 位表示闰月月份,值为0 为无闰月,BIT3-0 对应农历第1-4 月的大小•第二字节BIT7-0 对应农历第5-12 月大小,第三字节BIT7 表示农历第13 个月大小•月份对应的位为1 表示本农历月大(30 天),为0 表示小(29 天)•第三字节BIT6-5 表示春节的公历月份,BIT4-0 表示春节的公历日期•************************************************************************************ *************/•code uchar year_code[597]={• 0x04,0xAe,0x53, //1901• 0x0A,0x57,0x48, //1902• 0x55,0x26,0xBd, //1903• 0x0d,0x26,0x50, //1904• 0x0d,0x95,0x44, //1905• 0x46,0xAA,0xB9, //1906• 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• 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• 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• 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• 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• 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•};•///月份数据表•code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3}; •code uint day_code2[3]={0x111,0x130,0x14e};••bit c_moon;•data uchar year_moon,month_moon,day_moon;•/*子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0*/•bit get_moon_day(uchar month_p,uint table_addr)•{•uchar temp;• switch (month_p){• case 1:{temp=year_code[table_addr]&0x08;• if (temp==0)return(0);else return(1);}• case 2:{temp=year_code[table_addr]&0x04;• if (temp==0)return(0);else return(1);}• case 3:{temp=year_code[table_addr]&0x02;• if (temp==0)return(0);else return(1);}• case 4:{temp=year_code[table_addr]&0x01;• if (temp==0)return(0);else return(1);}• case 5:{temp=year_code[table_addr+1]&0x80;• if (temp==0) return(0);else return(1);}• case 6:{temp=year_code[table_addr+1]&0x40;• if (temp==0)return(0);else return(1);}• case 7:{temp=year_code[table_addr+1]&0x20;• if (temp==0)return(0);else return(1);}• case 8:{temp=year_code[table_addr+1]&0x10;• if (temp==0)return(0);else return(1);}• case 9:{temp=year_code[table_addr+1]&0x08;• if (temp==0)return(0);else return(1);}• case 10:{temp=year_code[table_addr+1]&0x04;• if (temp==0)return(0);else return(1);}• case 11:{temp=year_code[table_addr+1]&0x02;• if (temp==0)return(0);else return(1);}• case 12:{temp=year_code[table_addr+1]&0x01;• if (temp==0)return(0);else return(1);}• case 13:{temp=year_code[table_addr+2]&0x80;• if (temp==0)return(0);else return(1);}• }•}••/*********************************************************************************** **•函数功能:输入阳历数据,输出阴历数据(只允许1901-2099年)•调用函数示例:Conversion(c,year,month,day)•如:计算2004年10月16日Conversion(0,0x4,0x0A,0x10);•c,year,month,day均为16进制数据,c为世纪标志位,c_sun=0为21世纪,c_sun=1为19世纪•调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历16进制数据。
CC++程序设计——万年历(完整代码+实验报告)
2、主要函数流程图: (1)类的构造函数:用于给类的对象赋值。提示用户输入数据,并具有较强的错误输 入数据检查功能。(参见图 3-1) (2)闰年判断函数:判断哪些年份为闰年。 (图 3-2)
正文 第 2 页(共 18 页)
开 始
开 始
year%4==0
否
输入待查询的 年份
是
否
year%100==0
* ","***** ","***** ","***** ","***** ","***** "," * ","***** ","***** "," * ","***** ","***** ","
"***** ","
=====================公元"<<year<<"年日历====================="<<endl; =====================公元"<<year<<"年日历====================="<<endl;
/*实现 3 维数组图案的输出*/ for(int g=0;g<5;g++) { cout<<" fout<<" for(int i=0;i<4;i++) for(int h=0;h<7;h++) { cout<<ss[g][num[i]][h]; fout<<ss[g][num[i]][h]; } cout<<endl; fout<<endl; } }
C语言课程设计万年历 完整版
目录一引言 (2)二系统功能和数据说明 (3)一)功能简介 (3)二)程序中的数据说明 (3)三程序总体设计及流程图 (4)一)应用到的c语言 (4)二)程序的总框架 (5)四功能模块设计及调试 (5)一)算法说明 (5)1.总天数的算法 (5)2.计算输入日期是星期几 (6)3.对输入信息的汇总 (8)4..界面的控制 (10)二)调试结果 (11)五程序清单 (12)六结束语 (17)一引言通过大一上学期对C语言的学习,了解到了很多C语言的相关知识。
学习的过程有很多困惑但是当自己能够独立的看懂,能过独立的完成一个简单的程序时,心中就会收获无限的喜悦和成就感。
我可以里哟哦那个它看懂一些简单的程序,编写一些简单的计算程序,更多的是学会了一种思想——编程,它让我在去思考很多日常生活中的事物是怎么样通过一个个小小的函数实现功能的,激发我对探究的兴趣。
C语言是近年在国内外得到迅速推广应用的一种语言。
C语言功能丰富,表达能力强,使用灵活方便,应用面广,目标程序效率高,可移植性好,既具有高级语言的优点,又具有低级语言的许多特点。
因此,C语言特别适合于编写各种软件。
在这次的课程设计中我将把日常生活中最经常接触的——日期的查询利用C语言的程序编成一个简单的日历。
通过这个小小的日历可以实现很多功能。
在程序中你能看到很多熟悉的C语言关键字,同时也加入了很多自己课外了解到的一些关键字。
在不断的调试中最终才获得最为完整的程序。
接下来就是我的C 语言课程设计的具体内容来了二系统功能和数据说明(一)功能简介在我们的日常生活中能接触到很多不同类型的日历,在日历上我们通常希望它能简介明了的给我们最想要的日期信息。
在我的万年历当中,就是将日历,月历做的简单明了,很方便我们的使用。
下面是它要实现的一些基本功能:用C语言编写万年历1、输入年份,判断是否为闰年2、输入年月日,判断改日为星期几3、输入年份,打出12个月历,输入月份,打出该月的日历4、要求用多个函数实现[名称]万年历[修改]1、对输入的日期进行容错处理2、增加和修改为英文的月份和星期显示3、采用指针形式的weeks和month数组(二)程序中的数据说明①int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};这是定义的关于每个月天数的数组,根据大小月以及二月分的特殊情况将每个月的天数最为数组中的元素存入数组当中。
万年历程序设计说明书
万年历程序设计说明书一、引言万年历是一款方便用户查询日期、农历、节假日等信息的应用程序。
本文档将详细介绍万年历程序的设计和功能实现,以帮助开发人员理解和使用该程序。
二、需求分析1. 用户可以通过程序查询指定日期的公历和农历信息。
2. 用户可以查看指定日期是否为节假日,并显示节假日名称。
3. 用户可以选择查询某个年份的所有节假日。
4. 用户可以设置提醒功能,当指定日期到来时,程序会发送通知提醒用户。
5. 界面友好,操作简单,用户体验良好。
三、系统设计1. 技术选型本程序将使用Python语言进行开发,使用以下库和框架:- tkinter:用于创建程序的图形用户界面。
- datetime:用于处理日期和时间相关的操作。
- requests:用于发送HTTP请求,获取节假日数据。
- plyer:用于发送系统通知。
2. 模块划分本程序将分为以下几个模块:- 主界面模块:负责显示程序的主界面,包括日期选择框、查询按钮等。
- 日期查询模块:负责处理用户输入的日期,查询公历和农历信息,并显示在界面上。
- 节假日查询模块:负责查询指定日期是否为节假日,并显示节假日名称。
- 节假日提醒模块:负责设置提醒功能,当指定日期到来时发送系统通知提醒用户。
3. 数据获取与处理- 公历和农历信息:使用datetime库获取指定日期的公历和农历信息。
- 节假日数据:通过发送HTTP请求获取节假日数据,并使用json库进行解析和处理。
四、功能实现1. 主界面设计主界面应包括日期选择框、查询按钮、显示区域等组件,用户可以通过日期选择框选择查询日期,并点击查询按钮进行查询操作。
查询结果将显示在显示区域中。
2. 日期查询功能实现用户选择查询日期后,程序将使用datetime库获取公历和农历信息,并将其显示在界面上。
3. 节假日查询功能实现用户可以选择查询某个年份的所有节假日。
程序将通过发送HTTP请求获取节假日数据,并使用json库进行解析和处理。
万年历程序实验报告
万年历程序实验报告一、实验目的与要求1.实验目的:(1)能按照软件工程的思想,采用面向过程的方法开发出一个小型软件系统。
(2).在软件系统开发过程中,能综合利用一门编程语言、和软件工程二门课程的知识。
(3).通过实例使同学们掌握结构化数据流分析技术。
2.要求:(1)软件需求分析:要求做到使用结构化数据流分析技术分析课题需求,写出详细的数据流图和数据字典,数据流图的基本处理的个数不得少与5个。
(2)软件设计与编码:要求做到对上一实验的软件需求进行软件结构设计,模块数不少于四个。
然后再进行详细设计并编码。
程序要符合结构化程序的要求。
(3)软件测试和调试:要求掌握如何设计测试方案、撰写测试说明书,并掌握程序修改的常用技术。
要求对上一实验所编的程序进行测试,要分步进行,要有详细的测试说明书,要测试通过。
二、实验方案(一)软件需求分析1.需求分析:编制一个万年历程序,程序包括的功能查询具体某年某月某日是星期几,查询某一年是否闰年,打印某一年的年历。
2.根据需求画出初步的数据流图:把“万年历系统”细化,得出完整的数据流图如下:3.根据需求分析与数据流图,写出卡片式的数据字典(2)软件设计与编码1.首先进行总体设计,根据需求,系统应该划分功能模块如下:2.进行详细设计 按照需求,本系统有上图所示的三个功能模块,所以在编码设计的时候,应该就分别设计三个功能模块,三个功能模块主要就是三个分别的算法。
先定义本程序主要的几个数据项:char*month_str[]={"January","February","March","April","May","June","July","August","September","Oc tober","November","December"}; //月份的名称,在列印年历的功能时候使用到char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};//星期,在查询具体某天是星期几时候使用到接着就是三个算法,因为三个算法都是借鉴别人的,所以就不在这里显示了,最后在main 函数用switch 语句把三个功能组合起来。
51单片机c语言电子万年历完整程序
该程序为51单片机c语言电子万年历#include"reg52.h"//#include<stdio.h>#define uchar unsigned char#define uint unsigned intsbit lcden=P3^4;sbit lcdrs=P3^5;sbit DATA=P0^7;sbit RST=P0^5;sbit SCLK=P0^6;sbit menu=P3^0; //菜单sbit add=P3^1; //加一sbit dec=P3^7; //减一sbit led0=P1^0;sbit led1=P1^1;sbit led2=P1^2;sbit led3=P1^3;sbit ds=P3^2;//sbit beep=P3^3;uint temp;float f_temp;uint warn_l1=270;uint warn_l2=250;uint warn_h1=300;uint warn_h2=320;uint get_temp();void delayms(uint x);void write_com(uchar com);void write_data(uchar date);void init();void dis_temp(uint t);void Write1302(uchar dat);void WriteSet1302(uchar Cmd,uchar dat);uchar Read1302(void);uchar ReadSet1302(uchar Cmd);void Init_DS1302(void);void DisplaySecond(uchar x);void DisplayMinute(uchar x);void DisplayHour(uchar x);void DisplayDay(uchar x);void DisplayMonth(uchar x);void DisplayYear(uchar x);void DisplayWeek(uchar x);void dis_temp(uint t);void read_date(void);void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num);void key_scan(void);char code table[]="0123456789" ;uchar code table2[]= "TUEWESTHUFRISATSUNMON"; uchar second,minute,hour,day,month,year,week,count=0; uchar ReadValue,num,time;void delayms(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}////////////////////////////////////////////////////////////void write_com(uchar com){lcdrs=0;P2=com;delayms(5);lcden=1;delayms(5);lcden=0;}void write_data(uchar date){lcdrs=1;P2=date;delayms(5);lcden=1;delayms(5);lcden=0;}void init(){lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}/////////////////////////////////////////////////////////////////void Write1302(uchar dat){uchar i;SCLK=0; //拉低SCLK,为脉冲上升沿写入数据做好准备 delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续写8个二进制位数据DATA=dat&0x01; //取出dat的第0位数据写入1302delayms(2); //稍微等待,使硬件做好准备SCLK=1; //上升沿写入数据delayms(2); //稍微等待,使硬件做好准备SCLK=0; //重新拉低SCLK,形成脉冲dat>>=1; //将dat的各数据位右移1位,准备写入下一个数据位 }}void WriteSet1302(uchar Cmd,uchar dat){RST=0; //禁止数据传递SCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输delayms(2); //稍微等待,使硬件做好准备Write1302(Cmd); //写入命令字Write1302(dat); //写数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递}uchar Read1302(void){uchar i,dat;delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续读8个二进制位数据dat>>=1; //将dat的各数据位右移1位,因为先读出的是字节的最低位if(DATA==1) //如果读出的数据是1dat|=0x80; //将1取出,写在dat的最高位SCLK=1; //将SCLK置于高电平,为下降沿读出delayms(2); //稍微等待SCLK=0; //拉低SCLK,形成脉冲下降沿delayms(2); //稍微等待}return dat; //将读出的数据返回}uchar ReadSet1302(uchar Cmd){uchar dat;RST=0; //拉低RSTSCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输Write1302(Cmd); //写入命令字dat=Read1302(); //读出数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递return dat; //将读出的数据返回}void Init_DS1302(void){WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)<<4|(59%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)<<4|(23%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((28/10)<<4|(28%10))); //根据写日寄存器命令字,写入日的初始值 WriteSet1302(0x88,((2/10)<<4|(2%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((14/10)<<4|(14%10))); //nian//WriteSet1302(0x8a,((4/10)<<4|(4%10)));}/////////////////////////////////////////////////////////////////void DisplaySecond(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);write_com(0x80+0x48);write_data(' ');dis_temp(get_temp());}void DisSecond(uchar x){uchar i,j;ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);}void DisplayMinute(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x80+0x43);write_data(i+0x30);write_com(0x80+0x44);write_data(j+0x30);write_com(0x80+0x45);write_data(':');}void DisplayHour(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x40);write_data(i+0x30);write_com(0x80+0x41);write_data(j+0x30);write_com(0x80+0x42);write_data(':');}void DisplayDay(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x89);write_data(i+0x30);write_com(0x8a);write_data(j+0x30); }void DisplayMonth(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x86);write_data(i+0x30); write_com(0x87);write_data(j+0x30);write_com(0x88);write_data('/');}void DisplayYear(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x81);write_data(2+0x30);write_com(0x82);write_data(0+0x30);write_com(0x83);write_data(i+0x30);write_com(0x84);write_data(j+0x30);write_com(0x85);write_data('/');}void DisplayWeek(uchar x){ uchar i;x=x*3;// write_com(0x8c);write_data(table2[x]);// write_com(0x8d);write_data(table2[x+1]);// write_com(0x8e);write_data(table2[x+2]);write_com(0x8c);for(i=0;i<3;i++){write_data(table2[x]);x++;}}void read_date(void){ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = ReadSet1302(0x83);minute=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x85);hour=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x87);day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x89);month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x8d);year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue=ReadSet1302(0x8b); //读星期week=ReadValue&0x07;DisplayYear(year);DisplayMonth(month);DisplayDay(day);DisplayWeek(week);DisplayHour(hour);DisplayMinute(minute);DisplaySecond(second);dis_temp(get_temp()); ///温度显示key_scan(); ///按键检测}void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num){newval=ReadSet1302(newaddr); //读取当前时间newval=((newval&0x70)>>4)*10+(newval&0x0f); //将bcd码转换成十进制 if(flag) //判断是加一还是减一{newval++;switch(s1num){ case 1: if(newval>99) newval=0;DisplayYear(newval);break;case 2: if(newval>12) newval=1;DisplayMonth(newval);break;case 3: if(newval>31) newval=1;DisplayDay(newval);break;case 4: if(newval>6) newval=0;DisplayWeek(newval);break;case 5: if(newval>23) newval=0;DisplayHour(newval);break;case 6: if(newval>59) newval=0;DisplayMinute(newval);break;case 7: if(newval>59) newval=0;DisplaySecond(newval);break;default:break;}}else{newval--;switch(s1num){ case 1: if(newval==0) newval=99;DisplayYear(newval);break;case 2: if(newval==0) newval=12;DisplayMonth(newval);break;case 3: if(newval==0) newval=31;DisplayDay(newval);break;case 4: if(newval<0) newval=6;DisplayWeek(newval);break;case 5: if(newval<0) newval=23;DisplayHour(newval);break;case 6: if(newval<0) newval=59;DisplayMinute(newval);break;case 7: if(newval<0) newval=59;DisplaySecond(newval);break;default:break;}}WriteSet1302((newaddr-1),((newval/10)<<4)|(newval%10)); //将新数据写入寄存器}//////////////////////////////////////void dsreset(void){uint i;ds=0;i=103;while(i>0)i=4;while(i>0)i--;}bit tempreadbit(void){uint i;bit dat;ds=0;i++;ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return(dat);}uchar tempread(void){uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1); }return(dat);}void tempwritebyte(uchar dat) {bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb){ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0;i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void) {dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0x44);key_scan(); //////按键函数}uint get_temp(){uchar a,b;tempchange(); //////温度函数dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread();b=tempread();temp=b;temp<<=8;temp=temp|a;f_temp=temp*0.0625;temp=f_temp*10+0.5;f_temp=f_temp+0.05;return temp;}//////void dis_temp(uint t){uchar n1,n2,n3;n1=t/100;n2=t%100/10;n3=t%100%10;DisSecond(second); ///秒显示 write_com(0x80+0x49);write_data(table[n1]);//delayms(5);write_com(0x80+0x4a);write_data(table[n2]);//delayms(5);write_com(0x80+0x4b);write_data('.');// delayms(5);write_com(0x80+0x4c);write_data(table[n3]);//delayms(5);write_com(0x80+0x4d);write_data('^');//delayms(5);write_com(0x80+0x4e);write_data('C');//delayms(5);DisSecond(second); ////秒显示}/*********************液晶显示*****************/ void warn(uint s,uchar led){uchar i;i=s;// beep=0;P1=~(led);while(i--){dis_temp(get_temp());}// beep=1;P1=0xff;i=s;while(i--){dis_temp(get_temp());}}void deal(uint t){uchar i;if((t>warn_l2)&&(t<=warn_l1)){warn(40,0x01);}else if(t<=warn_l2){warn(10,0x03);}else if((t<warn_h2)&&(t>=warn_h1)) {warn(40,0x04);}else if(t>=warn_h2){warn(10,0x0c);}else{i=40;while(i--){dis_temp(get_temp());DisSecond(second);}}}///////////////////////////////////////void main(){init();Init_DS1302();while(1){tempchange();read_date();deal(temp);key_scan();}}////******************************************* void key_scan(void){// uchar miao,s1num=0;uchar s1num=0;if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;while(1){if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;}}// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,miao|0x80);write_com(0x0f);//光标闪射if(s1num==1){ //year=ReadSet1302(0x8d);write_com(0x80+4); //年光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(year,1,0x8d,1);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(year,0,0x8d,1);}}}if(s1num==2){//month=ReadSet1302(0x89);write_com(0x80+7); //月光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(month,1,0x89,2);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(month,0,0x89,2);}}}if(s1num==3){ //day=ReadSet1302(0x87);write_com(0x80+10);//日光标{delayms(3);if(add==0){ while(!add);turn_val(day,1,0x87,3);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(day,0,0x87,3); //写入日寄存器 }}}if(s1num==4){ //week=ReadSet1302(0x8b);write_com(0x80+14); //星期光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(week,1,0x8b,4);}}if(dec==0){delayms(3);{ while(!dec);turn_val(week,0,0x8b,4);}}}if(s1num==5){// hour=ReadSet1302(0x85)write_com(0x80+0x40+1); //时光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(hour,1,0x85,5);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(hour,0,0x85,5);}}}if(s1num==6)//调时间分{ // minute=ReadSet1302(0x83);write_com(0x80+0x40+4);if(add==0){delayms(5);if(add==0){ while(!add);turn_val(minute,1,0x83,6); //写入分寄存器}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(minute,0,0x83,6); //写入分寄存器}}}if(s1num==7)//调时间秒{// second=ReadSet1302(0x81);write_com(0x80+0x40+7);//秒光标if(add==0){delayms(3);if(add==0){ while(!add);if(second==0x60)second=0x00;turn_val(second,1,0x81,7);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(second,0,0x81,7);}}}if(s1num==8){// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,second&0x7f);s1num=0;//s1num清零//write_com(0x0c);//光标不闪烁//break;}}}}}。
课程设计说明万年历程序设计
《高级语言程序设计》课程设计说明书设计题目:万年历程序设计班级:学号:姓名:完成日期:一:课程设计目的《高级语言程序设计》课程设计是电子信息、光信息专业和物理学专业集中实践性环节之一,是学习完《高级语言程序设计》课程后进行的一次全面的综合练习,其目的在于加深对程序设计大体知识的明白得,把握利用C语言进行模块化软件设计的大体方式,提高通过编写程序解决实际问题的能力,为尔后从事设计工作和后续各类编程课程的学习打好基础。
二:需求分析题目:要求:输入年份和月份,自动输出该月的日历,清楚的显示每一天是礼拜几,输入年份,月份和具体日期能确信某天是礼拜几。
万年历是采纳数字电路实现对.时,分,秒.数字显示的计时装置,普遍用于个人家庭,车站, 码头办公室等公开场合,成为人们常生活中不可少的必需品,由于数字集成电路的进展和石英晶体振荡器的普遍应用,使得数字钟的精度,远远超过老式钟表, 钟表的数字化给人们生产生活带来了极大的方便,而且大大地扩展了钟表原先的报时功能。
诸如按时自动报警、按时自动打铃、时刻程序自动操纵、按时广播、自动起闭路灯、按时开关烘箱、通断动力设备、乃至各类按时电气的自动启用等,可是所有这些,都是以钟表数字化为基础的。
因此,研究万年历及扩大其应用,有着超级现实的意义。
它能够对年、月、日、周日、时、分、秒进行计时,关于数字电子万年历采纳直观的数字显示,能够同时显示年、月、日、周日、时、分、秒等信息,还具有时刻校准等功能。
综上所述此万年历具有读取方便、显示直观、功能多样、电路简练、本钱低廉等诸多优势,符合电子仪器仪表的进展趋势,具有广漠的市场前景。
三:概要设计1-判定是不是为闰年计算所输入的年份是不是可被4整除,假设不能被整除,为平年。
假设可被4整除,计算此年份可否被100整除,不能被100整除那么为闰年。
既能被4整除,也能被100整除的年份,假设也能被400整除,那么为闰年,不然为平年。
闰年366天,平年365天2-要紧设计思路四:详细设计源程序:#include <stdio.h> #include <stdlib.h>void DayOfWeek(){int rtnDay;int y,m,d;char ch;while (1){printf("请输入年月日(xxxx,xx,xx): ");scanf("%d,%d,%d", &y, &m, &d);fflush(stdin);rtnDay =(3*y-(7*(y+(m+9)/12))/4+(23*m)/9+d+17-((y+(m<3?-1:0))/100+1)*3/4) % 7; printf("%04d年%02d月%02d日是礼拜%d\n", y, m, d, rtnDay?rtnDay:7); printf("继续查询(y/n)");ch = getchar();if(ch != 'y' && ch != 'Y')break;}}void GetMonth(int D, int md, int n){int N ,m,d,y,c,Wd;int i,maxday,i1;if(md>=13||md<=0)printf("HAVE WRONG!");else{for(i1=0;;md++,i1++){if(md==0){md=12;D-=1;}elseif(md==13){md=1;D+=1;}m=md;printf("\n============%d,%2d============\n",D,md);if(0<m&&m<=12){switch(m){case 1:maxday=31;break;case 2:if(D%4==0&&D%100!=0||D%400==0) maxday=29;elsemaxday=28;break;case 3:maxday=31;break;case 4:maxday=30;break;case 5:maxday=31;break;case 6:maxday=30;break;case 7:maxday=31;break;case 8:maxday=31;break;case 9:maxday=30;break;case 10:maxday=31;break;case 11:maxday=30;break;case 12:maxday=31;break;default:printf("HAVE WRONG");}if(0<m&&m<=2){N=D-1;m=m+10;}else{N=D;m=m-2;}c=N/100;y=N%100;Wd=((1+(13*m-1)/5+y+y/4+c/4-2*c)%7+7)%7; printf(" SUN MOU TUE WED TUR FRI SAT\n"); for(i=0;i<Wd;i++)printf("%4c",' ');for(d=1;d<=maxday;d++){printf("%4d",d);Wd=(Wd+1)%7;if(Wd==0)printf("\n");}}elseprintf("HAVE WRONG!");if(i1==n)break;}}}void MonthOfYear(){char ch;int y, m;while (1){printf("请输入年月(xxxx,xx): "); scanf("%d,%d", &y, &m);fflush(stdin);GetMonth(y, m, 0);printf("\n继续查询(y/n)");ch = getchar();if(ch != 'y' && ch != 'Y')break;}}void GetYear(){char ch;int y;while (1){printf("请输入年: ");scanf("%d", &y);fflush(stdin);GetMonth(y, 1, 11);printf("\n继续查询(y/n)");ch = getchar();if(ch != 'y' && ch != 'Y')break;}}int main(){char ch;while(1){system("cls");printf("1.查询礼拜\n");printf("2.查询月份\n");printf("3.显示一年\n");printf("4.退出程序\n");ch = getchar();fflush(stdin);switch(ch){case '1':DayOfWeek();break;case '2':MonthOfYear();break;case '3':GetYear();break;case '4':printf("是不是退出(y/n)");ch = getchar();fflush(stdin);if(ch == 'y' || ch == 'Y')exit(0);break;}}}五:运行成效及分析1 登录界面输入3,通过输入年份可显示一年每一个月份的日历;输入2,通过输入年份和月份(中间以空格分开)可显示该月的日历;假设输入1,通过输入年份月份和日期(中间以逗号分开)可显示该日的礼拜;输入4,那么退出查询系统2查询礼拜输入1后回车,然后输入具体的年月日可查出该天为礼拜几3查询月历输入2回车,然后输入年月可显示该月月历4显示一年输入3后回车,输入一个有效年份可显示该年的十二个月的月历。
C++程序设计(万年历——说明书)
C++程序设计说明书题目:万年历班级学号:学生姓名:目录一.应用程序的名称二.应用程序的主题、设计目的三.应用程序简介1.程序的基本结构及内容2。
程序的运行环境四.主要运行界面的介绍五.程序亮点六.课程设计中存在的问题及解决方法一.课程设计名称万年历二.应用程序要求、目的主题:万年历目的:实现对年月的查询三.应用程序简介(1)基本结构:整个程序有cls_screen(清屏)、judgement(判断是否为闰年)、show_week(记录周几)、print_year(查询某年)、print_year_month(查询某年某月)等自定义函数,程序中涉及到switch语句、for语句、if语句等和多次函数调用语句,开头定义了day_of_month[]数组,主要目的是将12个月每个月有多少天依次排出,在后面又用if语句判断二月的天数是28还是29。
在主函数中运用while 语言与switch语句的嵌套,是程序拥有了循环的功能.用fflush(stdin);语句清除输入缓存,使程序在使用过程中不会太过眼花缭乱。
万年历的编程,需要两个方面的讨论和研究,一是要在用户输入年份的时候,判断该年是否为闰年,而对于闰年的判断,能被4整除但不能被100整除,或者能被400整除的年份为闰年,否则为平年。
所以会改变day_of_month[]数组中的二月份的数值。
二是在用户输入年月份的时候,判断该年该月的第一天是周几,从来好排列。
而对于判断周几,需要运用公式:w=(y+[y/4]+[c/4]—2c+[26(m+1)/10]+d-1)%7并用if语句使用判断。
通过这两个方面的讨论和实现,才能合理的编程出万年历的基本程序代码.(2)源程序代码:#include〈stdio.h>#include 〈string。
h〉#include 〈time.h>#include <math。
h〉#include 〈windows。
c万年历课程设计
c万年历课程设计一、课程目标知识目标:1. 学生能理解万年历的基本概念,掌握年、月、日的关系及其换算方法。
2. 学生能运用所学的数学知识,编写简单的万年历程序,计算任意日期是星期几。
3. 学生了解历法的起源和发展,理解我国农历与公历的区别和联系。
技能目标:1. 学生通过编写万年历程序,提高编程能力和逻辑思维能力。
2. 学生能够运用万年历知识解决实际问题,如计算节日日期、安排活动等。
3. 学生学会查阅相关资料,培养自主学习能力和合作精神。
情感态度价值观目标:1. 学生对万年历产生兴趣,激发学习数学和编程的热情。
2. 学生通过了解历法的发展,增强对传统文化的认识,培养民族自豪感。
3. 学生在合作学习中,学会尊重他人,培养团队协作意识。
本课程旨在结合学生年级特点,以提高学生的数学应用能力和编程技能为核心,通过万年历的学习,使学生在掌握知识的同时,培养良好的学习态度和价值观。
课程目标具体、可衡量,为教学设计和评估提供明确方向。
二、教学内容1. 万年历的基本概念:年、月、日的关系,闰年的判断方法,星期计算。
2. 编写万年历程序:流程图设计,算法实现,调试与优化。
3. 历法的起源与发展:古代历法,农历与公历的转换,二十四节气。
4. 实践应用:计算特定日期的星期,安排节日活动,了解历史上的今天。
教学大纲:第一课时:万年历的基本概念,闰年的判断方法,星期计算。
第二课时:编写万年历程序,学习流程图设计,实现基本算法。
第三课时:历法的起源与发展,了解农历与公历的转换,学习二十四节气。
第四课时:实践应用,解决实际问题,如计算节日日期,了解历史上的今天。
教学内容依据课程目标,结合课本章节,保证科学性和系统性。
通过以上安排,使学生掌握万年历相关知识,提高编程技能,并培养对传统文化的认识。
三、教学方法本课程采用以下多样化的教学方法,旨在激发学生的学习兴趣,提高主动性和参与度:1. 讲授法:教师通过生动的语言和形象的比喻,讲解万年历的基本概念、历法的发展和算法原理,为学生奠定坚实的理论基础。
万年历程序
万年历程序* ;***************************** ;67H(高两位年)66H(低两位年)65H(月)64H(星期) ;63H(日)62H(小时)61H(分)60H(秒)57H(农历月)56H(农历日) ;显示缓冲单元7CH~7BH(4CH~4BH)年低位,7AH~79H(4AH~49H)月,;78H(48H)星期,77H~76H(47H~46H)日,75H~74H(45H~44H)时,;73H72H(43H~42H)分,71H~70H(41H~40H)秒,5EH~5DH(55H~54H)农历年;5CH~5BH(53H~52H)农历月,5AH~59H(51H~50H)农历日;闰年标志为F0,定时器T1为调整时候闪烁用; ; SCLK EQU P3.2 IO EQU P3.3 RST EQU P3.4 YEAR DA TA 66H MONTH DA TA 65H WEEK DATA 64H DAY DATA 63H HOUR DATA 62H MINTUE DATA 61H SECOND DA TA 60H DS1302_ADDR DA TA 32H DS1302_DATA DA TA 31H ORG 0000H AJMP START ORG 0003H RETI ORG 000BH RETI ORG 0013H RETI ORG 001BH LJMP INTT1 ORG 0023H RETI ORG 002BH RETI ;************************************ ;* 主程序* ;************************************ START: SETB EA MOV SCON,#00H ;串行输出,方式0 MOV TMOD,#10H ;计数器1,方式1 MOV TL1,#00H MOV TH1,#00H MOV DS1302_ADDR,#8EH MOV DS1302_DATA,#00H ;允许写1302 LCALL WRITE MOV DS1302_ADDR,#90H MOV DS1302_DATA,#0A6H ;1302充电,充电电流 1.1mA LCALL WRITE MAIN1: MOV DS1302_ADDR,#8DH ;READ OUT YEAR LCALL READ MOV YEAR,DS1302_DATA MOV DS1302_ADDR,#8BH ;READ OUT WEEK LCALL READ MOV WEEK,DS1302_DA TA MOV DS1302_ADDR,#89H ;READ OUT MONTH LCALL READ MOV MONTH,DS1302_DATA MOV DS1302_ADDR,#87H ;READ OUT DAY LCALL READ MOV DAY,DS1302_DATA MOV DS1302_ADDR,#85H ;READ OUT HOUR LCALL READ MOV HOUR,DS1302_DATA MOV DS1302_ADDR,#83H ;READ OUT MINTUE LCALL READ MOV MINTUE,DS1302_DATA MOV DS1302_ADDR,#81H ;READ OUT SECOND LCALL READ MOV SECOND,DS1302_DA TA MOV R0,YEAR ;年分离,送显示缓存LCALL DIVIDE MOV 7BH,R1 MOV 4BH,R1 MOV 7CH,R2 MOV 4CH,R2 MOV 78H,WEEK MOV 48H,WEEK MOV R0,MONTH ;月分离,送显示缓存LCALL DIVIDE MOV 79H,R1 MOV 49H,R1 MOV 7AH,R2 MOV 4AH,R2 MOV R0,DAY ;日分离,送显示缓存LCALL DIVIDE MOV 76H,R1 MOV 46H,R1 MOV 77H,R2 MOV 47H,R2 MOV R0,HOUR ;小时分离,送显示缓存LCALL DIVIDE MOV 74H,R1 MOV 44H,R1 MOV 75H,R2 MOV 45H,R2 MOV R0,MINTUE ;分分离,送显示缓存LCALL DIVIDE MOV 72H,R1 MOV 42H,R1 MOV 73H,R2 MOV 43H,R2 MOV R0,SECOND ;秒分离,送显示缓存LCALL DIVIDE MOV 70H,R1 MOV 40H,R1 MOV 71H,R2 MOV 41H,R2 LCALL DISPLAY LCALL GENGXIN ;更新农历程序JNB P1.1,SETG JNB F0,SSS CLR P1.3 ;闰月,则p1.3清0,指示灯亮LJMP MAIN1 SSS: SETB P1.3 ;非闰月,则p1.3置位,指示灯灭LJMP MAIN1 ;******************************* ;* 公历设置程序* ;******************************* SETG: SETB TR1 SETB ET1 CLR 08H CLR 09H CLR 0AH CLR 0BH CLR 0CH CLR 0DH CLR 0EH CLR 0FH MOV DS1302_ADDR,#8EH MOV DS1302_DA TA,#00H ;允许写1302 LCALL WRITE MOV DS1302_ADDR,#80H MOV DS1302_DA TA,#80H ;1302停止振荡LCALL WRITE GW AIT: LCALL DISPLAY JNB P1.1,GW AIT SETG1: LCALL DISPLAY JNB P1.1,SETG2 JNB P1.2,GADDYEAR AJMP SETG1 GADDYEAR: MOV R7,66H ;年加 1 LCALL ADD1 MOV 66H,A CJNE A,#51H,GADDYEAR1 MOV 66H,#01H GADDYEAR1: MOV DS1302_ADDR,#8CH ;年值送1302 MOV DS1302_DATA,66H LCALL WRITE MOV R0,66H LCALL DIVIDE ;年分离送显示缓冲MOV 4BH,R1 MOV 7BH,R1 MOV 4CH,R2 MOV 7CH,R2 W AITT1: LCALL DISPLAY JNB P1.2,W AITT1 AJMP SETG1 SETG2: SETB 0AH ;调月时闪标志GW AIT2: LCALL DISPLAY JNB P1.1,GW AIT2 SETG3: LCALL DISPLAY JNB P1.1,SETG4 JNB P1.2,GADDMONTH AJMP SETG3 GADDMONTH: MOV R7,65H ;月加 1 LCALL ADD1 MOV 65H,A CJNE A,#13H,GADDMONTH1 MOV 65H,#01H GADDMONTH1: MOV DS1302_ADDR,#88H ;月值送1302 MOV DS1302_DA TA,65H LCALL WRITE MOV R0,65H LCALL DIVIDE ;月分离送显示缓冲MOV 79H,R1 MOV 49H,R1 MOV 7AH,R2 MOV 4AH,R2 W AITT2: LCALL DISPLAY JNB P1.2,W AITT2 AJMP SETG3 SETG4: SETB 0BH ;调日时闪标志GW AIT4: LCALL DISPLAY JNB P1.1,GWAIT4 SETG5: LCALL DISPLAY JNB P1.1,SETG6 JNB P1.2,GADDDAY AJMP SETG5 GADDDAY: MOV R7,63H ;日加 1 LCALL ADD1 MOV 63H,A CJNE A,#32H,GADDDAY1 MOV 63H,#01H GADDDAY1: MOV DS1302_ADDR,#86H ;日值送1302 MOV DS1302_DATA,63H LCALL WRITE MOV R0,63H LCALL DIVIDE ;日分离送显示缓冲MOV 76H,R1 MOV 46H,R1 MOV 77H,R2 MOV 47H,R2 WAITT3: LCALL DISPLAY JNB P1.2,W AITT3 AJMP SETG5 SETG6: SETB 0CH ;调星期时闪标志GW AIT6: LCALL DISPLAY JNB P1.1,GW AIT6 SETG7: LCALL DISPLAY JNB P1.1,SETG8 JNB P1.2,GADDWEEK AJMP SETG7 GADDWEEK: MOV R7,64H ;星期加 1 LCALL ADD1 MOV 64H,A CJNE A,#08H,GADDWEEK1 MOV 64H,#01H GADDWEEK1: MOV DS1302_ADDR,#8AH ;星期值送1302 MOV DS1302_DATA,64H LCALL WRITE MOV R0,64H LCALL DIVIDE ;星期分离送显示缓冲MOV 48H,R1 MOV 78H,R1 W AITT4: LCALL DISPLAY JNB P1.2,WAITT4 AJMP SETG7 SETG8: SETB 0DH ;调小时时闪标志GW AIT8: LCALL DISPLAY JNB P1.1,GWAIT8 SETG9: LCALL DISPLAY JNB P1.1,SETG10 JNB P1.2,GADDHOUR AJMP SETG9 GADDHOUR: MOV R7,62H ;小时加 1 LCALL ADD1 MOV 62H,A CJNE A,#24H,GADDHOUR1 MOV 62H,#00H GADDHOUR1: MOV DS1302_ADDR,#84H ;小时值送1302 MOV DS1302_DATA,62H LCALL WRITE MOV R0,62H LCALL DIVIDE ;小时分离送显示缓冲MOV 74H,R1 MOV 44H,R1 MOV 75H,R2 MOV 45H,R2 W AITT5: LCALL DISPLAY JNB P1.2,W AITT5 AJMP SETG9 SETG10: SETB 0EH ;调分时闪标志GW AIT10: LCALL DISPLAY JNB P1.1,GW AIT10 SETG11: LCALL DISPLAY JNB P1.1,SETGOUT JNB P1.2,GADDMINTUE AJMP SETG11 GADDMINTUE: MOV R7,61H ;分加 1 LCALL ADD1 MOV 61H,A CJNE A,#60H,GADDMINTUE1 MOV 61H,#00H GADDMINTUE1: MOV DS1302_ADDR,#82H ;分值送1302 MOV DS1302_DA TA,61H LCALL WRITE MOV R0,61H LCALL DIVIDE ;分分离送显示缓冲MOV 72H,R1 MOV 42H,R1 MOV 73H,R2 MOV 43H,R2 W AITT6: LCALL DISPLAY JNB P1.2,W AITT6 AJMP SETG11 SETGOUT: LCALL DISPLAY JNB P1.1,SETGOUT LCALL GENGXIN MOV DS1302_ADDR,#80H MOV DS1302_DATA,#00H ;1302晶振开始振荡LCALL WRITE MOV DS1302_ADDR,#8EH MOV DS1302_DATA,#80H ;禁止写入1302 LCALL WRITE CLR 08H CLR 09H CLR 0AH CLR 0BH CLR 0CH CLR 0DH CLR 0EH CLR 0FH CLR ET1 CLR TR1 ;关闪中断LJMP MAIN1 ;*********************************** ;* 闪动调时程序* ;*********************************** INTT1: PUSH ACC PUSH PSW GFLASH: CPL 0FH JB 0FH,GFLASH1 MOV 7CH,4CH ;全部显示MOV 7BH,4BH MOV 7AH,4AH MOV 79H,49H MOV 78H,48H MOV 77H,47H MOV 76H,46H MOV 75H,45H MOV 74H,44H MOV 73H,43H MOV 72H,42H MOV 71H,41H MOV 70H,40H GFLASHOUT: POP PSW POP ACC RETI GFLASH1: JB 0AH,GFLASH2 ;调年闪MOV 7CH,#0AH MOV 7BH,#0AH AJMP GFLASHOUT GFLASH2: JB 0BH,GFLASH3 ;调月闪MOV 7AH,#0AH MOV 79H,#0AH AJMP GFLASHOUT GFLASH3: JB 0CH,GFLASH4 ;调日闪MOV 77H,#0AH MOV 76H,#0AH AJMP GFLASHOUT GFLASH4: JB 0DH,GFLASH5 ;调星期闪MOV 78H,#0AH AJMP GFLASHOUT GFLASH5: JB 0EH,GFLASH6 ;调小时闪MOV 75H,#0AH MOV 74H,#0AH AJMP GFLASHOUT GFLASH6: MOV 73H,#0AH ;调分闪MOV 72H,#0AH AJMP GFLASHOUT ;********************************* ;* 加1程序* ;********************************* ADD1: MOV A,R7 ADD A,#01H DA A RET ;********************************* ;* 分离程序* ;********************************* DIVIDE: MOV A,R0 ANL A,#0FH MOV R1,A MOV A,R0 SW AP A ANL A,#0FH MOV R2,A RET ;********************************* ;* 写1302程序* ;********************************* WRITE: CLR SCLK NOP SETB RST NOP MOV A,DS1302_ADDR MOV R4,#8 WRITE1: RRC A ;送地址给1302 NOP NOP CLR SCLK NOP NOP NOP MOV IO,C NOP NOP NOP SETB SCLK NOP NOP DJNZ R4,WRITE1 CLR SCLK NOP MOV A,DS1302_DATA MOV R4,#8 WRITE2: RRC A NOP ;送数据给1302 CLR SCLK NOP NOP MOV IO,C NOP NOP NOP SETB SCLK NOP NOP DJNZ R4,WRITE2 CLR RST RET ;******************************* ;* 读1302程序* ;******************************* READ: CLR SCLK NOP NOP SETB RST NOP MOV A,DS1302_ADDR MOV R4,#8 READ1: RRC A NOP MOV IO,C NOP NOP NOP SETB SCLK NOP NOP NOP CLR SCLK NOP NOP DJNZ R4,READ1 MOV R4,#8 READ2: CLR SCLK NOP NOP NOP MOV C,IO NOP NOP NOP NOP NOP RRC A NOP NOP NOP NOPSETB SCLK NOP DJNZ R4,READ2 MOV DS1302_DATA,A CLR RST RET ;********************************* ;* BCD码转换为十进制程序* ;********************************* BCD_10: MOV A,R0 ANL A,#0FH MOV R1,A ;高位*10,加低位MOV A,R0 SWAP A ANL A,#0FH MOV B,#10 MUL AB ADD A,R1 MOV R0,A RET ;******************************** ;* 查一年数据程序* ;******************************** CHECKMONTH: LCALL BCD_10 MOV A,R0 MOV B,#04 MUL AB MOV R1,A MOV DPTR,#TAB MOVC A,@A+DPTR MOV R7,A ;第1字节送R7 INC R1 MOV A,R1 MOVC A,@A+DPTR MOV R6,A ;第2字节送R6 INC R1 MOV A,R1 MOVC A,@A+DPTR MOV R5,A ;第3字节送R5 INC R1 MOV A,R1 MOVC A,@A+DPTR MOV R4,A ;第4字节送R4 RET ;****************************** ;* 自动更新农历* ;****************************** GENGXIN: MOV R2,#00H MOV R3,#00H MOV A,MONTH CJNE A,#01H,GM2 MOV R0,DAY ;当前月为1月LCALL BCD_10 ;当前日转换为十进制,送R2 MOV A,R0 MOV R2,A AJMP TIAOZHENG1 GM2: CJNE A,#02H,GM3 MOV R0,DAY ;当前月为2月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加31送R2 ADD A,#31 MOV R2,A AJMP TIAOZHENG1 GM3: CJNE A,#03H,GM4 MOV R0,DAY ;当前月为3月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加59送R2 ADD A,#59 MOV R2,A AJMP TIAOZHENG1 GM4: CJNE A,#04H,GM5 MOV R0,DAY ;当前月为4月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加90送R2 ADD A,#90 MOV R2,A AJMP TIAOZHENG1 GM5: CJNE A,#05H,GM6 MOV R0,DAY ;当前月为5月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加120送R2 ADD A,#120 MOV R2,A AJMP TIAOZHENG1 GM6: CJNE A,#06H,GM7 MOV R0,DAY ;当前月为6月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加151送R2 ADD A,#151 MOV R2,A AJMP TIAOZHENG1 GM7: CJNE A,#07H,GM8 MOV R0,DAY ;当前月为7月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加181送R2 ADD A,#181 MOV R2,A AJMP TIAOZHENG1 GM8: CJNE A,#08H,GM9 MOV R0,DAY ;当前月为8月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加212送R2 ADD A,#212 MOV R2,A AJMP TIAOZHENG1 GM9: CJNE A,#09H,GM10 MOV R0,DAY ;当前月为9月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加243 CLR C ADD A,#243 JC GGG MOV R2,A ;A没溢出,送R2 AJMP TIAOZHENG1 GGG: MOV R3,#0FFH ;R3中送#0FFH INC A ;A溢出,A加1送R2 MOV R2,A AJMP TIAOZHENG1 GM10: CJNE A,#10H,GM11 MOV R0,DAY ;当前月为10月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加18送R2 ADD A,#18 MOV R2,A ;R3中送#0FFH MOV R3,#0FFH AJMP TIAOZHENG1 GM11: CJNE A,#11H,GM12 MOVR0,DAY ;当前月为11月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加49送R2 ADD A,#49 MOV R2,A ;R3中送#0FFH MOV R3,#0FFH AJMP TIAOZHENG1 GM12: MOV R0,DAY ;当前月为12月LCALL BCD_10 ;当前日转换为十进制MOV A,R0 ;再加79送R2 ADD A,#79 MOV R2,A MOV R3,#0FFH ;R3中送#0FFH AJMP TIAOZHENG1 TIAOZHENG1: MOV A,YEAR MOV B,#04 DIV AB MOV A,B JNZ GGG1 MOV A,65H ;当前月是 1 CJNE A,#01H,AAAA AJMP GGG1 ;当前月是2 AAAA: CJNE A,#02H,BBBB AJMP GGG1 BBBB: CJNE R2,#0FFH,GGG2 MOV R3,#0FFH ;当前月不是1和2 MOV R2,#01H ;R1和R2总和加1 AJMP GGG1 GGG2: INC R2 GGG1: MOV R0,YEAR ;查意一年数据LCALL CHECKMONTH MOV A,R2 CLR C SUBB A,R7 MOV P1.6,C CJNE A,#01H,GGG3 MOV R2,#01H MOV A,R3 CJNE A,#00H,NEW1 MOV 57H,#01H ;R2和R3总和减R7 MOV 56H,#01H ; 为0,则为春节LJMP S_PLAY GGG3: CJNE A,#00H,GGG6 MOV R2,A AJMP OLD GGG6: JNB P1.6,NEW2 MOV A,R7 CLR C SUBB A,R2 MOV R7,A CJNE R3,#0FFH,GGG5 MOV A,R3 CLR C SUBB A,R7 MOV R2,A MOV R3,#00H AJMP NEW GGG5: MOV A,R7 MOV R2,A AJMP OLD NEW1: MOV R2,#0FFH MOV R3,#01H AJMP NEW NEW2: MOV R2,A MOV A,R3 CJNE A,#0FFH,GGG4 MOV A,R2 MOV R3,A MOV R2,#0FFH GGG4: AJMP NEW ;*************************** ;* OLD * ;*************************** ;农历为公历的前一年OLD: MOV A,YEAR DEC A MOV R0,A LCALL CHECKMONTH MOV A,R2 CLR C SUBB A,#29 MOV P1.6,C MOV R6,A MOV A,R5 JB ACC.0,AAA ;12月为小月,转到AAA MOV A,R2 CLR C SUBB A,#30 MOV P1.6,C MOV R6,A AAA: MOV A,R6 CJNE A,#00H,BBB MOV 56H,#29H ;为0,则为11月最后一天MOV A,R5 ;小月为11月29 JB ACC.1,CCC MOV 56H,#30H ;大月为11月30 CCC: MOV 57H,#11H LJMP S_PLAY BBB: JNB P1.6,DDD MOV A,#29 ;CY位置位,为12月CLR C SUBB A,R2 MOV R4,A MOV A,R5 JB ACC.0,EEE MOV A,#30 CLR C SUBB A,R2 MOV R4,A EEE: MOV A,R4 MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#12H LJMP S_PLAY DDD: MOV A,#29 ;CY为0,为11月CLR C SUBB A,R4 MOV R1,A MOV A,R5 JB ACC.1,FFF MOV A,#30 CLR C SUBB A,R4 MOV R1,A FFF: MOV A,R1 MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#11H LJMP S_PLAY ;************************* ;* NEW * ;************************* ;农历和公历为同一年,此时R2中值为FFH时,R3有值,否则为0 NEW: NOP M1: MOV A,R2 ;R2减1月天数CLR C SUBB A,#29 MOV R1,A MOV A,R6 JB ACC.6,SM11 MOV A,R2 CLR C SUBB A,#30 MOV R1,A SM11: JC SM12 MOV A,R1 ;R2减1月天数够减MOV R2,A ;转2月AJMP M2 SM12: CJNE R3,#00H,M13 ;R2减1月天数LJMP Y1 ;不够减,判断R3为0否M13: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让1月减MOV R2,A ;还不够转Y1 MOV R3,#00H AJMP M1 M2: CLR C ;R2减1月天数够减MOV A,R2 ;转3月SUBB A,#29 MOV R1,A MOV A,R6 JBACC.5,M21 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M21: JC M22 MOV A,R1 ;R2减2月天数够减MOV R2,A AJMP MM2 M22: CJNE R3,#00H,M23 ;R2减2月天数LJMP Y2 ;不够减,判断R3为0否M23: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让2月减MOV R2,A ;还不够转Y2 MOV R3,#00H AJMP M2 MM2: LCALL CHECKRE ;查2月,如果不为闰月CJNE A,#02H,M3 ;转3月RM2: MOV A,R4 ANL A,#0FH ;R2减闰2月天数JNZ RM21 MOV A,R2 CLR C SUBB A,#30 MOV R1,A AJMP RM22 RM21: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM22: JC RM23 MOV A,R1 ;R2减闰2月天数,够减MOV R2,A ;转3月AJMP M3 RM23: CJNE R3,#00H,RM24 ; R2减润2月天数LJMP RY2 ;不够减,判断R3为0否RM24: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让闰2月减MOV R3,#00H ;还不够转RY2 AJMP RM2 M3: MOV A,R2 ;R2减3月天数CLR C SUBB A,#29 MOV R1,A MOV A,R6 JB ACC.4,M31 CLR C MOV A,R2 SUBB A,#30 MOV R1,A M31: JC M32 MOV A,R1 ; R2减3月天数,够减MOV R2,A AJMP MM3 M32: CJNE R3,#00H,M33 ;R2减3月天数LJMP Y3 ;不够减,判断R3为0否M33: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让3月减MOV R2,A ; 还不够,则转Y3 MOV R3,#00H AJMP M3 MM3: LCALL CHECKRE ;查3月,如果不为闰月CJNE A,#03H,M4 ;转4月RM3: MOV A,R4 ANL A,#0FH ;R2减闰3月天数JNZ RM31 MOV A,R2 CLR C SUBB A,#30 MOV R1,A AJMP RM32 RM31: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM32: JC RM33 MOV A,R1 ;R2减闰3月天数,够减MOV R2,A ;转4月AJMP M4 RM33: CJNE R3,#00H,RM34 LJMP RY3 ;R2减润3月天数RM34: MOV A,R2 ;不够减,判断R3为0否ADD A,R3 ;不为0,R3加到R2中MOV R2,A ;再让闰3月减MOV R3,#00H ;还不够转RY3 AJMP RM3 M4: MOV A,R2 ;R2减4月天数CLR C SUBB A,#29 MOV R1,A MOV A,R6 JB ACC.2,M41 CLR C MOV A,R2 SUBB A,#30 MOV R1,A M41: JC M42 MOV A,R1 ;R2减4月天数,够减MOV R2,A AJMP MM4 M42: CJNE R3,#00H,M43 ;R2减4月天数LJMP Y4 ;不够减,判断R3为0否M43: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让4月减MOV R2,A ;还不够,则转Y4 MOV R3,#00H AJMP M4 MM4: LCALL CHECKRE ;查4月,如果不为闰月CJNE A,#04H,M5 ;转5月RM4: MOV A,R4 ANLA,#0FH ;R2减闰4月天数JNZ RM41 MOV A,R2 CLR C SUBB A,#30 MOV R1,A AJMP RM42 RM41: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM42: JC RM43 MOV A,R1 ;R2减闰4月天数,够减MOV R2,A ;转5月AJMP M5 RM43: CJNE R3,#00H,RM44 LJMP RY4 ;R2减润4月天数RM44: MOV A,R2 ;不够减,判断R3为0否ADD A,R3 ;不为0,R3加到R2中MOV R2,A ;再让闰4月减MOV R3,#00H ;还不够转RY4 AJMP RM4 M5: MOV A,R2 ;R2减5月天数CLR C SUBB A,#29 MOV R1,A MOV A,R6 JB ACC.1,M51 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M51: JC M52 MOV A,R1 ;R2减5月天数,够减MOV R2,A AJMP MM5 M52: CJNE R3,#00H,M53 ;R2减5月天数LJMP Y5 ;不够减,判断R3为0否M53: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让5月减MOV R2,A ;还不够,则转Y5 MOV R3,#00H LJMP M5 MM5: LCALL CHECKRE ;查5月,如果不为闰月CJNE A,#05H,M6 ;转6月RM5: MOV A,R4 ANL A,#0FH ;R2减闰5月天数JNZ RM51 MOV A,R2 CLR C SUBB A,#30 MOV R1,A AJMP RM52 RM51: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM52: JC RM53 MOV A,R1 ;R2减闰5月天数,够减MOV R2,A ;转6月LJMP M6 RM53: CJNE R3,#00H,RM54 LJMP RY5 ;R2减润5月天数RM54: MOV A,R2 ;不够减,判断R3为0否ADD A,R3 ;不为0,R3加到R2中MOV R2,A ;再让闰5月减MOV R3,#00H ;还不够转RY5 LJMP RM5 M6: MOV A,R2 ;R2减6月天数CLR C SUBB A,#29 MOV R1,A MOV A,R6 JB ACC.0,M61 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M61: JC M62 MOV A,R1 ;R2减6月天数,够减MOV R2,A AJMP MM6 M62: CJNE R3,#00H,M63 ;R2减6月天数LJMP Y6 ;不够减,判断R3为0否M63: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让6月减MOV R2,A ;还不够,则转Y6 MOV R3,#00H AJMP M6 MM6: LCALL CHECKRE ;查6月,如果不为闰月CJNE A,#06H,M7 ;转7月RM6: MOV A,R4 ANL A,#0FH ;R2减闰6月天数JNZ RM61 MOV A,R2 CLR C SUBB A,#30 MOV R1,A AJMP RM62 RM61: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM62: JC RM63 MOV A,R1 ;R2减闰6月天数,够减MOV R2,A ;转7月LJMP M7 RM63: CJNE R3,#00H,RM64 LJMP RY6 ;R2减润6月天数RM64: MOV A,R2 ;不够减,判断R3为0否ADD A,R3 ;不为0,R3加到R2中MOV R2,A ;再让闰6月减MOVR3,#00H ;还不够转RY6 LJMP RM6 M7: MOV A,R2 ;R2减7月天数CLR C SUBB A,#29 MOV R1,A MOV A,R5 JB ACC.6,M71 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M71: JC M72 MOV A,R1 ;R2减7月天数,够减MOV R2,A AJMP MM7 M72: CJNE R3,#00H,M73 ;R2减7月天数LJMP Y7 ;不够减,判断R3为0否M73: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让7月减MOV R2,A ;还不够,则转Y7 MOV R3,#00H AJMP M7 MM7: LCALL CHECKRE ;查7月,如果不为闰月CJNE A,#07H,M8 ;转8月RM7: MOV A,R4 ANL A,#0FH ;R2减闰7月天数JNZ RM71 MOV A,R2 CLR C SUBB A,#30 MOV R1,A LJMP RM72 RM71: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM72: JC RM73 MOV A,R1 ;R2减闰7月天数,够减MOV R2,A ;转8月LJMP M8 RM73: CJNE R3,#00H,RM74 LJMP RY7 ;R2减润5月天数RM74: MOV A,R2 ;不够减,判断R3为0否ADD A,R3 ;不为0,R3加到R2中MOV R2,A ;再让闰7月减MOV R3,#00H ;还不够转RY7 LJMP RM7 M8: MOV A,R2 ;R2减8月天数CLR C SUBB A,#29 MOV R1,A MOV A,R5 JB ACC.5,M81 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M81: JC M82 MOV A,R1 ;R2减8月天数,够减MOV R2,A LJMP M9 ;转9月M82: CJNE R3,#00H,M83 ;R2减8月天数LJMP Y8 ;不够减,判断R3为0否M83: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让8月减MOV R2,A ;还不够,则转Y8 MOV R3,#00H LJMP M8 M9: MOV A,R2 ;R2减9月天数CLR C SUBB A,#29 MOV R1,A MOV A,R5 JB ACC.4,M91 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M91: JC M92 MOV A,R1 ;R2减9月天数,够减MOV R2,A LJMP MM9 M92: CJNE R3,#00H,M93 ;R2减9月天数LJMP Y9 ;不够减,判断R3为0否M93: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让9月减MOV R2,A ;还不够,则转Y7 MOV R3,#00H LJMP M9 MM9: LCALL CHECKRE ;查7月,如果不为闰月CJNE A,#09H,M10 ;转10月RM9: MOV A,R4 ANL A,#0FH ;R2减闰9月天数JNZ RM91 MOV A,R2 CLR C SUBB A,#30 MOV R1,A LJMP RM92 RM91: MOV A,R2 CLR C SUBB A,#29 MOV R1,A RM92: JC RM93 MOV A,R1 ;R2减闰9月天数,够减MOV R2,A ;转10月LJMP M10 RM93: CJNE R3,#00H,RM94 LJMP RY9 ;R2减润9月天数RM94: MOVA,R2 ;不够减,判断R3为0否ADD A,R3 ;不为0,R3加到R2中MOV R2,A ;再让闰9月减MOV R3,#00H ;还不够转RY9 LJMP RM9 M10: MOV A,R2 ;R2减10月天数CLR C SUBB A,#29 MOV R1,A MOV A,R5 JB ACC.2,M101 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M101: JC M102 MOV A,R1 ;R2减10月天数,够减MOV R2,A LJMP M11 ;转11月M102: CJNE R3,#00H,M103 ;R2减10月天数LJMP Y10 ;不够减,判断R3为0否M103: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让10月减MOV R2,A ;还不够,则转Y10 MOV R3,#00H LJMP M10 M11: MOV A,R2 ;R2减11月天数CLR C SUBB A,#29 MOV R1,A MOV A,R5 JB ACC.1,M111 MOV A,R2 CLR C SUBB A,#30 MOV R1,A M111: JC M112 MOV A,R1 ;R2减11月天数,够减MOV R2,A LJMP M12 M112: CJNE R3,#00H,M113 ;R2减11月天数LJMP Y11 ;不够减,判断R3为0否M113: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让10月减MOV R2,A ;还不够,则转Y10 MOV R3,#00H LJMP M11 M12: MOV A,R2 ;R2减12月天数CLR C SUBB A,#29 MOV R1,A MOV A,R5 JB ACC.0,M121 CLR C MOV A,R2 SUBB A,#30 MOV R1,A M121: CJNE R3,#00H,M122 ;R2减12月天数LJMP Y12 ;不够减,判断R3为0否M122: MOV A,R2 ;不为0,R3加到R2中ADD A,R3 ;再让10月减MOV R2,A ;还不够,则转Y10 MOV R3,#00H LJMP M12 Y1: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#01H CLR F0 ;非闰月LJMP S_PLAY Y2: CJNE R2,#00H,Y21 MOV A,R6 ;R2为0,则为前一月最后一天MOV 56H,#29H JB ACC.6,Y22 MOV 56H,#30H Y22: MOV 57H,#01H CLR F0 LJMP S_PLAY Y21: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#02H CLR F0 LJMP S_PLAY RY2: CJNE R2,#00H,RY21 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R6 JB ACC.5,RY22 MOV 56H,#30H RY22: MOV 57H,#02H CLR F0 LJMP S_PLAY RY21: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#02H SETB F0 ;闰月LJMP S_PLAY Y3: CJNE R2,#00H,Y31 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#02H,Y32 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y33 MOV 56H,#30H MOV 57H,#02H SETB F0 LJMP S_PLAY Y33: MOV 57H,#02H SETB F0 LJMP S_PLAY Y32: MOV 56H,#29H ;前一个月不是闰月MOV A,R6 JB ACC.5,Y34 MOV 56H,#30H Y34: MOV 57H,#02H CLR F0 LJMPS_PLAY Y31: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#03H CLR F0 LJMP S_PLAY RY3: CJNE R2,#00H,RY31 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R6 JB ACC.4,RY32 MOV 56H,#30H RY32: MOV 57H,#03H CLR F0 LJMP S_PLAY RY31: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#03H SETB F0 ;闰月LJMP S_PLAY Y4: CJNE R2,#00H,Y41 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#03H,Y42 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y43 MOV 56H,#30H MOV 57H,#03H SETB F0 LJMP S_PLAY Y43: MOV 57H,#03H SETB F0 LJMP S_PLAY Y42: MOV 56H,#29H ;前一个月不是闰月MOV A,R6 JB ACC.4,Y44 MOV 56H,#30H Y44: MOV 57H,#03H CLR F0 LJMP S_PLAY Y41: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#04H CLR F0 LJMP S_PLAY RY4: CJNE R2,#00H,RY41 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R6 JB ACC.2,RY42 MOV 56H,#30H RY42: MOV 57H,#04H CLR F0 LJMP S_PLAY RY41: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#04H SETB F0 ;闰月LJMP S_PLAY Y5: CJNE R2,#00H,Y51 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#04H,Y52 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y53 MOV 56H,#30H MOV 57H,#04H SETB F0 LJMP S_PLAY Y53: MOV 57H,#04H SETB F0 LJMP S_PLAY Y52: MOV 56H,#29H ;前一个月不是闰月MOV A,R6 JB ACC.2,Y44 MOV 56H,#30H Y54: MOV 57H,#04H CLR F0 LJMP S_PLAY Y51: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#05H CLR F0 LJMP S_PLAY RY5: CJNE R2,#00H,RY51 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R6 JB ACC.1,RY52 MOV 56H,#30H RY52: MOV 57H,#05H CLR F0 LJMP S_PLAY RY51: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#05H SETB F0 ;闰月LJMP S_PLAY Y6: CJNE R2,#00H,Y61 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#05H,Y62 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y63 MOV 56H,#30H MOV 57H,#05H SETB F0 LJMP S_PLAY Y63: MOV 57H,#05H SETB F0 LJMP S_PLAY Y62: MOV 56H,#29H ;前一个月不是闰月MOV A,R6 JB ACC.1,Y64 MOV 56H,#30H Y64: MOV 57H,#05H CLR F0 LJMP S_PLAY Y61: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#06H CLR F0 LJMP S_PLAY RY6: CJNE R2,#00H,RY61 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R6 JB ACC.0,RY62 MOV 56H,#30H RY62: MOV 57H,#06H CLR F0 LJMP S_PLAY RY61: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#06H SETB F0 ;闰月LJMP S_PLAY Y7: CJNE R2,#00H,Y71 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#06H,Y72 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y73 MOV 56H,#30H MOV 57H,#06H SETB F0 LJMP S_PLAY Y73: MOV 57H,#06H SETB F0 LJMP S_PLAY Y72: MOV 56H,#29H ;前一个月不是闰月MOV A,R6 JB ACC.0,Y74 MOV 56H,#30H Y74: MOV 57H,#06H CLR F0 LJMP S_PLAY Y71: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#07H CLR F0 LJMP S_PLAY RY7: CJNE R2,#00H,RY71 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R6 JB ACC.6,RY72 MOV 56H,#30H RY72: MOV 57H,#07H CLR F0 LJMP S_PLAY RY71: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#07H SETB F0 ;闰月LJMP S_PLAY Y8: CJNE R2,#00H,Y81 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#07H,Y82 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y83 MOV 56H,#30H MOV 57H,#07H SETB F0 LJMP S_PLAY Y83: MOV 57H,#07H SETB F0 LJMP S_PLAY Y82: MOV 56H,#29H ;前一个月不是闰月MOV A,R6 JB ACC.6,Y84 MOV 56H,#30H Y84: MOV 57H,#07H CLR F0 LJMP S_PLAY Y81: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#08H CLR F0 LJMP S_PLAY Y9: CJNE R2,#00H,Y91 MOV 56H,#29H MOV A,R5 JB ACC.5,Y94 MOV 56H,#30H Y94: MOV 57H,#08H CLR F0 LJMP S_PLAY Y91: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#09H CLR F0 LJMP S_PLAY RY9: CJNE R2,#00H,RY91 MOV 56H,#29H ;R2为0,则为前一月最后一天MOV A,R5 JB ACC.4,RY92 MOV 56H,#30H RY92: MOV 57H,#09H CLR F0 LJMP S_PLAY RY91: MOV A,R2 ;R2中为日的值MOV R0,A LCALL D_BCD MOV 56H,A MOV 57H,#09H SETB F0 ;闰月LJMP S_PLAY Y10: CJNE R2,#00H,Y101 LCALL CHECKRE ;查前一个月是否为闰月CJNE A,#09H,Y102 MOV 56H,#29H ;前一个月是闰月MOV A,R4 ANL A,#0FH ;R2中为0,则为前一个月的最后一天JNZ Y103 MOV 56H,#30H MOV 57H,#09H SETB F0 LJMP S_PLAY Y103: MOV 57H,#09H SETB F0 LJMP S_PLAY Y102: MOV 56H,#29H ;前一个月不是闰月MOV A,R5 JB ACC.4,Y104 MOV 56H,#30H Y104: MOV 57H,#09H CLR F0 LJMP S_PLAY Y101: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#010H CLR F0 LJMP S_PLAY Y11: CJNE R2,#00H,Y111 MOV 56H,#29H MOV A,R5 JB ACC.2,Y112 MOV 56H,#30H Y112: MOV 57H,#10H CLR F0 LJMP S_PLAY Y111: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#11H CLR F0 LJMP S_PLAY Y12: CJNE R2,#00H,Y121 MOV 56H,#29H MOV A,R5 JB ACC.1,Y122 MOV 56H,#30H Y122: MOV 57H,#11H CLR F0 LJMP S_PLAY Y121: MOV A,R2 MOV R0,A ;R2中为日的值LCALL D_BCD MOV 56H,A MOV 57H,#12H CLR F0 LJMP S_PLAY S_PLAY: MOV R0,57H LCALL DIVIDE ;月分离送显示缓存MOV 52H,R1 MOV 53H,R2 MOV R0,56H LCALL DIVIDE ;日分离送显示缓存MOV 50H,R1 MOV 51H,R2 RET ;******************************** ;* 查是否闰月程序* ;******************************** CHECKRE: MOV A,R4 SW AP A ANL A,#0FHRET ;******************************** ;* 十进制转换为BCD码程序* ;******************************** D_BCD: MOV A,R0 MOV B,#10 DIV AB SWAP A ORL A,B RET ;******************************** ;* 显示程序* ;******************************** DISPLAY: MOV 7EH,#02H MOV 7DH,#00H MOV R0,#76H MOV R1,#70H MOV R2,79H MOV R3,#79H MOV R5,#0FEH PLAY1: MOV A,#0FFH MOV P2,A MOV A,@R0 MOV DPTR,#TAB1 MOVC A,@A+DPTR MOV SBUF,A ;送第1字节JNB TI,$ CLR TI MOV A,@R1 MOV DPTR,#TAB1 MOVC A,@A+DPTR MOV SBUF,A ;送第2字节JNB TI,$ CLR TI MOV A,R2 MOV DPTR,#TAB1 MOVC A,@A+DPTR MOV SBUF,A ;送第3字节JNB TI,$ CLR TI INC R0 INC R1 INC R3 MOV A,R3 SETB PSW.4 ;选2组寄存器MOV R0,A MOV A,@R0 CLR PSW.4 MOV R2,A MOV A,R0 CJNE A,#78H,PLAY2 MOV R0,#50H PLAY2: CJNE A,#54H,PLAY3 MOV R0,#78H PLAY3: MOV A,R5 MOV P2,A LCALL DL1MS MOV A,R5 JNB ACC.6,PLAYOUT RL A MOV R5,A LJMP PLAY1 PLAYOUT: SETB P2.6 RET DL1MS: MOV R6,#14H DL1: MOV R7,#19H DL2: DJNZ R7,DL2 DJNZ R6,DL1 RET ;***************************** ;* 数据表* ;***************************** TAB1: DB 03H,9FH,25H,0DH,99H DB 49H,41H,1FH,01H,09H,0FFH TAB: DB 35,15H,51H,00H,23,11H,52H,41H,42,12H,65H,00H DB 31,11H,32H,00H,21,42H,52H,21H,39,52H,25H,00H DB 28,25H,04H,71H,48,66H,42H,00H,37,33H,22H,00H DB 25,15H,24H,51H,44,05H,52H,00H,33,22H,65H,00H DB 22,21H,25H,41H,40,24H,52H,00H,30,52H,42H,91H DB 49,55H,05H,00H,38,26H,44H,00H,27,53H,50H,60H DB 46,53H,24H,00H,35,25H,54H,00H,24,41H,52H,41H DB 42,45H,25H,00H,31,24H,52H,00H,21,51H,12H,21H DB 40,55H,11H,00H,28,26H,21H,61H,47,26H,61H,00H DB 36,13H,31H,00H,25,05H,31H,51H,43,12H,54H,00H DB 33,51H,25H,00H,22,42H,25H,31H,41,32H,22H,00H DB 30,55H,02H,71H,49,55H,22H,00H,38,26H,62H,00H DB 27,13H,64H,60H,45,13H,32H,00H,34,12H,55H,00H DB 23,10H,53H,51H,42,22H,45H,00H,31,52H,22H,00H DB 21,52H,44H,21H,40,55H,44H,00H,29,26H,50H,71H DB 47,26H,64H,00H,36,25H,32H,00H,25,23H,32H,50H DB 44,44H,55H,00H,32,24H,45H,00H,22,55H,11H,30H END。
万年历初始源程序
12864、DS1302制作的电子万年历代码如下:/********************************************************************名称:电子万年历功能:自动计时100年,节日、时间提醒作者:甘春生实验板:KX-1N/******************************************************************** #include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit RS=P2^0;sbit RW=P2^1;sbit EN=P2^2;sbit FUN=P1^0;sbit OK=P1^1;sbit INC=P1^2;sbit DEC=P1^3;sbit RST=P2^5;sbit IO=P2^6;sbit SCLK=P2^7;uchar hour=10;uchar min=18;uchar sec=30;uchar year=10;uchar mon=1;uchar date=20;uchar day=3;uchar count=0;uchar flag=0;uchar num=0;uchar i=6;void delay(uint i){uint j;for(;i>0;i--)for(j=0;j<125;j++);}void Write_Ds1302_Byte(uchar temp){uchar i;for(i=0;i<8;i++)SCLK=0;IO=temp&0x01;//只传最后一位temp>>=1;SCL=1;}}void Write_Ds1302(uchar address,uchar dat) {RST=0;SCLK=0;RST=1;Write_Ds1302_Byte(address);Write_Ds1302_Byte(dat);SCLK=1;RST=0;}uchar Read_Ds1302(uchar address){uchar i,temp=0x00;RST=0;SCLK=0;RST=1;Write_Ds1302_Byte(address);for(i=0;i<8;i++){temp>>=1;SCLK=0;if(IO)temp=temp|0x80;SCLK=1;}RST=0;IO=0;return temp;}void Check_Busy(void){RW=1;//RW=1为读RS=0;//RS=0为指令EN=1;while(P2&0X80);}void Write_com(uchar com)Check_Busy();RW=0;RS=0;P2=com;EN=1;delay(1);EN=0;}void Write_dat(uchar dat){Check_Busy();RW=0;RS=1;P2=dat;EN=1;delay(1);EN=0;}void Lcd_Pos(uchar Xpos,uchar Ypos){uchar tmp;Xpos&=0x0f;Ypos&=0x03;tmp=Xpos;if(Ypos==0)tmp|=0x80;if(Ypos==1)tmp|=0x90;if(Ypos==2)tmp|=0x88;if(Ypos==3)tmp|=0x98;Write_com(tmp);}void Write_LCD_String(uchar x,uchar y,uchar *s) {Lcd_Pos(x,y);Lcd_Pos(x,y);while (*s!='\0'){Write_dat(*s);s++;delay(1);}void LCD_Rst(void){Write_com(0x30);Check_Busy();Write_com(0x01);Write_com(0x06);Write_com(0x0c);delay(5);}void Write_day(uchar Sel){switch (Sel){case 1:Write_LCD_String(6,1,"一");break;case 2:Write_LCD_String(6,1,"二");break;case 3:Lcd_Pos(6,1);Write_dat(0xc8);Write_dat(0xfd);break;case 4:Write_LCD_String(6,1,"四");break;case 5:Write_LCD_String(6,1,"五");break;case 6:Write_LCD_String(6,1,"六");break;case 7:Write_LCD_String(6,1,"日");break;default :break;}}void Updat_Time(void){Lcd_Pos(3,0);Write_dat(0x30+(year/10));Write_dat(0x30+(year%10)); Lcd_Pos(0,1);Write_dat(0x30+(mon/10));Write_dat(0x30+(mon%10)); Lcd_Pos(2,1);Write_dat(0x30+(date/10));Write_dat(0x30+(date%10)); Lcd_Pos(0,2);Write_dat(0x30+(hour/10));Write_dat(0x30+(hour%10)); Lcd_Pos(2,2);Write_dat(0x30+(min/10));Write_dat(0x30+(min%10)); Lcd_Pos(4,2);Write_dat(0x30+(sec/10));Write_dat(0x30+(sec%10)); Write_day(day);}void Write_Time(void){Write_Ds1302(0x8e,0x00);Write_Ds1302(0x8c,(year/10)*16+year%10);Write_Ds1302(0x88,(mon/10)*16+mon%10);Write_Ds1302(0x86,(date/10)*16+date%10);Write_Ds1302(0x84,(hour/10)*16+hour%10);Write_Ds1302(0x82,(min/10)*16+min%10);Write_Ds1302(0x80,(sec/10)*16+sec%10);Write_Ds1302(0x8a,(day/10)*16+day%10);Write_Ds1302(0x8e,0x80);}void Read_Time(void){year=(Read_Ds1302(0x8d)>>4)*10+Read_Ds1302(0x8d)%16; mon=(Read_Ds1302(0x89)>>4)*10+Read_Ds1302(0x89)%16; date=(Read_Ds1302(0x87)>>4)*10+Read_Ds1302(0x87)%16; hour=(Read_Ds1302(0x85)>>4)*10+Read_Ds1302(0x85)%16; min=(Read_Ds1302(0x83)>>4)*10+Read_Ds1302(0x83)%16; sec=(Read_Ds1302(0x81)>>4)*10+Read_Ds1302(0x81)%16; day=(Read_Ds1302(0x8b)>>4)*10+Read_Ds1302(0x8b)%16; Updat_Time();}void Set_Time(void){if(FUN==0){delay(10);if(FUN==0){while(!FUN);flag=1;num++;if(num==8)num=1;}}if(flag){if(num==1){Lcd_Pos(0,2);Write_com(0x0d);if(INC==0){delay(10);if(INC==0){while(!INC);hour++;if(hour==24)hour=0;}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);hour--;if(hour==-1)hour=23;Updat_Time();}}}if(num==2){Lcd_Pos(2,2);Write_com(0x0d);if(INC==0){delay(10);if(INC==0){while(!INC);min++;if(min==60)min=0;Updat_Time();}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);min--;if(min==-1)min=59;}}}if(num==3){Lcd_Pos(4,2);Write_com(0x0d);if(INC==0){delay(10);if(INC==0){while(!INC);sec++;if(sec==60)sec=0;Updat_Time();}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);sec--;if(sec==-1)sec=59;Updat_Time();}}}if(num==4){Lcd_Pos(3,0);Write_com(0x0d);if(INC==0){delay(10);if(INC==0){while(!INC);year++;year=0;Updat_Time();}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);year--;if(year==-1)sec=99;Updat_Time();}}}if(num==5){Lcd_Pos(0,1);Write_com(0x0d); if(INC==0){delay(10);if(INC==0){while(!INC);mon++;if(mon==13)mon=1;Updat_Time();}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);mon--;if(mon==0)mon=12;Updat_Time();}}}if(num==6){Lcd_Pos(2,1);Write_com(0x0d); if(INC==0){delay(10);if(INC==0){while(!INC);date++;if(date==32)date=1;Updat_Time();}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);date--;if(date==0)date=31;Updat_Time();}}}if(num==7){Lcd_Pos(6,1);Write_com(0x0d); if(INC==0){delay(10);if(INC==0){while(!INC);day++;if(day==8)day=1;Updat_Time();}}if(DEC==0){delay(10);if(DEC==0){while(!DEC);day--;if(day==0)day=7;Updat_Time();}}}}if(OK==0){delay(10);if(OK==0){while(!OK);flag=0;num=0;Write_com(0x0c);Write_Time();}}}void Festival(void){if (mon == 1 && date== 1 ) { Write_LCD_String(0,3,"Happy New Year!!"); } elseif (mon == 1 && date== 28 ) { Write_LCD_String(0,3," 世界麻风日"); } elseif (mon == 2 && date== 2 ) { Write_LCD_String(0,3," 世界湿地日"); } elseif (mon == 2 && date== 13 ) { Write_LCD_String(0,3," 明天情人节了"); } elseif (mon == 2 && date== 14 ) { Write_LCD_String(0,3," 今天是情人节"); } elseif (mon == 3 && date== 1 ) { Write_LCD_String(0,3," 国际海豹日"); }elseif (mon == 3 && date== 3 ) { Write_LCD_String(0,3," 全国爱耳日"); } elseif (mon == 3 && date== 8 ) { Write_LCD_String(0,3," 3.8妇女节"); } elseif (mon == 3 && date== 12 ) { Write_LCD_String(0,3," 植树节"); } elseif (mon == 3 && date== 14 ) { Write_LCD_String(0,3," 国际警察日"); } elseif (mon == 3 && date== 15 ) { Write_LCD_String(0,3," 消费者权益日"); } elseif (mon == 3 && date== 17 ) { Write_LCD_String(0,3," 国际航海日"); } elseif (mon == 3 && date== 21 ) { Write_LCD_String(0,3," 世界森林日"); } elseif (mon == 3 && date== 22 ) { Write_LCD_String(0,3," 世界水日"); } elseif (mon == 3 && date== 23 ) { Write_LCD_String(0,3," 世界气象日"); } elseif (mon == 3 && date== 24 ) { Write_LCD_String(0,3,"世界防治结核病日"); } elseif (mon == 4 && date== 1 ) { Write_LCD_String(0,3,"愚人节小心上当"); } elseif (mon == 4 && date== 7 ) { Write_LCD_String(0,3," 世界卫生日"); } elseif (mon == 4 && date== 8 ) { Write_LCD_String(0,3," 复活节"); } elseif (mon == 4 && date== 13 ) { Write_LCD_String(0,3," 黑色星期五"); } elseif (mon == 5 && date== 1 ) { Write_LCD_String(0,3," 劳动节放假"); } elseif (mon == 5 && date== 4 ) { Write_LCD_String(0,3," 青年节"); } elseif (mon == 5 && date== 8 ) { Write_LCD_String(0,3," 世界红十字日"); } elseif (mon == 5 && date== 12 ) { Write_LCD_String(0,3," 国际护士节"); } elseif (mon == 5 && date== 5 ) { Write_LCD_String(0,3,"近日注意母亲节"); } // &nb, , , , , sp;elseif (mon == 5 && date== 15 ) { Write_LCD_String(0,3," 国际家庭日"); } elseif (mon == 5 && date== 31 ) { Write_LCD_String(0,3," 世界无烟日"); } elseelseif (mon == 6 && date== 5 ) { Write_LCD_String(0,3," 世界环境日"); } elseif (mon == 6 && date== 26 ) { Write_LCD_String(0,3," 国际禁毒日"); } elseif (mon == 6 && date== 6 ) { Write_LCD_String(0,3," 全国爱眼日"); } elseif (mon == 6 && date== 13 ) { Write_LCD_String(0,3,"近日注意父亲节"); } elseif (mon == 6 && date== 15 ) { Write_LCD_String(0,3,"近日注意父亲节"); } elseif (mon == 7 && date== 1 ) { Write_LCD_String(0,3,"香港回归记念日"); } elseif (mon == 7 && date== 7 ) { Write_LCD_String(0,3,"抗日战争记念日"); } elseif (mon == 7 && date== 11 ) { Write_LCD_String(0,3," 世界人口日"); } elseif (mon == 8 && date== 1 ) { Write_LCD_String(0,3," 八一建军节"); } elseif (mon == 8 && date== 8 ) { Write_LCD_String(0,3," 中国男子节"); } elseif (mon == 8 && date== 15 ) { Write_LCD_String(0,3,"抗战胜利记念日"); } elseif (mon == 9 && date== 10 ) { Write_LCD_String(0,3," 中国教师节"); } elseif (mon == 9 && date== 18 ) { Write_LCD_String(0,3,"九·一八事变记念"); } elseif (mon == 9 && date== 20 ) { Write_LCD_String(0,3," 国际爱牙日"); } elseif (mon == 9 && date== 27 ) { Write_LCD_String(0,3," 世界旅游日"); } elseif (mon == 10 && date== 1 ) { Write_LCD_String(0,3," 中国国庆节"); } elseif (mon == 10 && date== 4 ) { Write_LCD_String(0,3," 世界动物日"); } elseif (mon == 10 && date== 24 ){ Write_LCD_String(0,3," 联合国日"); } elseif (mon == 10 && date== 12 ){ Write_LCD_String(0,3,"明天国际教师节"); } elseif (mon == 10 && date== 13 ){ Write_LCD_String(0,3," 国际教师节"); } elseif (mon == 11 && date== 10 ){ Write_LCD_String(0,3," 世界青年节"); } elseelseif (mon == 12 && date== 1 ) { Write_LCD_String(0,3," 世界艾滋病日"); } elseif (mon == 12 && date== 23 ){ Write_LCD_String(0,3," 明晚平安夜"); }elseif (mon == 12 && date== 24 ){ Write_LCD_String(0,3," 今晚平安夜"); }elseif (mon == 12 && date== 25 ){ Write_LCD_String(0,3," 圣诞快乐"); }elseif (mon == 12 && date== 31 ){ Write_LCD_String(0,3," 明日新年"); }else{if(hour >= 4 && hour< 6 ) { Write_LCD_String(0,3,"★★__▲▲__凌晨"); } if(hour >= 6 && hour< 8 ) { Write_LCD_String(0,3,"☆○__▲△__早晨"); } if(hour >= 8 && hour< 12 ) { Write_LCD_String(0,3,"__●__▲▲__上午"); } if(hour == 12) { Write_LCD_String(0,3,"____▲●▲__中午"); }if(hour >= 13 && hour< 18 ){ Write_LCD_String(0,3,"__▲▲__●__下午"); } if(hour >= 18 && hour< 22 ){ Write_LCD_String(0,3,"△▲__●☆__晚上"); } if(hour >= 22 && hour<= 23 ){ Write_LCD_String(0,3,"△▲__★☆__夜里"); } if(hour >= 0 && hour< 4 ) { Write_LCD_String(0,3,"__★▲▲★__深夜"); } }}void System_Init(void){LCD_Rst();Write_LCD_String(0,1,"系统初始化");Lcd_Pos(5,1);while(i--){Write_dat(0x3e);delay(300);}Write_com(0x01);Write_LCD_String(0,1,"您好!欢迎使用!");delay(1500);Write_com(0x01);Write_LCD_String(2,0,"2010年");Write_LCD_String(0,1,"01月18日星期一");Write_LCD_String(0,2,"10时17分30秒");}void main(void){System_Init();Write_Time(); while(1){Set_Time();if(!flag){Read_Time();Festival();}}}。
万年历程序源代码
#include<iostream>#include<iomanip>#include <ctime>using namespace std;int dayofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dd[12]={ 31,28,31,30,31,30,31,31,30,31,30,31 };class datee{private:int d,m,y;public:int isLeap( int y );int getDayPerMonth( int y , int m );int getNumDay( datee D );bool legalDate( datee D );void count();};/*ÅжÏÊÇ·ñÊÇÈòÄê*/int datee::isLeap( int y ){if( !(y%4)&&y%100 || !(y%400) )return 1;return 0;}/*¼ÆË㲻ͬÇé¿ö¶þÔ·ݵÄÌìÊý*/int datee::getDayPerMonth( int y , int m ){if( m==2 )return dd[1]+isLeap( y );return dd[m-1];}/*¼ÆËãʱ¼ä²î*/int datee::getNumDay( datee D ){int ret=0;int i;for( i=1 ; i<=D.y ; i++ )ret+=365+isLeap( D.y );for( i=1 ; i<D.m ; i++ )ret+=getDayPerMonth( D.y , i );ret+=D.d;return ret;}/*ÔËÓò¼¶ûÐͺ¯ÊýÅжÏÊäÈëÊý¾ÝµÄÕýÎó*/bool datee::legalDate( datee D ){if( D.y<0||D.y>9999 )return false;if( D.m<1||D.m>12 )return false;if( D.d<1||D.d>getDayPerMonth( D.y , D.m ) )return false;return true;}void datee::count(){long day;datee sysDate,date;cout<<"please input what date you want to compared:"<<endl; cout<<"year month day"<<endl;cout<<"the one:";cin>>date.y>>date.m>>date.d;while( !legalDate(date) ){cout<<"illegal date,input again: "<<endl;cin>>date.y>>date.m>>date.d;}cout<<"the tow:";cin>>sysDate.y>>sysDate.m>>sysDate.d;while( !legalDate(sysDate) ){cout<<"illegal system date,input again: "<<endl;cin>>sysDate.y>>sysDate.m>>sysDate.d;}day=getNumDay(date)-getNumDay(sysDate);if(day<0)day=-day;cout<<"the time difference is :"<<endl<<day<<endl;}class Date{private:int year;int month;int day;public:void SetDate();int IsLeap(int year);void ShowDate();int getdays(int year,int month,int day);void showweek();void printmonth();void printyear();void input(){cin>>year>>month;while(month<1||month>12){cout<<"illegal month,please input again:"<<endl;cin>>year>>month;panduan();}}void panduan(){if(month<1||month>12){cout<<"illegal month,please input again:"<<endl;cin>>year>>month;}}};void Date::SetDate(){int yy,mm,dd;cout<<"please input the date:"<<endl<<"year month date"<<endl;cin>>yy>>mm>>dd;if(yy<=0){ cout<<"Year value error!"<<endl;}if(mm<=0&&mm>12){cout<<"Month value error!"<<endl;exit(1);}year=yy;month=mm;switch(month){case 4:case 6:case 9:case 11:{if(dd>=1&&dd<=30)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}} break;case 2:{if(IsLeap(year)){if(dd>=1&&dd<=29)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}}else{if(dd>=1&&dd<=28)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}}}break;default:{if(dd>=1&&dd<=31)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}}}}int Date::IsLeap(int year){if(year%400==0||(year%100!=0&&year%4==0))return 1;elsereturn 0;}void Date::ShowDate(){cout<<year<<"-"<<month<<"-"<<day<<" ";}int Date::getdays(int year,int month,int day){int i,sum=0;for(i=1;i<year;i++){if(IsLeap(i))sum+=366;elsesum+=365;}if(IsLeap(year))dayofmonth[2]=29;for(i=1;i<month;i++)sum+=dayofmonth[i];sum+=day;return sum;}void Date::showweek(){int s;s=getdays(year,month,day)%7;switch(s){case 0:cout<<"Sunday"<<endl;break;case 1:cout<<"Monday"<<endl;break;case 2:cout<<"Tuesday"<<endl;break;case 3:cout<<"Wednesday"<<endl;break;case 4:cout<<"Thursday"<<endl;break;case 5:cout<<"Friday"<<endl;break;case 6:cout<<"Saturday"<<endl;break;}}void Date::printmonth(){if(year%4==0)cout<<" "<<year<<" "<<month<<" "<<"(ÈòÄê)"<<endl;elsecout<<" "<<year<<" "<<month<<" ¡¡"<<"(·ÇÈòÄê)"<<endl; cout<<"ÈÕ "<<"Ò» "<<"¶þ "<<"Èý"<<" ËÄ "<<"Îå "<<"Áù "<<endl;cout<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<endl;int s=getdays(year,month,1);s=s%7;for(int m=0;m<s;m++)cout<<" ";cout<<setw(2)<<setfill(' ')<<"1"<<" ";if(IsLeap(year)) dayofmonth[2]=29;int yue=dayofmonth[month];for(int i=2;i<8-s;i++)cout<<setw(2)<<setfill(' ')<<i<<' ';cout<<endl;int j;for(i=8-s,j=1;i<=yue;i++,j++){cout<<setw(2)<<setfill(' ')<<i<<' ';if(j%7==0)cout<<endl;}cout<<endl;}void Date::printyear(){int y,m;cin>>y;year=y;if(year%4==0)cout<<"------the year is "<<year<<"£¨ÈòÄ꣩"<<"----------"<<endl;elsecout<<"------the year is "<<year<<"£¨·ÇÈòÄ꣩"<<"---------"<<endl;for( m=1;m<13;m++) //×ܹ²12¸öÔ·Ý{cout<<" ***********************"<<endl;cout<<endl<<setw(5)<<m<<""<<year<<"Äê"<<setw(5)<<m<<"Ô·Ý"<<endl;cout<<" "<<"ÈÕ "<<"Ò» "<<"¶þ "<<"Èý"<<" ËÄ "<<"Îå "<<"Áù "<<endl;cout<<" "<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<endl;month=m;int s=getdays(year,month,1);s=s%7;for(int m=0;m<s;m++)cout<<" ";cout<<" "<<setw(2)<<setfill(' ')<<"1"<<" ";if(IsLeap(year)) dayofmonth[2]=29;int yue=dayofmonth[month];for(int i=2;i<8-s;i++)cout<<setw(2)<<setfill(' ')<<i<<' ';cout<<endl<<" ";int j;for(i=8-s,j=1;i<=yue;i++,j++){cout<<setw(2)<<setfill(' ')<<i<<' ';if(j%7==0)cout<<endl<<" ";}cout<<endl<<" ***********************"<<endl;}}class m{private:int a,b,number;public:void months(){if (b==1||b==3||b==5||b==7||b==8||b==10||b==12){number=31;}elseif(b==4||b==6||b==9||b==11){number=30;}elseif(b==2){if(a%4==0){number=29;}else{number=28;}}cout<<number<<endl;}void month(){cin>>a>>b;if(b>0&&b<13)months();else{cout<<"error!"<<endl;cout<<"please input again:"<<endl;cin>>a>>b;months();}}};int main(){int choose;char ch;Date mydate;system("title »¶Ó-ʹÓÃÍòÄêÀú!");while(choose!=0){cout<<"************************************************************"<<endl;cout<<" »¶Ó-ʹÓÃÍòÄêÀú! Ô¸ÄúʹÓõÄ˳ÐÄ£¬¿ªÐÄ!"<<endl;/*»ñȡϵͳʱ¼ä*/time_t timen;time(&timen);tm* t_tm = localtime(&timen);cout<<" ϵͳµ±Ç°Ê±¼äΪ: "<<t_tm->tm_year+1900<<"Äê"<<t_tm->tm_mon+1<<"ÔÂ"<<t_tm->tm_mday<<"ÈÕ"<<endl;cout<<"ÇëÑ¡Ôñ¡°0¡«5¡±"<<endl;cout<<"1£º¼ÆËãʱ¼ä²î 2£º²éѯijÔÂÌìÊý"<<endl;cout<<"3£º²éѯijÈÕÐÇÆÚ 4£ºÏÔʾijÄêÈÕÀú"<<endl;cout<<"5£º²éѯijÄêijÔÂÔÂÀú"<<endl;cout<<"0£ºÍ˳ö"<<endl;cout<<"******************************************************** ****"<<endl;cin>>choose;switch(choose) //¹¦ÄÜÑ¡Ôñ{case 1:datee myday;myday.count();/*ÅжÏÊÇ·ñÇåÆÁ²¢ÏÔʾÖ÷²Ëµ¥*/cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");//ÇåÆÁ£¬²¢·µ»ØÖ÷²Ëµ¥system("COLOR a1");break;}elsebreak; //ÏÔʾÖ÷²Ëµ¥case 2:cout<<"please input what you wangt to know the year,month:";m a;a.month();cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 1b");break;}elsebreak;case 3:mydate.SetDate();mydate.ShowDate();mydate.showweek();mydate.printmonth();cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 6e");break;}elsebreak;case 4:cout<<"please input what years you want to know£º";cout<<endl;mydate.printyear();cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 3d");break;}elsebreak;case 5:cout<<"please input the year,month what you want to know."<<endl;mydate.input();mydate.printmonth();cout<<"please choose whether or not return the menu 'y' or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 55");break;}elsebreak;case 0:cout<<"ллʹÓÃÍòÄêÀúϵͳ£¬Çë¹Ø±Õ!!!!!"<<endl;exit(0);break;default:cout<<"ÄúÊäÈëÓÐÎó£¬ÇëÖØÐÂÊäÈë"<<endl;}}return 0;}。
万年历程序
万年历程序简介万年历(Perpetual Calendar)是一个可以显示公历和农历的时间工具。
在这个文档中,我们将讨论如何编写一个简单的万年历程序,以及其实现原理和功能。
功能该万年历程序将具备以下功能:1.显示当前日期2.显示当前周数3.显示当前月份的日历4.显示指定年份和月份的日历实现原理为了实现万年历程序,我们需要使用一些基本的计算方法。
以下是一些相关的计算原理:判断某年是否是闰年判断某年是否是闰年可以使用以下公式:is_leap_year = (year % 4 == 0 and year % 100 ! = 0) or (year % 400 == 0)若上述公式的值为真,则表示该年份是闰年。
计算指定年份和月份的天数可以使用以下公式来计算指定年份和月份的天数:days_in_month = [31, # 1月28 + is_leap_year, # 2月31, # 3月30, # 4月31, # 5月30, # 6月31, # 7月31, # 8月30, # 9月31, # 10月30, # 11月31 # 12月]这样,我们就可以得到每个月份的天数。
判断某年某月的第一天是星期几我们可以使用Zeller’s Congruence算法来判断某年某月的第一天是星期几。
以下是该算法的公式:h = (day + 26*(month+1)//10 + k + k//4 + j//4 + 5*j) % 7其中,h是星期几(0代表星期六,1代表星期日,依次类推),day是月份的第一天的日期,month是月份(3代表三月,4代表四月,依次类推),k是该年的前两位数,j是该年的后两位数。
实现步骤以下是实现万年历程序的步骤:1.获取当前日期和时间2.使用相关公式判断当前年份是否是闰年3.显示当前日期和时间4.计算当前周数并显示5.获取当前月份的相关信息:年份、月份、天数、星期几6.显示当前月份的日历7.提供用户界面,允许用户输入指定的年份和月份8.使用相关算法计算指定年份和月份的日历9.显示指定年份和月份的日历示例代码以下是使用Python语言实现的一个简单的万年历程序:```python import datetime获取当前日期today = datetime.date.today() current_year = today.year current_month = today.month current_day = today.day判断当前年份是否是闰年is_leap_year = (current_year % 4 == 0 and current_year % 100 != 0) or (current_year % 400 == 0)显示当前日期和时间print(。
万年历程序(不要嫌程序长,功能很齐全,而且对初学者很有帮助)
#include <REG51.H>#include <intrins.h>#define uint unsigned int#define uchar unsigned charvoid Stopwatch(); //声明秒表函数void Clock(); //声明闹钟函数void Output(); //声明输出函数void important(); //声明重要函数(相当于mian()函数)void TLimit(); //声明调节温度上下限函数sbit DS1302_CLK = P1^7; //实时时钟时钟线引脚sbit DS1302_IO = P1^6; //实时时钟数据线引脚sbit DS1302_RST = P1^5; //实时时钟复位线引脚sbit wireless_1 = P3^0; //温度过上限标志sbit wireless_2 = P3^1; //温度适中标志sbit wireless_3 = P3^2; //温度过下限标志sbit fan = P3^4; //开风扇标志sbit clock=P3^5; //开关闹钟sbit setclock = P3^6; //闹钟开启控制sbit stopwatch = P3^7; //秒表开启控制sbit ACC0 = ACC^0;sbit ACC7 = ACC^7;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; //秒,分,时到日,月,年位闪的计数sbit Set = P2^0; //模式切换键sbit Up = P2^1; //加法按钮sbit Down = P2^2; //减法按钮sbit out = P2^3; //立刻跳出调整模式按钮sbit DQ = P1^0; //温度传送数据IO口char c3,c2=0;char c=0,LowerBonud=20,UpperLimit=30;char a1=0,a2=0,a3=0,a4=0;char k[8]={'0','0',':','0','0',':','0','0'};void InitTimer0(void)//产生10ms时间间隔{TMOD = (TMOD & 0XF0) | 0X01; //定时器0工作在模式1,16Bit定时器模式TH0 = (65536 - 8000)/256;TL0 = (65536 - 8000)%256;TR0 =1; ET0 =1;//开启定时器和中断EA = 1;}char done,count,temp,flag,up_flag,down_flag; //uchar temp_value; //温度值uchar TempBuffer[5],week_value[2],h[8]; //unsigned char NL[5];void show_time(); //液晶显示程序/***********1602液晶显示部分子程序****************///Port Definitions**********************************************************sbit LcdRs = P2^5;sbit LcdRw = P2^6;sbit LcdEn = P2^7;sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口//内部等待函数************************************************************************** unsigned char LCD_Wait(void){LcdRs=0;LcdRw=1; _nop_();LcdEn=1; _nop_();LcdEn=0;return DBPort;}//向LCD写入命令或数据************************************************************#define LCD_COMMAND 0 // Command#define LCD_DATA 1 // Data#define LCD_CLEAR_SCREEN 0x01 // 清屏#define LCD_HOMING 0x02 // 光标返回原点void LCD_Write(bit style, unsigned char input){LcdEn=0;LcdRs=style;LcdRw=0; _nop_();DBPort=input; _nop_();//注意顺序LcdEn=1; _nop_();//注意顺序LcdEn=0; _nop_();LCD_Wait();}//设置显示模式************************************************************#define LCD_SHOW 0x04 //显示开#define LCD_HIDE 0x00 //显示关#define LCD_CURSOR 0x02 //显示光标#define LCD_NO_CURSOR 0x00 //无光标#define LCD_FLASH 0x01 //光标闪动#define LCD_NO_FLASH 0x00 //光标不闪动void LCD_SetDisplay(unsigned char DisplayMode){LCD_Write(LCD_COMMAND, 0x08|DisplayMode);}//设置输入模式************************************************************ #define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 // default#define LCD_MOVE 0x01 // 画面可平移#define LCD_NO_MOVE 0x00 //defaultvoid LCD_SetInput(unsigned char InputMode){LCD_Write(LCD_COMMAND, 0x04|InputMode);}//初始化LCD************************************************************ void LCD_Initial(){LcdEn=0;LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示, 无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增, 画面不动}//液晶字符输入的位置************************void GotoXY(unsigned char x, unsigned char y){if(y==0)LCD_Write(LCD_COMMAND,0x80|x);if(y==1)LCD_Write(LCD_COMMAND,0x80|(x-0x40));}//将字符输出到液晶显示void Print(unsigned char *str){while(*str!='\0')//while(*str!='\0'){LCD_Write(LCD_DATA,*str);str++;}}/***********DS1302时钟部分子程序******************/typedef struct SYSTEMTIME{unsigned char Second;unsigned char Minute;unsigned char Hour;unsigned char Week;unsigned char Day;unsigned char Month;unsigned char Year;unsigned char YearH;unsigned char YearL;unsigned char day;unsigned char month;unsigned char year;unsigned char DateString[11];unsigned char NLDateString[12];unsigned char TimeString[9];}SYSTEMTIME; //定义的时间类型SYSTEMTIME CurrentTime; //#define AM(X) X#define PM(X) (X+12) // 转成24小时制#define DS1302_SECOND 0x80 //时钟芯片的寄存器位置,存放时间#define DS1302_MINUTE 0x82#define DS1302_HOUR 0x84#define DS1302_WEEK 0x8A#define DS1302_DAY 0x86#define DS1302_MONTH 0x88#define DS1302_YEAR 0x8C#define DS1302_YEARH 0xC0void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) {unsigned char i;ACC = d;for(i=8; i>0; i--){DS1302_IO = ACC0; //相当于汇编中的RRCDS1302_CLK = 1;DS1302_CLK = 0;ACC = ACC >> 1;}}unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数){unsigned char i;for(i=8; i>0; i--){ACC = ACC >>1; //相当于汇编中的RRCACC7 = DS1302_IO;DS1302_CLK = 1;DS1302_CLK = 0;}return(ACC);}void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据{DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr); // 地址,命令DS1302InputByte(ucDa); // 写1Byte数据DS1302_CLK = 1;DS1302_RST = 0;}unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据{unsigned char ucData;DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr|0x01); // 地址,命令ucData = DS1302OutputByte(); // 读1Byte数据DS1302_CLK = 1;DS1302_RST = 0;return(ucData);}void DS1302_GetTime(SYSTEMTIME *Time) //获取时钟芯片的时钟数据到自定义的结构型数组{unsigned char ReadV alue;ReadValue = Read1302(DS1302_SECOND);Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MINUTE);Time->Minute = ((ReadV alue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_HOUR);Time->Hour = ((ReadV alue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_DAY);Time->Day = ((ReadV alue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_WEEK);Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_MONTH);Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_YEAR);Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadV alue&0x0F);Time->YearH = 20;}void DateToStr(SYSTEMTIME *Time) //将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]{ if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22{Time->DateString[0] = Time->YearH/10 + '0';Time->DateString[1] = Time->YearH%10 + '0';Time->DateString[2] = Time->Year/10 + '0';Time->DateString[3] = Time->Year%10 + '0';}else{Time->DateString[0] = ' ';Time->DateString[1] = ' ';Time->DateString[2] = ' ';Time->DateString[3] = ' ';}Time->DateString[4] = '/';if(hide_month<2){Time->DateString[5] = Time->Month/10 + '0';Time->DateString[6] = Time->Month%10 + '0';}else{Time->DateString[5] = ' ';Time->DateString[6] = ' ';}Time->DateString[7] = '/';if(hide_day<2){Time->DateString[8] = Time->Day/10 + '0';Time->DateString[9] = Time->Day%10 + '0';}else{Time->DateString[8] = ' ';Time->DateString[9] = ' ';}if(hide_week<2){week_value[0] = Time->Week%10 + '0'; //星期的数据另外放到week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示}else{week_value[0] = ' ';}week_value[1] = '\0';Time->DateString[10] = '\0'; //字符串末尾加'\0' ,判断结束字符}void TimeToStr(SYSTEMTIME *Time) //将时,分,秒数据转换成液晶显示字符放到数组TimeString[];{ if(hide_hour<2){Time->TimeString[0] = Time->Hour/10 + '0';Time->TimeString[1] = Time->Hour%10 + '0';h[0]=Time->TimeString[0];h[1]=Time->TimeString[1];}else{Time->TimeString[0] = ' ';Time->TimeString[1] = ' ';}Time->TimeString[2] = ':';h[2]=Time->TimeString[2];if(hide_min<2){Time->TimeString[3] = Time->Minute/10 + '0';Time->TimeString[4] = Time->Minute%10 + '0';h[3]=Time->TimeString[3];h[4]=Time->TimeString[4];}else{Time->TimeString[3] = ' ';Time->TimeString[4] = ' ';}Time->TimeString[5] = ':';h[5]=Time->TimeString[5];if(hide_sec<2){Time->TimeString[6] = Time->Second/10 + '0';Time->TimeString[7] = Time->Second%10 + '0';h[6]=Time->TimeString[6];h[7]=Time->TimeString[7];}else{Time->TimeString[6] = ' ';Time->TimeString[7] = ' ';}Time->DateString[8] = '\0';if ((a1==h[4])&&(a2==h[3])&&(a3<=h[7])&&((a4==h[6])||(a4==h[6]-1))) {clock=0;c=10;}}void Initial_DS1302(void) //时钟芯片初始化{unsigned char Second=Read1302(DS1302_SECOND);if(Second&0x80) //判断时钟芯片是否关闭{Write1302(0x8e,0x00); //写入允许Write1302(0xC0,0x20);Write1302(0x8c,0x09); //以下写入初始化时间日期:2007/07/25.星期: 3. 时间: 23:59:55 Write1302(0x88,0x06);Write1302(0x86,0x05);Write1302(0x8a,0x07); //xingqiWrite1302(0x84,0x23);Write1302(0x82,0x59);Write1302(0x80,0x55);Write1302(0x8e,0x80); //禁止写入}}/***********ds18b20子程序*************************//***********ds18b20延迟子函数(晶振12MHz )*******/void delay_18B20(unsigned int i){while(i--);}/**********ds18b20初始化函数**********************/void Init_DS18B20(void){unsigned char x=0;DQ = 1; //DQ复位delay_18B20(8); //稍做延时DQ = 0; //单片机将DQ拉低delay_18B20(80); //精确延时大于480usDQ = 1; //拉高总线delay_18B20(14);x=DQ; //稍做延时后如果x=0则初始化成功x=1则初始化失败delay_18B20(20);}/***********ds18b20读一个字节**************/unsigned char ReadOneChar(void){uchar i=0;uchar dat = 0;for (i=8;i>0;i--){DQ = 0; // 给脉冲信号dat>>=1; //数据右移一位DQ = 1; // 给脉冲信号if(DQ)dat|=0x80; //按位或,取最高位delay_18B20(4);}return(dat);}/*************ds18b20写一个字节****************/void WriteOneChar(uchar dat){unsigned char i=0;for (i=8; i>0; i--){DQ = 0;DQ = dat&0x01; //取最低位delay_18B20(5);DQ = 1; //上升沿将数据送入dat>>=1;}}/**************读取ds18b20当前温度************/void ReadTemp(void){unsigned char a=0;unsigned char b=0;unsigned char t=0;Init_DS18B20();WriteOneChar(0xCC); // 跳过读序号列号的操作WriteOneChar(0x44); // 启动温度转换delay_18B20(100); // this message is wery importantInit_DS18B20();WriteOneChar(0xCC); //跳过读序号列号的操作WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器)前两个就是温度delay_18B20(100);a=ReadOneChar(); //读取温度值低位b=ReadOneChar(); //读取温度值高位temp_value=b<<4; //左移四位,扩大16倍,变为一个字节的高四位temp_value+=(a&0xf0)>>4;//取温度值的地位的高四位,右移四位后与温度的高四位相加得到温度值if (temp_value>=UpperLimit) {wireless_1 =0;fan=1;}else {wireless_1 =1;fan=0;}if ((temp_value>LowerBonud)&&(temp_value<UpperLimit)) wireless_2 =0;else wireless_2 =1;if (temp_value<=LowerBonud) wireless_3 =0;else wireless_3 =1;}void temp_to_str() //温度数据转换成液晶字符显示{TempBuffer[0]=temp_value/10+'0'; //十位TempBuffer[1]=temp_value%10+'0'; //个位TempBuffer[2]=0xdf; //温度符号TempBuffer[3]='C';TempBuffer[4]='\0';}void Delay1ms(unsigned int count){unsigned int i,j;for(i=0;i<count;i++)for(j=0;j<120;j++){if (!stopwatch) {c++;Stopwatch();}if (!setclock) {c2++;Clock();}if ((!out)&&(!Set)) {TLimit();}if (!out) clock=1; //关闭闹钟}}/*延时子程序*/void mdelay(uint delay){ uint i;for(;delay>0;delay--)for(i=0;i<32;i++); //1ms延时.}/*****************************阳历转换阴历***********************************/ code unsigned char year_code[597]={0x04,0xAe,0x53, //1901 00x0A,0x57,0x48, //1902 30x55,0x26,0xBd, //1903 60x0d,0x26,0x50, //1904 90x0d,0x95,0x44, //1905 120x46,0xAA,0xB9, //1906 150x05,0x6A,0x4d, //1907 180x09,0xAd,0x42, //1908 210x24,0xAe,0xB6, //19090x04,0xAe,0x4A, //19100x6A,0x4d,0xBe, //19110x0A,0x4d,0x52, //19120x0d,0x25,0x46, //19130x5d,0x52,0xBA, //19140x0B,0x54,0x4e, //19150x0d,0x6A,0x43, //19160x29,0x6d,0x37, //19170x09,0x5B,0x4B, //19180x74,0x9B,0xC1, //19190x04,0x97,0x54, //19200x0A,0x4B,0x48, //19210x5B,0x25,0xBC, //19220x06,0xA5,0x50, //19230x06,0xd4,0x45, //19240x4A,0xdA,0xB8, //19250x02,0xB6,0x4d, //19260x09,0x57,0x42, //19270x24,0x97,0xB7, //19280x04,0x97,0x4A, //19290x66,0x4B,0x3e, //19300x0d,0x4A,0x51, //19310x0e,0xA5,0x46, //19320x56,0xd4,0xBA, //19330x05,0xAd,0x4e, //19340x02,0xB6,0x44, //19350x39,0x37,0x38, //19360x09,0x2e,0x4B, //19370x0C,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, //19810x0A,0x4B,0x4d, //19830xAB,0x25,0xC2, //19840x06,0xA5,0x54, //19850x06,0xd4,0x49, //19860x6A,0xdA,0x3d, //19870x0A,0xB6,0x51, //19880x09,0x37,0x46, //19890x54,0x97,0xBB, //19900x04,0x97,0x4f, //19910x06,0x4B,0x44, //19920x36,0xA5,0x37, //19930x0e,0xA5,0x4A, //19940x86,0xB2,0xBf, //19950x05,0xAC,0x53, //19960x0A,0xB6,0x47, //19970x59,0x36,0xBC, //19980x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //20010x0d,0x4A,0x4C, //20020x0d,0xA5,0x41, //20030x25,0xAA,0xB6, //20040x05,0x6A,0x49, //20050x7A,0xAd,0xBd, //20060x02,0x5d,0x52, //20070x09,0x2d,0x47, //20080x5C,0x95,0xBA, //20090x0A,0x95,0x4e, //20100x0B,0x4A,0x43, //20110x4B,0x55,0x37, //20120x0A,0xd5,0x4A, //20130x95,0x5A,0xBf, //20140x04,0xBA,0x53, //20150x0A,0x5B,0x48, //20160x65,0x2B,0xBC, //20170x05,0x2B,0x50, //20180x0A,0x93,0x45, //20190x47,0x4A,0xB9, //20200x06,0xAA,0x4C, //20210x0A,0xd5,0x41, //20220x24,0xdA,0xB6, //20230x04,0xB6,0x4A, //20240x69,0x57,0x3d, //20250x0d,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, //20690x8A,0x95,0xBf, //20710x0A,0x95,0x53, //20720x0B,0x4A,0x47, //20730x6B,0x55,0x3B, //20740x0A,0xd5,0x4f, //20750x05,0x5A,0x45, //20760x4A,0x5d,0x38, //20770x0A,0x5B,0x4C, //20780x05,0x2B,0x42, //20790x3A,0x93,0xB6, //20800x06,0x93,0x49, //20810x77,0x29,0xBd, //20820x06,0xAA,0x51, //20830x0A,0xd5,0x46, //20840x54,0xdA,0xBA, //20850x04,0xB6,0x4e, //20860x0A,0x57,0x43, //20870x45,0x27,0x38, //20880x0d,0x26,0x4A, //20890x8e,0x93,0x3e, //20900x0d,0x52,0x52, //20910x0d,0xAA,0x47, //20920x66,0xB5,0x3B, //20930x05,0x6d,0x4f, //20940x04,0xAe,0x45, //20950x4A,0x4e,0xB9, //20960x0A,0x4d,0x4C, //20970x0d,0x15,0x41, //20980x2d,0x92,0xB5, //2099};//月份数据表code unsigned char day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};code unsigned int day_code2[3]={0x111,0x130,0x14e};/**************************************************************************** * 子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0****************************************************************************/ bit GetMoonDay(unsigned char month_p,unsigned short table_addr){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:if((year_code[table_addr+2]&0x80)==0) return(0);else return(1);}return(0);}/****************************************************************************** ***************************** 函数名称:GetChinaCalendar** 功能描述:公农历转换(只允许1901-2099年)** 输入: year 公历年** month 公历月** day 公历日** p 储存农历日期地址** 输出: 1 成功** 0 失败** 作者: Campo** 修改: 赖皮** 日期: 2007年02月06日**-------------------------------------------------------------------------------------------------------** 修改人:** 日期:**------------------------------------------------------------------------------------------------------******************************************************************************* *************************/unsigned char GetChinaCalendar(unsigned int year,unsigned char month,unsigned char day){unsigned char temp1,temp2,temp3,month_p,yearH,yearL;unsigned char flag_y;unsigned short temp4,table_addr;yearH=year/100; yearL=year%100;if((yearH!=19)&&(yearH!=20)) return(0);// 定位数据表地址if(yearH==20) table_addr=(yearL+100-1)*3;else table_addr=(yearL-1)*3;// 取当年春节所在的公历月份temp1=year_code[table_addr+2]&0x60;temp1>>=5;// 取当年春节所在的公历日temp2=year_code[table_addr+2]&31;// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月if(temp1==1) t emp3=temp2-1;else temp3=temp2+31-1;// 计算公历日离当年元旦的天数if (month<10) temp4=day_code1[month-1]+day-1;else temp4=day_code2[month-10]+day-1;// 如果公历月大于2月并且该年的2月为闰月,天数加1if ((month>2)&&(yearL%4==0)) temp4++;// 判断公历日在春节前还是春节后if (temp4>=temp3){temp4-=temp3;month=1;month_p=1;flag_y=0;if(GetMoonDay(month_p,table_addr)==0) temp1=29; //小月29天else temp1=30; //大小30天// 从数据表中取该年的闰月月份,如为0则该年无闰月temp2=year_code[table_addr]/16;while(temp4>=temp1){temp4-=temp1;month_p++;if(month==temp2){NL[4]='R';flag_y=~flag_y;if(flag_y==0) {month++;NL[4]=' ';}}else {month++;NL[4]=' ';}if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;}day=temp4+1;}// 公历日在春节前使用下面代码进行运算else{temp3-=temp4;if (yearL==0){yearL=100-1;yearH=19;}else yearL--;table_addr-=3;month=12;temp2=year_code[table_addr]/16;if (temp2==0) {month_p=12;NL[4]=' ';}else {month_p=13;NL[4]='R';}flag_y=0;if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;while(temp3>temp1){temp3-=temp1;month_p--;if(flag_y==0) month--;if(month==temp2) flag_y=~flag_y;if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;}day=temp1-temp3+1;}/////////////////////////NL[0]=yearH;NL[1]=yearL;NL[2]=month;NL[3]=day;return(1);}void zhuan(SYSTEMTIME *Time){GetChinaCalendar(Time->YearH*100+Time->Year,Time->Month,Time->Day);Time->day = NL[3];Time->month =NL[2];//将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22{Time->NLDateString[0] = NL[0]/10 + '0';Time->NLDateString[1] = NL[0]%10 + '0';Time->NLDateString[2] = NL[1]/10 + '0';Time->NLDateString[3] = NL[1]%10 + '0';}else{Time->NLDateString[0] = ' ';Time->NLDateString[1] = ' ';Time->NLDateString[2] = ' ';Time->NLDateString[3] = ' ';}Time->NLDateString[4] = '/';if(hide_month<2){Time->NLDateString[5] =NL[4] ;Time->NLDateString[6] = Time->month/10 + '0';Time->NLDateString[7] = Time->month%10 + '0';}else{Time->NLDateString[6] = ' ';Time->NLDateString[7] = ' ';}Time->NLDateString[8] = '/';if(hide_day<2){Time->NLDateString[9] = Time->day/10 + '0';Time->NLDateString[10] = Time->day%10 + '0';}else{Time->NLDateString[9] = ' ';Time->NLDateString[10] = ' ';}Time->NLDateString[11] = '\0'; //字符串末尾加'\0' ,判断结束字符//////////////////////}//显示天干生肖void lcd_digan(){temp = NL[1]%12;switch(temp){case 0: GotoXY(3,0); Print("Chen LongYear");//龙break;case 1: GotoXY(3,0); Print("Si She ear");//蛇break;case 2: GotoXY(3,0); Print("Wu Ma Year");//马break;case 3: GotoXY(3,0); Print("Wei YangYear"); //羊break;case 4: GotoXY(3,0); Print("Chen Hou Year"); //猴break;case 5: GotoXY(3,0); Print("You Ji Year"); //鸡break;case 6: GotoXY(3,0); Print("Xu Gou Year"); //狗break;case 7: GotoXY(3,0); Print("Hai Zhu Year"); //猪break;case 8: GotoXY(3,0); Print("Zi Shu Year"); //鼠break;case 9: GotoXY(3,0); Print("Chou Niu Year"); //牛break;case 10:GotoXY(3,0); Print("Yin Hu Year"); //虎break;case 11:GotoXY(3,0); Print("Mao Tu Year"); //兔break;}}//显示天支void lcd_tianzhi(){temp = NL[1]%10;switch(temp){case 0: GotoXY(0,0); Print("Gen");//庚break;case 1: GotoXY(0,0); Print("Xin");//辛break;case 2: GotoXY(0,0); Print("Ren");//壬break;case 3: GotoXY(0,0); Print("Gui"); //癸?break;case 4: GotoXY(0,0); Print("Jia"); //甲break;case 5: GotoXY(0,0); Print("Yi"); //乙break;case 6: GotoXY(0,0); Print("Bin"); //丙break;case 7: GotoXY(0,0); Print("Din"); //丁break;case 8: GotoXY(0,0); Print("Wu"); //戊break;case 9: GotoXY(0,0); Print("Ji");//己break;}}void outkey() //跳出调整模式,返回默认显示{ uchar Second;if(out==0)//if(out==0||wireless_1==1) //{ mdelay(4);count=0;hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;Second=Read1302(DS1302_SECOND);Write1302(0x8e,0x00); //写入允许Write1302(0x80,Second&0x7f);Write1302(0x8E,0x80); //禁止写入done=0;while(out==0);//while(wireless_1==1);}}////////////////////////////////////////////////////////////////////////////////////////////////////////////void Upkey()//升序按键{Up=1;if(Up==0)//if(Up==0||wireless_2==1){mdelay(4);switch(count){case 1:temp=Read1302(DS1302_SECOND); //读取秒数temp=temp+1; //秒数加1up_flag=1; //数据调整后更新标志if((temp&0x7f)>0x59) //超过59秒,清零temp=0;break;case 2:temp=Read1302(DS1302_MINUTE); //读取分数temp=temp+1; //分数加1up_flag=1;if(temp>0x59) //超过59分,清零temp=0;break;case 3:temp=Read1302(DS1302_HOUR); //读取小时数temp=temp+1; //小时数加1up_flag=1;if(temp>0x23) //超过23小时,清零temp=0;break;case 4:temp=Read1302(DS1302_WEEK); //读取星期数temp=temp+1; //星期数加1up_flag=1;if(temp>0x7)temp=1;break;case 5:temp=Read1302(DS1302_DAY); //读取日数temp=temp+1; //日数加1up_flag=1;if(temp>0x31)temp=1;break;case 6:temp=Read1302(DS1302_MONTH); //读取月数temp=temp+1; //月数加1up_flag=1;if(temp>0x12)temp=1;break;case 7:temp=Read1302(DS1302_YEAR); //读取年数temp=temp+1; //年数加1up_flag=1;if(temp>0x85)temp=0;break;default:break;}while(Up==0);//while(wireless_2==1);}}////////////////////////////////////////////////////////////////////////////////////////////////////////////void Downkey()//降序按键{Down=1;if(Down==0)//if(Down==0||wireless_3==1){mdelay(4);switch(count){case 1:temp=Read1302(DS1302_SECOND); //读取秒数temp=temp-1; //秒数减1down_flag=1; //数据调整后更新标志if(temp==0x7f) //小于0秒,返回59秒temp=0x59;break;case 2:temp=Read1302(DS1302_MINUTE); //读取分数temp=temp-1; //分数减1down_flag=1;if(temp==-1)temp=0x59; //小于0秒,返回59秒break;case 3:temp=Read1302(DS1302_HOUR); //读取小时数temp=temp-1; //小时数减1down_flag=1;if(temp==-1)temp=0x23;break;case 4:temp=Read1302(DS1302_WEEK); //读取星期数temp=temp-1; //星期数减1down_flag=1;if(temp==0)temp=0x7;;break;case 5:temp=Read1302(DS1302_DAY); //读取日数temp=temp-1; //日数减1down_flag=1;if(temp==0)temp=31;break;case 6:temp=Read1302(DS1302_MONTH); //读取月数temp=temp-1; //月数减1down_flag=1;if(temp==0)temp=12;break;case 7:temp=Read1302(DS1302_YEAR); //读取年数temp=temp-1; //年数减1down_flag=1;if(temp==-1)temp=0x85;break;default:break;}while(Down==0);//while(wireless_3==1);}}void Setkey()//模式选择按键{Set=1;if(Set==0)//if(Set==0||wireless_4==1){mdelay(4);count=count+1; //Setkey按一次,count就加1done=1; //进入调整模式while(Set==0);//while(wireless_4==1);}}void keydone()//按键功能执行{ uchar Second;if(flag==0) //关闭时钟,停止计时{ Write1302(0x8e,0x00); //写入允许temp=Read1302(0x80);Write1302(0x80,temp|0x80);Write1302(0x8e,0x80); //禁止写入flag=1;。
c语言万年历并判断是否为闰年课程设计程序
【导言】C语言是一门被广泛使用的计算机编程语言,它的应用范围涵盖了各个领域。
而在计算机程序设计课程中,常常会有关于时间和日期的相关内容。
今天我们将讨论一个关于C语言编程的课程设计题目——C 语言万年历并判断是否为闰年程序设计。
【一、问题描述】1. 万年历的定义:所谓万年历,是指能够展示任意年份的日历,并且准确反映该年的月份和日期分布。
2. 闰年的定义:闰年是指公历中普通年份为365天,而闰年为366天的年份。
闰年有特定的判断规则。
【二、程序设计目标】1. 设计一个C语言程序,能够根据用户输入的年份,打印出该年的日历。
2. 在打印日历的判断该年份是否为闰年,并给出判断结果。
【三、程序设计思路】1. 确定程序的输入和输出:输入:用户输入一个年份输出:打印该年的日历,并给出是否为闰年的判断结果2. 了解计算闰年的规则:- 能被4整除但不能被100整除的年份为闰年- 能被400整除的年份也为闰年3. 设计程序的逻辑:- 首先根据输入的年份计算该年的日历,并以直观的形式输出- 然后根据闰年的判断规则,判断该年份是否为闰年,并给出判断结果【四、程序设计实现】以下是C语言程序的设计实现:```c#include <stdio.h>// 判断闰年的函数int isLeapYear(int year) {return (year 4 == 0 year 100 != 0) || (year 400 == 0);}// 打印日历的函数void printCalendar(int year) {// 实现日历打印的逻辑// 省略具体实现细节printf("这里是d年的日历\n", year);}int m本人n() {// 用户输入年份int year;printf("请输入年份:");scanf("d", year);// 打印该年的日历printCalendar(year);// 判断该年是否为闰年if (isLeapYear(year)) {printf("d年是闰年\n", year);} else {printf("d年不是闰年\n", year);}return 0;}```【五、程序设计测试】接下来我们对设计的C语言程序进行测试,例如输入2020年,程序将输出2020年的日历,并给出判断结果。
万年历程序代码
/*****************************************************4字LED点阵屏+DS1302万年历电子钟C 程序*******************************************************DS1302 接线图Vcc2 CLK I/O /RST| | | |-------------------| 8 7 6 5 || DS1302 || || 1 2 3 4 |-------------------| | | |VCC1 GND1 脚接+5V 2,3脚32768HZ晶振4脚接地5脚接S51的P02 6脚接S51的P01 7接S51的P008脚接后备电源,可以接老计算机主板上的3.6V电池,也可以通过二级管隔离接一个大容量电解电容电压在2.5V以上即可维持595连级输出数据,138行驱动。
*///(本程序引用网上程序,有点BUG经过修改后可以使用,经过修改调整加上了温度显示功能,节日提醒功能和家人生日提醒功能。
//程序没有有效简化)07电气工程,付春平!//手动添加定时器2寄存器定义sfr T2CON = 0xC8;(special function register)sfr TL2 = 0xCC;sfr TH2 = 0xCD;sfr RCAP2L = 0xCA;sfr RCAP2H = 0xCB;sbit TF2 = T2CON^7;sbit EXF2 = T2CON^6;sbit RCLK = T2CON^5;sbit TCLK = T2CON^4;sbit EXEN2 = T2CON^3;sbit TR2 = T2CON^2;sbit C_T2 = T2CON^1;sbit CP_RL2= T2CON^0;//sbit ET2 =0xAD;#include<A T89x51.H>#include <intrins.h>#define uchar unsigned char#define uint unsigned intunsigned char irtime;//红外用全局变量bit irokk;unsigned char IRcord[2];unsigned char irdata[17];unsigned char data temp_data[2] = {0x00,0x00} ;void Ircordpro(void);uchar code hanzi[]; //汉字字模uchar code hanzi1[]; //汉字字模uchar code timer[18][16]; //0~9数字uchar code sw[]; //138驱动数据void Show_word(); //待机显示按三秒间隔分别显示年、月日、星期、时分秒。
单片机万年历程序代码
单片机万年历程序代码以下是一个示例单片机万年历的程序代码:```c#include <reg51.h>typedef unsigned char uchar;typedef unsigned int uint;sbit K1 = P2^0; // 显示年份sbit K2 = P2^1; // 显示月份sbit K3 = P2^2; // 显示日期uchar code year_tab[] = {31,28,31,30,31,30,31,31,30,31,30,31}; uchar code week_tab[] = {0x06,0x07,0x01,0x02,0x03,0x04,0x05}; uchar year, month, day, week;void delay(uint ms){uint i, j;for(i=ms;i>0;i--)for(j=110;j>0;j--);}uchar getKey(){if(K1==0) {delay(5);if(K1==0)return 1;while(!K1);}if(K2==0) {delay(5);if(K2==0)return 2;while(!K2);}if(K3==0) {delay(5);if(K3==0)return 3;while(!K3);}return 0;}void display(uchar num) {P0 = num;delay(1);P0 = 0x00;}void init(){TMOD=0x01;TH0=0xFC;TL0=0x67;EA=1;ET0=1;TR0=1;}void main(){init();while(1) {uchar key = getKey();if(key == 1) {year++;if(year == 100)year = 0;}else if(key == 2) {month++;if(month == 13)month = 1;}else if(key == 3) {day++;if(day > year_tab[month-1]) { day = 1;}}display(year / 10);display(year % 10);display(month / 10);display(month % 10);display(day / 10);display(day % 10);display(week);}}void timer0() interrupt 1{TH0=0xFC;TL0=0x67;week++;if(week == 7)week = 0;}```该代码的主要思路是通过外部三个按键模拟年、月和日的调节,通过一个定时器不断更新星期的计数,然后将年、月、日和星期分别在数码管上显示出来。
万年历
编写一个万年历程序,能显示日历,查询任意日期。
日历初始化和显示。
能根据系统日期进行初始化,并显示当前月份的月历,并突出显示当前日期。
日期查询,可以输入一个日期,然后查询并正确显示日期所在月份的月历,并突出显示查询日期,如果输入日期不正确给出提示。
#include<stdio.h>#include<time.h>#include<iostream.h>#include"万年历.h"int main(){int y,m,d,h,min,sec;//系统时间参数int cho;//用户选项int yt,mt,dt;//拥护输入的时间参数while(1){cout<<"≡≡≡≡≡≡万年历菜单选项≡≡≡≡≡";cout<<"请按编号选择功能";cout<<" ⑴查询当前时间";cout<<" ⑵显示本月月历";cout<<" ⑶查找给定日期对应的月历";cout<<" ⑷退出万年历";cin>>cho;Initdate(y,m,d,h,min,sec);switch(cho){case 1: cout<<"当前时间为<<y<<"."<<m<<"."<<d<<"."<<" "<<h<<":"<<min<<":"<<sec<<" ";week(y,m,d);break;case 2: printmonth(y,m,d);break;case 3: cout<<"请输入所要查找的日期,格式如2000 7 5";cin>>yt<<mt<<dt;search(yt,mt,dt);break;case 4: exit(1);break;default :cout<<"输入错误,请重新输入";}}return;}void week(y,m,d)//日期对于的星期{int c,w;c=y/100+1;if(m<=2) {m=m+12;y=y-1;}w=(y+y/4+c/4-2*c+26*(m+1)/10+d-1)%7;if(w=0) cout("Sun");else if (w==1)cout<<"Mon";else if (w==2)cout<<"Tue";else if (w==3)cout<<"wed";else if (w==4)cout<<"Thu";else if (w=5)cout<<"Fri";else cout<<"Sta"; }void Initdate(&y,&m,&d,&h,&min,&sec)//根据系统时间初始化万年历{time_t t;time(&t);struct tm * local_time = localtime(&t);sec=local_time->tm_sec;min=local_time->tm_min ;h=local_time->tm_hour ;d=local_time->tm_mday ;m=local_time->tm_mon+1;y=local_time->tm_year+1900;}void printmonth(y,m,d)//打印出给定时间对于月历{int ym[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}};int leap,i;leap=(y%4==0&&y%100!=0||y%400==0)for(i=1;i<d;i++)cout<<i<<" "<<week(y,m,i)<<endl;cout<<"★" ;for(i=d;i<=ym[leap][m];i++)cout<<i<<" "<<week(y,m,i)<<endl;}int judge(y,m,d)//判断给定时间是否存在{int ym[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}};int leap,i;if(y<=0||m<1||m>12)return 0;leap=(y%4==0&&y%100!=0||y%400==0);if(d<1||d>ym[leap][m])return 0;else return 1;}int search(y,m,d)//查找打印给定时间对应阅历{if(judge(y,m,d)==0)cout<<"error date";else printmonth(y,m,d);}2011-8-9 16:29最佳答案// abc4.cpp : 定义控制台应用程序的入口点。
万年历代码c语言
万年历代码c语言万年历是一种实用的日历工具,它可以根据年、月、日来显示当天的日期信息,并可以切换到其他日期来查询对应的日期信息。
在编写万年历的代码时,我们需要考虑输入的年份是否为闰年、每个月的天数、以及每个月第一天是星期几等等。
以下是一份使用C语言编写万年历的参考代码:```c#include <stdio.h>// 判断是否为闰年int isLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 获取某年某月的天数int getMonthDays(int year, int month) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return days[month - 1];}// 获取某年某月第一天是星期几int getFirstDayOfWeek(int year, int month) {int day = 1;for (int i = 1800; i < year; i++) {if (isLeapYear(i)) {day = (day + 366) % 7;} else {day = (day + 365) % 7;}}for (int i = 1; i < month; i++) {day = (day + getMonthDays(year, i)) % 7;}return day;}int main() {int year, month;printf("请输入年份:");scanf("%d", &year);printf("请输入月份:");scanf("%d", &month);// 判断输入是否合法if (year < 1800 || month < 1 || month > 12) {printf("输入的年份或月份不合法!\n");return 0;}// 获取某年某月的天数和第一天是星期几int days = getMonthDays(year, month);int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印万年历printf("日一二三四五六\n");for (int i = 0; i < firstDayOfWeek; i++) {printf(" ");}for (int i = 1; i <= days; i++) {printf("%2d ", i);if ((firstDayOfWeek + i) % 7 == 0) {printf("\n");}}printf("\n");return 0;}```以上代码实现了一个简单的万年历功能,在控制台输出给定年份和月份的月历。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
我的单片机课程设计:AT89S52+DS1302+DS18B20多功能数字电子钟一单片机2010-07-16 17:35:55 阅读373 评论0 字号:大中小订阅实现功能:1.显示年、月、日、时、分、秒、星期,闰年自动调整2.可手动调整上述各项的值3.实时显示当前温度4. 可设定两个闹铃,每个有独立开关,并可以设置只响闹一次还是每天都响闹5. 闹钟共有两首音乐可供选择6. 可闹铃音乐放完可自动止闹,也可中途按键手动止闹7. 贪睡功能,有开关,并可设置贪睡时间8. 背光控制,有开关,并可设置多少秒后自动关闭背光,也可以按键开背光,然后按照设置的时间后自动关闭9. 秒表功能10. 掉电模式,同时液晶关背光,节省能源源代码第一部分:#include<at89x52.h> //2010.4.6-2010.4.13 Designed By Lu. QQ:475600597 2010.6.14再次修改#include<intrins.h>typedef unsigned char uchar;typedef unsigned intuint;#define LCD1602_DATA P0 //1602液晶数据口sbit LCD1602_RS=P1^0;sbit LCD1602_RW=P1^1;sbit LCD1602_EN=P1^2; //1602液晶使能控制sbit DS1302_CLK = P3^0; //实时时钟时钟线引脚sbit DS1302_IO = P3^1; //实时时钟数据线引脚sbit DS1302_RST = P3^2; //实时时钟复位线引脚sbit DQ = P2^3 ; //18B20接口sbitBeepIO = P2^7 ; //蜂鸣器sbit esc=P1^7; //取消播放音乐sbit mode = P1^4 ; //模式键sbitinc = P1^5 ; //增加键sbitdec = P1^6 ; //减少键sbit ok = P1^7 ; //OK键sbit light=P2^0; //背光控制sbit ACC0 = ACC^0; //累加器位定义sbit ACC7 = ACC^7;bit first_flag=1, //一级主界面显示标志second_flag, //二级菜单显示标志third_flag, //三级菜单显示标志playmusic, //播放音乐的标志light_flag1=1, //背光第一次计时标志light_flag=1 ; // 二三级界面开背光标志uchar count1,sec1,min1,hour1;//定时器2使用的变量停表用ucharcount,sec,min;//定时器1使用的变量贪睡用uchar temp; //温度暂存//////以下为播放音乐的设置#define SYSTEM_OSC 12000000#define SOUND_SPACE 4/5uint code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表uchar code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在频率表中的位置uchar code LengthTab[7]= { 1,2,4,8,16,32,64 };uchar Sound_Temp_TH0,Sound_Temp_TL0; //音符定时器初值暂存uchar Sound_Temp_TH1,Sound_Temp_TL1; //音长定时器初值暂存bit stop;char menu_num;//菜单号char num;//按键加减调整暂存数char position1,position2,position3,position4,position5,position6,position7; //菜单1-7的光标位置标志char code menu[][16]={"1.Time Setting ","2.Alarm Setting","3.Alarm Music ","4.Snooze ", "5.Back Light ","6.Stop Watch ","7.LowPower Mode","8.Exit "};char code on_off_flag[][4]={"OFF","ON "};char code alarm_times[][7]={"Once ","Repeat"};char code week[][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};uchar code self_table[]={0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年00x1f,0x11,0x1f,0x11,0x1f,0x11,0x15,0x17,//月10x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00,//日20x01,0x0c,0x17,0x14,0x17,0x0c,0x01,0x00,//闹钟标志30x10,0x18,0x1c,0x1e,0x1f,0x1c,0x18,0x10,//三角形符号,第五个自定义字符4 0x1f,0x15,0x0a,0x04,0x0a,0x15,0x1f,0x00}; //贪睡标志5/***********************延时函数(单位ms)**************************/void delay(uint z){uint x;uchar y;for(x=z;x>0;x--)for(y=112;y>0;y--);}/*****************************************************************************LCD1602驱动程序******************************************************************************//***********************************************************忙检测************************************************************/void LCD1602_check_busy(){LCD1602_DATA = 0xff;LCD1602_RS = 0 ;LCD1602_RW = 1 ;LCD1602_EN = 1 ;while(LCD1602_DATA & 0x80) ;LCD1602_EN = 0 ;}/**********************************************************写指令************************************************************/void LCD1602_write_cmd(ucharcmd){LCD1602_check_busy();LCD1602_RS = 0 ;LCD1602_RW = 0 ;LCD1602_DATA = cmd ;LCD1602_EN = 1 ;_nop_();LCD1602_EN = 0 ;}/***********************************************************写数据*************************************************************/void LCD1602_write_data(uchardat){LCD1602_check_busy();LCD1602_RS = 1 ;LCD1602_RW = 0 ;LCD1602_DATA = dat ;LCD1602_EN = 1 ;_nop_();LCD1602_EN = 0 ;}/***********************************************************1602初始化************************************************************/void LCD1602_init(){/****************写入自定义字符***************************/uchar i ;LCD1602_write_cmd(0x40);//CGRAM起始地址for(i=0;i<48;i++)LCD1602_write_data(self_table[i]);//写入6个自定义字符/*********************************************************/LCD1602_write_cmd(0x38); //8位数据,双列显示,5*7字形LCD1602_write_cmd(0x0c); //开显示,关光标,光标不闪烁LCD1602_write_cmd(0x01); //清屏}/************************************************************设置显示坐标************************************************************/void LCD1602_set_postion(uchar x , uchar y){if(!y)LCD1602_write_cmd(0x80+x) ;else if(y)LCD1602_write_cmd(0xc0+x) ;}/************************************************************指定位置写字符*************************************************************/void LCD1602_write_char(uchar x , uchar y , ucharchardata){LCD1602_set_postion(x,y) ;LCD1602_write_data(chardata) ;}/************************************************************指定位置写字符串*************************************************************/void LCD1602_write_string(uchar x , uchar y , uchar *string){LCD1602_set_postion(x,y) ;while((*string) != '\0'){LCD1602_write_data(*string) ;string++ ;}}/*****************************************************************************DS1302驱动程序******************************************************************************/void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) {unsigned char i;ACC = d;for(i=8; i>0; i--){DS1302_IO = ACC0; //相当于汇编中的RRCDS1302_CLK = 1;DS1302_CLK = 0;ACC = ACC >> 1;}}unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数) {unsigned char i;for(i=8; i>0; i--){ACC = ACC >>1; //相当于汇编中的RRCACC7 = DS1302_IO;DS1302_CLK = 1;DS1302_CLK = 0;}return(ACC);}void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据{DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr); // 地址,命令DS1302InputByte(ucDa); // 写1Byte数据DS1302_CLK = 1;DS1302_RST = 0;}unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据{unsigned char ucData;DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr|0x01); // 地址,命令ucData = DS1302OutputByte(); // 读1Byte数据DS1302_CLK = 1;DS1302_RST = 0;return(ucData);}void DS1302_init(){if(Read1302(0xc1) != 0xf0){Write1302(0x8e,0x00) ;//允许写操作Write1302(0xc0,0xf0) ; //写入初始化标志,系统上电后检测此标志,即此子函数只会在第一次初始化一次。