找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6240|回复: 10
打印 上一主题 下一主题
收起左侧

51单片机PM2.5室内环境检测仪源程序+PCB文件 还有时钟+温湿度功能

  [复制链接]
跳转到指定楼层
楼主
Altium Designer画的时钟+温湿度+PM2.5检测系统PCB图如下:(51hei附件中可下载工程文件)



单片机源程序如下:
  1. /*************************************
  2. 环境检测系统
  3. 主要功能及硬件:
  4. 1、功能:环境时钟的显示和调整,
  5.    硬件:DS1302
  6. 2、功能:温度(单位℃)和湿度(单位H%)显示
  7.    硬件:DHT22
  8. 3、功能:PM2.5(单位ug/m3)显示
  9.    硬件:ZPH11
  10. 屏幕:12864B(自带字库)
  11. 说明:
  12.     大家好,我叫小木(^_^)·
  13. *************************************/
  14. #include<reg51.h>
  15. #include<lcd12864.h>
  16. #include<ds1302.h>

  17. void showtime();
  18. void showweek();
  19. void showweek1();
  20. void showTH();
  21. void showPM();
  22. void Usartinit();
  23. unsigned char Read_SensorData(void);
  24. unsigned char Read_Sensor(void);
  25. void Delay_N10us(unsigned char t);
  26. void Delay_N1ms(unsigned int t);
  27. void form1();
  28. void form2();
  29. void data1();
  30. void clearping();
  31. void Hgarden(int x);//空心圆
  32. void Sgarden(int x);//实心圆
  33. void timesetting(int x);


  34. /********************************************
  35. 界面文字显示
  36. ********************************************/
  37. //第1界面显示
  38. unsigned char code CharCode[13]="0123456789.-:";
  39. unsigned char code Temperature[]="温度:";
  40. unsigned char code Tsign[]="℃";
  41. unsigned char code Humidity[]="湿度:";
  42. unsigned char code Hsign[]="RH";
  43. unsigned char code PM1[]="PM2.5:";
  44. unsigned char code TPM1[]="ug/m3";

  45. //第2界面显示
  46. unsigned char code year1[]="年";
  47. unsigned char code mouth1[]="月";
  48. unsigned char code day1[]="日";
  49. unsigned char code hour1[]="时";
  50. unsigned char code minute1[]="分";
  51. unsigned char code second1[]="秒";
  52. unsigned char code week3[]="周";
  53. unsigned char code week1[]="星期";

  54. //清屏
  55. unsigned char code clear[]="                ";

  56. unsigned char code week2_0[]="日";
  57. unsigned char code week2_1[]="一";
  58. unsigned char code week2_2[]="二";
  59. unsigned char code week2_3[]="三";
  60. unsigned char code week2_4[]="四";
  61. unsigned char code week2_5[]="五";
  62. unsigned char code week2_6[]="六";
  63. unsigned int  flagkeyup_down=1;
  64. unsigned int page=1;
  65. unsigned int i,j,m;
  66. unsigned int flag2,flag3;
  67.                                        
  68. uchar UART_Upload[9];
  69. uchar UARTnum[11]={"0123456789."};
  70. uchar UARTdata[5];
  71. uint pm_rateH,pm_rateL,pm_particle,pm_density;//低脉冲率高位。低脉冲率低位, 比率,颗粒,浓度
  72. uchar accord,pm_URV=20,temp_pM; //PM_URV  pm上限值


  73. uchar Sensor_Data[5]={0x00,0x00,0x00,0x00,0x00};
  74. uchar Sensor_AnswerFlag;  //收到起始标志位
  75. uchar Sensor_ErrorFlag;   //读取传感器错误标志
  76. uchar Sensor_Check;                  //校验和
  77. uint Sys_CNT;
  78. uint Tmp;
  79. uint t0;
  80. uchar num;
  81. uchar flag;
  82. sbit Sensor_SDA = P1^7;//温湿度传感器DHT22数据接入

  83. uchar timetest[7];
  84. /*******************************
  85. 定义按键:
  86. modekey  为功能选择按键/确定按键
  87. upkey    为向上选择/增加按键
  88. dowmkey  为向下选择/减少按键
  89. backkey  为返回/取消按键
  90. *******************************/
  91. sbit modekey=P1^6;
  92. sbit upkey  =P1^5;
  93. sbit downkey=P1^4;
  94. sbit backkey=P1^3;

  95. /*****************************
  96. 主函数main
  97. *****************************/
  98. void main()
  99. {
  100.   LCD12864_Init();          //12864屏幕模块的初始化
  101.   Usartinit();                  //串口初始化
  102.   form1();                          //form1为主界面
  103.   while(1)
  104.   {
  105.     data1();                  //加载时钟、DHT22和ZPH01数据
  106.         if(modekey==0)          //判断是否进入调整时间界面
  107.         {
  108.           Delay_N1ms(5);  //消抖处理,防止误判
  109.           if(modekey==0)  //二次判断,确定按下
  110.           {
  111.             for(i=0; i<8; i++)
  112.             {
  113.                   timetest[i]=TIME[i]; //截取当前时间放入第二界面,这样便于调整时钟
  114.                 }
  115.             form2();         //进入时间调整界面
  116.           }
  117.         }
  118.   }                                                                  
  119. }
  120. /*************************************
  121. form1为第一界面
  122. 即为开机时看到的界面,
  123. 为了加快运行数度,减少不必要的重复
  124. 将form1放在main函数主循环的前面
  125. *************************************/
  126. void form1()  
  127. {
  128.   LCD12864_SetWindow(0, 2);             //位置坐标显示
  129.   LCD12864_WriteData(year1[0]);  //年
  130.   LCD12864_WriteData(year1[1]);
  131.   LCD12864_SetWindow(0, 4);             //星期
  132.   for(i=0;i<4;i++)                               
  133.   LCD12864_WriteData(week1[i]);
  134.   LCD12864_SetWindow(1, 1);            //月
  135.   LCD12864_WriteData(mouth1[0]);
  136.   LCD12864_WriteData(mouth1[1]);
  137.   LCD12864_SetWindow(1, 3);           //日
  138.   LCD12864_WriteData(day1[0]);
  139.   LCD12864_WriteData(day1[1]);
  140.   LCD12864_SetWindow(1, 7);           //℃
  141.   for(i=0;i<2;i++)
  142.   LCD12864_WriteData(Tsign[i]);
  143.   LCD12864_SetWindow(2, 7);           //H%
  144.   for(i=0;i<2;i++)
  145.   LCD12864_WriteData(Hsign[i]);
  146.   LCD12864_SetWindow(3, 0);          //pm2.5
  147.   for(i=0;i<6;i++)
  148.   LCD12864_WriteData(PM1[i]);
  149. }
  150. /************************************
  151. form2为第二界面,即为时间调整界面当
  152. 按下第一个建,即确定键的时候,进入
  153. 该界面。
  154. ************************************/
  155. void form2()
  156. {
  157.   clearping(); //清屏函数
  158.   flag2=1;           //界面2标识循环判断,用于退出界面2
  159.   /******************************
  160.   本段代码用于显示时、分、秒、年、
  161.   月、日、周
  162.   ******************************/
  163.   LCD12864_SetWindow(0, 1);
  164.   for(i=0;i<2;i++)
  165.     LCD12864_WriteData(hour1[i]);
  166.   LCD12864_SetWindow(1, 1);                  
  167.   for(i=0;i<2;i++)
  168.     LCD12864_WriteData(minute1[i]);
  169.   LCD12864_SetWindow(2, 1);
  170.   for(i=0;i<2;i++)
  171.     LCD12864_WriteData(second1[i]);
  172.   LCD12864_SetWindow(3, 1);
  173.   for(i=0;i<2;i++)
  174.     LCD12864_WriteData(week3[i]);
  175.   LCD12864_SetWindow(0, 5);
  176.   for(i=0;i<2;i++)
  177.     LCD12864_WriteData(year1[i]);
  178.   LCD12864_SetWindow(1, 5);
  179.   for(i=0;i<2;i++)
  180.     LCD12864_WriteData(mouth1[i]);
  181.   LCD12864_SetWindow(2, 5);
  182.   for(i=0;i<2;i++)
  183.     LCD12864_WriteData(day1[i]);
  184.   LCD12864_SetWindow(3, 5);
  185.   LCD12864_WriteData(0xc8);
  186.   LCD12864_WriteData(0xb7);
  187.   LCD12864_WriteData(0xb6);
  188.   LCD12864_WriteData(0xa8);
  189.   /************************************
  190.   本段代码用于显示进入时间调整界面时的
  191.   当前时间显示
  192.   ************************************/
  193.   LCD12864_SetWindow(0, 6);                        //年
  194.   LCD12864_WriteData(CharCode[timetest[6]/16]);
  195.   LCD12864_WriteData(CharCode[(timetest[6]&0x0f)]);
  196.   showweek1();                                     //星期
  197.   LCD12864_SetWindow(1, 6);
  198.   LCD12864_WriteData(CharCode[timetest[4]/16]);           //月
  199.   LCD12864_WriteData(CharCode[(timetest[4]&0x0f)]);
  200.   LCD12864_SetWindow(2, 6);
  201.   LCD12864_WriteData(CharCode[timetest[3]/16]);           //日
  202.   LCD12864_WriteData(CharCode[(timetest[3]&0x0f)]);
  203.   LCD12864_SetWindow(0, 2);
  204.   LCD12864_WriteData(CharCode[timetest[2]/16]);           //小时
  205.   LCD12864_WriteData(CharCode[(timetest[2]&0x0f)]);
  206.   LCD12864_SetWindow(1, 2);
  207.   LCD12864_WriteData(CharCode[timetest[1]/16]);           //分钟
  208.   LCD12864_WriteData(CharCode[(timetest[1]&0x0f)]);
  209.   LCD12864_SetWindow(2, 2);
  210.   LCD12864_WriteData(CharCode[timetest[0]/16]);           //秒
  211.   LCD12864_WriteData(CharCode[(timetest[0]&0x0f)]);
  212.   /***********************************************
  213.   分隔线
  214.   ***********************************************/
  215.   LCD12864_SetWindow(0, 3);
  216.   LCD12864_WriteData(0xa3);
  217.   LCD12864_WriteData(0xfc);
  218.   LCD12864_SetWindow(1, 3);
  219.   LCD12864_WriteData(0xa3);
  220.   LCD12864_WriteData(0xfc);
  221.   LCD12864_SetWindow(2, 3);
  222.   LCD12864_WriteData(0xa3);
  223.   LCD12864_WriteData(0xfc);
  224.   LCD12864_SetWindow(3, 3);
  225.   LCD12864_WriteData(0xa3);
  226.   LCD12864_WriteData(0xfc);
  227.   //-------------------------------------------
  228.   while(flag2)               //在界面二中进入键盘扫描
  229.   {
  230.     Hgarden(flagkeyup_down);         //显示按键指针,以便知道在什么位置
  231.     /****向上************/
  232.     if(upkey==0)        
  233.         {
  234.           Delay_N1ms(5);
  235.       if(upkey==0)
  236.           {
  237.             flagkeyup_down--;          //按键指针向上运动          
  238.             if(flagkeyup_down<1) //当按键指针运动到最上时不在运动
  239.             {
  240.               flagkeyup_down=8;
  241.             }
  242.             Hgarden(flagkeyup_down);       
  243.             i=0;                 
  244.             while ((i<40) && (upkey==0))//检测按键是否松开,如果按着不动,则400ms后自动跳出向下运行
  245.             {
  246.               Delay_N1ms(10);
  247.               i++;
  248.             }
  249.           }
  250.     }
  251.         /****向下************/
  252.     if(downkey==0)        
  253.         {
  254.           Delay_N1ms(5);
  255.           if(downkey==0)
  256.           {       
  257.             flagkeyup_down++;          //按键指针向下运动       
  258.                 if(flagkeyup_down>8) //当按键指针运动到最下时不在运动
  259.                 {
  260.               flagkeyup_down=1;
  261.                 }
  262.                 Hgarden(flagkeyup_down);
  263.                 i=0;                         
  264.             while ((i<40) && (downkey==0))         //检测按键是否松开
  265.             {
  266.               Delay_N1ms(10);
  267.                   i++;
  268.             }
  269.           }
  270.         }
  271.         /****确定************/
  272.         if(modekey==0)         //按下确定键后,可以进行时间的修改
  273.         {
  274.           Delay_N1ms(10);
  275.           if(modekey==0)
  276.           {       
  277.                 i=0;
  278.             while ((i<40) && (modekey==0))         //检测按键是否松开
  279.             {
  280.               Delay_N1ms(10);
  281.                   i++;
  282.             }
  283.                 if(flagkeyup_down==8)
  284.                 {
  285.                   timetest[0]++;
  286.                   for(j=0; j<8; j++)
  287.                   TIME[j]=timetest[j];
  288.                   Ds1302Init();
  289.                   flag2=0;
  290.                   clearping(); //清屏函数
  291.                   form1();
  292.                 }
  293.                 else
  294.                 {
  295.                 flag3=1;
  296.                 while(flag3)
  297.                 {
  298.                   Sgarden(flagkeyup_down);
  299.           timesetting(flagkeyup_down);
  300.                   if(modekey==0)         //按下确定键后,可以进行时间的修改
  301.               {
  302.                 Delay_N1ms(15);
  303.                 if(modekey==0)
  304.                 {
  305.                            flag3=0;
  306.                    while ((i<40) && (modekey==0))         //检测按键是否松开
  307.                    {
  308.                      Delay_N1ms(10);
  309.                          i++;
  310.                    }
  311.                          }                                                 
  312.               }
  313.             }
  314.           }
  315.           }
  316.         }
  317.         /****返回************/
  318.     if(backkey==0)         //按下确定键后,可以进行时间的修改
  319.         {
  320.           Delay_N1ms(10);
  321.           if(backkey==0)
  322.           {       
  323.                 i=0;
  324.                 flag2=0;
  325.                 clearping(); //清屏函数
  326.                 form1();                 
  327.             while ((i<50) && (backkey==0))         //检测按键是否松开
  328.             {
  329.                   Delay_N1ms(10);
  330.                   i++;
  331.                 }
  332.           }
  333.         }
  334.   }
  335. }

  336. void timesetting(int x)
  337. {
  338.     unsigned int d;
  339.         unsigned int m1,m2,m3,m4,m5,m6,m7;
  340.         /****加************/
  341.     if(upkey==0)        
  342.         {
  343.           Delay_N1ms(5);
  344.       if(upkey==0)
  345.           {
  346.             if(x==1)//小时
  347.                 {
  348.                   timetest[2]++;
  349.                   if((timetest[2]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  350.                   {
  351.                     timetest[2]=timetest[2]+6;
  352.                   }
  353.               if(timetest[2]>0x23)
  354.                      {
  355.                            timetest[2]=0;
  356.                            m1=1;
  357.                          }
  358.                   LCD12864_SetWindow(0, 2);
  359.           LCD12864_WriteData(CharCode[timetest[2]/16]);          
  360.           LCD12864_WriteData(CharCode[(timetest[2]&0x0f)]);
  361.                 }
  362.                 else if(x==2)//分钟
  363.                 {
  364.                   timetest[1]++;
  365.                   if((timetest[1]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  366.                   {
  367.                     timetest[1]=timetest[1]+6;
  368.                   }
  369.               if(timetest[1]>0x59)
  370.                   {
  371.                            timetest[1]=0;
  372.                            m2=1;
  373.                          }
  374.                   LCD12864_SetWindow(1, 2);
  375.           LCD12864_WriteData(CharCode[timetest[1]/16]);          
  376.           LCD12864_WriteData(CharCode[(timetest[1]&0x0f)]);
  377.                 }
  378.                 else if(x==3)//秒
  379.                 {
  380.                   timetest[0]++;
  381.                   if((timetest[0]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  382.                   {
  383.                     timetest[0]=timetest[0]+6;
  384.                   }
  385.               if(timetest[0]>0x59)
  386.                   {
  387.                            timetest[0]=0;
  388.                            m3=1;
  389.                   }
  390.                   LCD12864_SetWindow(2, 2);
  391.           LCD12864_WriteData(CharCode[timetest[0]/16]);          
  392.           LCD12864_WriteData(CharCode[(timetest[0]&0x0f)]);
  393.                 }
  394.                 else if(x==4)//周
  395.                 {
  396.                   timetest[5]++;
  397.               if(timetest[5]>0x6)
  398.                   {
  399.                            timetest[5]=0;
  400.                            m4=1;
  401.                          }
  402.                   showweek1();  
  403.                 }
  404.                 else if(x==5)//年
  405.                 {
  406.                   timetest[6]++;
  407.                   if((timetest[6]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  408.                   {
  409.                     timetest[6]=timetest[6]+6;
  410.                   }
  411.               if(timetest[6]>0x90)
  412.                   {
  413.                            timetest[6]=0;
  414.                            m5=1;
  415.                          }
  416.                   LCD12864_SetWindow(0, 6);                       
  417.           LCD12864_WriteData(CharCode[timetest[6]/16]);
  418.           LCD12864_WriteData(CharCode[(timetest[6]&0x0f)]);
  419.                 }
  420.                 else if(x==6)//月
  421.                 {
  422.                   timetest[4]++;
  423.                   if((timetest[4]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  424.                   {
  425.                     timetest[4]=timetest[4]+6;
  426.                   }
  427.               if(timetest[4]>0x12)
  428.                   {
  429.                            timetest[4]=0x01;
  430.                            m6=1;
  431.                          }
  432.                   LCD12864_SetWindow(1, 6);                        
  433.           LCD12864_WriteData(CharCode[timetest[4]/16]);
  434.           LCD12864_WriteData(CharCode[(timetest[4]&0x0f)]);
  435.                 }
  436.                 else if(x==7)//日
  437.                 {
  438.                  
  439.                   if(timetest[4]==0x01||timetest[4]==0x03||timetest[4]==0x05||timetest[4]==0x07||timetest[4]==0x08||timetest[4]==0x10||timetest[4]==0x12)
  440.                 d=0x31;
  441.                   else if(timetest[4]==0x04||timetest[4]==0x06||timetest[4]==0x09||timetest[4]==0x11)
  442.                     d=0x30;
  443.                   else if((CharCode[timetest[6]/16]*10+CharCode[(timetest[6]&0x0f)])%4==0)
  444.                     d=0x29;
  445.                   else
  446.                     d=0x28;
  447.                   timetest[3]++;
  448.                   if((timetest[3]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  449.                   {
  450.                     timetest[3]=timetest[3]+6;
  451.                   }
  452.                   if(timetest[3]>d)
  453.                   {
  454.                            timetest[3]=0;
  455.                            m7=1;
  456.                          }
  457.                   LCD12864_SetWindow(2, 6);                        
  458.           LCD12864_WriteData(CharCode[timetest[3]/16]);
  459.           LCD12864_WriteData(CharCode[(timetest[3]&0x0f)]);
  460.                 }
  461.                        
  462.             i=0;                 
  463.             while ((i<40) && (upkey==0))//检测按键是否松开,如果按着不动,则400ms后自动跳出向下运行
  464.             {
  465.               Delay_N1ms(10);
  466.               i++;
  467.             }
  468.           }
  469.     }
  470.         /****减************/
  471.     if(downkey==0)        
  472.         {
  473.           Delay_N1ms(5);
  474.           if(downkey==0)
  475.           {       
  476.            if(x==1)//小时
  477.                 {
  478.                   timetest[2]--;
  479.                   if((timetest[2]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  480.                   {
  481.                     timetest[2]=timetest[2]-6;
  482.                   }
  483.                   if(m1==1)
  484.                   timetest[2]=0x23;
  485.               if(timetest[2]==0x00)
  486.                      m1=1;
  487.                   else m1=0;
  488.                   LCD12864_SetWindow(0, 2);
  489.           LCD12864_WriteData(CharCode[timetest[2]/16]);          
  490.           LCD12864_WriteData(CharCode[(timetest[2]&0x0f)]);
  491.                 }
  492.                 else if(x==2)//分钟
  493.                 {
  494.                   timetest[1]--;
  495.                   if((timetest[1]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  496.                   {
  497.                     timetest[1]=timetest[1]-6;
  498.                   }
  499.               if(m2==1)
  500.                   timetest[1]=0x59;
  501.               if(timetest[1]==0x00)
  502.                      m2=1;
  503.                   else m2=0;
  504.                   LCD12864_SetWindow(1, 2);
  505.           LCD12864_WriteData(CharCode[timetest[1]/16]);          
  506.           LCD12864_WriteData(CharCode[(timetest[1]&0x0f)]);
  507.                 }
  508.                 else if(x==3)//秒
  509.                 {
  510.                   timetest[0]--;
  511.                   if((timetest[0]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  512.                   {
  513.                     timetest[0]=timetest[0]-6;
  514.                   }
  515.               if(m3==1)
  516.                   timetest[0]=0x59;
  517.               if(timetest[0]==0x00)
  518.                      m3=1;
  519.                   else m3=0;
  520.                   LCD12864_SetWindow(2, 2);
  521.           LCD12864_WriteData(CharCode[timetest[0]/16]);          
  522.           LCD12864_WriteData(CharCode[(timetest[0]&0x0f)]);
  523.                 }
  524.                 else if(x==4)//周
  525.                 {
  526.                   timetest[5]--;
  527.               if(m4==1)
  528.                   timetest[5]=0x06;
  529.               if(timetest[5]==0x00)
  530.                      m4=1;
  531.                   else m4=0;
  532.                   showweek1();  
  533.                 }
  534.                 else if(x==5)//年
  535.                 {
  536.                   timetest[6]--;
  537.                   if((timetest[6]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  538.                   {
  539.                     timetest[6]=timetest[6]-6;
  540.                   }
  541.               if(m5==1)
  542.                   timetest[6]=0x59;
  543.               if(timetest[6]==0x00)
  544.                      m5=1;
  545.                   else m5=0;
  546.                   LCD12864_SetWindow(0, 6);                       
  547.           LCD12864_WriteData(CharCode[timetest[6]/16]);
  548.           LCD12864_WriteData(CharCode[(timetest[6]&0x0f)]);
  549.                 }
  550.                 else if(x==6)//月
  551.                 {
  552.                   timetest[4]--;
  553.                   if((timetest[4]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  554.                   {
  555.                     timetest[4]=timetest[4]-6;
  556.                   }
  557.               if(m6==1)
  558.                   timetest[4]=0x12;
  559.               if(timetest[4]==0x00)
  560.                      m6=1;
  561.                   else m6=0;
  562.                   LCD12864_SetWindow(1, 6);                        
  563.           LCD12864_WriteData(CharCode[timetest[4]/16]);
  564.           LCD12864_WriteData(CharCode[(timetest[4]&0x0f)]);
  565.                 }
  566.                 else if(x==7)//日
  567.                 {
  568.                  
  569.                   if(timetest[4]==0x01||timetest[4]==0x03||timetest[4]==0x05||timetest[4]==0x07||timetest[4]==0x08||timetest[4]==0x10||timetest[4]==0x12)
  570.                 d=0x31;
  571.                   else if(timetest[4]==0x04||timetest[4]==0x06||timetest[4]==0x09||timetest[4]==0x11)
  572.                     d=0x30;
  573.                   else if((CharCode[timetest[6]/16]*10+CharCode[(timetest[6]&0x0f)])%4==0)
  574.                     d=0x29;
  575.                   else
  576.                     d=0x28;
  577.                   timetest[3]--;
  578.                   if((timetest[3]&0x0f)>9)                                         //换成BCD码,跳过a-e段
  579.                   {
  580.                     timetest[3]=timetest[3]-6;
  581.                   }
  582.                   if(m7==1)
  583.                   timetest[3]=d;
  584.               if(timetest[3]==0x00)
  585.                      m7=1;
  586.                   else m7=0;
  587.                   LCD12864_SetWindow(2, 6);                        
  588.           LCD12864_WriteData(CharCode[timetest[3]/16]);
  589.           LCD12864_WriteData(CharCode[(timetest[3]&0x0f)]);
  590.                 }
  591.                 i=0;                         
  592.             while ((i<40) && (downkey==0))         //检测按键是否松开
  593.             {
  594.               Delay_N1ms(10);
  595.                   i++;
  596.             }
  597.           }
  598.         }


  599. }
  600. void Hgarden(int x)
  601. {
  602.     if(x==1)
  603.         {
  604.         LCD12864_SetWindow(3, 4);
  605.           LCD12864_WriteData(0xa1);       
  606.         LCD12864_WriteData(0xa0);
  607.         LCD12864_SetWindow(0, 0);
  608.         LCD12864_WriteData(0xa1);       
  609.         LCD12864_WriteData(0xf0);       
  610.         LCD12864_SetWindow(1, 0);
  611.           LCD12864_WriteData(0xa1);       
  612.         LCD12864_WriteData(0xa0);
  613.         }
  614.         else if(x==2)
  615.         {
  616.         LCD12864_SetWindow(0, 0);
  617.           LCD12864_WriteData(0xa1);       
  618.         LCD12864_WriteData(0xa0);         
  619.         LCD12864_SetWindow(1, 0);
  620.         LCD12864_WriteData(0xa1);       
  621.         LCD12864_WriteData(0xf0);
  622.     LCD12864_SetWindow(2, 0);
  623.           LCD12864_WriteData(0xa1);       
  624.         LCD12864_WriteData(0xa0);
  625.         }
  626.         else if(x==3)
  627.         {
  628.         LCD12864_SetWindow(1, 0);
  629.           LCD12864_WriteData(0xa1);       
  630.         LCD12864_WriteData(0xa0);         
  631.         LCD12864_SetWindow(2, 0);
  632.         LCD12864_WriteData(0xa1);       
  633.         LCD12864_WriteData(0xf0);
  634.     LCD12864_SetWindow(3, 0);
  635.           LCD12864_WriteData(0xa1);       
  636.         LCD12864_WriteData(0xa0);
  637.         }
  638.         else if(x==4)
  639.         {
  640.         LCD12864_SetWindow(2, 0);
  641.           LCD12864_WriteData(0xa1);       
  642.         LCD12864_WriteData(0xa0);         
  643.         LCD12864_SetWindow(3, 0);
  644.         LCD12864_WriteData(0xa1);       
  645.         LCD12864_WriteData(0xf0);
  646.     LCD12864_SetWindow(0, 4);
  647.           LCD12864_WriteData(0xa1);       
  648.         LCD12864_WriteData(0xa0);
  649.         }
  650.         else if(x==5)
  651.         {
  652.         LCD12864_SetWindow(3, 0);
  653.           LCD12864_WriteData(0xa1);       
  654.         LCD12864_WriteData(0xa0);         
  655.         LCD12864_SetWindow(0, 4);
  656.         LCD12864_WriteData(0xa1);       
  657.         LCD12864_WriteData(0xf0);
  658.     LCD12864_SetWindow(1, 4);
  659.           LCD12864_WriteData(0xa1);       
  660.         LCD12864_WriteData(0xa0);
  661.         }
  662.         else if(x==6)
  663.         {
  664.         LCD12864_SetWindow(0, 4);
  665.           LCD12864_WriteData(0xa1);       
  666.         LCD12864_WriteData(0xa0);         
  667.         LCD12864_SetWindow(1, 4);
  668.         LCD12864_WriteData(0xa1);       
  669.         LCD12864_WriteData(0xf0);
  670.     LCD12864_SetWindow(2, 4);
  671.           LCD12864_WriteData(0xa1);       
  672.         LCD12864_WriteData(0xa0);
  673.         }
  674.         else if(x==7)
  675.         {
  676.         LCD12864_SetWindow(1, 4);
  677.           LCD12864_WriteData(0xa1);       
  678.         LCD12864_WriteData(0xa0);         
  679.         LCD12864_SetWindow(2, 4);
  680.         LCD12864_WriteData(0xa1);       
  681.         LCD12864_WriteData(0xf0);
  682.     LCD12864_SetWindow(3, 4);
  683.           LCD12864_WriteData(0xa1);       
  684.         LCD12864_WriteData(0xa0);
  685.         }
  686.         else if(x==8)
  687.         {
  688.         LCD12864_SetWindow(0, 0);
  689.           LCD12864_WriteData(0xa1);       
  690.         LCD12864_WriteData(0xa0);
  691.         LCD12864_SetWindow(2, 4);
  692.           LCD12864_WriteData(0xa1);       
  693.         LCD12864_WriteData(0xa0);         
  694.         LCD12864_SetWindow(3, 4);
  695.         LCD12864_WriteData(0xa1);       
  696.         LCD12864_WriteData(0xf0);
  697.         }               
  698. }

  699. void Sgarden(int x)
  700. {
  701.     if(x==1)
  702.         {
  703.         LCD12864_SetWindow(0, 0);
  704.         LCD12864_WriteData(0xa1);       
  705.         LCD12864_WriteData(0xf1);       
  706.         }
  707.         else if(x==2)
  708.         {         
  709.         LCD12864_SetWindow(1, 0);
  710.         LCD12864_WriteData(0xa1);       
  711.         LCD12864_WriteData(0xf1);
  712.         }
  713.         else if(x==3)
  714.         {         
  715.         LCD12864_SetWindow(2, 0);
  716.         LCD12864_WriteData(0xa1);       
  717.         LCD12864_WriteData(0xf1);
  718.         }
  719.         else if(x==4)
  720.         {         
  721.         LCD12864_SetWindow(3, 0);
  722.         LCD12864_WriteData(0xa1);       
  723.         LCD12864_WriteData(0xf1);
  724.         }
  725.         else if(x==5)
  726.         {         
  727.         LCD12864_SetWindow(0, 4);
  728.         LCD12864_WriteData(0xa1);       
  729.         LCD12864_WriteData(0xf1);
  730.         }
  731.         else if(x==6)
  732.         {         
  733.         LCD12864_SetWindow(1, 4);
  734.         LCD12864_WriteData(0xa1);       
  735.         LCD12864_WriteData(0xf1);
  736.         }
  737.         else if(x==7)
  738.         {         
  739.         LCD12864_SetWindow(2, 4);
  740.         LCD12864_WriteData(0xa1);       
  741.         LCD12864_WriteData(0xf1);
  742.         }
  743.         else if(x==8)
  744.         {
  745.         LCD12864_SetWindow(3, 4);
  746.         LCD12864_WriteData(0xa1);       
  747.         LCD12864_WriteData(0xf1);
  748.         }
  749. }
  750. void data1()
  751. {
  752.   showtime();
  753.   showTH();
  754.   showPM();
  755. }



  756. void showtime()
  757. {
  758.   Ds1302ReadTime();
  759.   LCD12864_SetWindow(0, 0);
  760.   LCD12864_WriteData(CharCode[2]);
  761.   LCD12864_WriteData(CharCode[0]);                                   //年
  762.   LCD12864_WriteData(CharCode[TIME[6]/16]);
  763.   LCD12864_WriteData(CharCode[(TIME[6]&0x0f)]);
  764.   
  765.   showweek();//星期
  766.   LCD12864_SetWindow(1, 0);
  767.   LCD12864_WriteData(CharCode[TIME[4]/16]);           //月
  768.   LCD12864_WriteData(CharCode[(TIME[4]&0x0f)]);
  769.   LCD12864_SetWindow(1, 2);
  770.   LCD12864_WriteData(CharCode[TIME[3]/16]);           //日
  771.   LCD12864_WriteData(CharCode[(TIME[3]&0x0f)]);
  772.   LCD12864_SetWindow(2, 0);
  773.   LCD12864_WriteData(CharCode[TIME[2]/16]);          //小时
  774.   LCD12864_WriteData(CharCode[(TIME[2]&0x0f)]);
  775.   LCD12864_WriteData(CharCode[12]);
  776.   LCD12864_WriteData(CharCode[TIME[1]/16]);         //分钟
  777.   LCD12864_WriteData(CharCode[(TIME[1]&0x0f)]);
  778.   LCD12864_WriteData(CharCode[12]);
  779.   LCD12864_WriteData(CharCode[TIME[0]/16]);         //秒
  780.   LCD12864_WriteData(CharCode[(TIME[0]&0x0f)]);
  781. }

  782. void showweek()         //周
  783. {
  784.   uint wek;         
  785.   wek=CharCode[(TIME[5]&0x0f)];
  786.   LCD12864_SetWindow(0, 6);
  787.   if(wek=='0')
  788.    for(i=0;i<2;i++)
  789.    LCD12864_WriteData(week2_0[i]);
  790.   else if(wek=='1')
  791.    for(i=0;i<2;i++)
  792.    LCD12864_WriteData(week2_1[i]);       
  793.   else if(wek=='2')
  794.    for(i=0;i<2;i++)
  795.    LCD12864_WriteData(week2_2[i]);
  796.   else if(wek=='3')
  797.    {
  798.     LCD12864_WriteData(0xc8);
  799.     LCD12864_WriteData(0xfd);
  800.     }
  801.   else if(wek=='4')
  802.    for(i=0;i<2;i++)
  803.    LCD12864_WriteData(week2_4[i]);
  804.   else if(wek=='5')
  805.    for(i=0;i<2;i++)
  806.    LCD12864_WriteData(week2_5[i]);
  807.   else if(wek=='6')
  808.    for(i=0;i<2;i++)
  809.    LCD12864_WriteData(week2_6[i]);
  810.   else
  811.    ;                            
  812. }

  813. void showweek1()         //周
  814. {
  815.   uint wek;         
  816.   wek=CharCode[(timetest[5]&0x0f)];
  817.   LCD12864_SetWindow(3, 2);
  818.   if(wek=='0')
  819.    for(i=0;i<2;i++)
  820.    LCD12864_WriteData(week2_0[i]);
  821.   else if(wek=='1')
  822.    for(i=0;i<2;i++)
  823.    LCD12864_WriteData(week2_1[i]);       
  824.   else if(wek=='2')
  825.    for(i=0;i<2;i++)
  826.    LCD12864_WriteData(week2_2[i]);
  827.   else if(wek=='3')
  828.    {
  829.     LCD12864_WriteData(0xc8);
  830.     LCD12864_WriteData(0xfd);
  831.     }
  832.   else if(wek=='4')
  833.    for(i=0;i<2;i++)
  834.    LCD12864_WriteData(week2_4[i]);
  835.   else if(wek=='5')
  836.    for(i=0;i<2;i++)
  837.    LCD12864_WriteData(week2_5[i]);
  838.   else if(wek=='6')
  839.    for(i=0;i<2;i++)
  840.    LCD12864_WriteData(week2_6[i]);
  841.   else
  842.    ;                            
  843. }

  844. void showTH()
  845. {
  846.    Read_Sensor();
  847.    Tmp = Sensor_Data[2]*256+Sensor_Data[3];
  848.    LCD12864_SetWindow(1, 5);
  849.    LCD12864_WriteData(CharCode[Tmp/100%10]);
  850.    LCD12864_WriteData(CharCode[Tmp/10%10]);
  851.    LCD12864_WriteData(CharCode[10]);
  852.    LCD12864_WriteData(CharCode[Tmp%10]);

  853.    Tmp = Sensor_Data[0]*256+Sensor_Data[1];
  854.    LCD12864_SetWindow(2, 5);
  855.    LCD12864_WriteData(CharCode[Tmp/100%10]);
  856.    LCD12864_WriteData(CharCode[Tmp/10%10]);
  857.    LCD12864_WriteData(CharCode[10]);
  858.    LCD12864_WriteData(CharCode[Tmp%10]);

  859. }

  860. unsigned char Read_SensorData(void)
  861.   {
  862.         unsigned char i,cnt;
  863.         unsigned char buffer,tmp;
  864.         buffer = 0;
  865.         for(i=0;i<8;i++)
  866.         {
  867.                 cnt=0;
  868.                 while(!Sensor_SDA)        //检测上次低电平是否结束
  869.                 {
  870.                   if(++cnt >= 300)
  871.                    {
  872.                           break;
  873.                    }
  874.                 }
  875.                 //延时Min=26us Max50us 跳过数据"0" 的高电平
  876.                 Delay_N10us(4);         //延时30us   
  877.                
  878.                 //判断传感器发送数据位
  879.                 tmp =0;
  880.                 if(Sensor_SDA)         
  881.                 {
  882.                   tmp = 1;
  883.                 }  
  884.                 cnt =0;
  885.                 while(Sensor_SDA)                //等待高电平 结束
  886.                 {
  887.                            if(++cnt >= 200)
  888.                         {
  889.                           break;
  890.                         }
  891.                 }
  892.                 buffer <<=1;
  893.                 buffer |= tmp;       
  894.         }
  895.         return buffer;
  896.   }

  897. /********************************************\
  898. |* 功能: 读传感器                              *|
  899. \********************************************/
  900. unsigned char Read_Sensor(void)
  901.   {
  902.         unsigned char i;
  903.         //主机拉低(Min=800US Max=20Ms)
  904.         Sensor_SDA = 0;
  905.         Delay_N1ms(10);  //延时2Ms
  906.           
  907.         //释放总线 延时(Min=30us Max=50us)
  908.         Sensor_SDA = 1;        
  909.         Delay_N10us(4);//延时30us
  910.         //主机设为输入 判断传感器响应信号
  911.         Sensor_SDA = 1;
  912.                
  913.         Sensor_AnswerFlag = 0;  // 传感器响应标志         

  914.         //判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行          
  915.         if(Sensor_SDA ==0)
  916.         {
  917.            Sensor_AnswerFlag = 1;//收到起始信号
  918.            Sys_CNT = 0;
  919.            //判断从机是否发出 80us 的低电平响应信号是否结束         
  920.            while((!Sensor_SDA))
  921.            {
  922.              if(++Sys_CNT>300) //防止进入死循环
  923.                  {
  924.                    Sensor_ErrorFlag = 1;
  925.                    return 0;
  926.                   }
  927.             }
  928.             Sys_CNT = 0;
  929.             //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
  930.             while((Sensor_SDA))
  931.             {
  932.                if(++Sys_CNT>300) //防止进入死循环
  933.                    {
  934.                      Sensor_ErrorFlag = 1;
  935.                      return 0;
  936.                    }
  937.             }                  
  938.             // 数据接收        传感器共发送40位数据
  939.             // 即5个字节 高位先送  5个字节分别为湿度高位 湿度低位 温度高位 温度低位 校验和
  940.             // 校验和为:湿度高位+湿度低位+温度高位+温度低位
  941.             for(i=0;i<5;i++)
  942.             {
  943.               Sensor_Data[i] = Read_SensorData();
  944.             }
  945.           }
  946.           else
  947.           {
  948.             Sensor_AnswerFlag = 0;          // 未收到传感器响应       
  949.           }
  950. ……………………

  951. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
51单片机室内环境检测仪.zip (9.74 MB, 下载次数: 244)


评分

参与人数 3黑币 +65 收起 理由
kljie1000 + 10
zqy181818 + 5 赞一个!
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:334964 发表于 2018-5-21 16:07 | 只看该作者
谢谢楼主~~~~~~~~~~~~
回复

使用道具 举报

板凳
ID:161797 发表于 2018-11-10 11:45 | 只看该作者
不错,好东西.谢谢.
回复

使用道具 举报

地板
ID:364389 发表于 2018-11-24 21:13 | 只看该作者
下载的里面没有原理图也没有程序,白浪费币了
回复

使用道具 举报

5#
ID:364389 发表于 2018-11-24 21:14 | 只看该作者
下载的里面没有pcb也没有程序
回复

使用道具 举报

6#
ID:364389 发表于 2018-11-24 21:21 | 只看该作者
里面没有原理图也没有程序,pcb是错的
回复

使用道具 举报

7#
ID:364389 发表于 2018-11-24 21:25 | 只看该作者
下载的没有原理图。。。。
回复

使用道具 举报

8#
ID:624914 发表于 2019-10-16 18:07 | 只看该作者
有没有仿真图
回复

使用道具 举报

9#
ID:599674 发表于 2019-10-25 14:22 来自手机 | 只看该作者
感谢楼主
回复

使用道具 举报

10#
ID:441206 发表于 2020-2-14 16:00 | 只看该作者
这个里面有仿真吗
回复

使用道具 举报

11#
ID:502881 发表于 2020-3-9 20:37 | 只看该作者
好好好,正好需要,非常感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表