Altium Designer畫的時鐘+溫濕度+PM2.5檢測系統(tǒng)PCB圖如下:(51hei附件中可下載工程文件)
0.jpg (98.97 KB, 下載次數(shù): 72)
下載附件
2018-3-25 20:41 上傳
捕獲1.GIF (24.21 KB, 下載次數(shù): 73)
下載附件
2018-3-25 19:49 上傳
單片機源程序如下:
- /*************************************
- 環(huán)境檢測系統(tǒng)
- 主要功能及硬件:
- 1、功能:環(huán)境時鐘的顯示和調(diào)整,
- 硬件:DS1302
- 2、功能:溫度(單位℃)和濕度(單位H%)顯示
- 硬件:DHT22
- 3、功能:PM2.5(單位ug/m3)顯示
- 硬件:ZPH11
- 屏幕:12864B(自帶字庫)
- 說明:
- 大家好,我叫小木(^_^)·
- *************************************/
- #include<reg51.h>
- #include<lcd12864.h>
- #include<ds1302.h>
- void showtime();
- void showweek();
- void showweek1();
- void showTH();
- void showPM();
- void Usartinit();
- unsigned char Read_SensorData(void);
- unsigned char Read_Sensor(void);
- void Delay_N10us(unsigned char t);
- void Delay_N1ms(unsigned int t);
- void form1();
- void form2();
- void data1();
- void clearping();
- void Hgarden(int x);//空心圓
- void Sgarden(int x);//實心圓
- void timesetting(int x);
- /********************************************
- 界面文字顯示
- ********************************************/
- //第1界面顯示
- unsigned char code CharCode[13]="0123456789.-:";
- unsigned char code Temperature[]="溫度:";
- unsigned char code Tsign[]="℃";
- unsigned char code Humidity[]="濕度:";
- unsigned char code Hsign[]="RH";
- unsigned char code PM1[]="PM2.5:";
- unsigned char code TPM1[]="ug/m3";
- //第2界面顯示
- unsigned char code year1[]="年";
- unsigned char code mouth1[]="月";
- unsigned char code day1[]="日";
- unsigned char code hour1[]="時";
- unsigned char code minute1[]="分";
- unsigned char code second1[]="秒";
- unsigned char code week3[]="周";
- unsigned char code week1[]="星期";
- //清屏
- unsigned char code clear[]=" ";
- unsigned char code week2_0[]="日";
- unsigned char code week2_1[]="一";
- unsigned char code week2_2[]="二";
- unsigned char code week2_3[]="三";
- unsigned char code week2_4[]="四";
- unsigned char code week2_5[]="五";
- unsigned char code week2_6[]="六";
- unsigned int flagkeyup_down=1;
- unsigned int page=1;
- unsigned int i,j,m;
- unsigned int flag2,flag3;
-
- uchar UART_Upload[9];
- uchar UARTnum[11]={"0123456789."};
- uchar UARTdata[5];
- uint pm_rateH,pm_rateL,pm_particle,pm_density;//低脈沖率高位。低脈沖率低位, 比率,顆粒,濃度
- uchar accord,pm_URV=20,temp_pM; //PM_URV pm上限值
- uchar Sensor_Data[5]={0x00,0x00,0x00,0x00,0x00};
- uchar Sensor_AnswerFlag; //收到起始標志位
- uchar Sensor_ErrorFlag; //讀取傳感器錯誤標志
- uchar Sensor_Check; //校驗和
- uint Sys_CNT;
- uint Tmp;
- uint t0;
- uchar num;
- uchar flag;
- sbit Sensor_SDA = P1^7;//溫濕度傳感器DHT22數(shù)據(jù)接入
- uchar timetest[7];
- /*******************************
- 定義按鍵:
- modekey 為功能選擇按鍵/確定按鍵
- upkey 為向上選擇/增加按鍵
- dowmkey 為向下選擇/減少按鍵
- backkey 為返回/取消按鍵
- *******************************/
- sbit modekey=P1^6;
- sbit upkey =P1^5;
- sbit downkey=P1^4;
- sbit backkey=P1^3;
- /*****************************
- 主函數(shù)main
- *****************************/
- void main()
- {
- LCD12864_Init(); //12864屏幕模塊的初始化
- Usartinit(); //串口初始化
- form1(); //form1為主界面
- while(1)
- {
- data1(); //加載時鐘、DHT22和ZPH01數(shù)據(jù)
- if(modekey==0) //判斷是否進入調(diào)整時間界面
- {
- Delay_N1ms(5); //消抖處理,防止誤判
- if(modekey==0) //二次判斷,確定按下
- {
- for(i=0; i<8; i++)
- {
- timetest[i]=TIME[i]; //截取當前時間放入第二界面,這樣便于調(diào)整時鐘
- }
- form2(); //進入時間調(diào)整界面
- }
- }
- }
- }
- /*************************************
- form1為第一界面
- 即為開機時看到的界面,
- 為了加快運行數(shù)度,減少不必要的重復
- 將form1放在main函數(shù)主循環(huán)的前面
- *************************************/
- void form1()
- {
- LCD12864_SetWindow(0, 2); //位置坐標顯示
- LCD12864_WriteData(year1[0]); //年
- LCD12864_WriteData(year1[1]);
- LCD12864_SetWindow(0, 4); //星期
- for(i=0;i<4;i++)
- LCD12864_WriteData(week1[i]);
- LCD12864_SetWindow(1, 1); //月
- LCD12864_WriteData(mouth1[0]);
- LCD12864_WriteData(mouth1[1]);
- LCD12864_SetWindow(1, 3); //日
- LCD12864_WriteData(day1[0]);
- LCD12864_WriteData(day1[1]);
- LCD12864_SetWindow(1, 7); //℃
- for(i=0;i<2;i++)
- LCD12864_WriteData(Tsign[i]);
- LCD12864_SetWindow(2, 7); //H%
- for(i=0;i<2;i++)
- LCD12864_WriteData(Hsign[i]);
- LCD12864_SetWindow(3, 0); //pm2.5
- for(i=0;i<6;i++)
- LCD12864_WriteData(PM1[i]);
- }
- /************************************
- form2為第二界面,即為時間調(diào)整界面當
- 按下第一個建,即確定鍵的時候,進入
- 該界面。
- ************************************/
- void form2()
- {
- clearping(); //清屏函數(shù)
- flag2=1; //界面2標識循環(huán)判斷,用于退出界面2
- /******************************
- 本段代碼用于顯示時、分、秒、年、
- 月、日、周
- ******************************/
- LCD12864_SetWindow(0, 1);
- for(i=0;i<2;i++)
- LCD12864_WriteData(hour1[i]);
- LCD12864_SetWindow(1, 1);
- for(i=0;i<2;i++)
- LCD12864_WriteData(minute1[i]);
- LCD12864_SetWindow(2, 1);
- for(i=0;i<2;i++)
- LCD12864_WriteData(second1[i]);
- LCD12864_SetWindow(3, 1);
- for(i=0;i<2;i++)
- LCD12864_WriteData(week3[i]);
- LCD12864_SetWindow(0, 5);
- for(i=0;i<2;i++)
- LCD12864_WriteData(year1[i]);
- LCD12864_SetWindow(1, 5);
- for(i=0;i<2;i++)
- LCD12864_WriteData(mouth1[i]);
- LCD12864_SetWindow(2, 5);
- for(i=0;i<2;i++)
- LCD12864_WriteData(day1[i]);
- LCD12864_SetWindow(3, 5);
- LCD12864_WriteData(0xc8);
- LCD12864_WriteData(0xb7);
- LCD12864_WriteData(0xb6);
- LCD12864_WriteData(0xa8);
- /************************************
- 本段代碼用于顯示進入時間調(diào)整界面時的
- 當前時間顯示
- ************************************/
- LCD12864_SetWindow(0, 6); //年
- LCD12864_WriteData(CharCode[timetest[6]/16]);
- LCD12864_WriteData(CharCode[(timetest[6]&0x0f)]);
- showweek1(); //星期
- LCD12864_SetWindow(1, 6);
- LCD12864_WriteData(CharCode[timetest[4]/16]); //月
- LCD12864_WriteData(CharCode[(timetest[4]&0x0f)]);
- LCD12864_SetWindow(2, 6);
- LCD12864_WriteData(CharCode[timetest[3]/16]); //日
- LCD12864_WriteData(CharCode[(timetest[3]&0x0f)]);
- LCD12864_SetWindow(0, 2);
- LCD12864_WriteData(CharCode[timetest[2]/16]); //小時
- LCD12864_WriteData(CharCode[(timetest[2]&0x0f)]);
- LCD12864_SetWindow(1, 2);
- LCD12864_WriteData(CharCode[timetest[1]/16]); //分鐘
- LCD12864_WriteData(CharCode[(timetest[1]&0x0f)]);
- LCD12864_SetWindow(2, 2);
- LCD12864_WriteData(CharCode[timetest[0]/16]); //秒
- LCD12864_WriteData(CharCode[(timetest[0]&0x0f)]);
- /***********************************************
- 分隔線
- ***********************************************/
- LCD12864_SetWindow(0, 3);
- LCD12864_WriteData(0xa3);
- LCD12864_WriteData(0xfc);
- LCD12864_SetWindow(1, 3);
- LCD12864_WriteData(0xa3);
- LCD12864_WriteData(0xfc);
- LCD12864_SetWindow(2, 3);
- LCD12864_WriteData(0xa3);
- LCD12864_WriteData(0xfc);
- LCD12864_SetWindow(3, 3);
- LCD12864_WriteData(0xa3);
- LCD12864_WriteData(0xfc);
- //-------------------------------------------
- while(flag2) //在界面二中進入鍵盤掃描
- {
- Hgarden(flagkeyup_down); //顯示按鍵指針,以便知道在什么位置
- /****向上************/
- if(upkey==0)
- {
- Delay_N1ms(5);
- if(upkey==0)
- {
- flagkeyup_down--; //按鍵指針向上運動
- if(flagkeyup_down<1) //當按鍵指針運動到最上時不在運動
- {
- flagkeyup_down=8;
- }
- Hgarden(flagkeyup_down);
- i=0;
- while ((i<40) && (upkey==0))//檢測按鍵是否松開,如果按著不動,則400ms后自動跳出向下運行
- {
- Delay_N1ms(10);
- i++;
- }
- }
- }
- /****向下************/
- if(downkey==0)
- {
- Delay_N1ms(5);
- if(downkey==0)
- {
- flagkeyup_down++; //按鍵指針向下運動
- if(flagkeyup_down>8) //當按鍵指針運動到最下時不在運動
- {
- flagkeyup_down=1;
- }
- Hgarden(flagkeyup_down);
- i=0;
- while ((i<40) && (downkey==0)) //檢測按鍵是否松開
- {
- Delay_N1ms(10);
- i++;
- }
- }
- }
- /****確定************/
- if(modekey==0) //按下確定鍵后,可以進行時間的修改
- {
- Delay_N1ms(10);
- if(modekey==0)
- {
- i=0;
- while ((i<40) && (modekey==0)) //檢測按鍵是否松開
- {
- Delay_N1ms(10);
- i++;
- }
- if(flagkeyup_down==8)
- {
- timetest[0]++;
- for(j=0; j<8; j++)
- TIME[j]=timetest[j];
- Ds1302Init();
- flag2=0;
- clearping(); //清屏函數(shù)
- form1();
- }
- else
- {
- flag3=1;
- while(flag3)
- {
- Sgarden(flagkeyup_down);
- timesetting(flagkeyup_down);
- if(modekey==0) //按下確定鍵后,可以進行時間的修改
- {
- Delay_N1ms(15);
- if(modekey==0)
- {
- flag3=0;
- while ((i<40) && (modekey==0)) //檢測按鍵是否松開
- {
- Delay_N1ms(10);
- i++;
- }
- }
- }
- }
- }
- }
- }
- /****返回************/
- if(backkey==0) //按下確定鍵后,可以進行時間的修改
- {
- Delay_N1ms(10);
- if(backkey==0)
- {
- i=0;
- flag2=0;
- clearping(); //清屏函數(shù)
- form1();
- while ((i<50) && (backkey==0)) //檢測按鍵是否松開
- {
- Delay_N1ms(10);
- i++;
- }
- }
- }
- }
- }
- void timesetting(int x)
- {
- unsigned int d;
- unsigned int m1,m2,m3,m4,m5,m6,m7;
- /****加************/
- if(upkey==0)
- {
- Delay_N1ms(5);
- if(upkey==0)
- {
- if(x==1)//小時
- {
- timetest[2]++;
- if((timetest[2]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[2]=timetest[2]+6;
- }
- if(timetest[2]>0x23)
- {
- timetest[2]=0;
- m1=1;
- }
- LCD12864_SetWindow(0, 2);
- LCD12864_WriteData(CharCode[timetest[2]/16]);
- LCD12864_WriteData(CharCode[(timetest[2]&0x0f)]);
- }
- else if(x==2)//分鐘
- {
- timetest[1]++;
- if((timetest[1]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[1]=timetest[1]+6;
- }
- if(timetest[1]>0x59)
- {
- timetest[1]=0;
- m2=1;
- }
- LCD12864_SetWindow(1, 2);
- LCD12864_WriteData(CharCode[timetest[1]/16]);
- LCD12864_WriteData(CharCode[(timetest[1]&0x0f)]);
- }
- else if(x==3)//秒
- {
- timetest[0]++;
- if((timetest[0]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[0]=timetest[0]+6;
- }
- if(timetest[0]>0x59)
- {
- timetest[0]=0;
- m3=1;
- }
- LCD12864_SetWindow(2, 2);
- LCD12864_WriteData(CharCode[timetest[0]/16]);
- LCD12864_WriteData(CharCode[(timetest[0]&0x0f)]);
- }
- else if(x==4)//周
- {
- timetest[5]++;
- if(timetest[5]>0x6)
- {
- timetest[5]=0;
- m4=1;
- }
- showweek1();
- }
- else if(x==5)//年
- {
- timetest[6]++;
- if((timetest[6]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[6]=timetest[6]+6;
- }
- if(timetest[6]>0x90)
- {
- timetest[6]=0;
- m5=1;
- }
- LCD12864_SetWindow(0, 6);
- LCD12864_WriteData(CharCode[timetest[6]/16]);
- LCD12864_WriteData(CharCode[(timetest[6]&0x0f)]);
- }
- else if(x==6)//月
- {
- timetest[4]++;
- if((timetest[4]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[4]=timetest[4]+6;
- }
- if(timetest[4]>0x12)
- {
- timetest[4]=0x01;
- m6=1;
- }
- LCD12864_SetWindow(1, 6);
- LCD12864_WriteData(CharCode[timetest[4]/16]);
- LCD12864_WriteData(CharCode[(timetest[4]&0x0f)]);
- }
- else if(x==7)//日
- {
-
- if(timetest[4]==0x01||timetest[4]==0x03||timetest[4]==0x05||timetest[4]==0x07||timetest[4]==0x08||timetest[4]==0x10||timetest[4]==0x12)
- d=0x31;
- else if(timetest[4]==0x04||timetest[4]==0x06||timetest[4]==0x09||timetest[4]==0x11)
- d=0x30;
- else if((CharCode[timetest[6]/16]*10+CharCode[(timetest[6]&0x0f)])%4==0)
- d=0x29;
- else
- d=0x28;
- timetest[3]++;
- if((timetest[3]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[3]=timetest[3]+6;
- }
- if(timetest[3]>d)
- {
- timetest[3]=0;
- m7=1;
- }
- LCD12864_SetWindow(2, 6);
- LCD12864_WriteData(CharCode[timetest[3]/16]);
- LCD12864_WriteData(CharCode[(timetest[3]&0x0f)]);
- }
-
- i=0;
- while ((i<40) && (upkey==0))//檢測按鍵是否松開,如果按著不動,則400ms后自動跳出向下運行
- {
- Delay_N1ms(10);
- i++;
- }
- }
- }
- /****減************/
- if(downkey==0)
- {
- Delay_N1ms(5);
- if(downkey==0)
- {
- if(x==1)//小時
- {
- timetest[2]--;
- if((timetest[2]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[2]=timetest[2]-6;
- }
- if(m1==1)
- timetest[2]=0x23;
- if(timetest[2]==0x00)
- m1=1;
- else m1=0;
- LCD12864_SetWindow(0, 2);
- LCD12864_WriteData(CharCode[timetest[2]/16]);
- LCD12864_WriteData(CharCode[(timetest[2]&0x0f)]);
- }
- else if(x==2)//分鐘
- {
- timetest[1]--;
- if((timetest[1]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[1]=timetest[1]-6;
- }
- if(m2==1)
- timetest[1]=0x59;
- if(timetest[1]==0x00)
- m2=1;
- else m2=0;
- LCD12864_SetWindow(1, 2);
- LCD12864_WriteData(CharCode[timetest[1]/16]);
- LCD12864_WriteData(CharCode[(timetest[1]&0x0f)]);
- }
- else if(x==3)//秒
- {
- timetest[0]--;
- if((timetest[0]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[0]=timetest[0]-6;
- }
- if(m3==1)
- timetest[0]=0x59;
- if(timetest[0]==0x00)
- m3=1;
- else m3=0;
- LCD12864_SetWindow(2, 2);
- LCD12864_WriteData(CharCode[timetest[0]/16]);
- LCD12864_WriteData(CharCode[(timetest[0]&0x0f)]);
- }
- else if(x==4)//周
- {
- timetest[5]--;
- if(m4==1)
- timetest[5]=0x06;
- if(timetest[5]==0x00)
- m4=1;
- else m4=0;
- showweek1();
- }
- else if(x==5)//年
- {
- timetest[6]--;
- if((timetest[6]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[6]=timetest[6]-6;
- }
- if(m5==1)
- timetest[6]=0x59;
- if(timetest[6]==0x00)
- m5=1;
- else m5=0;
- LCD12864_SetWindow(0, 6);
- LCD12864_WriteData(CharCode[timetest[6]/16]);
- LCD12864_WriteData(CharCode[(timetest[6]&0x0f)]);
- }
- else if(x==6)//月
- {
- timetest[4]--;
- if((timetest[4]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[4]=timetest[4]-6;
- }
- if(m6==1)
- timetest[4]=0x12;
- if(timetest[4]==0x00)
- m6=1;
- else m6=0;
- LCD12864_SetWindow(1, 6);
- LCD12864_WriteData(CharCode[timetest[4]/16]);
- LCD12864_WriteData(CharCode[(timetest[4]&0x0f)]);
- }
- else if(x==7)//日
- {
-
- if(timetest[4]==0x01||timetest[4]==0x03||timetest[4]==0x05||timetest[4]==0x07||timetest[4]==0x08||timetest[4]==0x10||timetest[4]==0x12)
- d=0x31;
- else if(timetest[4]==0x04||timetest[4]==0x06||timetest[4]==0x09||timetest[4]==0x11)
- d=0x30;
- else if((CharCode[timetest[6]/16]*10+CharCode[(timetest[6]&0x0f)])%4==0)
- d=0x29;
- else
- d=0x28;
- timetest[3]--;
- if((timetest[3]&0x0f)>9) //換成BCD碼,跳過a-e段
- {
- timetest[3]=timetest[3]-6;
- }
- if(m7==1)
- timetest[3]=d;
- if(timetest[3]==0x00)
- m7=1;
- else m7=0;
- LCD12864_SetWindow(2, 6);
- LCD12864_WriteData(CharCode[timetest[3]/16]);
- LCD12864_WriteData(CharCode[(timetest[3]&0x0f)]);
- }
- i=0;
- while ((i<40) && (downkey==0)) //檢測按鍵是否松開
- {
- Delay_N1ms(10);
- i++;
- }
- }
- }
- }
- void Hgarden(int x)
- {
- if(x==1)
- {
- LCD12864_SetWindow(3, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(0, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(1, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==2)
- {
- LCD12864_SetWindow(0, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(1, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(2, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==3)
- {
- LCD12864_SetWindow(1, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(2, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(3, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==4)
- {
- LCD12864_SetWindow(2, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(3, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(0, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==5)
- {
- LCD12864_SetWindow(3, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(0, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(1, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==6)
- {
- LCD12864_SetWindow(0, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(1, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(2, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==7)
- {
- LCD12864_SetWindow(1, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(2, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- LCD12864_SetWindow(3, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- }
- else if(x==8)
- {
- LCD12864_SetWindow(0, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(2, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xa0);
- LCD12864_SetWindow(3, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf0);
- }
- }
- void Sgarden(int x)
- {
- if(x==1)
- {
- LCD12864_SetWindow(0, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==2)
- {
- LCD12864_SetWindow(1, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==3)
- {
- LCD12864_SetWindow(2, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==4)
- {
- LCD12864_SetWindow(3, 0);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==5)
- {
- LCD12864_SetWindow(0, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==6)
- {
- LCD12864_SetWindow(1, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==7)
- {
- LCD12864_SetWindow(2, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- else if(x==8)
- {
- LCD12864_SetWindow(3, 4);
- LCD12864_WriteData(0xa1);
- LCD12864_WriteData(0xf1);
- }
- }
- void data1()
- {
- showtime();
- showTH();
- showPM();
- }
- void showtime()
- {
- Ds1302ReadTime();
- LCD12864_SetWindow(0, 0);
- LCD12864_WriteData(CharCode[2]);
- LCD12864_WriteData(CharCode[0]); //年
- LCD12864_WriteData(CharCode[TIME[6]/16]);
- LCD12864_WriteData(CharCode[(TIME[6]&0x0f)]);
-
- showweek();//星期
- LCD12864_SetWindow(1, 0);
- LCD12864_WriteData(CharCode[TIME[4]/16]); //月
- LCD12864_WriteData(CharCode[(TIME[4]&0x0f)]);
- LCD12864_SetWindow(1, 2);
- LCD12864_WriteData(CharCode[TIME[3]/16]); //日
- LCD12864_WriteData(CharCode[(TIME[3]&0x0f)]);
- LCD12864_SetWindow(2, 0);
- LCD12864_WriteData(CharCode[TIME[2]/16]); //小時
- LCD12864_WriteData(CharCode[(TIME[2]&0x0f)]);
- LCD12864_WriteData(CharCode[12]);
- LCD12864_WriteData(CharCode[TIME[1]/16]); //分鐘
- LCD12864_WriteData(CharCode[(TIME[1]&0x0f)]);
- LCD12864_WriteData(CharCode[12]);
- LCD12864_WriteData(CharCode[TIME[0]/16]); //秒
- LCD12864_WriteData(CharCode[(TIME[0]&0x0f)]);
- }
- void showweek() //周
- {
- uint wek;
- wek=CharCode[(TIME[5]&0x0f)];
- LCD12864_SetWindow(0, 6);
- if(wek=='0')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_0[i]);
- else if(wek=='1')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_1[i]);
- else if(wek=='2')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_2[i]);
- else if(wek=='3')
- {
- LCD12864_WriteData(0xc8);
- LCD12864_WriteData(0xfd);
- }
- else if(wek=='4')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_4[i]);
- else if(wek=='5')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_5[i]);
- else if(wek=='6')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_6[i]);
- else
- ;
- }
- void showweek1() //周
- {
- uint wek;
- wek=CharCode[(timetest[5]&0x0f)];
- LCD12864_SetWindow(3, 2);
- if(wek=='0')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_0[i]);
- else if(wek=='1')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_1[i]);
- else if(wek=='2')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_2[i]);
- else if(wek=='3')
- {
- LCD12864_WriteData(0xc8);
- LCD12864_WriteData(0xfd);
- }
- else if(wek=='4')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_4[i]);
- else if(wek=='5')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_5[i]);
- else if(wek=='6')
- for(i=0;i<2;i++)
- LCD12864_WriteData(week2_6[i]);
- else
- ;
- }
- void showTH()
- {
- Read_Sensor();
- Tmp = Sensor_Data[2]*256+Sensor_Data[3];
- LCD12864_SetWindow(1, 5);
- LCD12864_WriteData(CharCode[Tmp/100%10]);
- LCD12864_WriteData(CharCode[Tmp/10%10]);
- LCD12864_WriteData(CharCode[10]);
- LCD12864_WriteData(CharCode[Tmp%10]);
- Tmp = Sensor_Data[0]*256+Sensor_Data[1];
- LCD12864_SetWindow(2, 5);
- LCD12864_WriteData(CharCode[Tmp/100%10]);
- LCD12864_WriteData(CharCode[Tmp/10%10]);
- LCD12864_WriteData(CharCode[10]);
- LCD12864_WriteData(CharCode[Tmp%10]);
- }
- unsigned char Read_SensorData(void)
- {
- unsigned char i,cnt;
- unsigned char buffer,tmp;
- buffer = 0;
- for(i=0;i<8;i++)
- {
- cnt=0;
- while(!Sensor_SDA) //檢測上次低電平是否結(jié)束
- {
- if(++cnt >= 300)
- {
- break;
- }
- }
- //延時Min=26us Max50us 跳過數(shù)據(jù)"0" 的高電平
- Delay_N10us(4); //延時30us
-
- //判斷傳感器發(fā)送數(shù)據(jù)位
- tmp =0;
- if(Sensor_SDA)
- {
- tmp = 1;
- }
- cnt =0;
- while(Sensor_SDA) //等待高電平 結(jié)束
- {
- if(++cnt >= 200)
- {
- break;
- }
- }
- buffer <<=1;
- buffer |= tmp;
- }
- return buffer;
- }
- /********************************************\
- |* 功能: 讀傳感器 *|
- \********************************************/
- unsigned char Read_Sensor(void)
- {
- unsigned char i;
- //主機拉低(Min=800US Max=20Ms)
- Sensor_SDA = 0;
- Delay_N1ms(10); //延時2Ms
-
- //釋放總線 延時(Min=30us Max=50us)
- Sensor_SDA = 1;
- Delay_N10us(4);//延時30us
- //主機設(shè)為輸入 判斷傳感器響應信號
- Sensor_SDA = 1;
-
- Sensor_AnswerFlag = 0; // 傳感器響應標志
- //判斷從機是否有低電平響應信號 如不響應則跳出,響應則向下運行
- if(Sensor_SDA ==0)
- {
- Sensor_AnswerFlag = 1;//收到起始信號
- Sys_CNT = 0;
- //判斷從機是否發(fā)出 80us 的低電平響應信號是否結(jié)束
- while((!Sensor_SDA))
- {
- if(++Sys_CNT>300) //防止進入死循環(huán)
- {
- Sensor_ErrorFlag = 1;
- return 0;
- }
- }
- Sys_CNT = 0;
- //判斷從機是否發(fā)出 80us 的高電平,如發(fā)出則進入數(shù)據(jù)接收狀態(tài)
- while((Sensor_SDA))
- {
- if(++Sys_CNT>300) //防止進入死循環(huán)
- {
- Sensor_ErrorFlag = 1;
- return 0;
- }
- }
- // 數(shù)據(jù)接收 傳感器共發(fā)送40位數(shù)據(jù)
- // 即5個字節(jié) 高位先送 5個字節(jié)分別為濕度高位 濕度低位 溫度高位 溫度低位 校驗和
- // 校驗和為:濕度高位+濕度低位+溫度高位+溫度低位
- for(i=0;i<5;i++)
- {
- Sensor_Data[i] = Read_SensorData();
- }
- }
- else
- {
- Sensor_AnswerFlag = 0; // 未收到傳感器響應
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
51單片機室內(nèi)環(huán)境檢測儀.zip
(9.74 MB, 下載次數(shù): 247)
2018-3-25 19:53 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|