LCD12864(ST7920)线路图
- // 51单片机的12864液晶时钟完整程序
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit E=P2^5;
- sbit RW=P2^6;
- sbit f=P1^5;
- sbit RS=P2^7;
- #define lcd_bus P0
- #define lcd_clear_display 0x1
- #define lcd_funtion_base 0x30
- #define lcd_set_ddram_address 0x80
- sbit T_CLK = P3^7; /*实时时钟时钟线引脚 */
- sbit T_IO = P3^2; /*实时时钟数据线引脚 */
- sbit T_RST = P2^4; /*实时时钟复位线引脚 */
- sbit ACC0=ACC^0;
- sbit ACC7=ACC^7;
- sbit k1= P3^3; //设置
- sbit k2= P3^4; //加
- sbit k3= P1^6; //减
- sbit k4= P3^5; //闹钟
- sbit k5= P1^7; // 退出
- bit w=0;
- bit b=0;
- bit flag2;
- uchar flag,flag1,e,k1num,a;
- char miao,fen,shi,yue,ri,nian,ns,nf,sec;
- /**************液晶驱动程序****************************/
- void delay(int time1)
- {
- int i, j;
- for(i=time1;i>0;i--)
- for(j=110;j>0;j--);
- }
- void lcd_check_busy(void) //判断LCD忙程序
- {
- uchar signalbuff;
- RS=0;
- RW=1; //读状态
- do{
- lcd_bus=0xff; //端口电平全部拉高
- E=1; //开始读取LCD内部状态
- signalbuff=lcd_bus;
- E=0;
- } //停止读取
- while(signalbuff&0x80); //判断P2_7是否为0(液晶内部是否忙),如果为0,则跳出循环
- }
- void WRI(char instru) //液晶写指令
- {
- lcd_check_busy();
- RS = 0 ; //显示指令
- RW = 0 ; //写
- lcd_bus = instru;
- E = 1 ;
- E = 0 ;
- }
- void WRD(char lcddata) //液晶写数据
- {
- lcd_check_busy();
- RS = 1 ; //显示数据
- RW = 0 ; //写
- lcd_bus = lcddata;
- E = 1 ; //下降沿触发
- E = 0 ;
- }
- void lcd_set_xy(uchar x,uchar y)
- {
- uchar address;
- switch(y)
- {
- case 0:
- address = 0x80 + x;
- break;
- case 1:
- address = 0x90 + x;
- break;
- case 2:
- address = 0x88 + x;
- break;
- case 3:
- address = 0x98 + x;
- break;
- default:address = 0x80 + x;
- }
- WRI(lcd_set_ddram_address|address);
- }
- void init_lcd()
- {
- WRI(0x30); //基本指令集
- WRI(0x0c); //显示打开,光标关,反白显示关
- WRI(0x06); //DDRAM的地址计数器(AC)加1
- WRI(0x01); //清除屏幕显示,将DDRAM的地址计数器归零
- delay(100); //等待初始化结束
- }
- void lcd_write_string(uchar x,uchar y,uchar *s)
- {
- WRI(lcd_funtion_base);
- lcd_set_xy(x,y);
- while (*s!='\0')
- {
- WRD(*s);
- s++;
- }
- }
- void write_time(uchar x,uchar y,uchar time)
- {
- uchar shi,ge;
- shi=time/10;
- ge=time%10;
- WRI(lcd_funtion_base);//
- lcd_set_xy(x,y);
- WRD(0x30+shi);
- WRD(0x30+ge);
- }
- /******************DS1302驱动程序***********************************/
- void v_WTInputByte(uchar ucDa)
- {
- uchar i;
- ACC= ucDa;
- for(i=8; i>0; i--)
- {
- T_IO = ACC0; //*相当于汇编中的 RRC
- T_CLK = 1;
- T_CLK = 0;
- ACC =ACC>> 1;
- }
- }
- uchar uc_RTOutputByte(void)
- {
- uchar i;
- for(i=8; i>0; i--)
- {
- ACC = ACC>>1; //*相当于汇 编中的 RRC
- ACC7 = T_IO;
- T_CLK = 1;
- T_CLK = 0;
- }
- return(ACC);
- }
- void v_W1302(uchar ucAddr, uchar ucDa)
- {
- T_RST = 0;
- T_CLK = 0;
- T_RST = 1;
- v_WTInputByte(ucAddr); /* 地址,命令 */
- v_WTInputByte(ucDa); /* 写1Byte数据*/
- T_CLK = 1;
- T_RST =0;
- }
- uchar uc_R1302(uchar ucAddr)
- {
- uchar ucDa;
- T_RST = 0;
- T_CLK = 0;
- T_RST = 1;
- v_WTInputByte(ucAddr); /* 地址,命令 */
- ucDa = uc_RTOutputByte(); /* 读1Byte数据 */
- T_CLK = 1;
- T_RST =0;
- return(ucDa);
- }
- /********************************************************/
- void days()
- {
- if ( yue== 0x01 && ri== 0x01 ){ lcd_write_string(4,3,"元旦节"); }
- if ( yue== 0x02 && ri== 0x14 ){ lcd_write_string(4,3,"情人节"); }
- if ( yue== 0x03 && ri== 0x08 ){ lcd_write_string(4,3,"妇女节"); }
- if ( yue== 0x03 && ri== 0x12 ){ lcd_write_string(4,3,"植树节"); }
- if ( yue== 0x04 && ri== 0x01 ){ lcd_write_string(4,3,"愚人节"); }
- if ( yue== 0x04 && ri== 0x08 ){ lcd_write_string(4,3,"复活节"); }
- if ( yue== 0x05 && ri== 0x01 ){ lcd_write_string(4,3,"劳动节"); }
- if ( yue== 0x05 && ri== 0x04 ){ lcd_write_string(4,3,"青年节"); }
- if ( yue== 0x06 && ri== 0x01 ){ lcd_write_string(4,3,"儿童节"); }
- if ( yue== 0x08 && ri== 0x01 ){ lcd_write_string(4,3,"建军节"); }
- if ( yue== 0x09 && ri== 0x10 ){ lcd_write_string(4,3,"教师节"); }
- if ( yue== 0x10 && ri== 0x01 ){ lcd_write_string(4,3,"国庆节"); }
- if ( yue== 0x12 && ri== 0x24 ){ lcd_write_string(4,3,"平安夜"); }
- if ( yue== 0x12 && ri== 0x25 ){ lcd_write_string(4,3,"圣诞节"); }
- }
- /************************************************************/
- void base_disp()
- {
- WRI(0x01);//清屏
- lcd_write_string(3,0,"20");
- lcd_write_string(5,0,"年");
- lcd_write_string(1,1,"月");
- lcd_write_string(3,1,"日");
- lcd_write_string(5,1,"星期");
- lcd_write_string(1,2,"时");
- lcd_write_string(3,2,"分");
- lcd_write_string(5,2,"秒");
- }
- void disp()
- {
- lcd_write_string(3,1,"欢迎");
- lcd_write_string(1.5,2,"欢迎");
- lcd_write_string(5.5,2,"欢迎");
- lcd_write_string(3,3,"欢迎");
- delay(1000);
- base_disp();
- }
- /********************DS1302初始化*************************************/
- void init_ds1302() //
- {
- uchar flag ;
- flag=uc_R1302(0x81); // 在秒寄存器读数 ,ch=
- if (flag&0x80) //
- {
- v_W1302(0x8e,0x00); //
- v_W1302(0x80,0x45); //0秒
- v_W1302(0x82,0x29); //35分钟
- v_W1302(0x84,0x16); // 16时
- v_W1302(0x86,0x01); //
- v_W1302(0x88,0x01); //
- v_W1302(0x8a,0x01); //
- v_W1302(0x8c,0x16); // 16年
- v_W1302(0x8e,0x80);//
- }
- }
- /********************获取时间并显示************************************/
- void get_time()
- {
- miao=((uc_R1302(0x81))/16)*10+(uc_R1302(0x81))%16;
- fen=((uc_R1302(0x83))/16)*10+(uc_R1302(0x83))%16;
- shi=((uc_R1302(0x85))/16)*10+(uc_R1302(0x85))%16;
- yue=((uc_R1302(0x89))/16)*10+(uc_R1302(0x89))%16;
- ri=((uc_R1302(0x87))/16)*10+(uc_R1302(0x87))%16;
- nian=((uc_R1302(0x8d))/16)*10+(uc_R1302(0x8d))%16;
- days();
- // get_temp();
- write_time(4,0,nian);
- write_time(0,1,yue);
- write_time(2,1,ri);
- write_time(0,2,shi);
- write_time(2,2,fen);
- write_time(4,2,miao);
- a=(uc_R1302(0x8b))%16;//星期
- if(a==7){lcd_write_string(7,1,"日");}
- if(a==6){lcd_write_string(7,1,"六");}
- if(a==5){lcd_write_string(7,1,"五");}
- if(a==4){lcd_write_string(7,1,"四");}
- if(a==3){WRI(0x97);WRD(0xc8);WRD(0xfd);}
- if(a==2){lcd_write_string(7,1,"二");}
- if(a==1){lcd_write_string(7,1,"一");}
- if(fen==0&&miao==0){f=0;delay(300);f=1;delay(300);} // 整点报时 响两下
- if(fen==30&&miao==0){f=0;delay(100);f=1;delay(100);} //半点报时 响一下
- if(shi==ns&&fen==nf)//闹钟
- { if(flag2==0)
- {f=0;delay(200);f=1;delay(200);}
- }
- }
- /************************农历转**************************************/
- 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数据
- */
- bit c_moon;
- data uchar year_moon,month_moon,day_moon,week;
- /*子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回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);}
- }
- }
- /*
- 函数功能:输入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;
- }
- void change_nl()
- {
- uchar year,month,day;
- WRI(0x01);
- lcd_write_string(0,0,"农历:");
- Conversion(0,nian/10*16+nian%10,yue/10*16+yue%10,ri/10*16+ri%10);
- year=year_moon/16*10+year_moon%16;
- month=month_moon/16*10+month_moon%16;
- day=day_moon/16*10+day_moon%16;
- write_time(1,1,year);
- lcd_write_string(2,1,"年");
- write_time(3,1,month);
- lcd_write_string(4,1,"月");
- write_time(5,1,day);
- lcd_write_string(6,1,"日");
- //shengxiao();
- }
- /****************生肖显示**************************************/
- /*void shengxiao (void)
- {uchar yy1,yy2;
- yy1=(year_moon/16)*10+year_moon%16;
- yy2=yy1%12;
- switch(yy2)
- {
- case 0: lcd_write_string(6,0,"龙");
- break;
- case 1: lcd_write_string(6,0,"蛇");
- break;
- case 2: lcd_write_string(6,0,"马");
- break;
- case 3: lcd_write_string(6,0,"羊");
- break;
- case 4: lcd_write_string(6,0,"猴");
- break;
- case 5:lcd_write_string(6,0,"鸡");
- break;
- case 6:lcd_write_string(6,0,"狗");
- break;
- case 7: lcd_write_string(6,0,"猪");
- break;
- case 8: lcd_write_string(6,0,"鼠");
- break;
- case 9:lcd_write_string(6,0,"牛");
- break;
- case 10: lcd_write_string(6,0,"虎");
- break;
- case 11: lcd_write_string(6,0,"兔");
- break;
- }
- }*/
- /**********************键盘扫描**************************************/
- void keyscan()
- {
- if (k1==0&&w==0) //设置键
- {
- delay(5);//消抖;
- if (k1==0)
- {
- b=1;// 使能加键
- flag1=1; //关闭get-time
- k1num++;
- while (!k1);//松手检测;
- if (k1num==1)
- {
- lcd_write_string(4,3,"调整秒钟");
- lcd_set_xy(4,2);//把光标回到秒位
- WRI(0x0f);//显示光标并闪烁
- }
- if (k1num==2)
- {
- lcd_write_string(4,3,"调整分钟");
- lcd_set_xy(2,2); //
- }
- if (k1num==3)
- {
- lcd_write_string(4,3,"调整小时");
- lcd_set_xy(0,2);
- }
- if (k1num==4)
- {
- lcd_write_string(4,3,"调整星期");
- lcd_set_xy(7,1);
- }
- if (k1num==5)
- {
- lcd_write_string(4,3,"调整日期");
- lcd_set_xy(2,1);
- }
- if (k1num==6)
- {
- lcd_write_string(4,3,"调整月份");
- lcd_set_xy(0,1);
- }
- if (k1num==7)
- {
- lcd_write_string(4,3,"调整年份");
- lcd_set_xy(4,0);
- }
- if (k1num==8)
- {
- k1num=0;
- }
- }
- }
- /************************************************************************/
- if (k1!=0)//第一个键松手
- {
- if (k2==0&&b==1) //第二个键按下 加键
- {
- delay(5);
- if (k2==0)
- {
- while (!k2);
- if (k1num==1)
- {
- miao++;
- if (miao==60)
- {miao=0;}
- write_time(4,2,miao);
- lcd_set_xy(4,2);
- }
- if (k1num==2)
- {
- fen++;
- if (fen==60)
- fen=0;
- write_time(2,2,fen);
- lcd_set_xy(2,2);
- }
- if (k1num==3)
- {
- shi++;
- if (shi==25)
- shi=0;
- write_time(0,2,shi);
- lcd_set_xy(0,2);
- }
- if (k1num==4) //星期
- {
- a++;
- if(a==8)a=1;
- if(a==7){lcd_write_string(7,1,"日");}
- if(a==6){lcd_write_string(7,1,"六");}
- if(a==5){lcd_write_string(7,1,"五");}
- if(a==4){lcd_write_string(7,1,"四");}
- if(a==3){WRI(0x97);WRD(0xc8);WRD(0xfd);} //
- if(a==2){lcd_write_string(7,1,"二");}
- if(a==1){lcd_write_string(7,1,"一");}
- lcd_set_xy(7,1);
- }
- if (k1num==5)
- {
- ri++;
- if (ri==32)
- ri=1;
- write_time(2,1,ri);
- lcd_set_xy(2,1);
- }
- if (k1num==6)
- {
- yue++;
- if (yue==13)
- yue=1;
- write_time(0,1,yue);
- lcd_set_xy(0,1);
- }
- if (k1num==7)
- {
- nian++;
- if (nian==99)
- nian=0;
- write_time(4,0,nian);
- lcd_set_xy(4,0);
- }
- }
- }
- /***********************************************************************/
- if(k3==0)//第三键 减键
- {
- delay(5);
- if(k3==0)
- {
- while(!k3);
- if (k1num==1)
- {
- miao--;
- if(miao==-1)
- miao=59;
- write_time(4,2,miao);
- lcd_set_xy(4,2);
- }
- if (k1num==2)
- {
- fen--;
- if(fen==-1)
- fen=59;
- write_time(2,2,fen);
- lcd_set_xy(2,2);
- }
- if (k1num==3)
- {
- shi--;
- if(shi==-1)
- shi=23;
- write_time(0,2,shi);
- lcd_set_xy(0,2);
- }
- if (k1num==4) //星期
- {
- a--;
- if(a==0)a=7;
- if(a==7){lcd_write_string(7,1,"日");}
- if(a==6){lcd_write_string(7,1,"六");}
- if(a==5){lcd_write_string(7,1,"五");}
- if(a==4){lcd_write_string(7,1,"四");}
- if(a==3){WRI(0x97);WRD(0xc8);WRD(0xfd);}
- if(a==2){lcd_write_string(7,1,"二");}
- if(a==1){lcd_write_string(7,1,"一");}
- lcd_set_xy(7,1);
- }
- if (k1num==5)
- {
- ri--;
- if(ri==-1)
- ri=31;
- write_time(2,1,ri);
- lcd_set_xy(2,1);
- }
- if (k1num==6)
- {
- yue--;
- if(yue==-1)
- yue=12;
- write_time(0,1,yue);
- lcd_set_xy(0,1);
- }
- if (k1num==7)
- {
- nian--;
- if(nian==-1)
- nian=99;
- write_time(4,0,nian);
- lcd_set_xy(4,0);
- }
- }
- }
- }
- /************************************************************************/
- if(k5==0) //退出键
- {
- delay(5);
- if(k5==0)
- while(!k5);
- base_disp();
- WRI(0x0c); //关光标
- k1num=0; //
- v_W1302(0x8e,0x00); //
- v_W1302(0x80,(miao/10)*16+miao%10); //0秒
- v_W1302(0x82,(fen/10)*16+fen%10); //35分钟
- v_W1302(0x84,(shi/10)*16+shi%10); // 16时
- v_W1302(0x86,(ri/10)*16+ri%10); // 天数
- v_W1302(0x88,(yue/10)*16+yue%10); // 月份
- v_W1302(0x8a,a); //星期
- v_W1302(0x8c,(nian/10)*16+nian%10); // 09年
- v_W1302(0x8e,0x80);//
- flag1=0; //使能get-time
- w=0; //
- b=0; //nongli
- flag2=~flag2;// 闹钟开
- }
- /*************************************************************************/
- if(k4==0&&b==0) // 进入闹钟模式
- {
- delay(5);
- if(k4==0)
- {
- flag1=1; //关时间
- flag2=1; //闹钟关
- w=1; //取消按键1功能
- WRI(0x01); //
- lcd_write_string(0,0,"闹钟:");
- lcd_write_string(4,1,"时");
- lcd_write_string(6,1,"分");
- write_time(3,1,uc_R1302(0xc1));
- write_time(5,1,uc_R1302(0xc3));
- ns=uc_R1302(0xc1);
- nf=uc_R1302(0xc3);
- /***********************设置闹钟********************/
- while(k5==1)
- {
- if(k1==0&&w==1)
- {
- delay(5);
- if(k1==0)
- {
- k1num++;
- while (!k1);//松手检测;
- if (k1num==1)
- {
- lcd_write_string(3,2,"调整小时");
- lcd_set_xy(3,1);//把光标回
- WRI(0x0f);//显示光标并闪烁
- }
- if (k1num==2)
- {
- lcd_write_string(3,2,"调整分钟");
- lcd_set_xy(5,1); //
- }
- if (k1num==3)
- {
- k1num=0;
- }
- }
- }
- /****************加********************/
- if (k1!=0)//第一个键松手
- {
- if (k2==0) //第二个键按下 加键
- {
- delay(5);
- if (k2==0)
- {
- while (!k2);
- if (k1num==1)
- {
- ns++;
- if (ns==24)
- {ns=0;}
- write_time(3,1,ns);
- lcd_set_xy(3,1);
- }
- if (k1num==2)
- {
- nf++;
- if (nf==60)
- nf=0;
- write_time(5,1,nf);
- lcd_set_xy(5,1);
- }
- }
- }
- /****************减*********************/
- if(k3==0)
- {
- delay(5);
- if(k3==0)
- {
- while(!k3);
- if (k1num==1)
- {
- ns--;
- if(ns==-1)
- ns=23;
- write_time(3,1,ns);
- lcd_set_xy(3,1);
- }
- if (k1num==2)
- {
- nf--;
- if(nf==-1)
- nf=59;
- write_time(5,1,nf);
- lcd_set_xy(5,1);
- }
- }
- }
- }
- v_W1302(0x8e,0x00);
- // v_W1302(0xc0,(ns/10)*16+ns%10);
- // v_W1302(0xc2,(nf/10)*16+nf%10);
- v_W1302(0xc0,ns);
- v_W1302(0xc2,nf);
- v_W1302(0x8e,0x80);
- }
- }
- }
- /*****************转农历*********************/
- if (k2==0&&b==0)
- {
- delay(5);
- if(k2==0)
- w=1; //
- flag1=1;//
- while(!k2);
- change_nl();
- while(k5==0);
- }
- }
- /************主函数*********************************/
- void main(void)
- {
- init_lcd();
- init_ds1302();
- disp();
- while(1)
- {
- if(flag1==0)
- {
- get_time();
- }
- keyscan();
- }
- }
复制代码 |