完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include #define uchar unsigned char #define uint unsigned int ***it reset = P1 ^ 0; //DS1302 ***it sclk = P1 ^ 2; ***it io = P1 ^ 1; ***it dis_DIN=P2^0; //MAX7219 ***it dis_LOAD=P2^2; ***it dis_CLK=P2^1; ***it DQ=P1^3; //DS18B20 温度输入口 ***it KEY_DIS_Change=P1^7; //切换按键 ***it KEY_SET=P1^4; //设置按键 ***it KEY_ADD=P1^5; //"加"按键 ***it KEY_MIN=P1^6; //"减"按键 #define No_Op 0x00 //不工作寄存器地址 #define Digit0 0x01 #define Digit1 0x02 #define Digit2 0x03 #define Digit3 0x04 #define Digit4 0x05 #define Digit5 0x06 #define Digit6 0x07 #define Digit7 0x08 #define ScanLimit 0x0b /*设置扫描界限*/ #define DecodeMode 0x09 /*设置译码模式*/ #define Intensity 0x0a /*设置亮度*/ #define ShutDown 0x0c /*设置电源工作模式*/ #define Display_Test 0x0f //显示检测寄存器地址/ uchar t,temp_value; uchar c_moon,year_moon,month_moon,day_moon; uchar week1 ; uchar month_lunar_h ,month_lunar_l ,date_lunar_l,date_lunar_h; uchar shi,fen,miao,nian,yue,ri,week; unsigned long fre; uchar led[6]; uchar hr0,hr1; uint count; //uchar tflag; //温度正负标志 //******************标示位定义********************/ uchar KEY_DIS_Change_Flog=0; //切换显示标示位 uchar KEY_SET_Flog=0; //设置标示位 uchar Dis_timer_num=0; //设置时显示计数 bit Set_Dis_Flog=0; //设置显示标示位 //bit ALARM_Flog=0; //闹铃开关标示位 //bit VOICE_Flog=0; //语音播报标示位 uchar TempBuffer[3]; uchar init[7] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; uchar init1[7]={0x00, 0x08, 0x08, 0x08, 0x08, 0x01, 0x11}; //秒, 分, 时, 日, 月,星期,年 uchar code max7219_7led_code[10]={0x7e,0x30,0x6d,0x79, 0x33,0x5b,0x5f,0x70,0x7f,0x7b}; //0-1- code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3}; code uint day_code2[3]={0x111,0x130,0x14e}; code uchar table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表 uchar code tab1[6]={0x82,0x84,0x86,0x88,0x8c,0x8a}; 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 }; //---------------延时函数---------------- void delayNOP() { _nop_(); _nop_(); _nop_(); _nop_(); } void delay_18B20(uint i) { while(i--); } void Delay_1ms(uint xms) { uint i,j; for(i=xms;i>0;i--) for(j=122;j>0;j--); } //----------DS1302 写字节子函数------------------------ void write_byte(uchar inbyte) { uchar ii; for (ii = 0; ii < 8; ii++) { sclk = 0; delayNOP(); inbyte = inbyte >> 1; //右移一位,最低位移入CY io = CY; //写入CY sclk = 1; delayNOP(); } } //DS1302 读字节子函数/ uchar read_byte() { uchar ii, temp = 0; io = 1; //设置为输入口 for (ii = 0; ii < 8; ii++) { sclk = 0; delayNOP(); temp = temp >> 1; //右移一位,最高位补"0" if (io == 1) //读 temp = temp | 0x80; //最高位补"1" sclk = 1; delayNOP(); } return (temp); } //-------往ds1302的某个地址写入数据------------- void write_ds1302(uchar cmd, uchar indata) { reset = 0; delayNOP(); sclk = 0; //为低电平时 delayNOP(); reset = 1; //才能置为高电平 delayNOP(); write_byte(cmd); //先写地址 write_byte(indata); //然后再写数据 sclk = 1; reset = 0; } /********读ds1302某地址的的数据**************/ uchar read_ds1302(uchar addr) { uchar backdata; reset = 0; delayNOP(); sclk = 0; //为低电平时 delayNOP(); reset = 1; //才能置为高电平 delayNOP(); write_byte(addr); //先写地址 backdata = read_byte(); //然后再读数据 sclk = 1; reset = 0; return (backdata); } //-----------写入初始时间子函数------------------ void set_ds1302(uchar addr, uchar *p, uchar n) //写入n个数据 { if(init1[0]&0x80) //判断时钟是否关闭 { write_ds1302(0x8e, 0x00); //写控制字,允许写操作 for (; n > 0; n--) { write_ds1302(addr, *p); p++; addr = addr + 2; } write_ds1302(0x8e, 0x80); //写保护,不允许写 } } /*************读取当前时间子函数****************/ void read_nowtime(uchar addr, uchar *p, uchar n) { for (; n > 0; n--) { *p = read_ds1302(addr); p++; addr = addr + 2; } } /*--------DS1302初始化-----------*/ void init_ds1302() { reset = 0; sclk = 0; write_ds1302(0x8e, 0x00); //写控制字,允许写操作 write_ds1302(0x80, 0x00); //时钟启动 write_ds1302(0x90, 0xa6); //一个二极管+4K电阻充电 write_ds1302(0x8e, 0x80); //写控制字,禁止写操作 } /**********ds18b20初始化函数**********************/ void Init_DS18B20(void) { uchar 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读一个字节**************/ uchar 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) { uchar i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; delay_18B20(5); DQ = 1; dat>>=1; } } /**************读取ds18b20当前温度************/ void ReadTemp(void) { uchar a=0; uchar b=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(); //读取温度值高位 t=b; t<<=8; t=t|a; //if(t<0xfff) tflag=0; //判断温度的正负值 //else //{ //t=~t+1; //tflag=1; //} t=t*(0.625)+0.5; //DS18B20的精度为0.0625度,即读回温度的最低值位代表0.0625度,将他放大10倍,使显示时可显示小数点后一位, // 并对小数点后第二位进行四舍五入。 temp_value=b<<4; temp_value+=(a&0xf0)>>4; } //温度数据转换成十进制字符显示 void temp_to_str() { ReadTemp(); TempBuffer[0]=temp_value/10; //十位 TempBuffer[1]=temp_value%10; //个位 TempBuffer[2]=t%10; //小数 } //向MAX7219写入字节(8位) void SendChar (uchar ch) { uchar i; dis_CLK=0; for (i=0;i<8;i++) { dis_DIN=(bit)(ch&0x80); ch=ch<<1; dis_CLK=1; dis_CLK=0; } } /*----------- 向MAX7219写入字(16位---------*/ void WriteWord_3 (uchar addr,uchar num) { dis_LOAD=0; SendChar (addr); SendChar (num); _nop_(); SendChar (No_Op); SendChar (0); _nop_(); SendChar (No_Op); SendChar (0); dis_LOAD=1; dis_LOAD=0; } /*---------向MAX7219写入字(16位)---------------*/ void WriteWord_2 (uchar addr,uchar num)//发现与第二及第三块相连的数码管闪烁,与test9相比增加了空操作后就不闪烁了!! { dis_LOAD=0; SendChar (No_Op); SendChar (0); _nop_(); SendChar (addr); SendChar (num); _nop_(); SendChar (No_Op); SendChar (0); dis_LOAD=1; dis_LOAD=0; } /*--------向MAX7219写入字(16位)*/ void WriteWord_1 (uchar addr,uchar num)//发现与第二及第三块相连的数码管闪烁,与test9相比增加了空操作后就不闪烁了!! { dis_LOAD=0; SendChar (No_Op); SendChar (0); _nop_(); SendChar (No_Op); SendChar (0); _nop_(); SendChar (addr); SendChar (num); dis_LOAD=1; dis_LOAD=0; } /*--------------MAX7219初始化----------------*/ void InitDis (void) { WriteWord_1 (ScanLimit,0x07); //*设置扫描界限*/ WriteWord_1 (DecodeMode,0x00); //*设置译码模式*/ WriteWord_1 (Intensity,0x01); //*设置亮度*/ WriteWord_1 (ShutDown,0x01); //*设置电源工作模式*/ WriteWord_1 (Display_Test, 0x00); //显示检测寄存器地址/ WriteWord_2 (ScanLimit,0x05); //*设置扫描界限*/ WriteWord_2 (DecodeMode,0x00); //*设置译码模式*/ WriteWord_2 (Intensity,0x01); //*设置亮度*/ WriteWord_2 (ShutDown,0x01); //*设置电源工作模式*/ WriteWord_2 (Display_Test, 0x00); //*显示检测寄存器地址/ WriteWord_3 (ScanLimit,0x07); //*设置扫描界限*/ WriteWord_3 (DecodeMode,0x00); //*设置译码模式*/ WriteWord_3 (Intensity,0x01); //*设置亮度*/ WriteWord_3 (ShutDown,0x01); //*设置电源工作模式*/ WriteWord_3 (Display_Test, 0x00); //*显示检测寄存器地址/ } //----------------//阴历的计算函数//------------------- 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); break; 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); } } 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; read_nowtime(0x81, init, 7); year=init[6]; month=init[4]; day=init[3]; 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; } //星期的计算函数// void Conver_week(bit c,uchar year,uchar month,uchar day) //c=0 为21世纪,c=1 为19世纪 输入输出数据均为BCD数据 { uchar temp1,temp2; read_nowtime(0x81, init, 7); year=init[6]; month=init[4]; day=init[3]; 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; week1=temp2%0x7; } void init_time() { TMOD=0x16; TH0=0; //计数器初值为0 TL0=0; TR0=1; //计数器开始计数 ET0=1; //打开计数器0中断 TH1=(65536-50000)/256; TL1=(65536-50000)%256; TR1=0; ET1=1; RCAP2H=(65536-62500)/256; //在程序初始化的时候给RCAP2L和RCAP2H赋值, RCAP2L=(65536-62500)%256; //TH2和TL2将会在中断产生时自动使TH2=RCAP2H,TL2=RCAP2L。 TH2=RCAP2H; //12M晶振下每次中断62.5ms TL2=RCAP2L; ET2=1; //打开定时器2中断 TR2=1; //定时器2开始计时 EA=1; //开总中断 } void timer2(void) interrupt 5 //定时器2中断(62.5ms) { uchar time; time++; TF2=0; //定时器2的中断标志位TF2不能够由硬件清零,所以要在中断服务程序中将其清零 if(time==16) //定时1s时间到 { time=0; //计时清0 EA=0; //关中断 fre=(long)count*256+TL0; //count*256强制转换成long型,否则将不产生进位 TL0=0; //清零计数器0计数 TH0=0; count=0; //清零计数器0计数 EA=1; //开中断 } } //---------------------------------------------------------------- void timer0(void) interrupt 1 //计数器0中断 { count++; } void tran() { if(( 0 <= fre) && ( fre<= 7200) ) //相对湿度在有效范围内(0%--100%) { if(( 0 < fre) && ( fre <= 1) ) { hr0 = 0; hr1 =(fre-0)*10/1; } if( (1 < fre) && ( fre <= 3) ) { hr0 = 1; hr1 =(fre-1)*10/2;} if( (3 < fre) && ( fre <= 5 ) ) { hr0 = 2; hr1 =(fre-3)*10/4;} if( (5 < fre) && ( fre <= 23 ) ) { hr0 = 3; hr1 =(fre-5)*10/20;} if( (23 < fre) && ( fre <= 81 ) ) { hr0 = 4; hr1 =(fre-23)*10/58;} if( (81 < fre) && ( fre <= 226 ) ) { hr0 = 5; hr1 =(fre-81)*10/145;} if( (226 < fre) && ( fre <= 529 ) ) { hr0 = 6; hr1 =(fre-226)*10/193;} if( (529 < fre) && ( fre <= 1520 ) ) { hr0 = 7; hr1 =(fre-529)*10/991;} if( (1520< fre) && ( fre <= 2164 )) { hr0 = 8; hr1 =(fre-1520)*10/644;} if( (2164 < fre) && (fre <= 7200)) { hr0 = 9; hr1 =(fre-2164)*10/5036;} } else { hr0 = 0; hr1 = 0; } } /*******************按键子程序******************/ void T0_time() interrupt 3 { TH1=(65536-50000)/256; TL1=(65536-50000)%256; Dis_Timer_num++; } void keyscan() { if(KEY_DIS_Change==0) //切换按键 { Delay_1ms(5); if(KEY_DIS_Change==0) { KEY_SET_Flog=0; KEY_DIS_Change_Flog++; if(KEY_DIS_Change_Flog==3) KEY_DIS_Change_Flog=0; while(!KEY_DIS_Change); Delay_1ms(5); //延时 } } if(KEY_SET==0) //设置按键 { Delay_1ms(5); if(KEY_SET==0) { KEY_SET_Flog++; if(KEY_SET_Flog==5) KEY_SET_Flog=0; while(!KEY_SET); } } switch(KEY_DIS_Change_Flog) { case 0: Dis_Timer_num=0; TR1=0; break; case 1: TR1=1; if(Dis_Timer_num>10) { Dis_Timer_num=0; Set_Dis_Flog=~Set_Dis_Flog; } write_ds1302(0x8e, 0x00); //禁止写保护 switch(KEY_SET_Flog) //时钟设置 { case 0: if(KEY_ADD==0) //"年" 加设置 { Delay_1ms(5); if(KEY_ADD==0) { if(nian<99) nian++; write_ds1302(0x8c,(nian/10)*16+nian%10 ); //年 while(!KEY_ADD); } } if(KEY_MIN==0) //"年" 减设置 { Delay_1ms(5); if(KEY_MIN==0) { if(nian>0) nian--; write_ds1302(0x8c,(nian/10)*16+nian%10); //年 while(!KEY_MIN); } } break; case 1: if(KEY_ADD==0) //"月" 加设置 { Delay_1ms(5); if(KEY_ADD==0) { if(yue<12) yue++; write_ds1302(0x88,(yue/10)*16+yue%10); //月 while(!KEY_ADD); } } if(KEY_MIN==0) //"月" 减设置 { Delay_1ms(5); if(KEY_MIN==0) { if(yue>0) yue--; write_ds1302(0x88,(yue/10)*16+yue%10); //月 while(!KEY_MIN); } } break; case 2: if(KEY_ADD==0) //"日" 加设置 { Delay_1ms(5); if(KEY_ADD==0) { if(ri<31) ri++; write_ds1302(0x86,(ri/10)*16+ri%10); //日 while(!KEY_ADD); } } if(KEY_MIN==0) //"日" 减设置 { Delay_1ms(5); if(KEY_MIN==0) { if(ri>0) ri--; write_ds1302(0x86,(ri/10)*16+ri%10); //日 while(!KEY_MIN); } } break; case 3: if(KEY_ADD==0) //"小时" 加设置 { Delay_1ms(5); if(KEY_ADD==0) { if(shi<23) shi++; write_ds1302(0x84,(shi/10)*16+shi%10); //小时 while(!KEY_ADD); } } if(KEY_MIN==0) //"小时" 减设置 { Delay_1ms(5); if(KEY_MIN==0) { if(shi>0) shi--; write_ds1302(0x84,(shi/10)*16+shi%10); //小时 while(!KEY_MIN); } } break; case 4: if(KEY_ADD==0) //"分" 加设置 { Delay_1ms(5); if(KEY_ADD==0) { if(fen<59) fen++; write_ds1302(0x82,(fen/10)*16+fen%10); //分 while(!KEY_ADD); } } if(KEY_MIN==0) //"分" 减设置 { Delay_1ms(5); if(KEY_MIN==0) { if(fen>0) fen--; write_ds1302(0x82,(fen/10)*16+fen%10); //分 while(!KEY_MIN); } } break; default: break; } write_ds1302(0x8e, 0x80); //允许写保护 break; } } //-----------显示程序-------------------------------- void display() { bit cen; Conver_week(cen,year_moon,month_moon,day_moon); if(week1==0) week1=8; Conversion(cen,year_moon,month_moon,day_moon); month_lunar_h=(month_moon/16); month_lunar_l=(month_moon%16);// 阴历月个位 date_lunar_h=(day_moon/16); date_lunar_l=(day_moon%16);//阴历日个位 temp_to_str(); tran(); WriteWord_1 (Digit0,(max7219_7led_code[(TempBuffer[1])])|0x80); WriteWord_1 (Digit1,max7219_7led_code[(TempBuffer[2])]); WriteWord_1 (Digit2,max7219_7led_code[hr1]); WriteWord_1 (Digit3,max7219_7led_code[(TempBuffer[0])]); WriteWord_1 (Digit4,max7219_7led_code[week1]); WriteWord_1 (Digit5,max7219_7led_code[hr0]); WriteWord_1 (Digit6,max7219_7led_code[month_lunar_h]); WriteWord_1 (Digit7,max7219_7led_code[month_lunar_l]); WriteWord_2 (Digit0,max7219_7led_code[(init[0]/16)]); WriteWord_2 (Digit1,max7219_7led_code[(init[0]%16)]); WriteWord_2 (Digit2,max7219_7led_code[(init[1]/16)]); WriteWord_2 (Digit3,max7219_7led_code[(init[1]%16)]); WriteWord_2 (Digit4,max7219_7led_code[(init[2]/16)]); WriteWord_2 (Digit5,max7219_7led_code[(init[2]%16)]); WriteWord_3 (Digit0,max7219_7led_code[(init[3]/16)]); WriteWord_3 (Digit1,max7219_7led_code[(init[3]%16)]); WriteWord_3 (Digit2,max7219_7led_code[(init[4]/16)]); WriteWord_3 (Digit3,max7219_7led_code[(init[4]%16)]); WriteWord_3 (Digit4,max7219_7led_code[(init[6]/16)]); WriteWord_3 (Digit5,max7219_7led_code[(init[6]%16)]); WriteWord_3 (Digit6,max7219_7led_code[date_lunar_h]); WriteWord_3 (Digit7,max7219_7led_code[date_lunar_l]); } //---------------主函数----------------- void main() { InitDis (); Init_DS18B20(); init_ds1302(); set_ds1302(0x80,init1,7); init_time(); P2=0xff; while(1) { display(); keyscan(); } } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
请问一个迭代法图形二值化的程序代码把宽改成240之后整个屏幕不显示是什么原因?
2674 浏览 0 评论
2666 浏览 1 评论
1903 浏览 1 评论
2057 浏览 2 评论
2621 浏览 2 评论
734浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-28 19:43 , Processed in 0.750705 second(s), Total 83, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号