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