|
50黑幣
為什么這里程序沒問題,總是數(shù)據(jù)上不去 用的是mqtt協(xié)議,一直搞不懂- #include <REG52.H>
- #include "LCD1602.h"
- #include "intrins.h"
- #include "esp8266.h"
- #include "timer0.h"
- #include "usart.h"
- //typedef unsigned char uchar;
- //typedef unsigned int uint;
-
- //定義變量
- sbit Data=P3^6;
- uchar sdata; //存放整數(shù)溫度
- uchar sdatahum; //存放整數(shù)濕度
- unsigned char rec_dat[13];//用于保存接收到的數(shù)據(jù)組
- void DHT11_delay_us(unsigned char n)
- {
- while(--n);
- }
- void DHT11_delay_ms(unsigned int z)
- {
- unsigned int i,j;
- for(i=z;i>0;i--)
- for(j=110;j>0;j--);
- }
- void DHT11_start()
- {
- Data=1;
- DHT11_delay_us(2);
- Data=0;
- DHT11_delay_ms(20); //延時18ms以上
- Data=1;
- DHT11_delay_us(30);
- }
- unsigned char DHT11_rec_byte() //接收一個字節(jié)
- {
- unsigned char i,dat=0;
- for(i=0;i<8;i++) //從高到低依次接收8位數(shù)據(jù)
- {
- while(!Data); //等待50us低電平過去
- DHT11_delay_us(8); //延時60us,如果還為高則數(shù)據(jù)為1,否則為0
- dat<<=1; //移位使正確接收8位數(shù)據(jù),數(shù)據(jù)為0時直接移位
- if(Data==1) //數(shù)據(jù)為1時,使dat加1來接收數(shù)據(jù)1
- dat+=1;
- while(Data); //等待數(shù)據(jù)線拉低
- }
- return dat;
- }
- void DHT11_receive() //接收40位的數(shù)據(jù)
- {
- unsigned char R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
- DHT11_start();
- if(Data==0)
- {
- while(Data==0); //等待拉高
- DHT11_delay_us(40); //拉高后延時80us
- R_H=DHT11_rec_byte(); //接收濕度高八位
- R_L=DHT11_rec_byte(); //接收濕度低八位
- T_H=DHT11_rec_byte(); //接收溫度高八位
- T_L=DHT11_rec_byte(); //接收溫度低八位
- revise=DHT11_rec_byte(); //接收校正位
- DHT11_delay_ms(25); //結束
- if((R_H+R_L+T_H+T_L)==revise) //校正
- {
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- }
-
-
- /*數(shù)據(jù)處理,方便顯示*/
- rec_dat[0]=RH/10+'0';
- rec_dat[1]=(RH%10)+'0';
- rec_dat[7]=(TH/10)+'0';
- rec_dat[8]=(TH%10)+'0';
- sdata=rec_dat[7]*10+rec_dat[8]*1;
- sdatahum=rec_dat[0]*10+rec_dat[1]*1;
- }
- }
- //void Timer0(void) //interrupt 1
- //{
- // static uint count = 0;
- // TL0 = (65536 - 20000)%256; //裝初值,低8位初值是1000即1ms
- // TH0 = (65536 - 20000)/256; //高8位
- // count++;
- // if(count >= 75) //20*50
- // {
- // count = 0;
- // ESP_Send_Data("tem",sdata);
- // ESP_Send_Data("hum",sdatahum);
- // }
- //
- //}
- void main()
- {
- LCD_Init();
- Timer0_Init();
- UART_init();
- ESP_Init();
- while(1)
- {
- // Timer0();
- DHT11_receive();
- LCD_ShowChar(1,2,rec_dat[0]);
- LCD_ShowChar(1,3,rec_dat[1]);
- LCD_ShowString(1,4,"%RH");
- LCD_ShowChar(1,9,rec_dat[7]);
- LCD_ShowChar(1,10,rec_dat[8]);
- LCD_ShowString(1,11,"^CLH");
- ESP_Send_Data("temp",sdata);
- ESP_Send_Data("hum",sdatahum);
- DHT11_delay_ms(1000);
-
- }
- }
- #include <REG52.H>
- #include "LCD1602.h"
- #include "intrins.h"
- #include "esp8266.h"
- #include "timer0.h"
- #include "usart.h"
- //typedef unsigned char uchar;
- //typedef unsigned int uint;
-
- //定義變量
- sbit Data=P3^6;
- uchar sdata; //存放整數(shù)溫度
- uchar sdatahum; //存放整數(shù)濕度
- unsigned char rec_dat[13];//用于保存接收到的數(shù)據(jù)組
- void DHT11_delay_us(unsigned char n)
- {
- while(--n);
- }
- void DHT11_delay_ms(unsigned int z)
- {
- unsigned int i,j;
- for(i=z;i>0;i--)
- for(j=110;j>0;j--);
- }
- void DHT11_start()
- {
- Data=1;
- DHT11_delay_us(2);
- Data=0;
- DHT11_delay_ms(20); //延時18ms以上
- Data=1;
- DHT11_delay_us(30);
- }
- unsigned char DHT11_rec_byte() //接收一個字節(jié)
- {
- unsigned char i,dat=0;
- for(i=0;i<8;i++) //從高到低依次接收8位數(shù)據(jù)
- {
- while(!Data); //等待50us低電平過去
- DHT11_delay_us(8); //延時60us,如果還為高則數(shù)據(jù)為1,否則為0
- dat<<=1; //移位使正確接收8位數(shù)據(jù),數(shù)據(jù)為0時直接移位
- if(Data==1) //數(shù)據(jù)為1時,使dat加1來接收數(shù)據(jù)1
- dat+=1;
- while(Data); //等待數(shù)據(jù)線拉低
- }
- return dat;
- }
- void DHT11_receive() //接收40位的數(shù)據(jù)
- {
- unsigned char R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
- DHT11_start();
- if(Data==0)
- {
- while(Data==0); //等待拉高
- DHT11_delay_us(40); //拉高后延時80us
- R_H=DHT11_rec_byte(); //接收濕度高八位
- R_L=DHT11_rec_byte(); //接收濕度低八位
- T_H=DHT11_rec_byte(); //接收溫度高八位
- T_L=DHT11_rec_byte(); //接收溫度低八位
- revise=DHT11_rec_byte(); //接收校正位
- DHT11_delay_ms(25); //結束
- if((R_H+R_L+T_H+T_L)==revise) //校正
- {
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- }
-
-
- /*數(shù)據(jù)處理,方便顯示*/
- rec_dat[0]=RH/10+'0';
- rec_dat[1]=(RH%10)+'0';
- rec_dat[7]=(TH/10)+'0';
- rec_dat[8]=(TH%10)+'0';
- sdata=rec_dat[7]*10+rec_dat[8]*1;
- sdatahum=rec_dat[0]*10+rec_dat[1]*1;
- }
- }
- //void Timer0(void) //interrupt 1
- //{
- // static uint count = 0;
- // TL0 = (65536 - 20000)%256; //裝初值,低8位初值是1000即1ms
- // TH0 = (65536 - 20000)/256; //高8位
- // count++;
- // if(count >= 75) //20*50
- // {
- // count = 0;
- // ESP_Send_Data("tem",sdata);
- // ESP_Send_Data("hum",sdatahum);
- // }
- //
- //}
- void main()
- {
- LCD_Init();
- Timer0_Init();
- UART_init();
- ESP_Init();
- while(1)
- {
- // Timer0();
- DHT11_receive();
- LCD_ShowChar(1,2,rec_dat[0]);
- LCD_ShowChar(1,3,rec_dat[1]);
- LCD_ShowString(1,4,"%RH");
- LCD_ShowChar(1,9,rec_dat[7]);
- LCD_ShowChar(1,10,rec_dat[8]);
- LCD_ShowString(1,11,"^CLH");
- ESP_Send_Data("temp",sdata);
- ESP_Send_Data("hum",sdatahum);
- DHT11_delay_ms(1000);
-
- }
- }
復制代碼
|
-
-
比賽 - 副本.rar
2022-4-2 21:25 上傳
點擊文件名下載附件
61.96 KB, 下載次數(shù): 8
|