這里我主機用的是15w系列的單片機,15w系列的資料太大了傳不上去,有興趣的可以和我要- #include <STC15F2K60S2.h>
- #include<string.h>
- #include<12864.h>
- #include<keyscan.h>
- #include<ds1302.h>
- #include<send.h>
- #include<comma.h>
- #include<hongwai.h>
- uchar clk_time[3]; //秒,分,時寄存器初始值
- uchar date[4]; //日,月,年寄存器
- uchar date_ng,date_ns,date_yg,date_ys,date_rg,date_rs,date_s,date_g,temp,week;
- uchar clock_ss,clock_sg,clock_fs,clock_fg,clock_ms,clock_mg,b,clock_s,clock_g;
- uchar code table2[]=" : : ";
- uchar code table3[]={"歡迎使用本系統"};
- uchar code table4[]={"20 年 月 日"};
- uchar code table6[]={"星期 "};
- uchar code table0[]={"溫度: ℃"};
- uchar code table1[]={"濕度: %"};
- uchar code date_x1[]={" 一"};
- uchar code date_x2[]={" 二"};
- uchar code date_x3[]={" 三"};
- uchar code date_x4[]={" 四"};
- uchar code date_x5[]={" 五"};
- uchar code date_x6[]={" 六"};
- uchar code date_x7[]={" 日"};
- uchar xdata shuzu[]={0x80,0x82,0x84,0x91,0x93,0x95,0x86}; //顯示在12864指定的位置上
- uchar xdata shuzu10[]={12,30,00,15,5,22,4};//存儲設定時間的值
- uchar idata IRCOM[7];
- uchar revbuf[20];
- uchar idata xianshi1[5];
- uchar idata xianshi2[5];
- uchar revlength=0,revFlag=0;
- uchar idata jiaflag=0,jianflag=0,setflag=0,yiweiflag=0,shezhinum;
- uchar idata dsFlag=0;
- /*************顯示函數********************/
- void xianshihanzi()
- {
- uchar j;
- lcd_pos(0,0);
- for(j=0;table2[j]!='\0';j++)
- write_dat(table2[j]);
- lcd_pos(1,0);
- for(j=0;table4[j]!='\0';j++)
- write_dat(table4[j]);
- lcd_pos(0,5);
- for(j=0;table6[j]!='\0';j++)
- write_dat(table6[j]);
- }
- //ds1302寫時間進去
- void DS1302write()
- {
-
- write_1302(0x8e,0x00); //WP=0 寫操作
- write_1302(0x80,0x00); //寫秒
- write_1302(0x82,0x32); //寫分
- write_1302(0x84,0x20); //寫時
- write_1302(0x86,0x22); //寫日
- write_1302(0x88,0x05); //寫月
- write_1302(0x8a,0x05); //寫星期
- write_1302(0x8c,0x15); //寫年
- write_1302(0x8e,0x80); //WP=1 寫保護
- }
- void write_week(uchar week)
- { // 星期轉換
- lcd_pos(0,7);
- switch(week)
- {
- case 1: for(week=1;date_x1[week]!='\0';week++)
- write_dat(date_x1[week]);
- break;
- case 2: for(week=2;date_x2[week]!='\0';week++)
- write_dat(date_x2[week]);
- break;
- case 3: for(week=3;date_x3[week]!='\0';week++)
- write_dat(date_x3[week]);
- break;
- case 4: for(week=4;date_x4[week]!='\0';week++)
- write_dat(date_x4[week]);
- break;
- case 5: for(week=5;date_x5[week]!='\0';week++)
- write_dat(date_x5[week]);
- break;
- case 6: for(week=6;date_x6[week]!='\0';week++)
- write_dat(date_x6[week]);
- break;
- case 7: for(week=7;date_x7[week]!='\0';week++)
- write_dat(date_x7[week]);
- break;
- }
- }
- void nyr()
- {
- date_rg=date[0]%16; date_rs=date[0]/16; /****寫年月日****/
- date_yg=date[1]%16; date_ys=date[1]/16;
- date_ng=date[3]%16; date_ns=date[3]/16;
- week=date[2]%16; //根據年月日計算得
- lcd_pos(1,1);
- write_dat(0x30+date_ns);
- write_dat(0x30+date_ng);
- lcd_pos(1,3);
- write_dat(0x30+date_ys);
- write_dat(0x30+date_yg);
- lcd_pos(1,5);
- write_dat(0x30+date_rs);
- write_dat(0x30+date_rg);
- }
- void sfm()
- {
- clock_ms=clk_time[0]/ 16; clock_mg=clk_time[0]%16;
- clock_fs=clk_time[1]/ 16; clock_fg=clk_time[1]%16;
- clock_ss=clk_time[2]/ 16; clock_sg=clk_time[2]%16;
- lcd_pos(0,0);
- write_dat(0x30+clock_ss);
- write_dat(0x30+clock_sg);
- lcd_pos(0,2);
- write_dat(0x30+clock_fs);
- write_dat(0x30+clock_fg);
- lcd_pos(0,4);
- write_dat(0x30+clock_ms);
- write_dat(0x30+clock_mg);
- }
- /***************寫時分秒,年月日函數*****************/
- void display()
- {
- uchar temp=0x81;
- uchar j,dat=0x00;
- if(b==0)
- {
- for(j=0;j<3;j++)
- {
- clk_time[j]=read_1302(temp);
- temp+=2;
- }
- for(j=0;j<4;j++)
- {
- date[j]=read_1302(temp);
- temp+=2;
- }
- sfm();
- nyr();
- write_week(week);
- }
- if(b!=0)
- {
- clock_s=dat/ 10; clock_g=dat%10;
- lcd_pos(0,0);
- write_dat(0x30+clock_s);
- write_dat(0x30+clock_g);
- lcd_pos(1,0);
- date_s=dat/ 10; date_g=dat%10;
- write_dat(0x30+date_s);
- write_dat(0x30+date_g);
- }
-
- }
-
- /******************主屏切換函數*********************/
- void qiehuan()
- {
- uchar n,qFlag=1;
- if(qFlag==1)
- {
- lcd_pos(2,0);
- for(n=0;table3[n]!='\0';n++)
- {
- write_dat(table3[n]);
- }
- }
- delay(1000);
- qFlag=0;
- write_cmd(0x01); //清除lcd12864顯示內容
- xianshihanzi();
-
- }
- /*******按鍵修改*******/
- /***************鍵盤掃描******************/
- void keyscan()
- {
- if(k3==1)
- {
- delay(1);
- if(k3==1)
- {
- k3=0;
- jiaflag=1;
-
- }
- while(!k3);
- }
- if(k4==1)
- {
- delay(1);
- if(k4==1)
- {
- k4=0;
- jianflag=1;
-
- }
- while(!k4);
- }
- if(k2==1)
- {
- delay(1);
- if(k2==1)
- {
- k2=0;
- setflag++;
- yiweiflag=1;
- }
- while(!k2);
- }
- }
- /*************時間設置*******************/
- void shezhi()
- {
- uchar num1;
- if(setflag>=1)
- {
- if(setflag==1)
- {
- setflag=2;
- lcd_pos(0,0);
- for(num1=0;num1<8;num1++)
- write_dat(table2[num1]);
- delay(1);
- lcd_pos(1,0);
- for(num1=0;num1<8;num1++)
- write_dat(table4[num1]);
- delay(1);
- }
- if(setflag>=2)
- {
- write_cmd(shuzu[shezhinum]); //顯示在12864指定的位置上
-
- if(jianflag==1)shuzu10[shezhinum]=timeupdate(shezhinum,--shuzu10[shezhinum]);
- if(jiaflag==1)shuzu10[shezhinum]=timeupdate(shezhinum,++shuzu10[shezhinum]);
- if(shezhinum!=6)
- {
- write_dat(48+shuzu10[shezhinum]/10);
- write_dat(48+shuzu10[shezhinum]%10);
- }
-
- //寫星期
- if(shezhinum==6)
- write_week(shuzu10[6]);
- write_cmd(shuzu[shezhinum]);
- write_cmd(0x0f); //光標閃爍
- delay(1);
- write_cmd(0x0e); //關掉光標閃爍
- jianflag=0;
- jiaflag=0;
- if(yiweiflag==1&&setflag>=3)
- {
- yiweiflag=0;
- shezhinum++;
- }
- if(shezhinum==7)setflag=10;
- }
- if(setflag==10)
- {
- ES=1;
- yiweiflag=0;
- jianflag=0;
- jiaflag=0;
- setflag=0;
- shezhinum=0;
- //將設置的時間存入1302
- write_1302(0x8e,0x00);
- write_1302(0x84,shuzu10[0]%10+(shuzu10[0]/10)*16);
- write_1302(0x82,shuzu10[1]%10+(shuzu10[1]/10)*16);
- write_1302(0x80,shuzu10[2]%10+(shuzu10[2]/10)*16);
- write_1302(0x8a,shuzu10[6]%10+(shuzu10[6]/10)*16);
- write_1302(0x8c,shuzu10[3]%10+(shuzu10[3]/10)*16);
- write_1302(0x88,shuzu10[4]%10+(shuzu10[4]/10)*16);
- write_1302(0x86,shuzu10[5]%10+(shuzu10[5]/10)*16);
- write_1302(0x8e,0x80);
- }
- }
- }
- /**************紅外修改************/
- void IR_IN(void) interrupt 2
- {
- unsigned char j,k,N=0;
- EX1 = 0;
- IRdelay(15);
- if (IRIN==1)
- { EX1 =1;
- return;
- }
- //確認IR信號出現
- while (!IRIN) //等IR變為高電平,跳過9ms的前導低電平信號。
- {IRdelay(1);}
- for (j=0;j<4;j++) //收集四組數據
- {
- for (k=0;k<8;k++) //每組數據有8位
- {
- while (IRIN) //等 IR 變為低電平,跳過4.5ms的前導高電平信號。
- {IRdelay(1);}
- while (!IRIN) //等 IR 變為高電平
- {IRdelay(1);}
- while (IRIN) //計算IR高電平時長
- {
- IRdelay(1);
- N++;
- if (N>=30)
- { EX1=1;
- return;} //0.14ms計數過長自動離開。
- } //高電平計數完畢
- IRCOM[j]=IRCOM[j] >> 1; //數據最高位補“0”
- if (N>=8) {IRCOM[j] = IRCOM[j] | 0x80;} //數據最高位補“1”
- N=0;
- }
- }
-
- if (IRCOM[2]!=~IRCOM[3])
- {
- EX1=1;
- return;
- }
- EX1 = 1;
- }
- void hongwai()
- {
- if(IRCOM[2]==0x09)
- {
- delay(1);
- if(IRCOM[2]==0x09)
- {
- setflag++;
- yiweiflag=1;
- IRCOM[2]=0;
- }
- }
- if(IRCOM[2]==0x15)
- {
- delay(1);
- if(IRCOM[2]==0x15)
- {
- jiaflag=1;
- IRCOM[2]=0;
- }
- }
- if(IRCOM[2]==0x07)
- {
- delay(1);
- if(IRCOM[2]==0x07)
- {
- jianflag=1;
- IRCOM[2]=0;
- }
- }
- }
-
- /**************溫濕度顯示函數*******************/
- void LCD_Display1(uchar X,uchar Y,uchar *str_str)
- {
- uchar i=0;
- lcd_pos(X,Y);
- while(str_str[i]!='\0')
- {
- write_dat(str_str[i]);
- i++;
- }
- }
- /**************清零函數***************/
- void paraInit()
- {
- memset(revbuf,'\0',strlen(revbuf));
- revFlag=0;
- revlength=0;
- }
- /****************主函數************/
- void main()
- {
- lcd12864Init();
- qiehuan();
- init();
- int_init();
- LCD_Display1(2,0,table0);
- LCD_Display1(3,0,table1);
-
- // DS1302write();
- //write_1302(0x8e,0x00); //WP=0 寫操作
- //write_1302(0x90,0xa4);//開涓流充電
- //write_1302(0x8e,0x80);// /WP=1 寫保護
- while(1)
- {
- SendString("aaa");
- delay(200);
- if(revFlag==1)
- {
- GetDestString(revbuf,',',1,',',xianshi1);
- LCD_Display1(2,3,xianshi1);
- GetDestString(revbuf,',',2,',',xianshi2);
- LCD_Display1(3,3,xianshi2);
- paraInit();
- }
- hongwai();
- keyscan();
- if(setflag==0) display();
- if((setflag<=10&&setflag>=1)||jiaflag==1||jianflag==1)
- {
- ES=0;
- shezhi();
- }
-
- }
-
- }
- /****************定時中斷****************/
- /*void Timer0() interrupt 1
- {
- uchar num2;
- dsFlag=1;
- TL0 = 0x00; //設置定時初值
- TH0 = 0x4c; //設置定時初值
- num2++;
- if(num2==50)
- num2=0;
-
- }
- /**************串口接收中斷****************/
- void time() interrupt 4
- {
-
- if(RI==1)
- {
- RI=0;
- revFlag=1;
- revbuf[revlength++]=SBUF;
-
- }
- }
復制代碼
|