main.lst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. C51 COMPILER V9.54 MAIN 02/18/2022 11:34:52 PAGE 1
  2. C51 COMPILER V9.54, COMPILATION OF MODULE MAIN
  3. OBJECT MODULE PLACED IN .\Objects\main.obj
  4. COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE main.c LARGE OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\Listing
  5. -s\main.lst) TABS(2) OBJECT(.\Objects\main.obj)
  6. line level source
  7. 1 #include "drive_1602.h"
  8. 2 #include "string.h"
  9. 3 #include "stdio.h"
  10. 4 #include "dht11.h"
  11. 5
  12. 6 #define uint unsigned int
  13. 7 #define uchar unsigned char
  14. 8
  15. 9 /*
  16. 10 按键实现人机交互的主要方式,温湿度传感器可以感测环境温度湿度,
  17. 11 并且采集电信号至单片机,供电模块给单片机提供电能保证系统能够正常运行,
  18. 12 水位传感器能够检测水田水位保证一定的水位。
  19. 13 显示屏可以显示温湿度信息和水位信息,
  20. 14 蓝牙传输模块能够将采集的信号都传输至手机APP,并实现图形化显示。
  21. 15 实现的功能:
  22. 16 1、通过蓝牙能够检测到水田的水位、温度、湿度。
  23. 17 2、通过USB +5V 供电
  24. 18 3、能够具有阈值报警的功能
  25. 19 4、阈值可以进行调节。
  26. 20 5、实现APP的温湿度信息监测,可在APP端图形化实时监测温度湿度变化。
  27. 21 */
  28. 22 sbit SW1=P3^7;
  29. 23 sbit SW2=P3^6;
  30. 24 sbit SW3=P3^5;
  31. 25
  32. 26 sbit JR_LED=P2^2;
  33. 27 sbit JW_LED=P2^4;
  34. 28
  35. 29 sbit buzz=P2^1;
  36. 30 sbit HY_IN=P2^3;
  37. 31
  38. 32 uchar temp,humi;
  39. 33 int sw_val=0;
  40. 34
  41. 35 void delay_xms(int xms)
  42. 36 {
  43. 37 1 char ix=0;
  44. 38 1 for(;xms>0;xms--)
  45. 39 1 for(ix=110;ix>0;ix--);
  46. 40 1 }
  47. 41 uchar SET=0;
  48. 42 char WdL=20;
  49. 43 char WdH=30;
  50. 44
  51. 45 char SdL=60;
  52. 46 char SdH=80;
  53. 47
  54. 48 char SWL=2;
  55. 49 char SWH=6;
  56. 50
  57. 51 char keyflag=0;
  58. 52
  59. 53 void KeyRead(void)
  60. 54 {
  61. C51 COMPILER V9.54 MAIN 02/18/2022 11:34:52 PAGE 2
  62. 55 1 if(!SW1)
  63. 56 1 {
  64. 57 2 if(!SW1&&keyflag==0)
  65. 58 2 {
  66. 59 3 keyflag=1;
  67. 60 3 if(++SET>6)SET=0;
  68. 61 3 while(!SW1);
  69. 62 3 }
  70. 63 2 }
  71. 64 1 else if(!SW2)
  72. 65 1 {
  73. 66 2 if(!SW2&&keyflag==0)
  74. 67 2 {
  75. 68 3 keyflag=1;
  76. 69 3 switch(SET)
  77. 70 3 {
  78. 71 4 case 1:
  79. 72 4 if(++WdH>50)WdH=0;
  80. 73 4 break;
  81. 74 4
  82. 75 4 case 2:
  83. 76 4 if(++WdL>50)WdL=0;
  84. 77 4 break;
  85. 78 4
  86. 79 4
  87. 80 4 case 3:
  88. 81 4 if(++SdH>90)SdH=0;
  89. 82 4 break;
  90. 83 4
  91. 84 4 case 4:
  92. 85 4 if(++SdL>90)SdH=0;
  93. 86 4 break;
  94. 87 4
  95. 88 4 case 5:
  96. 89 4 if(++SWH>8)SWH=0;
  97. 90 4 break;
  98. 91 4
  99. 92 4 case 6:
  100. 93 4 if(++SWL>8)SWL=0;
  101. 94 4 break;
  102. 95 4 }
  103. 96 3 while(!SW2);
  104. 97 3 }
  105. 98 2 }
  106. 99 1
  107. 100 1 else if(!SW3)
  108. 101 1 {
  109. 102 2 if(!SW3&&keyflag==0)
  110. 103 2 {
  111. 104 3 keyflag=1;
  112. 105 3 switch(SET)
  113. 106 3 {
  114. 107 4 case 1:
  115. 108 4 if(--WdH<0)WdH=50;
  116. 109 4 break;
  117. 110 4
  118. 111 4 case 2:
  119. 112 4 if(--WdL<0)WdL=50;
  120. 113 4 break;
  121. 114 4
  122. 115 4 case 3:
  123. 116 4 if(--SdH<0)SdH=90;
  124. C51 COMPILER V9.54 MAIN 02/18/2022 11:34:52 PAGE 3
  125. 117 4 break;
  126. 118 4
  127. 119 4 case 4:
  128. 120 4 if(--SdL<0)SdL=90;
  129. 121 4 break;
  130. 122 4
  131. 123 4 case 5:
  132. 124 4 if(--SWH<0)SWH=8;
  133. 125 4 break;
  134. 126 4
  135. 127 4 case 6:
  136. 128 4 if(--SWL<0)SWL=8;
  137. 129 4 break;
  138. 130 4 }
  139. 131 3 while(!SW3);
  140. 132 3 }
  141. 133 2 }else
  142. 134 1 {
  143. 135 2 keyflag=0;
  144. 136 2 }
  145. 137 1
  146. 138 1
  147. 139 1 }
  148. 140
  149. 141 void Timer0Init(void) //10毫秒@11.0592MHz
  150. 142 {
  151. 143 1
  152. 144 1 TMOD &= 0xF0; //设置定时器模式
  153. 145 1 TMOD |= 0x01; //设置定时器模式
  154. 146 1 TL0 = 0x00; //设置定时初值
  155. 147 1 TH0 = 0xDC; //设置定时初值
  156. 148 1 TF0 = 0; //清除TF0标志
  157. 149 1 ET0=1;
  158. 150 1 TR0 = 1; //定时器0开始计时
  159. 151 1 EA=1;
  160. 152 1
  161. 153 1 }
  162. 154 bit bj_flag=0;
  163. 155 uchar Bj_CNt=0;
  164. 156 uchar JCount=0;
  165. 157 void TimeISR()interrupt 1
  166. 158 {
  167. 159 1 TL0 = 0x00; //设置定时初值
  168. 160 1 TH0 = 0xDC; //设置定时初值
  169. 161 1 KeyRead();
  170. 162 1 if(bj_flag)
  171. 163 1 {
  172. 164 2 if(++Bj_CNt>30)
  173. 165 2 {
  174. 166 3 Bj_CNt=0;
  175. 167 3 buzz=~buzz;
  176. 168 3 }
  177. 169 2 }else
  178. 170 1 {
  179. 171 2 buzz=1;
  180. 172 2 }
  181. 173 1 JCount++;
  182. 174 1 }
  183. 175
  184. 176 void UartInit(void) //9600bps@11.0592MHz
  185. 177 {
  186. 178 1 PCON &= 0x7F; //波特率不倍速
  187. C51 COMPILER V9.54 MAIN 02/18/2022 11:34:52 PAGE 4
  188. 179 1 SCON = 0x50; //8位数据,可变波特率
  189. 180 1 TMOD &= 0x0F; //清除定时器1模式位
  190. 181 1 TMOD |= 0x20; //设定定时器1为8位自动重装方式
  191. 182 1 TL1 = 0xFD; //设定定时初值
  192. 183 1 TH1 = 0xFD; //设定定时器重装值
  193. 184 1 ET1 = 0; //禁止定时器1中断
  194. 185 1 TR1 = 1; //启动定时器1
  195. 186 1 EA=1;
  196. 187 1 ES=0;
  197. 188 1 }
  198. 189
  199. 190
  200. 191 void sendData(char *p,unsigned char n)
  201. 192 {
  202. 193 1 if( p == 0) return ;
  203. 194 1 ES = 0;
  204. 195 1 if(n > 0)
  205. 196 1 {
  206. 197 2 while(n --)
  207. 198 2 {
  208. 199 3 SBUF = *p++ ;
  209. 200 3 while(!TI) ;
  210. 201 3 TI = 0 ;
  211. 202 3 }
  212. 203 2 }
  213. 204 1 // ES = 1;
  214. 205 1 }
  215. 206
  216. 207
  217. 208
  218. 209 // 往串口发送字符串
  219. 210 void sendString(char *p)
  220. 211 {
  221. 212 1 if(p == 0) return ;
  222. 213 1 sendData(p,strlen(p));
  223. 214 1 }
  224. 215 // 接收中断函数
  225. 216 void usart() interrupt 4
  226. 217 {
  227. 218 1 if(RI == 1)
  228. 219 1 {
  229. 220 2 // setUsartRxData(SBUF);
  230. 221 2 }
  231. 222 1 RI = 0;
  232. 223 1 TI = 0;
  233. 224 1 }
  234. 225
  235. 226 char data showbuff[16]={0};
  236. 227 char data send_buff[16]={0};
  237. 228
  238. 229 sbit IN_PL = P1^6; //SHIFT/!LOA引脚
  239. 230 sbit IN_Data = P1^7; // QH数据输出引脚
  240. 231 sbit SCK = P3^6; //CLOCK引脚
  241. 232
  242. 233 unsigned char Read74HC165(void)
  243. 234 {
  244. 235 1 unsigned char indata;
  245. 236 1 unsigned char i;
  246. 237 1 IN_PL = 0;//装载8位并行数据
  247. 238 1 _nop_();
  248. 239 1 IN_PL = 1;//转换8位并行数据为串行数据
  249. 240 1 _nop_();
  250. C51 COMPILER V9.54 MAIN 02/18/2022 11:34:52 PAGE 5
  251. 241 1
  252. 242 1 indata = 0;
  253. 243 1 for(i = 0; i < 8; i ++) //将8个串行数据存入数组indata中
  254. 244 1 {
  255. 245 2 indata = indata << 1;//开始时,indata数组先左移一位,以后每个循环左移一次
  256. 246 2 SCK = 0;//时钟低电平到来后,并行数据开始转换串行数据
  257. 247 2 _nop_();
  258. 248 2
  259. 249 2 indata |= IN_Data; //将转换完成的串行数据一位位存入数组
  260. 250 2 SCK = 1; //时钟变为高电平,再次变为低电平时,开始传出下一位串行数据
  261. 251 2 }
  262. 252 1 return indata;
  263. 253 1 }
  264. 254
  265. 255 bit clear_flag=0;
  266. 256
  267. 257 void main()
  268. 258 {
  269. 259 1 lcd1602_init();
  270. 260 1 UartInit();
  271. 261 1 dht11_value(&temp,&humi,DHT11_UINT8);
  272. 262 1 buzz=1;
  273. 263 1 delay_xms(200);
  274. 264 1 dht11_value(&temp,&humi,DHT11_UINT8);
  275. 265 1 Timer0Init();
  276. 266 1 while(1)
  277. 267 1 {
  278. 268 2
  279. 269 2 if(JCount>50)//500ms采集一次
  280. 270 2 {
  281. 271 3 JCount=0;
  282. 272 3 if(SET==0)
  283. 273 3 {
  284. 274 4 if(clear_flag==1)
  285. 275 4 {
  286. 276 5 clear_flag=0;
  287. 277 5
  288. 278 5 Write_1602_com(0x01);//清显示
  289. 279 5 delay_1ms(100);
  290. 280 5 }
  291. 281 4
  292. 282 4 dht11_value(&temp,&humi,DHT11_UINT8);
  293. 283 4 L1602_printf(0,0,"temp:%2d",temp);
  294. 284 4 L1602_printf(0,1,"humi:%2d",humi);
  295. 285 4 L1602_printf(12,0,"%s","SW");
  296. 286 4 L1602_printf(12,1,"%2dcm",sw_val);
  297. 287 4 if((temp<WdL||temp>WdH)||(humi<SdL||humi>SdH)||(sw_val<SWL||sw_val>SWH))
  298. 288 4 {
  299. 289 5 bj_flag=1;
  300. 290 5 }else
  301. 291 4 {
  302. 292 5 bj_flag=0;
  303. 293 5 }
  304. 294 4 sprintf(send_buff,"X%dH%dD%dZ\r\n",temp,humi,sw_val);
  305. 295 4 sendString(send_buff);
  306. 296 4 }
  307. 297 3 else
  308. 298 3 {
  309. 299 4 if(clear_flag==0)
  310. 300 4 {
  311. 301 5 clear_flag=1;
  312. 302 5
  313. C51 COMPILER V9.54 MAIN 02/18/2022 11:34:52 PAGE 6
  314. 303 5 Write_1602_com(0x01);//清显示
  315. 304 5 delay_1ms(100);
  316. 305 5 }
  317. 306 4 switch(SET)
  318. 307 4 {
  319. 308 5 case 1:
  320. 309 5 case 2:
  321. 310 5 WriteLcd1602_String(0,0,"Set temp");
  322. 311 5 L1602_printf(0,1,"H:%2d L:%2d",WdH,WdL);
  323. 312 5 break;
  324. 313 5
  325. 314 5 case 3:
  326. 315 5 case 4:
  327. 316 5 WriteLcd1602_String(0,0,"Set humi");
  328. 317 5 L1602_printf(0,1,"H:%2d L:%2d",SdH,SdL);
  329. 318 5 break;
  330. 319 5
  331. 320 5 case 5:
  332. 321 5 case 6:
  333. 322 5 WriteLcd1602_String(0,0,"Set SW ");
  334. 323 5 L1602_printf(0,1,"H:%2d L:%2d",SWH,SWL);
  335. 324 5 break;
  336. 325 5 }
  337. 326 4
  338. 327 4 }
  339. 328 3 }
  340. 329 2
  341. 330 2 }
  342. 331 1
  343. 332 1 }
  344. MODULE INFORMATION: STATIC OVERLAYABLE
  345. CODE SIZE = 1230 ----
  346. CONSTANT SIZE = 83 ----
  347. XDATA SIZE = 14 6
  348. PDATA SIZE = ---- ----
  349. DATA SIZE = 32 ----
  350. IDATA SIZE = ---- ----
  351. BIT SIZE = 2 ----
  352. END OF MODULE INFORMATION.
  353. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)