当前位置:文档之家› LCD12864单片机电子万年历程序

LCD12864单片机电子万年历程序

*********************************#include<>#include<>#define LCD_Data P0#define uchar unsigned charunsigned charsec,min,hour,day,month,year,cen,week, next,aa,bb,cc,dd,mm,temp0,LunarMonth, LunarDay,LunarYear;int temp;uchar tt=1;bit c_moon;bit cenbit=1;bit w;sbit LCD_RS=P2^0;sbit LCD_RW=P2^1;sbit LCD_E=P2^2;sbit PSB=P2^3;sbit DS1302_CLK=P3^7;sbit DS1302_IO=P3^6;sbit DS1302_RST=P3^5;sbit DS18B20=P2^5;sbit speak=P2^7;sbit SetKey=P1^4; // 按键功能:设置sbit SureKey=P1^5; // 按键功能:确认sbit PlusKey=P1^6; // 按键功能:加sbit ReduceKey=P1^7; // 按键功能:减sbit scl=P1^1;sbit sda=P1^0;void DisplayShengXiao(void);void delay(unsigned int a)// 延时 1MS/ 次{unsigned char i;while(--a){for(i=0;i<125;i++) ;}}void delayb(unsigned int count){while(count--);}void delays(){ ; ;}void start(){sda=1;scl=1;delays();sda=0;delays();}void stop(){sda=0;scl=1;delays();sda=1;delays();}void response(){uchar i;while((sda==1)&&i<255)i++; //给应答信号 sda=0 ;错误( while (( sda==0 )|i<255 ) i++ ;)程序进入 while 死循环scl=1;delays();scl=0; // 没有这一步出错,如果没有 sda 将保持 0 状态,一直处于应答中。

程序停止。

delays();}void write24c02(uchar date){uchar temp,i;temp=date;for(i=0;i<8;i++){scl=0;sda=temp&0x80;delays();scl=1;delays();temp=temp<<1;}scl=0;delays();sda=1;delays();}uchar read24c02(){uchar k,i;scl=0;delays();sda=1;// 仅仅是释放数据线,可有可无for(i=0;i<8;i++){scl=1;delays();k=k<<1;if(sda)k++;scl=0;delays();}return k;}void write_24c02add(uchar address,uchar date){start();write24c02(0xa0);response();write24c02(address);response();write24c02(date);response();stop();}uchar read_24c02add(uchar address) {uchar a;start();write24c02(0xa0);response();write24c02(address);response();start();write24c02(0xa1);response();a=read24c02();stop();return a;}unsigned char DS18B20Init(){ unsigned char x;DS18B20=1;delayb(2);DS18B20=0;delayb(80);DS18B20=1;delayb(5);x=DS18B20;delayb(20);return x;}unsigned char TempRead(void){unsigned char i,dat;DS18B20=1;delayb(1);for(i=0;i<8;i++){DS18B20=0;dat=dat>>1;DS18B20=1;if(DS18B20)dat=dat|0x80;delayb(4);}return dat;}void TempWriteByte(unsigned chardat){unsigned int i;DS18B20=1;delayb(2);for(i=0;i<8;i++){DS18B20=0;DS18B20=dat&0x01;delayb(8);DS18B20=1;dat=dat>>1;delayb(2);}}int GetTemp(){float tt;unsigned char a,b;DS18B20Init();TempWriteByte(0xcc); TempWriteByte(0x44);delayb(100);DS18B20Init();TempWriteByte(0xcc); TempWriteByte(0xbe);delayb(200);a=TempRead();b=TempRead();temp=b;temp<<=8;temp=temp|a;tt=temp*;temp=tt*10+;return temp;}void WriteDs1302Byte(unsigned char temp){unsigned char i;for (i=8;i>0;i--){DS1302_IO=temp&0x01;DS1302_CLK=0;DS1302_CLK=1;temp>>=1;}}void WriteDs1302( unsigned char address,unsigned char dat ){DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;delay(1);WriteDs1302Byte(address);WriteDs1302Byte(dat);DS1302_RST=0;}unsigned char read_byte(){ unsigned char i;for(i=8;i>0;i--){if(DS1302_IO)temp0=temp0|0x80;DS1302_CLK=1;DS1302_CLK=0;temp0=temp0>>1;}return temp0;}unsigned char ReadDs1302(unsignedchar address){unsigned char temp;DS1302_RST=0;DS1302_CLK=0;DS1302_RST=1;WriteDs1302Byte(address);temp=read_byte();DS1302_RST=0;DS1302_CLK=1;return temp;}void InitDS1302(){// unsigned charSecond=ReadDs1302(0x80);// if(Second&0x80){WriteDs1302(0x8e,0x00);delay(5);WriteDs1302(0x8c,0x10);// 写入年份 10 年delay(5);WriteDs1302(0x8a,0x06);// 写入星期 6delay(5);WriteDs1302(0x88,0x11);// 定入月分 11 月delay(5);WriteDs1302(0x86,0x6);// 写入日期 6 日delay(5);WriteDs1302(0x84,0x17);// 写入小时 17 点delay(5);WriteDs1302(0x82,0x43);delay(5);WriteDs1302(0x80,0x00);// 写入秒 30 秒delay(5);WriteDs1302(0x8e,0x80);// 控制命令, WP 为 1, 禁止写操作}}/* 阳历对应的阴历数据,每年三字节,格式第一字节 BIT7- 4 位表示闰月月份,为 0, 则无闰月, BIT3-0 对应阴历第 1-4 月的大小,第二字节 BIT7- 0 对应阴历第 5-1 2 月大小,第三字节 BIT7 表示阴历第 13 月大小月分对应的位为 1, 表示农历月大( 3 0天)为 0 表示小( 29 天)第三字节 BIT6- 5 表示春节的公历月份,BIT4-0 表示春节公历日期*/code unsigned char YearCode[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, //19120x5d,0x52,0xBA, //19140x0B,0x54,0x4e, //19150x0d,0x6A,0x43, //1916 0x29,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, //19370x7C,0x96,0xBf, //19380x0C,0x95,0x53, //19390x0d,0x4A,0x48, //19400x6d,0xA5,0x3B, //19410x0B,0x55,0x4f, //19420x05,0x6A,0x45, //19430x4A,0xAd,0xB9, //19440x02,0x5d,0x4d, //19450x09,0x2d,0x42, //19460x2C,0x95,0xB6, //19470x0A,0x95,0x4A, //19480x7B,0x4A,0xBd, //19490x06,0xCA,0x51, //19500x0B,0x55,0x46, //19510x55,0x5A,0xBB, //19520x04,0xdA,0x4e, //19530x0A,0x5B,0x43, //19540x35,0x2B,0xB8, //19550x05,0x2B,0x4C, //19560x8A,0x95,0x3f, //19570x06,0xAA,0x48, //19590x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //19610x04,0xB6,0x45, //19620x4A,0x57,0x39, //19630x0A,0x57,0x4d, //19640x05,0x26,0x42, //19650x3e,0x93,0x35, //19660x0d,0x95,0x49, //19670x75,0xAA,0xBe, //19680x05,0x6A,0x51, //19690x09,0x6d,0x46, //19700x54,0xAe,0xBB, //19710x04,0xAd,0x4f, //19720x0A,0x4d,0x43, //19730x4d,0x26,0xB7, //19740x0d,0x25,0x4B, //19750x8d,0x52,0xBf, //19760x0B,0x54,0x52, //19770x0B,0x6A,0x47, //19780x69,0x6d,0x3C, //19790x09,0x5B,0x50, //19800x04,0x9B,0x45, //19810x4A,0x4B,0xB9, //19820x0A,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 2940x0C,0x96,0x45, //2000 2970x4d,0x4A,0xB8, //2001 3000x0d,0x4A,0x4C, //2002 3030x0d,0xA5,0x41, //2003 3060x25,0xAA,0xB6, //2004 309 0x05,0x6A,0x49, //2005 3120x7A,0xAd,0xBd, //2006 3150x02,0x5d,0x52, //2007 3180x09,0x2d,0x47, //2008 3210x5C,0x95,0xBA, //2009 3240x0A,0x95,0x4e, //2010 3270x0B,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, //20250x0A,0x4e,0x51, //20260x0d,0x26,0x46, //20270x5e,0x93,0x3A, //20280x0d,0x53,0x4d, //20290x05,0xAA,0x43, //20300x36,0xB5,0x37, //20310x09,0x6d,0x4B, //20320xB4,0xAe,0xBf, //20330x04,0xAd,0x53, //20340x0A,0x4d,0x48, //20350x6d,0x25,0xBC, //20360x0d,0x25,0x4f, //20370x0d,0x52,0x44, //20380x5d,0xAA,0x38, //20390x0B,0x5A,0x4C, //20400x05,0x6d,0x41, //20410x24,0xAd,0xB6, //20420x04,0x9B,0x4A, //20430x7A,0x4B,0xBe, //20440x0A,0x4B,0x51, //20450x0A,0xA5,0x46, //20460x5B,0x52,0xBA, //20470x06,0xd2,0x4e, //20480x35,0x5B,0x37, //20500x09,0x37,0x4B, //20510x84,0x97,0xC1, //20520x04,0x97,0x53, //20530x06,0x4B,0x48, //20540x66,0xA5,0x3C, //20550x0e,0xA5,0x4f, //20560x06,0xB2,0x44, //20570x4A,0xB6,0x38, //20580x0A,0xAe,0x4C, //20590x09,0x2e,0x42, //20600x3C,0x97,0x35, //20610x0C,0x96,0x49, //20620x7d,0x4A,0xBd, //20630x0d,0x4A,0x51, //20640x0d,0xA5,0x45, //20650x55,0xAA,0xBA, //20660x05,0x6A,0x4e, //20670x0A,0x6d,0x43, //20680x45,0x2e,0xB7, //20690x05,0x2d,0x4B, //20700x8A,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, //2092 0x66,0xB5,0x3B, //20930x04,0xAe,0x45, //20950x4A,0x4e,0xB9, //20960x0A,0x4d,0x4C, //20970x0d,0x15,0x41, //20980x2d,0x92,0xB5, //2099};void WriteDataLCD(unsigned chardat){LCD_RS=1;LCD_RW=0;LCD_E=0;delay(2);LCD_Data=dat;delay(2);LCD_E=1;delay(5);LCD_E=0;delay(5);}void WriteCommandLCD(unsigned charudat){LCD_RS=0;LCD_RW=0;LCD_E=0;delay(2);LCD_Data=udat;delay(2);LCD_E=1;delay(5);LCD_E=0;delay(5);}void LCDInit(void){WriteCommandLCD(0x30);WriteCommandLCD(0x01); WriteCommandLCD(0x06);WriteCommandLCD(0x0C);}void LCDClear(void){WriteCommandLCD(0x01);WriteCommandLCD(0x34);WriteCommandLCD(0x30);}void LCDSendWord(unsigned char *p {while(*p>0){WriteDataLCD(*p);p++;}}void LCDTestWord(bit i,unsigned char word){if(i==0){WriteCommandLCD(word);}else{WriteDataLCD(word);}}void DisplayYear(void){year=ReadDs1302(0x8d); LCDTestWord(0,0x81); LCDTestWord(1,(year/16)+0x30); LCDTestWord(1,year%16+0x30); LCDTestWord(0,0x82); LCDSendWord(" 年 "); DisplayShengXiao();}void DisplayMonth(void){month=ReadDs1302(0x89); LCDTestWord(0,0x83);if(month/16!=0){LCDTestWord(1,(month/16)+0x30); }else{LCDTestWord(1,0x20);}LCDTestWord(1,month%16+0x30);LCDTestWord(0,0x84); LCDSendWord(" 月 "); DisplayShengXiao();}void DisplayDay(void){day=ReadDs1302(0x87); LCDTestWord(0,0x85);if(day/16!=0)LCDTestWord(1,(day/16)+0x30); } else{ LCDTestWord(1,0x20);} LCDTestWord(1,day%16+0x30); LCDTestWord(0,0x86); LCDSendWord(" 日 "); DisplayShengXiao();}void DisplayHour(void){hour=ReadDs1302(0x85); LCDTestWord(0,0x88); LCDTestWord(1,(hour/16)+0x30); LCDTestWord(1,hour%16+0x30);}void DisplayMin(void){min=ReadDs1302(0x83); LCDTestWord(0,0x89);// LCDTestWord(1,0x3a); LCDSendWord(" 点 "); LCDTestWord(1,(min/16)+0x30); LCDTestWord(1,min%16+0x30); LCDSendWord(" 分 ");}void DisplaySec(void){unsigned char i=0;unsigned int a=0,b=0,c=0;sec=ReadDs1302(0x81); LCDTestWord(0,0x8c); LCDTestWord(1,(sec/16)+0x30); LCDTestWord(1,sec%16+0x30);}void DisplayTemp(void){unsigned int i;unsigned char a,b,c; WriteCommandLCD(0x8c);LCDTestWord(0,0x8d);LCDSendWord(" ");i=GetTemp();a=i/100;LCDTestWord(1,a+0x30);b=i%100/10;LCDTestWord(1,b+0x30);LCDTestWord(1,0x2e);c=i-a*100-b*10;LCDTestWord(1,c+0x30);LCDSendWord("C");}code unsigned charDayCode1[9]={0x00,0x1f,0x3b,0x5a,0x780x97,0xb5,0xd4,0xf3};code unsigned intDayCode2[3]={0x111,0x130,0x14e};// 计算公历日离当年元旦的天数 , 为了减少运算 , 用了两个表DayCode1[9],DayCode2[3]// 如果公历月在九月或前 , 天数会少于0xff, 用表 DayCode1[9], 在九月后 , 天数大于 0xff, 用表 DayCode2[3]// 如输入公历日为 8 月 1 0 日 , 则公历日离元旦天数为 DayCode1[8-1]+10-1, 如输入公历日为 11 月 10 日 , 则公历日离元旦天数为 DayCode2[11-10]+10-1/* 读取数据表中农历月的大月或小月 ,如果该月为大返回 1, 为小返回 0*/bit GetMoonDay(unsigned char LunarMonth,unsigned int TableAddr){unsigned char temp;switch (LunarMonth)//LunarMonth 指向农历月份{case 1:{temp=YearCode[TableAddr]&0x08//1 月,对应年份表里第一字节的 BIT3 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);// 为 0, 月小// 为 1, 月大}case 2:{temp=YearCode[TableAddr]&0x04; // 2月,对应年份表里第一字节的 BIT2 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);// 为 0, 月小else return(1);// 为 1, 月大}case 3:{temp=YearCode[TableAddr]&0x02; // 3月,对应第一字节的 BIT1 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);// 为 0, 月小else return(1);// 为 1, 月大}case 4:{temp=YearCode[TableAddr]&0x01; // 1月,对应第一字节的 BIT0 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 5:{temp=YearCode[TableAddr+1]&0x80; // 5月,对应第二字节的 BIT7 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 6:{temp=YearCode[TableAddr+1]&0x40; // 6月,对应第二字节的 BIT6 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);}case 7:{temp=YearCode[TableAddr+1]&0x20; // 7月,对应第二字节的 BIT5 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 8:{temp=YearCode[TableAddr+1]&0x10; // 8月,对应第二字节的 BIT4 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 9:{temp=YearCode[TableAddr+1]&0x08; // 9月,对应第二字节的 BIT3 位,如果是 1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 10:{temp=YearCode[TableAddr+1]&0x04;//10 月,对应第二字节的 BIT2 位,如果是1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 11:{temp=YearCode[TableAddr+1]&0x02;//11 月,对应第二字节的 BIT1 位,如果是1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 12:{temp=YearCode[TableAddr+1]&0x01;//12 月,对应第二字节的 BIT0 位,如果是1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}case 13:{temp=YearCode[TableAddr+2]&0x80;//13 月,对应第三字节的 BIT7 位,如果是1, 则月大,如果是 0, 则月小if (temp==0) return(0);else return(1);}}}void Conversion(bitcenbit,unsigned char year,unsigned charmonth,unsigned char day){unsigned chartemp1,temp2,temp3,MonthP;//temp3,temp4 分别表示春节距元旦的天数,公历日离元旦的天数unsigned int temp4,TableAddr;bit flag2,flag_y;temp1=year/16;//BCD->hex 先把数据转换为十六进制高位temp2=year%16; // 低位year=temp1*10+temp2; // 把年数据转换成 10 进制temp1=month/16; // 月份高位temp2=month%16; // 月份低位month=temp1*10+temp2; // 把月数据转换成 10 进制temp1=day/16; // 日期高位temp2=day%16; // 日期低位day=temp1*10+temp2; // 把日数据转换成 16 进制// 如果是 21 世纪TableAddr=(year+0x64-1)*0x0// 定位数据表地址 year 对应的年份表中的地址是 (year+99)*3 如 201 0 年其地址是327LCDTestWord(0,0x80);LCDSendWord("20");temp1=YearCode[TableAddr+2]&0x60;// 取当年春节所在的公历月份年份表中第三字节 BIT6-5 表示春节的公历月份temp1=_cror_(temp1,5);// 循环右移 5 位,得到春节所在的公历月份temp2=YearCode[TableAddr+2]&0x1f;// 取当年春节所在的公历日年份表中第三字节 BIT4-0 表示当年春节所在的公历日if(temp1==0x01)// 计算当年春年离当年元旦的天数 , 春节只会在公历 1 月或 2 月temp3=temp2-1;// 假如春节在公历 1 月,则元旦离春节的天数为 temp2-1 天elsetemp3=temp2+0x1f-1;// 假如春节在公历 2 月,则无理离春节的天数为 temp2+0x1f-1 天if (month<10)temp4=DayCode1[month-1]+day-1;//0 到 8 月某日距元旦的天数elsetemp4=DayCode2[month-10]+day-1;//9 月开始的某一天距元旦的天数if((month>0x02)&&(year%0x04==0))// 如果公历月大于 2 月并且该年的 2 月为闰月 , 天数加 1temp4+=1;// 计算机出公历日距元旦的天数和春节距元旦的天数,则是为了比较公历日是在春节前还是春节后// 如果 temp3>temp4 则公历日在春节之前if (temp4>=temp3)// 公历日在春节后或就是春节当日使用下面代码进行运算{temp4-=temp3;// 公历日离春节的天数因为公历日在春节后所以为 temp4-temp3month=0x01;MonthP=0x01;//LunarMonth 为月份指向 , 公历日在春节前或就是春节当日 LunarMonth 指向首月flag2=GetMoonDay(MonthP,TableAddr); //检查该农历月为大小还是小月 , 大月返回 1,小月返回 0flag_y=0;if(flag2==0)//GetMoonDay() 函数返回的是 0{temp1=0x1d;}// 小月 29 天else//GetMoonDay() 函数返回的是 1{temp1=0x1e;}// 大月 30 天temp2=YearCode[TableAddr]&0xf0;// 年份数据表中第 1 字节 BIT7-4 为闰月,为 0 则这年无闰月,如为 1, 表示有闰月temp2=_cror_(temp2,4);// 从数据表中取该年的闰月月份 , 如为 0 ,则该年无闰月 BIT3- 0 表示阴历 1 到 4 月的大小 1 为大 0 为小while(temp4>=temp1){temp4-=temp1;MonthP+=1;if(month==temp2){flag_y=~flag_y;if(flag_y==0){month+=1;}}else{month+=1;}flag2=GetMoonDay(MonthP,TableAddr);if(flag2==0){temp1=0x1d;}else{temp1=0x1e;}}day=temp4+1;}else{ // 公历日在春节前使用下面代码进行运算temp3-=temp4;// 公历日离春节的天数因为公历日在春节前所以为 temp3-temp4 if(year==0x00){year=0x63;cenbit=1;}else year-=1;TableAddr-=0x03;month=0x0c;temp2=YearCode[TableAddr]&0xf0;格式第一字节 BIT7-4 位表示闰月月份,则无闰月, BIT3-0 对应阴历第 1-4 月的小,temp2=_cror_(temp2,4);if (temp2==0)MonthP=0xelse MonthP=0x0d; ///* MonthP 为月份指向 , 如当年有闰月 , 一年有十三个月 , 月指向 1闰月指向 12*/flag_y=0;flag2=GetMoonDay(MonthP,TableAddr)if(flag2==0)temp1=0x1delse temp1=0x1e;while(temp3>temp1){temp3-=temp1;MonthP-=1;if(flag_y==0)month-=1;if(month==temp2)flag_y=~flag_y;flag2=GetMoonDay(MonthP,TableAddr)if(flag2==0)temp1=0x1d;else temp1=0x1e;}day=temp1-temp3+1;}c_moon=cenbit;temp1=year/10;temp1=_crol_(temp1,4);temp2=year%10;LunarYear=temp1|temp2;temp1=month/10;temp1=_crol_(temp1,4); temp2=month%10;LunarMonth=temp1|temp2;temp1=day/10;temp1=_crol_(temp1,4);temp2=day%10;LunarDay=temp1|temp2;}void DisplayShengXiao(void){unsigned char LunarYearD,ReYear;// 农历年份的十进制数和取模后的余数if(cen==0x19) {cenbit=1;}if(cen==0x20) {cenbit=0;}Conversion(cenbit,year,month,day);// 将公历日期转换成农历LCDTestWord(0,0x94);// 显示在 LCD 的 0X94 位置上LCDTestWord(1,LunarYear/16+0x30);// 农历年十位LCDTestWord(1,LunarYear%16+0x30);// 农历年个位LCDTestWord(1,'-');// 用 - 隔开LCDTestWord(1,LunarMonth/16+0x30);// 农历月十位LCDTestWord(1,LunarMonth%16+0x30);// 农历月个位LCDTestWord(1,'-');LCDTestWord(1,LunarDay/16+0x30);// 农历日十位LCDTestWord(1,LunarDay%16+0x30);// 农历日个位LunarYearD=(LunarYear/16)*10+LunarYea r%16; // 农历年转换成 10 进制数ReYear=LunarYearD%12;// 农历年模 12, 取余运算switch(ReYear) {case 0:LCDTestWord(0,0x93);LCDSendWord(" 龙");break; // 余 0 即整除农历龙年case 1:LCDTestWord(0,0x93);LCDSendWord(" 蛇");break; // 蛇年case 2:LCDTestWord(0,0x93);LCDSendWord(" 马");break;case 3:LCDTestWord(0,0x93);LCDSendWord(" 羊");break;case 4:LCDTestWord(0,0x93);LCDSendWord(" 猴");break;case 5:LCDTestWord(0,0x93);LCDSendWord(" 鸡");break;case 6:LCDTestWord(0,0x93);LCDSendWord(" 狗");break;case 7:LCDTestWord(0,0x93);LCDSendWord(" 猪");break;case 8:LCDTestWord(0,0x93);LCDSendWord(" 鼠");break;case 9:LCDTestWord(0,0x93);LCDSendWord(" 牛");break;case10:LCDTestWord(0,0x93);LCDSendWord("虎 ");break;case11:LCDTestWord(0,0x93);LCDSendWord("兔 ");break;}}void UpDate(void){DisplayYear();DisplayMonth();DisplayDay();DisplayWeek();DisplayHour();DisplayMin();DisplaySec();DisplayShengXiao();}void SetTime(unsigned char count) {char address,item;unsigned char max,mini;if(count<8){LCDTestWord(0,0x98); LCDSendWord(" 调整 ");if(count==7) {LCDSendWord(" 秒钟请按 + - "); address=0x81;max=59;mini=0;}if(count==2) {LCDSendWord(" 分钟请按 + - "); address=0x83;max=59;mini=0;}if(count==1) {LCDSendWord(" 小时请按 + - ");address=0x85;max=23;mini=0;}if(count==5) {LCDSendWord(" 星期请按 + - "); address=0x8b;max=7;mini=1;}if(count==3) {LCDSendWord(" 日期请按 + - ");address=0x87; max=31; mini=1;}if(count==4) {LCDSendWord(" 月份请按 + - "); address=0x89;max=12;mini=1;}if(count==6) {LCDSendWord(" 年份请按 + - "); address=0x8d;max=99;mini=0;}item=ReadDs1302(address);// 读取DS1302 某地址上的数值赋给 item item=(item/16)*10+item%16;if(PlusKey==0) //PlusKey-加{ item++; } // 数加 1if(ReduceKey==0) //ReduceKey- 减{item--; } // 数减 1if(item>max)item=mini; // 查看数值有效范围if(item<mini)item=max;WriteDs1302(0x8e,0x00);item=(item/10)*16+item%10;WriteDs1302(address-1,item); // 将调整好的 item 值写入 DS1302UpDate(); }else{if(count==8){LCDTestWord(0,0x01); WriteCommandLCD(0x0c);aa=read_24c02add(30);if(PlusKey==0) //PlusKey-加aa++; // 数加 1if(ReduceKey==0)//ReduceKey- 减aa--;write_24c02add(30,aa);LCDTestWord(0,0x80);LCDSendWord(" 请输入温度报警上");LCDTestWord(0,0x90);LCDSendWord(" 限 :");LCDTestWord(0,0x92);LCDTestWord(1,aa/10+0x30); LCDTestWord(1,aa%10+0x30); LCDSendWord(" 度 ");}if(count==9){bb=read_24c02add(31); WriteCommandLCD(0x0c);if(PlusKey==0) //PlusKey-加bb++; // 数加 1if(ReduceKey==0)//ReduceKey- 减bb--;write_24c02add(31,bb); LCDTestWord(0,0x88);LCDSendWord(" 请输入温度报警下限 :");LCDTestWord(0,0x98);LCDSendWord(" 限 :");LCDTestWord(0,0x9a);LCDTestWord(1,bb/10+0x30); LCDTestWord(1,bb%10+0x30); LCDSendWord(" 度 ");}if(count==10){cc=read_24c02add(32);LCDTestWord(0,0x01); WriteCommandLCD(0x0c); LCDTestWord(0,0x92);if(PlusKey==0) //PlusKey-加cc++; // 数加 1if(ReduceKey==0)//ReduceKey- 减cc--;write_24c02add(32,cc); LCDTestWord(0,0x80);LCDSendWord(" 请设置闹钟 1"); LCDTestWord(0,0x93);LCDSendWord(" 点 ");LCDTestWord(0,0x95);LCDSendWord(" 分 ");LCDTestWord(0,0x92);LCDTestWord(1,cc/10+0x30); LCDTestWord(1,cc%10+0x30); LCDTestWord(0,0x94);LCDTestWord(1,dd/10+0x30); LCDTestWord(1,dd%10+0x30); LCDTestWord(0,0x88);LCDSendWord(" 闹钟: "); LCDTestWord(0,0x98);LCDSendWord(" 设置闹钟小时位 ");}if(count==11){ dd=read_24c02add(33); if(PlusKey==0) //PlusKey-加dd++; // 数加 1if(ReduceKey==0)//ReduceKey- 减dd--;write_24c02add(33,dd);LCDTestWord(0,0x94);LCDTestWord(1,dd/10+0x30);LCDTestWord(1,dd%10+0x30);LCDTestWord(0,0x98);LCDSendWord(" 设置闹钟分钟位 ");}if(count==12){LCDTestWord(0,0x98);LCDSendWord(" 设置闹钟状态 ");if(PlusKey==0) //PlusKey-加mm=1;if(ReduceKey==0)//ReduceKey- 减mm=0;write_24c02add(34,mm);if(mm==1){LCDTestWord(0,0x8b);LCDSendWord(" 开");}if(mm==0){LCDTestWord(0,0x8b);LCDSendWord(" 关");}}}}void Key(void){if (SetKey==0) // 设置时间{delay(10); // 按键消抖if(SetKey==0&&w==0) // 当是正常状态时就进入调时状态{w=1; // 进入调时SetTime(next); // 调整}if(SetKey==0&&w==1) // 当是调时状态本键用于调整下一项{next++;if(next==15) {next=0;WriteCommandLCD(0x0c);}SetTime(next); // 调整}while(SetKey==0); // 等待键松开}switch(next){case 1: WriteCommandLCD(0x0f);LCDTestWord(0,0x88);break;case 2: WriteCommandLCD(0x0f);LCDTestWord(0,0x8a);break;case 3: WriteCommandLCD(0x0f);LCDTestWord(0,0x85);break;case 4: WriteCommandLCD(0x0f);LCDTestWord(0,0x83);break;case 5: WriteCommandLCD(0x0f);LCDTestWord(0,0x92);break;case 6: WriteCommandLCD(0x0f);LCDTestWord(0,0x81);break;case 7: WriteCommandLCD(0x0f);LCDTestWord(0,0x8c);break;case 8: WriteCommandLCD(0x0f);LCDTestWord(0,0x92);break;case 9: WriteCommandLCD(0x0f);LCDTestWord(0,0x9a);break;case 10: WriteCommandLCD(0x0f);LCDTestWord(0,0x92);break;case 11: WriteCommandLCD(0x0f);LCDTestWord(0,0x94);break;case 12: WriteCommandLCD(0x0f);LCDTestWord(0,0x8b);break;}if(SureKey==0) // 当在调时状态时就退出调时{delay(10); // 按键消抖if(SureKey==0&&w==1){w=0;next=0;Holidays();}while(SureKey==0); // 等待键松开}if (PlusKey==0) // 加调整{delay(10); // 按键消抖if(PlusKey==0&&w==1){SetTime(next); // 调整}while(PlusKey==0); // 等待键松开}if (ReduceKey==0) // 减调整{delay(10); // 按键消抖if(ReduceKey==0&&w==1){SetTime(next); // 调整}while(ReduceKey==0); // 等待键松开}}void beep(){ int i;uchar hour1,min1,high,low;hour1=read_24c02add(32);min1=read_24c02add(33);high=read_24c02add(30);low=read_24c02add(31);mm=read_24c02add(34);hour=ReadDs1302(0x85)/16*10+ReadDs1302(0x85)%16;min=ReadDs1302(0x83)/16*10+ReadDs1302(0x83)%16;i=GetTemp()/;//tt=1;if(mm==1){LCDTestWord(0,0x87);WriteDataLCD(0x0e);WriteDataLCD(0x0e);}if(mm==0){LCDTestWord(0,0x87);WriteDataLCD(0x01);WriteDataLCD(0x01);}if(((hour>6&&hour<23)&&sec==0&&min==0) &&mm==1){speak=0;delay(20);speak=1;delay(10);}if((hour==hour1&&min==min1)&&mm==1){speak=0;delay(20);speak=1;delay(10);}if((i+1>high||i<low)&&mm==1){speak=0;delay(150);speak=1;delay(50);}}void main(){SetKey=1;SureKey=1;PlusKey=1;ReduceKey=1;PSB=1;next=0;delayb(100);LCDInit();LCDClear();InitDS1302();while(1){ while(!next){Key();WriteCommandLCD(0x0c);DisplayYear(); // 显示年DisplayMonth(); // 显示月DisplayDay(); // 显示日DisplayWeek(); // 显示星期DisplayHour(); // 显示时DisplayMin(); // 显示分DisplaySec(); // 显示秒beep();}Key();}}31。

相关主题