下面是楼主的主程序:
- #include <math.h>
- #include <stdio.h>
- #include <INTRINS.H>
- #include "reg52.h"
- #include "lcd.h"
- #include "ds18.h"
- #include "serc.h"
- #include "yl38.h"
- #include "bhmc.h"
- #ifndef uchar
- #define uchar unsigned char
- #endif
- #ifndef uint
- #define uint unsigned int
- #endif
- sbit ssm=P3^3; //测试数据发送引脚,每隔20s反一次
- sbit en=P3^2; //wifi模块初始化引脚
- uchar as[2][21];
- uchar dssz[5]; //DS18缓存
- uchar gysz[6]; //GY30缓存
- uchar ylsz[5]; //YL38缓存
- uchar code ssd[8]={0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00};//℃
- uchar code lux[8]={0x00,0x10,0x10,0x10,0x15,0x12,0x15,0x00};//lux
- uchar timee; //时间旗子
- static uchar zt; //步进电机旗子
- void Delay1ms(uint c) //误差 0us
- {
- uchar a,b;
- for (;c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
- }
- void Delay5us() //误差 0ns
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- void main()
- {
- uchar j,k; //检索
- LcdInit();
- YL38Init();
- GY30Init();
- SercInit(); //初始化串口
- ssm=0;
- DS18done(dssz); //操作DS18,数据存在dssz数组中
- YL38done(ylsz,50,70); //操作YL38,数据存在ylsz数组中,后面第一个为反馈下限,小于其则启动反馈P2^4,第二个为反馈上限,大于其则关闭反馈
- GY30done(gysz,25000); //操作GY30,数据存在gysz数组中,后面跟着反馈限制值,超过则启动反馈引脚P1^2,同时输出‘T’
- en=0;
- DS18done(dssz);
- YL38done(ylsz,50,70);
- GY30done(gysz,25000);
- Delay1ms(500);
- en=1; //重新启动esp
- DS18done(dssz);
- YL38done(ylsz,50,70);
- GY30done(gysz,25000);
- DIYshow(1,ssd,8,1); //摄氏度℃
- DIYshow(2,lux,8,2); //勒克斯lux
- DisplayOneChar(1,1,'T'); //温度
- DisplayOneChar(2,1,':');
- DisplayOneChar(10,1,'H'); //湿度
- DisplayOneChar(11,1,':');
- DisplayOneChar(16,1,'%'); //%
- DisplayOneChar(1,2,'L'); //光照
- DisplayOneChar(2,2,':');
- DisplayOneChar(10,2,'F'); //反馈
- DisplayOneChar(11,2,':');
- DisplayOneChar(12,2,'G'); //光照反馈前缀
- DisplayOneChar(14,2,'S'); //湿度反馈前置
- DisplayOneChar(3,1,dssz[0]);
- DisplayOneChar(4,1,dssz[1]);
- DisplayOneChar(5,1,dssz[2]);
- DisplayOneChar(6,1,dssz[3]);
- DisplayOneChar(7,1,dssz[4]);
- DisplayOneChar(12,1,ylsz[0]);
- DisplayOneChar(13,1,ylsz[1]);
- DisplayOneChar(14,1,ylsz[2]);
- DisplayOneChar(15,1,ylsz[3]);
- DisplayOneChar(15,2,ylsz[4]); //湿度反馈
- DisplayOneChar(3,2,gysz[0]);
- DisplayOneChar(4,2,gysz[1]);
- DisplayOneChar(5,2,gysz[2]);
- DisplayOneChar(6,2,gysz[3]);
- DisplayOneChar(7,2,gysz[4]);
- DisplayOneChar(13,2,gysz[5]); //光照反馈
- DS18done(dssz);
- YL38done(ylsz,50,70);
- GY30done(gysz,50000);
- Delay1ms(10000);
- SendString("AT+CIPMODE=1\r\n");
- Delay1ms(1000) ;
- SendString("AT+CIPSTART=\"TCP\",\"tcp.lewei50.com\",9960\r\n");
- Delay1ms(1000);
- SendString("AT+CIPSEND\r\n");
- Delay1ms(1000);
- while(1)
- {
- DS18done(dssz);
- GY30done(gysz,25000);
- YL38done(ylsz,50,70);
- DisplayOneChar(3,1,dssz[0]);
- DisplayOneChar(4,1,dssz[1]);
- DisplayOneChar(5,1,dssz[2]);
- DisplayOneChar(6,1,dssz[3]);
- DisplayOneChar(7,1,dssz[4]);
- DisplayOneChar(12,1,ylsz[0]);
- DisplayOneChar(13,1,ylsz[1]);
- DisplayOneChar(14,1,ylsz[2]);
- DisplayOneChar(15,1,ylsz[3]);
- DisplayOneChar(15,2,ylsz[4]);
- DisplayOneChar(3,2,gysz[0]);
- DisplayOneChar(4,2,gysz[1]);
- DisplayOneChar(5,2,gysz[2]);
- DisplayOneChar(6,2,gysz[3]);
- DisplayOneChar(7,2,gysz[4]);
- DisplayOneChar(13,2,gysz[5]);
- for(j=0;j<2;j++) //检测是否有手动浇水命令发来单片机端,有就判断能否启动YLFB引脚
- {
- for(k=0;k<21;k++)
- {
- if(as[j][k]=='r'&& as[j][k-1]=='e' || as[j][k]=='e'&& as[j][k-1]=='t')
- {
- SendString("{\"method\":\"response\",\"result\":{\"successful\":true,\"message\":\"Done\"}}&^!\r\n");
- if(ylsz[0]=='5' || ylsz[0]=='6') //检测当前湿度能否开启水泵
- {
- YLFB=1;
- }
- }
- }
- }
- if(zt==0 && gysz[5]=='T' && ylsz[4]=='F')
- {
- FG();
- zt=1;
- }
- if(zt==1 && gysz[5]=='F' && ylsz[4]=='F')
- {
- RG();
- zt=0;
- }
- if(timee==1) //每隔20秒执行一次
- {
- SendString("{\"method\": \"update\",\"gatewayNo\": \"01\",\"userkey\": \"f40a5569285c418c9744d8d627a168ed\"}&^!\r\n");
- SendString("{\"method\":\"upload\",\"data\":[{\"Name\":\"t1\",\"Value\":\"");
- SendoString(dssz,5);
- SendString("\"},{\"Name\":\"x1\",\"Value\":\"");
- SendoString(gysz,5);
- SendString("\"},{\"Name\":\"h1\",\"Value\":\"");
- SendoString(ylsz,4);
- SendString("\"}]}&^!\r\n");
- timee=0;
- }
- }
- }
- void timer() interrupt 1
- {
- static uchar time;
- TH0=0X04; //50ms初值
- TL0=0X00;
- time++;
- if(time==250)
- {
- time=0;
- timee=1;
- ssm=~ssm;
- }
- }
- void ser()interrupt 4 //串口中断
- {
- static uchar io;
- static uchar jj;
- TI=0;
- RI=0;
- as[jj][io]=SBUF;
- io++;
- if(io==21)
- {
- io=0;
- switch(jj)
- {
- case 0:jj=1;break;
- case 1:jj=0;break;
- }
- }
- }
复制代码 |