完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include #define uchar unsigned char #define uint unsigned int #define NoOp 0x00 /*空操作*/ #define Digit0 0x01 /*数码管1*/ #define Digit1 0x02 /*数码管2*/ #define Digit2 0x03 /*数码管3*/ #define Digit3 0x04 /*数码管4*/ #define Digit4 0x05 /*数码管5*/ #define Digit5 0x06 /*数码管6*/ #define Digit6 0x07 /*数码管7*/ #define Digit7 0x08 /*数码管8*/ #define DecodeMode 0x09 /*译码模式*/ #define Intensity 0x0a /*亮度*/ #define ScanLimit 0x0b /*扫描界限*/ #define ShutDown 0x0c /*掉电模式*/ #define DisplayTest 0x0f /*显示测试*/ #define ShutdownMode 0x00 /*掉电方式工作*/ #define NormalOperation 0x01 /*正常操作方式*/ #define DecodeDigit 0xff /*译码位数设置*/ #define IntensityGrade 0x0f /*显示亮度级别设置*/ #define ScanDigit 0x07 /*扫描位数设置*/ #define TestMode 0x01 /*显示测试方式*/ #define TextEnd 0x00 /*显示测试结束,正常工作*/ extern uchar year_h; extern uchar year_l; extern uchar week; extern uchar month_h; extern uchar month_l; extern uchar day_h; extern uchar day_l; extern uchar hour_h; extern uchar hour_l; extern uchar min_h; extern uchar min_l; extern uchar sec_h; extern uchar sec_l; extern uchar month_lunar_h,month_lunar_l,date_lunar_h,date_lunar_l; extern uchar time_year,time_week,time_month,time_day,time_hour,time_min,time_sec; extern uchar hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; extern uchar c_moon,year_moon,month_moon,day_moon; extern uchar TempBuffer[2]; extern uchar temp_value; extern bit down_flag; extern bit up_flag; extern bit flag; extern uchar count; extern uchar temp_1; #define DS12C887_DATA P3 sfr WDT_CONTR = 0xe1; ***it dis_DIN=P0^0; /*显示串行数据输入端*/ ***it dis_LOAD=P0^1; /*显示数据锁存端*/ ***it dis_CLK=P0^2; /*显示时钟输入端*/ ***it DQ=P1^0; ***it DS12C887_DS=P2^0; ***it DS12C887_WR=P2^1; ***it DS12C887_AS=P2^2; ***it DS12C887_CS=P2^3; ***it DS12C887_IRQ=P2^4; ***it set=P1^4; ***it up=P1^3; ***it down=P1^2; ***it out=P1^1; extern void _nop_ (void); extern bit _testbit_ (bit); extern unsigned char _cror_ (unsigned char, unsigned char); extern unsigned int _iror_ (unsigned int, unsigned char); extern unsigned long _lror_ (unsigned long, unsigned char); extern unsigned char _crol_ (unsigned char, unsigned char); extern unsigned int _irol_ (unsigned int, unsigned char); extern unsigned long _lrol_ (unsigned long, unsigned char); extern unsigned char _chkfloat_(float); extern void _push_ (unsigned char _sfr); extern void _pop_ (unsigned char _sfr); void SendChar (uchar ch); void WriteWord_3 (uchar addr,uchar num); void WriteWord_2 (uchar addr,uchar num); void WriteWord_1 (uchar addr,uchar num); unsigned char ReadOneChar(void); void WriteOneChar(uchar dat); void ReadTemp(void); void temp_to_str(); void Init_DS18B20(void); void delay_18B20(uint i); void DS12C887_write(unsigned char add,unsigned char dat); unsigned char DS12C887_read(unsigned char add); void DS12C887_Gettime(); void WriteWord_1 (unsigned char addr,unsigned char num); void InitDis (void); void display(); bit get_moon_day(unsigned char month_p,unsigned int table_addr); void Conversion(bit c,unsigned char year,unsigned char month,unsigned char day); void Delay_xMs(unsigned int x); void timedone(); void downkey(); void upkey(); void scankey(); void outkey(); uchar time_year,time_month,time_day,time_week,time_hour,time_min,time_sec; uchar c_moon,year_moon,month_moon,day_moon; uchar year_h,year_l,week,month_h,month_l,day_h,day_l,hour_h,hour_l,min_h,min_l,sec_h,sec_l; uchar month_lunar_h ,month_lunar_l ,date_lunar_l,date_lunar_h; uchar hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; uchar temp_value; uchar TempBuffer[2]; bit flag; uchar count; bit up_flag; bit down_flag; uchar temp_1; void DS12C887_write(uchar add,uchar dat) { DS12C887_CS=0; DS12C887_AS=1; DS12C887_DS=1; DS12C887_WR=1; DS12C887_DATA=add; DS12C887_AS=0; DS12C887_WR=0; DS12C887_DATA=dat; DS12C887_WR=1; DS12C887_AS=1; DS12C887_CS=1; } uchar DS12C887_read(uchar add) { uchar ds_date; DS12C887_AS=1; DS12C887_DS=1; DS12C887_WR=1; DS12C887_CS=0; DS12C887_DATA=add; DS12C887_AS=0; DS12C887_DS=0; DS12C887_DATA=0xff; ds_date=DS12C887_DATA; DS12C887_DS=1; DS12C887_AS=1; DS12C887_CS=1; return ds_date; } void DS12C887_Gettime() { time_year=DS12C887_read(0x09); time_month=DS12C887_read(0x08); time_day=DS12C887_read(0x07); time_week=DS12C887_read(0x06); time_hour=DS12C887_read(0x04); time_min=DS12C887_read(0x02); time_sec=DS12C887_read(0x00); } /***********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); //精确延时 大于 480us DQ = 1; //拉高总线 delay_18B20(14); x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败 delay_18B20(20); } /***********ds18b20读一个字节**************/ unsigned char ReadOneChar(void) { unsigned char i=0; unsigned char 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(unsigned char 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 important Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 delay_18B20(100); a=ReadOneChar(); //读取温度值低位 b=ReadOneChar(); //读取温度值高位 temp_value=b<<4; temp_value+=(a&0xf0)>>4; } void temp_to_str() //温度数据转换成液晶字符显示 { TempBuffer[0]=temp_value/10; //十位 TempBuffer[1]=temp_value%10; //个位 } /*C51写的公历转农历和星期 C51写的公历转农历和星期 收集:杨本荣 来源: 网络 发表于 2005-9-3 */ /* 公历年对应的农历数据,每年三字节, 格式第一字节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 0 0x0A,0x57,0x48, //1902 3 0x55,0x26,0xBd, //1903 6 0x0d,0x26,0x50, //1904 9 0x0d,0x95,0x44, //1905 12 0x46,0xAA,0xB9, //1906 15 0x05,0x6A,0x4d, //1907 18 0x09,0xAd,0x42, //1908 21 0x24,0xAe,0xB6, //1909 0x04,0xAe,0x4A, //1910 0x6A,0x4d,0xBe, //1911 0x0A,0x4d,0x52, //1912 0x0d,0x25,0x46, //1913 0x5d,0x52,0xBA, //1914 0x0B,0x54,0x4e, //1915 0x0d,0x6A,0x43, //1916 0x29,0x6d,0x37, //1917 0x09,0x5B,0x4B, //1918 0x74,0x9B,0xC1, //1919 0x04,0x97,0x54, //1920 0x0A,0x4B,0x48, //1921 0x5B,0x25,0xBC, //1922 0x06,0xA5,0x50, //1923 0x06,0xd4,0x45, //1924 0x4A,0xdA,0xB8, //1925 0x02,0xB6,0x4d, //1926 0x09,0x57,0x42, //1927 0x24,0x97,0xB7, //1928 0x04,0x97,0x4A, //1929 0x66,0x4B,0x3e, //1930 0x0d,0x4A,0x51, //1931 0x0e,0xA5,0x46, //1932 0x56,0xd4,0xBA, //1933 0x05,0xAd,0x4e, //1934 0x02,0xB6,0x44, //1935 0x39,0x37,0x38, //1936 0x09,0x2e,0x4B, //1937 0x7C,0x96,0xBf, //1938 0x0C,0x95,0x53, //1939 0x0d,0x4A,0x48, //1940 0x6d,0xA5,0x3B, //1941 0x0B,0x55,0x4f, //1942 0x05,0x6A,0x45, //1943 0x4A,0xAd,0xB9, //1944 0x02,0x5d,0x4d, //1945 0x09,0x2d,0x42, //1946 0x2C,0x95,0xB6, //1947 0x0A,0x95,0x4A, //1948 0x7B,0x4A,0xBd, //1949 0x06,0xCA,0x51, //1950 0x0B,0x55,0x46, //1951 0x55,0x5A,0xBB, //1952 0x04,0xdA,0x4e, //1953 0x0A,0x5B,0x43, //1954 0x35,0x2B,0xB8, //1955 0x05,0x2B,0x4C, //1956 0x8A,0x95,0x3f, //1957 0x0e,0x95,0x52, //1958 0x06,0xAA,0x48, //1959 0x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //1961 0x04,0xB6,0x45, //1962 0x4A,0x57,0x39, //1963 0x0A,0x57,0x4d, //1964 0x05,0x26,0x42, //1965 0x3e,0x93,0x35, //1966 0x0d,0x95,0x49, //1967 0x75,0xAA,0xBe, //1968 0x05,0x6A,0x51, //1969 0x09,0x6d,0x46, //1970 0x54,0xAe,0xBB, //1971 0x04,0xAd,0x4f, //1972 0x0A,0x4d,0x43, //1973 0x4d,0x26,0xB7, //1974 0x0d,0x25,0x4B, //1975 0x8d,0x52,0xBf, //1976 0x0B,0x54,0x52, //1977 0x0B,0x6A,0x47, //1978 0x69,0x6d,0x3C, //1979 0x09,0x5B,0x50, //1980 0x04,0x9B,0x45, //1981 0x4A,0x4B,0xB9, //1982 0x0A,0x4B,0x4d, //1983 0xAB,0x25,0xC2, //1984 0x06,0xA5,0x54, //1985 0x06,0xd4,0x49, //1986 0x6A,0xdA,0x3d, //1987 0x0A,0xB6,0x51, //1988 0x09,0x37,0x46, //1989 0x54,0x97,0xBB, //1990 0x04,0x97,0x4f, //1991 0x06,0x4B,0x44, //1992 0x36,0xA5,0x37, //1993 0x0e,0xA5,0x4A, //1994 0x86,0xB2,0xBf, //1995 0x05,0xAC,0x53, //1996 0x0A,0xB6,0x47, //1997 0x59,0x36,0xBC, //1998 0x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //2001 0x0d,0x4A,0x4C, //2002 0x0d,0xA5,0x41, //2003 0x25,0xAA,0xB6, //2004 0x05,0x6A,0x49, //2005 0x7A,0xAd,0xBd, //2006 0x02,0x5d,0x52, //2007 0x09,0x2d,0x47, //2008 0x5C,0x95,0xBA, //2009 0x0A,0x95,0x4e, //2010 0x0B,0x4A,0x43, //2011 0x4B,0x55,0x37, //2012 0x0A,0xd5,0x4A, //2013 0x95,0x5A,0xBf, //2014 0x04,0xBA,0x53, //2015 0x0A,0x5B,0x48, //2016 0x65,0x2B,0xBC, //2017 0x05,0x2B,0x50, //2018 0x0A,0x93,0x45, //2019 0x47,0x4A,0xB9, //2020 0x06,0xAA,0x4C, //2021 0x0A,0xd5,0x41, //2022 0x24,0xdA,0xB6, //2023 0x04,0xB6,0x4A, //2024 0x69,0x57,0x3d, //2025 0x0A,0x4e,0x51, //2026 0x0d,0x26,0x46, //2027 0x5e,0x93,0x3A, //2028 0x0d,0x53,0x4d, //2029 0x05,0xAA,0x43, //2030 0x36,0xB5,0x37, //2031 0x09,0x6d,0x4B, //2032 0xB4,0xAe,0xBf, //2033 0x04,0xAd,0x53, //2034 0x0A,0x4d,0x48, //2035 0x6d,0x25,0xBC, //2036 0x0d,0x25,0x4f, //2037 0x0d,0x52,0x44, //2038 0x5d,0xAA,0x38, //2039 0x0B,0x5A,0x4C, //2040 0x05,0x6d,0x41, //2041 0x24,0xAd,0xB6, //2042 0x04,0x9B,0x4A, //2043 0x7A,0x4B,0xBe, //2044 0x0A,0x4B,0x51, //2045 0x0A,0xA5,0x46, //2046 0x5B,0x52,0xBA, //2047 0x06,0xd2,0x4e, //2048 0x0A,0xdA,0x42, //2049 0x35,0x5B,0x37, //2050 0x09,0x37,0x4B, //2051 0x84,0x97,0xC1, //2052 0x04,0x97,0x53, //2053 0x06,0x4B,0x48, //2054 0x66,0xA5,0x3C, //2055 0x0e,0xA5,0x4f, //2056 0x06,0xB2,0x44, //2057 0x4A,0xB6,0x38, //2058 0x0A,0xAe,0x4C, //2059 0x09,0x2e,0x42, //2060 0x3C,0x97,0x35, //2061 0x0C,0x96,0x49, //2062 0x7d,0x4A,0xBd, //2063 0x0d,0x4A,0x51, //2064 0x0d,0xA5,0x45, //2065 0x55,0xAA,0xBA, //2066 0x05,0x6A,0x4e, //2067 0x0A,0x6d,0x43, //2068 0x45,0x2e,0xB7, //2069 0x05,0x2d,0x4B, //2070 0x8A,0x95,0xBf, //2071 0x0A,0x95,0x53, //2072 0x0B,0x4A,0x47, //2073 0x6B,0x55,0x3B, //2074 0x0A,0xd5,0x4f, //2075 0x05,0x5A,0x45, //2076 0x4A,0x5d,0x38, //2077 0x0A,0x5B,0x4C, //2078 0x05,0x2B,0x42, //2079 0x3A,0x93,0xB6, //2080 0x06,0x93,0x49, //2081 0x77,0x29,0xBd, //2082 0x06,0xAA,0x51, //2083 0x0A,0xd5,0x46, //2084 0x54,0xdA,0xBA, //2085 0x04,0xB6,0x4e, //2086 0x0A,0x57,0x43, //2087 0x45,0x27,0x38, //2088 0x0d,0x26,0x4A, //2089 0x8e,0x93,0x3e, //2090 0x0d,0x52,0x52, //2091 0x0d,0xAA,0x47, //2092 0x66,0xB5,0x3B, //2093 0x05,0x6d,0x4f, //2094 0x04,0xAe,0x45, //2095 0x4A,0x4e,0xB9, //2096 0x0A,0x4d,0x4C, //2097 0x0d,0x15,0x41, //2098 0x2d,0x92,0xB5, //2099 }; ///月份数据表 code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3}; code uint day_code2[3]={0x111,0x130,0x14e}; /* 函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年) 调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun) 如:计算2004年10月16日Conversion(0,0x4,0x10,0x16); c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世 纪,c_sun=1为19世纪 调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据 */ /*子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回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);}default :return(1); } } /* 函数功能:输入BCD阳历数据,输出BCD阴历数据(只允许1901-2099年) 调用函数示例:Conversion(c_sun,year_sun,month_sun,day_sun) 如:计算2004年10月16日Conversion(0,0x4,0x10,0x16); c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世 纪,c_sun=1为19世纪 调用函数后,原有数据不变,读c_moon,year_moon,month_moon,day_moon得出阴历BCD数据 */ void Conversion(bit c,uchar year,uchar month,uchar day) { //c=0 为21世纪,c=1 为19世纪 输入输出数据均为BCD数据 uchar temp1,temp2,temp3,month_p; uint temp4,table_addr; bit flag2,flag_y; temp1=year/16; //BCD->hex 先把数据转换为十六进制 temp2=year%16; year=temp1*10+temp2; temp1=month/16; temp2=month%16; month=temp1*10+temp2; temp1=day/16; temp2=day%16; day=temp1*10+temp2; //定位数据表地址 if(c==0){ table_addr=(year+0x64-1)*0x3; } else { table_addr=(year-1)*0x3; } //定位数据表地址完成 //取当年春节所在的公历月份 temp1=year_code[table_addr+2]&0x60; temp1=_cror_(temp1,5); //取当年春节所在的公历月份完成 //取当年春节所在的公历日 temp2=year_code[table_addr+2]&0x1f; //取当年春节所在的公历日完成 // 计算当年春年离当年元旦的天数,春节只会在公历1月或2月 if(temp1==0x1){ temp3=temp2-1; } else{ temp3=temp2+0x1f-1; } // 计算当年春年离当年元旦的天数完成 //计算公历日离当年元旦的天数,为了减少运算,用了两个表 //day_code1[9],day_code2[3] //如果公历月在九月或前,天数会少于0xff,用表day_code1[9], //在九月后,天数大于0xff,用表day_code2[3] //如输入公历日为8月10日,则公历日离元旦天数为day_code1[8-1]+10-1 //如输入公历日为11月10日,则公历日离元旦天数为day_code2[11-10]+10-1 if (month<10){ temp4=day_code1[month-1]+day-1; } else{ temp4=day_code2[month-10]+day-1; } if ((month>0x2)&&(year%0x4==0)){ //如果公历月大于2月并且该年的2月为闰月,天数加1 temp4+=1; } //计算公历日离当年元旦的天数完成 //判断公历日在春节前还是春节后 if (temp4>=temp3){ //公历日在春节后或就是春节当日使用下面代码进行运算 temp4-=temp3; month=0x1; month_p=0x1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月 flag2=get_moon_day(month_p,table_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0 flag_y=0; if(flag2==0)temp1=0x1d; //小月29天 else temp1=0x1e; //大小30天 temp2=year_code[table_addr]&0xf0; temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0则该年无闰月 while(temp4>=temp1){ temp4-=temp1; month_p+=1; if(month==temp2){ flag_y=~flag_y; if(flag_y==0)month+=1; } else month+=1; flag2=get_moon_day(month_p,table_addr); if(flag2==0)temp1=0x1d; else temp1=0x1e; } day=temp4+1; } else{ //公历日在春节前使用下面代码进行运算 temp3-=temp4; if (year==0x0){year=0x63;c=1;} else year-=1; table_addr-=0x3; month=0xc; temp2=year_code[table_addr]&0xf0; temp2=_cror_(temp2,4); if (temp2==0)month_p=0xc; else month_p=0xd; // /* month_p为月份指向,如果当年有闰月,一年有十三个月,月指向13, 无闰月指向12 */ flag_y=0; flag2=get_moon_day(month_p,table_addr); if(flag2==0)temp1=0x1d; else temp1=0x1e; while(temp3>temp1){ temp3-=temp1; month_p-=1; if(flag_y==0)month-=1; if(month==temp2)flag_y=~flag_y; flag2=get_moon_day(month_p,table_addr); if(flag2==0)temp1=0x1d; else temp1=0x1e; } day=temp1-temp3+1; } c_moon=c; //HEX->BCD ,运算结束后,把数据转换为BCD数据 temp1=year/10; temp1=_crol_(temp1,4); temp2=year%10; year_moon=temp1|temp2; temp1=month/10; temp1=_crol_(temp1,4); temp2=month%10; month_moon=temp1|temp2; temp1=day/10; temp1=_crol_(temp1,4); temp2=day%10; day_moon=temp1|temp2; } /* 函数功能:输入BCD阳历数据,输出BCD星期数据(只允许1901-2099年) 调用函数示例:Conver_week(c_sun,year_sun,month_sun,day_sun) 如:计算2004年10月16日Conversion(0,0x4,0x10,0x16); c_sun,year_sun,month_sun,day_sun均为BCD数据,c_sun为世纪标志位,c_sun=0为21世 纪,c_sun=1为19世纪 调用函数后,原有数据不变,读week得出阴历BCD数据 */ code uchar table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表 /* 算法:日期+年份+所过闰年数+月较正数之和除7 的余数就是星期但如果是在 闰年又不到3 月份上述之和要减一天再除7 星期数为0 */ /* void Conver_week(bit c,uchar year,uchar month,uchar day) {//c=0 为21世纪,c=1 为19世纪 输入输出数据均为BCD数据 uchar temp1,temp2; temp1=year/16; //BCD->hex 先把数据转换为十六进制 temp2=year%16; year=temp1*10+temp2; temp1=month/16; temp2=month%16; month=temp1*10+temp2; temp1=day/16; temp2=day%16; day=temp1*10+temp2; if (c==0){year+=0x64;} //如果为21世纪,年份数加100 temp1=year/0x4; //所过闰年数只算1900年之后的 temp2=year+temp1; temp2=temp2%0x7; //为节省资源,先进行一次取余,避免数大于0xff,避免使用整型数据 temp2=temp2+day+table_week[month-1]; if (year%0x4==0&&month<3)temp2-=1; week=temp2%0x7; } */ //test /* uchar c_sun,year_sun,month_sun,day_sun; void main(){ c_sun=0; year_sun=0x7; month_sun=0x08; day_sun=0x11; Conver_week(c_sun,year_sun,month_sun,day_sun); Conversion(c_sun,year_sun,month_sun,day_sun); while(1); } */ /*------------------------------------------------- 向MAX7219写入字节(8位) --------------------------------------------------*/ void SendChar (unsigned char ch) { unsigned char i,temp; _nop_(); for (i=0;i<8;i++) { temp=ch&0x80; ch=ch<<1; if(temp) { dis_DIN=1; dis_CLK=0; dis_CLK=1; } else { dis_DIN=0; dis_CLK=0; dis_CLK=1; } } } /*------------------------------------------------- 向MAX7219写入字(16位) -------------------------------------------------*/ void WriteWord_3 (unsigned char addr,unsigned char num) { dis_LOAD=0; _nop_(); SendChar (addr); _nop_(); SendChar (num); _nop_(); SendChar (NoOp); _nop_(); SendChar (0); _nop_(); SendChar (NoOp); _nop_(); SendChar (0); _nop_(); dis_LOAD=1; } /*------------------------------------------------- 向MAX7219写入字(16位) -------------------------------------------------*/ void WriteWord_2 (unsigned char addr,unsigned char num)//发现与第二及第三块相连的数码管闪烁,与test9相比增加了空操作后就不闪烁了!! { dis_LOAD=0; _nop_(); SendChar (NoOp); _nop_(); SendChar (0); _nop_(); SendChar (addr); _nop_(); SendChar (num); _nop_(); SendChar (NoOp); _nop_(); SendChar (0); _nop_(); dis_LOAD=1; } /*------------------------------------------------- 向MAX7219写入字(16位) -------------------------------------------------*/ void WriteWord_1 (unsigned char addr,unsigned char num)//发现与第二及第三块相连的数码管闪烁,与test9相比增加了空操作后就不闪烁了!! { dis_LOAD=0; _nop_(); SendChar (NoOp); _nop_(); SendChar (0); _nop_(); SendChar (NoOp); _nop_(); SendChar (0); _nop_(); SendChar (addr); _nop_(); SendChar (num); _nop_(); dis_LOAD=1; } /*------------------------------------------------- MAX7219初始化 -------------------------------------------------*/ void InitDis (void) { WriteWord_1 (ScanLimit,ScanDigit); /*设置扫描界限*/ WriteWord_1 (DecodeMode,DecodeDigit); /*设置译码模式*/ WriteWord_1 (Intensity,IntensityGrade); /*设置亮度*/ WriteWord_1 (ShutDown,NormalOperation); /*设置电源工作模式*/ WriteWord_2 (ScanLimit,ScanDigit); /*设置扫描界限*/ WriteWord_2 (DecodeMode,DecodeDigit); /*设置译码模式*/ WriteWord_2 (Intensity,IntensityGrade); /*设置亮度*/ WriteWord_2 (ShutDown,NormalOperation); /*设置电源工作模式*/ WriteWord_3 (ScanLimit,ScanDigit); /*设置扫描界限*/ WriteWord_3 (DecodeMode,DecodeDigit); /*设置译码模式*/ WriteWord_3 (Intensity,IntensityGrade); /*设置亮度*/ WriteWord_3 (ShutDown,NormalOperation); /*设置电源工作模式*/ } void Delay_xMs(unsigned int x) { unsigned int i,j; for(i=0; i for(j=0; j<3; j++); } } void outkey() { if(out==0) Delay_xMs(100); if(out==0) { count=0; flag=0; hide_min=0; hide_hour=0; hide_day=0; hide_week=0; hide_month=0; hide_year=0; } } void scankey() { if(set==0) Delay_xMs(100); if(set==0) { flag=1; count++; if(count==7) { count=0; } } while(!set); //等键松开 } void upkey()//升序按键 { if(up==0) Delay_xMs(100); if(up==0) { switch(count) { case 1: temp_1=time_min; //读取分数 temp_1=temp_1+1; //分数加1 up_flag=1; if(temp_1>59) //超过59分,清零 temp_1=0; break; case 2: temp_1=time_hour; //读取小时数 temp_1=temp_1+1; //小时数加1 up_flag=1; if(temp_1>12) //超过23小时,清零 temp_1=1; break; case 3: temp_1=time_week; //读取星期数 temp_1=temp_1+1; //星期数加1 up_flag=1; if(temp_1>7) temp_1=1; break; case 4: temp_1=time_day; //读取日数 temp_1=temp_1+1; //日数加1 up_flag=1; if(temp_1>31) temp_1=1; break; case 5: temp_1=time_month; //读取月数 temp_1=temp_1+1; //月数加1 up_flag=1; if(temp_1>12) temp_1=1; break; case 6: temp_1=time_year; //读取年数 temp_1=temp_1+1; //年数加1 up_flag=1; if(temp_1>99) temp_1=0; break; default:break; } while(up==0); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////// void downkey()//降序按键 { if(down==0) Delay_xMs(100); if(down==0) { switch(count) { case 1: temp_1=time_min; //读取分数 temp_1=temp_1-1; //分数减1 down_flag=1; if(temp_1==-1) temp_1=59; //小于0秒,返回59秒 break; case 2: temp_1=time_hour; //读取小时数 temp_1=temp_1-1; //小时数减1 down_flag=1; if(temp_1==-1) temp_1=12; break; case 3: temp_1=time_week; //读取星期数 temp_1=temp_1-1; //星期数减1 down_flag=1; if(temp_1==0) temp_1=7;; break; case 4: temp_1=time_day; //读取日数 temp_1=temp_1-1; //日数减1 down_flag=1; if(temp_1==0) temp_1=31; break; case 5: temp_1=time_month; //读取月数 temp_1=temp_1-1; //月数减1 down_flag=1; if(temp_1==0) temp_1=12; break; case 6: temp_1=time_year; //读取年数 temp_1=temp_1-1; //年数减1 down_flag=1; if(temp_1==-1) temp_1=85; break; default:break; } while(down==0); } } /***********************************按键选择*********************************/ void timedone()//按键功能执行 { while(flag==1) { WDT_CONTR = 0X37; scankey(); //扫描模式切换按键 switch(count) { case 1:do //count=2,调整分 { outkey(); upkey(); downkey(); if(temp_1>60) temp_1=0; if(up_flag==1||down_flag==1) { DS12C887_write(0x02,temp_1); } hide_min++; if(hide_min>9) hide_min=0; hide_year=0; hide_hour=0; hide_month=0; hide_day=0; hide_week=0; display(); }while(count==2);break; case 2:do //count=3,调整小时 { hide_min=0; outkey(); upkey(); downkey(); if(up_flag==1||down_flag==1) { DS12C887_write(0x04,temp_1); up_flag=0; down_flag=0; } hide_hour++; if(hide_hour>9) hide_hour=0; hide_min=0; hide_year=0; hide_month=0; hide_day=0; hide_week=0; display(); }while(count==3);break; case 3:do //count=4,调整星期 { hide_hour=0; outkey(); upkey(); downkey(); if(up_flag==1||down_flag==1) { DS12C887_write(0x06,temp_1); up_flag=0; down_flag=0; } hide_week++; if(hide_week>9) hide_week=0; hide_min=0; hide_year=0; hide_hour=0; hide_month=0; hide_day=0; hide_week=0; display(); }while(count==4);break; case 4:do //count=5,调整日 { hide_week=0; outkey(); upkey(); downkey(); if(up_flag==1||down_flag==1) { DS12C887_write(0x07,temp_1); up_flag=0; down_flag=0; } hide_day++; if(hide_day>9) hide_day=0; hide_hour=0; hide_min=0; hide_year=0; hide_month=0; hide_week=0; display(); }while(count==5);break; case 5:do //count=6,调整月 { hide_day=0; outkey(); upkey(); downkey(); if(up_flag==1||down_flag==1) { DS12C887_write(0x08,temp_1); up_flag=0; down_flag=0; } hide_month++; if(hide_month>9) hide_month=0; hide_hour=0; hide_min=0; hide_year=0; hide_day=0; hide_week=0; display(); }while(count==6);break; case 6:do //count=7,调整年 { hide_month=0; outkey(); upkey(); downkey(); if(up_flag==1||down_flag==1) { DS12C887_write(0x09,temp_1); up_flag=0; down_flag=0; } hide_year++; if(hide_year>9) hide_year=0; hide_hour=0; hide_min=0; hide_month=0; hide_day=0; hide_week=0; display(); }while(count==7);break; case 7: count=0; hide_year=0; hide_hour=0; hide_min=0; hide_month=0; hide_day=0; hide_week=0;; //count8, 跳出调整模式,返回默认显示状态 break; flag=0; default: break; } } } void display() { bit cen; DS12C887_Gettime(); if(hide_year<6) { year_h=time_year/10; year_l=time_year%10; } else { year_h=0x0f; year_l=0x0f; } if(hide_month<6) { month_h=time_month/10; if(month_h==0) { month_h=0x0f; } month_l=time_month%10; } else { month_h=0x0f; month_l=0x0f; } if(hide_day<6) { day_h=time_day/10; if(day_h==0) { day_h=0x0f; } day_l=time_day%10; } else { day_h=0x0f; day_l=0x0f; } if(hide_week<6) { week=time_week; if(week==7) week=8; } else { week=0x0f; } if(hide_hour<6) { if(time_hour>12) time_hour=time_hour%12; if(time_hour==0) time_hour=12; hour_h=time_hour/10; hour_l=time_hour%10; if(hour_h==0) { hour_h=0x0f; } } else { hour_h=0x0f; hour_l=0x0f; } if(hide_min<6) { if(min_h==0) min_h=0x0f; min_h=time_min/10; min_l=time_min%10; } else { min_h=0x0f; min_l=0x0f; } sec_h=time_sec/10; sec_l=time_sec%10; Conversion(cen,year_moon,month_moon,day_moon); month_lunar_h=(month_moon/10); month_lunar_l=(month_moon%10);// 阴历月个位 if(month_lunar_h==0) { month_lunar_h=month_lunar_l; month_lunar_l=0x7a; } if(month_lunar_h==1) { month_lunar_h=(month_moon/10); month_lunar_l=(month_moon%10);// 阴历月个位 } date_lunar_h=(day_moon/10); date_lunar_l=(day_moon%10);//阴历日个位 if(date_lunar_h==0) { date_lunar_h=0x0f; } if(sec_l%2) { WriteWord_1 (Digit0,hour_h); WriteWord_1 (Digit1,hour_l); WriteWord_1 (Digit2,0x0f); WriteWord_1 (Digit3,min_h); WriteWord_1 (Digit4,min_l); WriteWord_1 (Digit5,0x0f); WriteWord_1 (Digit6,sec_h); WriteWord_1 (Digit7,sec_l); WriteWord_2 (Digit0,2); WriteWord_2 (Digit1,0); WriteWord_2 (Digit2,year_h); WriteWord_2 (Digit3,year_l); WriteWord_2 (Digit4,month_h); WriteWord_2 (Digit5,month_l); WriteWord_2 (Digit6,day_h); WriteWord_2 (Digit7,day_l); WriteWord_3 (Digit0,month_lunar_h); WriteWord_3 (Digit1,month_lunar_l); WriteWord_3 (Digit2,date_lunar_h); WriteWord_3 (Digit3,date_lunar_l); WriteWord_3 (Digit4,TempBuffer[0]); WriteWord_3 (Digit5,0x0f); WriteWord_3 (Digit6,TempBuffer[1]); WriteWord_3 (Digit7,week); } else { WriteWord_1 (Digit0,hour_h); WriteWord_1 (Digit1,hour_l); WriteWord_1 (Digit2,0x7a); WriteWord_1 (Digit3,min_h); WriteWord_1 (Digit4,min_l); WriteWord_1 (Digit5,0x7a); WriteWord_1 (Digit6,sec_h); WriteWord_1 (Digit7,sec_l); WriteWord_2 (Digit0,2); WriteWord_2 (Digit1,0); WriteWord_2 (Digit2,year_h); WriteWord_2 (Digit3,year_l); WriteWord_2 (Digit4,month_h); WriteWord_2 (Digit5,month_l); WriteWord_2 (Digit6,day_h); WriteWord_2 (Digit7,day_l); WriteWord_3 (Digit0,month_lunar_h); WriteWord_3 (Digit1,month_lunar_l); WriteWord_3 (Digit2,date_lunar_h); WriteWord_3 (Digit3,date_lunar_l); WriteWord_3 (Digit4,TempBuffer[0]); WriteWord_3 (Digit5,0x0f); WriteWord_3 (Digit6,TempBuffer[1]); WriteWord_3 (Digit7,week); } } void main() { InitDis (); Init_DS18B20(); WDT_CONTR = 0X37; while(1) { while(flag) { timedone(); } while(!flag) { ReadTemp(); temp_to_str(); display(); scankey(); WDT_CONTR = 0X37; } } }
|
|
相关推荐
1个回答
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
过采样提高12位ADC精度在STM32主控上的实现(附电路及代码)
385 浏览 0 评论
飞凌嵌入式-ELFBOARD 解决PCB布线时无法捕捉到焊盘中心的问题
826 浏览 0 评论
Openharmony软件评估指南-米尔瑞芯微RK3568开发板
1172 浏览 0 评论
STM32CUBEMX(7)--移植雅特力AT32F403AVGT(兼容STM32F103VGT6),DAC输出电压
1204 浏览 0 评论
【RA-Eco-RA0E1-32PIN-V1.0开发板试用】开发板简介与上电测试
1243 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11085 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-9-14 17:09 , Processed in 0.753129 second(s), Total 71, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号