This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
645 lines (615 loc) · 16.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include "Uart.c"
#include "LCD.c"
#include "main.h"
void deal_data()//GPSRMC数据长度72
{
uchar i=0,j=0,k=0;//i循环用 j计算有几个‘,’ k为赋值用下标
uint s=0;
for(i=0; i<72; i++)
{
if(GPSRMC[i]==',')
{
i++;
j++;
}
switch(j)
{
case 1://UTC_time
if(k<10)
{
if(k<2)
{
hour[k]=GPSRMC[i];
}
else if(k<4)
{
minute[k-2]=GPSRMC[i];
}
else sec[k-4]=GPSRMC[i];
// UTC_time[k]=GPSRMC[i];
k++;
}
break;
case 2://A有效定位 V无效定位
k=0;
valid=GPSRMC[i];
break;
case 3://纬度
if(k<9)
{
if(k<2)latitude_d[k]=GPSRMC[i];
else latitude_m[k-2]=GPSRMC[i];
// latitude[k]=GPSRMC[i];
k++;
}
break;
case 4://N or S
// latitude[k]=GPSRMC[i];
NS=GPSRMC[i];
k=0;
break;
case 5://经度
if(k<10)
{
if(k<3)longitude_d[k]=GPSRMC[i];
else longitude_m[k-3]=GPSRMC[i];
// longitude[k]=GPSRMC[i];
k++;
}
break;
case 6://E or W
// longitude[k]=GPSRMC[i];
WE=GPSRMC[i];
k=0;
break;
case 9://UTC_date
if(k<6)
{
if(k<2)day[k]=GPSRMC[i];
else if(k<4)month[k-2]=GPSRMC[i];
else year[k-2]=GPSRMC[i];
// UTC_date[k]=GPSRMC[i];
k++;
}
break;
default:
break;
}
}
sscanf(hour,"%u",&s);
s+=8;
sprintf(hour,"%u",s);
}
void SendAirQuality()
{
sendData("--------------AirQuality--------------\r\n");
sendData("Checksum:");
sendData(cChecksum);
sendData(" == ");
sendData("CalChecksum:");
sendData(cCalChecksum);
SendEndl();
sendData("FrameLen:");
sendData(cFrameLen);
SendEndl();
sendData("PM1_0CF1:");
sendData(cPM1_0CF1);
SendEndl();
sendData("PM2_5CF1:");
sendData(cPM2_5CF1);
SendEndl();
sendData("PM10CF1:");
sendData(cPM10CF1);
SendEndl();
sendData("PM1_0AE:");
sendData(cPM1_0AE);
SendEndl();
sendData("PM2_5AE:");
sendData(cPM2_5AE);
SendEndl();
sendData("PM10AE:");
sendData(cPM10AE);
SendEndl();
sendData("Gt0_3um:");
sendData(cGt0_3um);
SendEndl();
sendData("Gt0_5um:");
sendData(cGt0_5um);
SendEndl();
sendData("Gt1_0um:");
sendData(cGt1_0um);
SendEndl();
sendData("Gt2_5um:");
sendData(cGt2_5um);
SendEndl();
sendData("Gt5_0um:");
sendData(cGt5_0um);
SendEndl();
sendData("Gt10um:");
sendData(cGt10um);
SendEndl();
sendData("Version:");
SendChar1(Version+'0');
SendEndl();
sendData("ErrorCode:");
SendChar1(ErrorCode+'0');
SendEndl();
sendData("-------------------END-------------------\r\n");
}
void ESP_SendGPS()
{
sprintf(ESP_UP_DATA,GPS_data1,year,month,day);
sprintf(ESP_UP_DATA,GPS_data2,ESP_UP_DATA,latitude_d,latitude_m);
sprintf(ESP_UP_DATA,GPS_data3,ESP_UP_DATA,longitude_d,longitude_m);
sprintf(ESP_UP_DATA,GPS_data4,ESP_UP_DATA,hour,minute);
sprintf(ESP_UP_DATA,GPS_data5,ESP_UP_DATA,sec,"gps");
sendData4(ESP_UP_DATA);
// sendData(ESP_UP_DATA);
ESP8266=2;
}
void ESP_SendAIR()
{
sprintf(ESP_UP_DATA,Air_data1,"pms7003",cPM1_0CF1,cPM2_5CF1);
sprintf(ESP_UP_DATA,Air_data2,ESP_UP_DATA,cPM10CF1,cPM1_0AE);
sprintf(ESP_UP_DATA,Air_data3,ESP_UP_DATA,cPM2_5AE,cPM10AE);
sprintf(ESP_UP_DATA,Air_data4,ESP_UP_DATA,cGt0_3um,cGt0_5um);
sprintf(ESP_UP_DATA,Air_data5,ESP_UP_DATA,cGt1_0um,cGt2_5um);
sprintf(ESP_UP_DATA,Air_data6,ESP_UP_DATA,cGt5_0um,cGt10um);
sendData4(ESP_UP_DATA);
// sendData(ESP_UP_DATA);
ESP8266=3;
}
void DisPlay_LCD()
{
switch(Touch)
{
case -1:
return;
break;
case 0:
DispS(1,0,"****设备状况****");
if(ATK_GPS==3)DispS(2,0,"ATK_GPS:Normal ");
else if(ATK_GPS==2)DispS(2,0,"ATK_GPS:Error ");
else if(ATK_GPS==1)DispS(2,0,"ATK_GPS:Wait ");
else DispS(2,0,"ATK_GPS:未接入 ");
if(Pms7003==3)DispS(3,0,"Pms7003:Normal ");
else if(Pms7003==2)DispS(3,0,"Pms7003:Error ");
else if(Pms7003==1)DispS(3,0,"Pms7003:Wait ");
else DispS(3,0,"Pms7003:未接入 ");
if(ESP8266==1)DispS(4,0,"ESP8266:已接入 ");
else if(ESP8266==2)DispS(4,0,"ESP8266:SendGPS ");
else if(ESP8266==3)DispS(4,0,"ESP8266:SendAir ");
else (4,0,"ESP8266:Wait ");
break;
case 1:
DispS(1,0,"ug/m3 ATEN CF=1");
DispS(2,0,"PM1.0:");
sprintf(strTemp, "%-4.4s%2s%4.4s",cPM1_0AE,"//",cPM1_0CF1);
DispS(2,3,strTemp);
DispS(3,0,"PM2.5:");
sprintf(strTemp, "%-4.4s%2s%4.4s",cPM2_5AE,"//",cPM2_5CF1);
DispS(3,3,strTemp);
DispS(4,0,"PM10 :");
sprintf(strTemp, "%-4.4s%2s%4.4s",cPM10AE,"//",cPM10CF1);
DispS(4,3,strTemp);
break;
case 2:
DispS(1,0,"**um in 0.1L Air");
sprintf(strTemp, "%5s%-5.5s%4s","G0.3:",cGt0_3um,"G10:");
sprintf(strTemp, "%s%-2.2s",strTemp,cGt10um);
DispS(2,0,strTemp);
sprintf(strTemp, "%5s%-5.5s%3s","G0.5:",cGt0_5um,"G5:");
sprintf(strTemp, "%s%-3.3s",strTemp,cGt5_0um);
DispS(3,0,strTemp);
sprintf(strTemp, "%5s%-4.4s%3s","G2.5:",cGt2_5um,"G1:");
sprintf(strTemp, "%s%-4.4s",strTemp,cGt1_0um);
DispS(4,0,strTemp);
break;
case 3:
sprintf(strTemp,"%4.4s年%2.2s月%2.2s日",year,month,day);
sprintf(strTemp,"%14.14s %c",strTemp,valid);
DispS(1,0,strTemp);
sprintf(strTemp,"U+8 %2.2s:%2.2s:%6.6s",hour,minute,sec);
DispS(2,0,strTemp);
sprintf(strTemp,"%4s%2s%7.7s",latitude_d,"°",latitude_m);
sprintf(strTemp,"%13.13s%1s %c",strTemp,"'",NS);
DispS(3,0,strTemp);
sprintf(strTemp,"%4s%2s%7.7s",longitude_d,"°",longitude_m);
sprintf(strTemp,"%13.13s%1s %c",strTemp,"'",WE);
DispS(4,0,strTemp);
break;
default:
break;
}
}
//void TM0() interrupt 1
//{
// cnt++; //5ms进入1次中断
// if(cnt == 200) //200次中断被响应后,正好1000ms
// {
// led = ~led; //将指示灯取反更换亮灭状态
// cnt = 0;
// }
// //进入中断时会将定时器中断溢出标志位硬件清零,因此下面一句可以不加的
// TF0 = 0; //清除T0中断溢出标志位
//}
void INT1() interrupt 2
{
if(Touch==-1)Touch=0;
else
{
Touch=-1;
DispS(1,0,"****************");
DispS(2,0,"**LCD12864关闭**");
DispS(3,0,"** INT1 **");
DispS(4,0,"****************");
}
}
void INT3() interrupt 11
{
if(Touch==-1)return;
else if(Touch==3)Touch=0;
else Touch++;
DisPlay_LCD();
// SendChar1(Touch+'0');
}
void main()
{
UartInit1();
UartInit2();
UartInit3();
UartInit4();
// Timer0Init();
Init_Lcd();
IE1 = 0; //将INT1中断请求标志位清"0"
EX1 = 1; //使能INT1中断允许位
IT1 = 1; //选择INT1为下降沿触发方式
INTCLKO |= 0x20; //使能INT3中断允许位 P3.7 下降沿中断方式
while(1)
{
}
}
void Uart1() interrupt 4 using 1
{
//strncpy函数字符串截取(原函数,截取的函数,多少)
//WifiAPMode
static uchar i=0;
uchar temp;
if (RI) //串行接收到停止位的中间时刻时,该位置1
{
RI = 0; //清除RI位 (该位必须软件清零)
temp=SBUF;
Rec[i] = temp;
// if(Com1==1)SendChar1(temp);
i++;
if(i>5)
{
i=0;
}
if(temp=='\n')
{
Cmd[0]=Rec[0];
Cmd[1]=Rec[1];
Cmd[2]=Rec[2];
if(strcmp(Cmd,"M01")==0)
{
if(Com1!=1)
{
Com1=1;
SendString1("COM1-ON\r\n",9);
}
else
{
Com1=0;
SendString1("COM1-OFF\r\n",10);
}
}
else if(strcmp(Cmd,"M02")==0)
{
if(Com2!=1)
{
Com2=1;
SendString1("COM2-ON\r\n",9);
}
else
{
Com2=0;
SendString1("COM2-OFF\r\n",10);
}
}
else if(strcmp(Cmd,"M03")==0)
{
if(Com3!=1)
{
Com3=1;
SendString1("COM3-ON\r\n",9);
}
else
{
Com3=0;
SendString1("COM3-OFF\r\n",10);
}
}
else if(strcmp(Cmd,"M04")==0)
{
if(Com4!=1)
{
Com4=1;
SendString1("COM4-ON\r\n",9);
}
else
{
Com4=0;
SendString1("COM4-OFF\r\n",10);
}
}
else if(strcmp(Cmd,"M05")==0)
{
if(Com2!=2)
{
Com2=2;
sendData("GPS-ON\r\n");
}
else
{
Com2=0;
sendData("GPS-OFF\r\n");
}
}
else if(strcmp(Cmd,"M06")==0)
{
if(Com3!=2)
{
Com3=2;
sendData("AirQuality-ON\r\n");
}
else
{
Com3=0;
sendData("AirQuality-OFF\r\n");
}
}
else if(strcmp(Cmd,"M07")==0)
{
if(Touch==-1)
{
Touch=0;
sendData("LCD-ON\r\n");
}
else
{
Touch=-1;
DispS(1,0,"****************");
DispS(2,0,"**LCD12864关闭**");
DispS(3,0,"**发送 M07开启**");
DispS(4,0,"****************");
sendData("LCD-OFF\r\n");
}
}
else
{
sendData("Error\r\n");
sendData(Cmd);
SendEndl();
}
i=0;
}
}
}
void Uart2() interrupt 8 using 1
{
static uchar m=0;
uchar temp;
if (S2CON & S2RI) //串行接收到停止位的中间时刻时,该位置1
{
S2CON &= ~S2RI; //清除S2CON寄存器对应S2RI位(该位必须软件清零)
temp=S2BUF;
if(m==0&&temp=='$')
{
GPSRMC[m]=temp;
Calsum=0;
m++;
}
else if(temp=='\r'&&m!=70)
{
m=0;
Calsum=0;
}
else if(temp=='\n'&&m!=71)
{
m=0;
Calsum=0;
}
else if(m<67)
{
GPSRMC[m]=temp;
Calsum^=temp;
m++;
}
else if(m==68||m==69)
{
GPSRMC[m]=temp;
Chesum[m-68]=GPSRMC[m];
m++;
}
else
{
GPSRMC[m]=temp;
m++;
}
if(m>=72)
{
m=0;
if(Com2==1)sendData(GPSRMC);
sprintf(cCalsum,"%X",Calsum);
if(strcmp(Chesum,cCalsum)==0)
{
if(Com2==2)sendData(GPSRMC);
deal_data();
ESP_SendGPS();
ATK_GPS=3;
}
else
{
ATK_GPS=2;
sendData("GPSERROR\r\n");
sendData(Chesum);
SendEndl();
sendData(cCalsum);
SendEndl();
}
DisPlay_LCD();
ESP8266=0;
ATK_GPS=1;
Calsum=0;
}
}
}
void Uart3() interrupt 17 using 1
{
static uint j=0;
uint val;
uint temp;
if (S3CON & S3RI) //串行接收到停止位的中间时刻时,该位置1
{
S3CON &= ~S3RI; //清除S3CON寄存器对应S3RI位(该位必须软件清零)
temp=S3BUF;
if(Com3==1)SendChar1(temp);
if(j==0)
{
if(temp==0x42)
{
AirSensor[j]=temp;
CalChecksum = 0x42;
j++;
}
else j=0;
}
else if(j==1)
{
if(temp==0x4d)
{
AirSensor[j]=temp;
CalChecksum += 0x4d;
j++;
}
else j=0;
}
else if(j>=2)
{
AirSensor[j]=temp;
if(j<30)CalChecksum+=temp;
j++;
val=AirSensor[j-1]+(AirSensor[j-2]<<8);
switch(j)
{
case 4:
FrameLen= val+j;
sprintf(cFrameLen, "%u",FrameLen);
break;
case 6:
PM1_0CF1 = val;
sprintf(cPM1_0CF1, "%u",PM1_0CF1);
break;
case 8:
PM2_5CF1 = val;
sprintf(cPM2_5CF1, "%u",PM2_5CF1);
break;
case 10:
PM10CF1 = val;
sprintf(cPM10CF1, "%u",PM10CF1);
break;
case 12:
PM1_0AE = val;
sprintf(cPM1_0AE, "%u",PM1_0AE);
break;
case 14:
PM2_5AE = val;
sprintf(cPM2_5AE, "%u",PM2_5AE);
break;
case 16:
PM10AE = val;
sprintf(cPM10AE, "%u",PM10AE);
break;
case 18:
Gt0_3um = val;
sprintf(cGt0_3um, "%u",Gt0_3um);
break;
case 20:
Gt0_5um = val;
sprintf(cGt0_5um, "%u",Gt0_5um);
break;
case 22:
Gt1_0um = val;
sprintf(cGt1_0um, "%u",Gt1_0um);
break;
case 24:
Gt2_5um = val;
sprintf(cGt2_5um, "%u",Gt2_5um);
break;
case 26:
Gt5_0um = val;
sprintf(cGt5_0um, "%u",Gt5_0um);
break;
case 28:
Gt10um = val;
sprintf(cGt10um, "%u",Gt10um);
break;
case 29:
val= AirSensor[j-1];
Version = val;
break;
case 30:
val= AirSensor[j-1];
ErrorCode = val;
break;
case 32:
j=0;
Checksum = val;
sprintf(cChecksum, "%u",Checksum);
sprintf(cCalChecksum, "%u",CalChecksum);
if(Checksum==CalChecksum)
{
if(Com3==2)SendAirQuality();
ESP_SendAIR();
Pms7003=3;
}
else
{
Pms7003=2;
sendData("AirERROR\r\n");
sendData(cChecksum);
SendEndl();
sendData(cCalChecksum);
SendEndl();
}
DisPlay_LCD();
ESP8266=0;
Pms7003=1;
break;
default:
break;
}
}
else j=0;
}
}
void Uart4() interrupt 18 using 1
{
static uchar n=0;
uchar temp;
if(S4CON & S4RI) //串行接收到停止位的中间时刻时,该位置1
{
S4CON &= ~S4RI; //清除S4CON寄存器对应S4RI位(该位必须软件清零)
temp=S4BUF;
// ESP[n]=temp;
ESP8266=1;
n++;
if(Com4==1)SendChar1(temp);
// if(temp=='\n'||n>=100)
// {
//
// n=0;
// }
}
}