51单片机通过DHT11温度传感器读取温度(4)
作者:互联网
实现通过LCD1602显示DHT11采集的温湿度数据
1 #include <REGX52.H> 2 #include "intrins.h" 3 sbit Datas = P2^0; 4 sbit RS = P2^1; 5 sbit RW = P2^2; 6 sbit E = P2^3; 7 sbit fan = P2^4; 8 char datas[5]; 9 char Tem[8]; 10 char hum[8]; 11 sfr AUXR = 0X8E; 12 #define data_DB P0 13 void Delay1000ms() //@11.0592MHz 14 { 15 unsigned char i, j, k; 16 17 _nop_(); 18 i = 8; 19 j = 1; 20 k = 243; 21 do 22 { 23 do 24 { 25 while (--k); 26 } while (--j); 27 } while (--i); 28 } 29 void Delay35ms() //@11.0592MHz 30 { 31 unsigned char i, j; 32 33 i = 63; 34 j = 190; 35 do 36 { 37 while (--j); 38 } while (--i); 39 } 40 void Delay45us() //@11.0592MHz 41 { 42 unsigned char i; 43 44 i = 18; 45 while (--i); 46 } 47 void Delay15ms() //@11.0592MHz 48 { 49 unsigned char i, j; 50 51 i = 27; 52 j = 226; 53 do 54 { 55 while (--j); 56 } while (--i); 57 } 58 void Delay5ms() //@11.0592MHz 59 { 60 unsigned char i, j; 61 62 i = 9; 63 j = 244; 64 do 65 { 66 while (--j); 67 } while (--i); 68 } 69 70 71 /*DHT11启动信号*/ 72 void dht11_start() 73 { 74 Datas = 1; 75 Datas = 0; 76 Delay35ms(); 77 Datas = 1; 78 while(Datas); //根据时序图可知,当datas由高电平运行到低电平时执行下方的程序,所以写出(while(datas))。 79 while(!Datas); 80 while(Datas); 81 } 82 /*串口初始化*/ 83 void UartInit() 84 { 85 SCON = 0x40; 86 TMOD &= 0x0F; 87 TMOD |= 0x20; 88 TL1 = 0xFD; 89 TH1 = 0xFD; 90 ET1 = 0; 91 TR1 = 1; 92 } 93 /*串口数据发送:字节发送*/ 94 void sendbyte(data_msg) 95 { 96 SBUF = data_msg; //将形式参数data_msg传送到SBUF 97 while(TI == 0); //当一个字节传输完毕之后,TI被自动置位 98 TI = 0; //中断请求结束之后,软件自动复位为0 99 } 100 /*字符串数据发送*/ 101 void sendstring(char* string) 102 { 103 while( *string != '\0') 104 { 105 sendbyte(*string); 106 string++; 107 } 108 } 109 /*检测LCD是否忙碌*/ 110 void check_busy() 111 { 112 char tep = 0x80; 113 char data_DB = 0X80; 114 while(tep & 0x80) 115 { 116 RS = 0; 117 RW = 1; 118 E = 0; 119 _nop_(); 120 tep = data_DB; 121 E = 1; 122 _nop_(); 123 E = 0; 124 _nop_(); 125 } 126 } 127 /*LCD写入数据函数*/ 128 void write_data(char datashow) 129 { 130 check_busy(); 131 RS = 1; 132 RW = 0; 133 E = 0; 134 _nop_(); 135 data_DB = datashow; 136 E = 1; 137 _nop_(); 138 E = 0; 139 _nop_(); 140 } 141 /*LCD写入指令函数*/ 142 void write_cmd(char cmd) 143 { 144 check_busy(); 145 RS = 0; 146 RW = 0; 147 E = 0; 148 _nop_(); 149 data_DB = cmd; 150 E = 1; 151 _nop_(); 152 E = 0; 153 _nop_(); 154 } 155 /*LCD1602初始化函数*/ 156 void LCD_1602_begin() 157 { 158 Delay15ms(); 159 write_cmd(0x38); 160 Delay5ms(); 161 write_cmd(0x38); 162 write_cmd(0x08); 163 write_cmd(0x01); 164 write_cmd(0x06); 165 write_cmd(0x0c); 166 } 167 /*读取DHT11数据*/ 168 void read_dht11datas() 169 { 170 char i;//定义轮数 171 char j;//每轮数据读取的位数 172 char temp; 173 char flag; 174 dht11_start(); 175 for(i=0;i<5;i++) 176 { 177 for(j=0;j<8;j++) 178 { 179 while(!Datas); 180 Delay45us(); 181 if(Datas == 1) 182 { 183 flag = 1; 184 while(Datas); 185 } 186 else 187 { 188 flag = 0; 189 } 190 temp = temp << 1; 191 temp |= temp; 192 } 193 datas[i] = temp; 194 } 195 } 196 /*LCD1602显示方式设定函数*/ 197 void LCD1602_datashow_line(char hang,char lie,char *string) 198 { 199 switch(hang) 200 { 201 case 1: 202 203 write_cmd(0x80+lie); 204 while(*string) 205 { 206 write_data(*string); 207 string++; 208 } 209 210 break; 211 case 2: 212 write_cmd(0x80+0x40+lie); 213 while(*string) 214 { 215 write_data(*string); 216 string++; 217 } 218 break; 219 } 220 } 221 /*数据组合函数 222 为了保障ascll码不会混乱,因此在每个数据高位都会加0x30*/ 223 void data_build() 224 { 225 hum[0] = 'H'; 226 hum[1] = datas[0]/10 + 0x30; 227 hum[2] = datas[0]%10 + 0x30; 228 hum[3] = '.'; 229 hum[4] = datas[1]/10 + 0x30; 230 hum[5] = datas[1]%10 + 0x30; 231 hum[6] = '%'; 232 hum[7] = '\0'; 233 234 Tem[0] = 'T'; 235 Tem[1] = datas[2]/10 + 0x30; 236 Tem[2] = datas[2]%10 + 0x30; 237 Tem[3] = '.'; 238 Tem[4] = datas[3]/10 + 0x30; 239 Tem[5] = datas[3]%10 + 0x30; 240 Tem[6] = 'C'; 241 Tem[7] = '\0'; 242 } 243 void main() 244 { 245 Delay1000ms(); 246 LCD_1602_begin(); 247 UartInit(); 248 Delay1000ms(); 249 Delay1000ms(); 250 while(1) 251 { 252 Delay1000ms(); 253 read_dht11datas(); 254 if(datas[2 > 24]) 255 { 256 fan = 0; 257 } 258 else 259 { 260 fan = 1; 261 } 262 data_build(); 263 sendstring(hum); 264 sendstring("\r\n"); 265 sendstring(Tem); 266 sendstring("\r\n"); 267 LCD1602_datashow_line(1,2,Tem); 268 LCD1602_datashow_line(2,2,hum); 269 } 270 }
标签:温度传感器,--,data,void,51,char,单片机,while,nop 来源: https://www.cnblogs.com/shitou-geigei/p/16120591.html