Altium Designer画的时钟+温湿度+PM2.5检测系统PCB图如下:(51hei附件中可下载工程文件)
单片机源程序如下:
- /*************************************
- 环境检测系统
- 主要功能及硬件:
- 1、功能:环境时钟的显示和调整,
- 硬件: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数据接入
- uchar timetest[7];
- /*******************************
- 定义按键:
- modekey 为功能选择按键/确定按键
- upkey 为向上选择/增加按键
- dowmkey 为向下选择/减少按键
- backkey 为返回/取消按键
- *******************************/
- sbit modekey=P1^6;
- sbit upkey =P1^5;
- sbit downkey=P1^4;
- sbit backkey=P1^3;
- /*****************************
- 主函数main
- *****************************/
- void main()
- {
- LCD12864_Init(); //12864屏幕模块的初始化
- Usartinit(); //串口初始化
- form1(); //form1为主界面
- while(1)
- {
- data1(); //加载时钟、DHT22和ZPH01数据
- if(modekey==0) //判断是否进入调整时间界面
- {
- Delay_N1ms(5); //消抖处理,防止误判
- if(modekey==0) //二次判断,确定按下
- {
- for(i=0; i<8; i++)
- {
- timetest[i]=TIME[i]; //截取当前时间放入第二界面,这样便于调整时钟
- }
- form2(); //进入时间调整界面
- }
- }
- }
- }
- /*************************************
- form1为第一界面
- 即为开机时看到的界面,
- 为了加快运行数度,减少不必要的重复
- 将form1放在main函数主循环的前面
- *************************************/
- 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为第二界面,即为时间调整界面当
- 按下第一个建,即确定键的时候,进入
- 该界面。
- ************************************/
- void form2()
- {
- clearping(); //清屏函数
- flag2=1; //界面2标识循环判断,用于退出界面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);
- /************************************
- 本段代码用于显示进入时间调整界面时的
- 当前时间显示
- ************************************/
- 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(); //清屏函数
- 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(); //清屏函数
- 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) //检测上次低电平是否结束
- {
- if(++cnt >= 300)
- {
- break;
- }
- }
- //延时Min=26us Max50us 跳过数据"0" 的高电平
- Delay_N10us(4); //延时30us
-
- //判断传感器发送数据位
- tmp =0;
- if(Sensor_SDA)
- {
- tmp = 1;
- }
- cnt =0;
- while(Sensor_SDA) //等待高电平 结束
- {
- 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
- //主机设为输入 判断传感器响应信号
- Sensor_SDA = 1;
-
- Sensor_AnswerFlag = 0; // 传感器响应标志
- //判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
- if(Sensor_SDA ==0)
- {
- Sensor_AnswerFlag = 1;//收到起始信号
- Sys_CNT = 0;
- //判断从机是否发出 80us 的低电平响应信号是否结束
- while((!Sensor_SDA))
- {
- if(++Sys_CNT>300) //防止进入死循环
- {
- Sensor_ErrorFlag = 1;
- return 0;
- }
- }
- Sys_CNT = 0;
- //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
- while((Sensor_SDA))
- {
- if(++Sys_CNT>300) //防止进入死循环
- {
- Sensor_ErrorFlag = 1;
- return 0;
- }
- }
- // 数据接收 传感器共发送40位数据
- // 即5个字节 高位先送 5个字节分别为湿度高位 湿度低位 温度高位 温度低位 校验和
- // 校验和为:湿度高位+湿度低位+温度高位+温度低位
- for(i=0;i<5;i++)
- {
- Sensor_Data[i] = Read_SensorData();
- }
- }
- else
- {
- Sensor_AnswerFlag = 0; // 未收到传感器响应
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
51单片机室内环境检测仪.zip
(9.74 MB, 下载次数: 247)
|