-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
673 lines (528 loc) · 13.5 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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#if defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#elif defined(__PIC24H__)
#include <p24hxxxx.h>
#endif
#include "timer.h"
#include "adc.h"
#include "sci.h"
#include "ecan.h"
#include "collect.h"
#include "stdio.h"
_FBS(0xCF);
_FSS(0xCF);
_FGS(0x07);
_FOSCSEL(0xA2); //Select HS without PLL
_FOSC(0x46); // HS oscilator, OSC2 is clock output
_FWDT(0xDF);
_FPOR(0xE7);
//_FICD(0xC3);
#define CLRWDT {__asm__ volatile ("CLRWDT");}
/****
四大指示灯
**/
#define COMM LATCbits.LATC14
#define WORK LATCbits.LATC13
#define STAT LATDbits.LATD0
#define FAIL LATDbits.LATD11
/**
网口复位引脚
****/
#define Nrest LATFbits.LATF6
/****
MIAN_ID与IP地址的最后两位对应
**********/
unsigned char MAIN_ID=4;
#define BOARD_NUM 2 // Number of board
//ooo1234567
int count_rx = 0;
unsigned char send_data[200]={1,2,3,4,5,6,7,8,9,10};
char send_ascii[250];
char flag_ascii_or_bin = 'b';
unsigned int freq[BOARD_NUM][9],lq;
//unsigned int temp[BOARD_NUM][9];
unsigned int temp_freq[8];
unsigned int temp[BOARD_NUM][8]; // Temperature
unsigned int Tick_40S=0; // 用来记录定时中断次数
unsigned char work_enable = 0;//采集模块工作使能位
unsigned char uart2_enable = 0;//串口使能位
unsigned char uart1_enable = 0;//网口使能位
unsigned char Slave_Message_Count[BOARD_NUM]; // 记录各从板返回的数据个数
//unsigned char state;
mID canTxMessage;
mID canRxMessage;
ECAN1MSGBUF ecan1msgBuf __attribute__((space(dma),aligned(ECAN1_MSG_BUF_LENGTH*16)));
void DELAY(unsigned int t)
{
unsigned int i,j;
for(i=0;i<50;i++)
{
CLRWDT
for(j=0;j<t;j++)
{
asm("nop");
}
}
}
void UART2_Send(unsigned char str[], int len)
{
int i;
for(i=0;i<len;i++)
{
U2TXREG = str[i];
while((U2STA&0x0100)==0x0000){asm("nop");}
}
}
void UART1_Send(unsigned char str[], int len)
{
int i;
for(i=0;i<len;i++)
{
U1TXREG = str[i];
while((U1STA&0x0100)==0x0000){asm("nop");}
}
}
void __attribute__((interrupt,no_auto_psv)) _T6Interrupt(void) // 0.8s interrupt
{
IFS2bits.T6IF = 0;
FAIL = ~FAIL;
Tick_40S++;//加一次是0.8s
if(Tick_40S > 2714) //36min
{
InitSCI();
Nrest=0;
DELAY(1000);
Nrest=1;
Tick_40S = 0;
}
}
/******
网口中断
**********/
void __attribute__((interrupt,no_auto_psv)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0;
unsigned char data[17];
unsigned int UART_Timeout = 0;
unsigned char i = 0;
int nian,yue,ri,shi,fen,miao;
//unsigned int num;
data[0] = U1RXREG;
while(data[i]!='E')
{
i++;
while((0==(U1STA&0x0001))&&(UART_Timeout<50000))
{UART_Timeout++;}
if(UART_Timeout>=50000)
{
data[i]='E'; // 如果接收超时,退出中断
}
else
{
data[i] = U1RXREG;
}
UART_Timeout = 0;
}
if( (i==16)&&(data[2]==0X03)&&(data[3]==0X04)&&(data[0]=='S') )
{
flag_ascii_or_bin = 'b';
work_enable = 1;//握手指令中的ID与箱号一致时,才会开启该采集箱激频拾频
uart1_enable =1;
Tick_40S=0;
}//if(i==16)
return;
}//
/******
外部串口中断
**********/
int start_judge = 0;
void __attribute__((interrupt,no_auto_psv)) _U2RXInterrupt(void)
{
unsigned int UART_Timeout = 0;
unsigned char i = 0;
unsigned char dat;
unsigned char receive_buf[60];
IFS1bits.U2RXIF = 0;
dat = U2RXREG;
if((start_judge == 0) && (dat == 'S'))
{
start_judge = 1;
receive_buf[0] = dat;
}
else if((start_judge == 0) && (dat == 'C'))
{
start_judge = 3;
}
else if(start_judge == 1)
{
while(receive_buf[i]!='E')
{
if(i == 0)
{
i = 1;
receive_buf[i] = dat;
}
i++;
while((0==(U2STA&0x0001))&&(UART_Timeout<50000))
{UART_Timeout++;}
if(UART_Timeout>=50000)
{
receive_buf[i]='E'; // 如果接收超时,退出中断
}
else
{
receive_buf[i] = U2RXREG;
}
UART_Timeout = 0;
}
start_judge = 0;
if((i==16)&&(receive_buf[2]==0X01)&&(receive_buf[3]==0X02))
{
work_enable = 1;//握手指令中的ID与箱号一致时,才会开启该采集箱激频拾频
uart2_enable =1;
flag_ascii_or_bin = 'b';
count_rx = 0;
}
}
else if((start_judge == 0) && (dat == '5'))
{
start_judge = 2;
receive_buf[0] = dat;
}
else if(start_judge == 2)
{
while((receive_buf[i] != '5') || (i == 0))
{
if(i == 0)
{
i = 1;
receive_buf[i] = dat;
}
i++;
while((0==(U2STA&0x0001))&&(UART_Timeout<50000))
{UART_Timeout++;}
if(UART_Timeout>=50000)
{
receive_buf[i]='5'; // 如果接收超时,退出中断
}
else
{
receive_buf[i] = U2RXREG;
}
UART_Timeout = 0;
}
start_judge = 0;
if((i==49)&&(receive_buf[7]=='1')&&(receive_buf[10]=='2'))
{
work_enable = 1;//握手指令中的ID与箱号一致时,才会开启该采集箱激频拾频
uart2_enable =1;
flag_ascii_or_bin = 'a';
count_rx = 0;
}
}
else if(start_judge == 3)
{
start_judge = 0;
if(dat == 'Q')
{
IEC1bits.U2RXIE = 0; // Enable UART2 RX interrupt
IEC1bits.U2TXIE = 0;
IEC0bits.U1RXIE = 0; // Enable UART1 RX interrupt
IEC0bits.U1TXIE = 0;
IEC2bits.T6IE = 0;
IEC2bits.C1IE=0;
C1INTEbits.RBIE=0;
while(1); //饿狗,让狗来重启
}
}
return;
}
void __attribute__((interrupt,no_auto_psv))_C1Interrupt(void)
{
/* check to see if the interrupt is caused by receive */
if(C1INTFbits.RBIF)
{
/* check to see if buffer 1 is full */
if(C1RXFUL1bits.RXFUL1)
{
/* set the buffer full flag and the buffer received flag */
canRxMessage.buffer_status=CAN_BUF_FULL;
canRxMessage.buffer=1;
}
/* check to see if buffer 2 is full */
else if(C1RXFUL1bits.RXFUL2)
{
/* set the buffer full flag and the buffer received flag */
canRxMessage.buffer_status=CAN_BUF_FULL;
canRxMessage.buffer=2;
}
/* check to see if buffer 3 is full */
else if(C1RXFUL1bits.RXFUL3)
{
/* set the buffer full flag and the buffer received flag */
canRxMessage.buffer_status=CAN_BUF_FULL;
canRxMessage.buffer=3;
}
else;
/* clear flag */
C1INTFbits.RBIF = 0;
}
else if(C1INTFbits.TBIF)
{
/* clear flag */
C1INTFbits.TBIF = 0;
}
else;
/* clear interrupt flag */
IFS2bits.C1IF=0;
}
unsigned char CAN_FLAG=0;
int main()
{
int p;
int i = 1000; // 扫频频率 473Hz~7.042kHz
int k,j;
int n,m;
int s=0;
int temp1;
unsigned char year,month,day,hour,minute,second;
unsigned int humi_val_i,temp_val_i;
unsigned char error,checksum;
unsigned char read_temp;
CLRWDT
OSCCON = 0x2200;
TRISCbits.TRISC13 = 0;//LIGNT
TRISCbits.TRISC14 = 0;
TRISDbits.TRISD11 = 0;
TRISDbits.TRISD0 = 0;
TRISFbits.TRISF6 = 0; //interet
TRISFbits.TRISF1 = 0;//CAN
TRISFbits.TRISF0 = 1;
s_connectionreset();
/*temp hr */
AD1PCFGLbits.PCFG5 = 1;//端口引脚处于数字模式,使能端口读输入
AD1PCFGLbits.PCFG4 = 1;
AD2PCFGLbits.PCFG5 = 1;//端口引脚处于数字模式,使能端口读输入
AD2PCFGLbits.PCFG4 = 1;
WORK=1;//COMMM1
STAT=0;
COMM=0;//COMMM1
FAIL=1;
Nrest=1;//
InitTimer6(); //// Timer6 提供0.8s中断定时
StartTimer6();
InitSCI();
initECAN();
initDMAECAN();
IEC2bits.C1IE=1;
C1INTEbits.RBIE=1;
CLRWDT
DELAY(200); // 600~9ms
/*
for(n=0;n<BOARD_NUM;n++)//获取slave的采集数据
{
CLRWDT
DELAY(800); // 600~9ms
CLRWDT
canTxMessage.message_type=CAN_MSG_DATA;
canTxMessage.frame_type=CAN_FRAME_STD;
canTxMessage.buffer=0;
canTxMessage.id=(0x002|(n+1)<<4);
canTxMessage.data[0]=0x01;
canTxMessage.data[1]=0x02;
canTxMessage.data[2]=0x03;
canTxMessage.data[3]=0x04;
canTxMessage.data[4]=0x05;
canTxMessage.data[5]=0x06;
canTxMessage.data[6]=0x07;
canTxMessage.data[7]=0x08;
canTxMessage.data_length=8;
sendECAN(&canTxMessage);//发送报文,指示第n+1块板子返回采集值
Slave_Message_Count[n] = 0;
while(Slave_Message_Count[n]<4) // 每个从板需要回复2帧
{
if(canRxMessage.buffer_status==CAN_BUF_FULL)
{
rxECAN(&canRxMessage);
canRxMessage.buffer_status=CAN_BUF_EMPTY;
Slave_Message_Count[n]++;
temp1 = ((unsigned int)(canRxMessage.data[0])<<8)+canRxMessage.data[1];
temp1 = ((unsigned int)(canRxMessage.data[2])<<8)+canRxMessage.data[3];
temp1 = ((unsigned int)(canRxMessage.data[4])<<8)+canRxMessage.data[5];
temp1 = ((unsigned int)(canRxMessage.data[6])<<8)+canRxMessage.data[7];
}
}
CAN_FLAG=1;
}
*/
UART2_Send("restart",7);
while(1)
{
CLRWDT
if((U1STA & 0x000E) != 0x0000)
{
read_temp = U1RXREG;
U1STAbits.OERR = 0;
}
if((U2STA & 0x000E) != 0x0000)
{
read_temp = U2RXREG;
U2STAbits.OERR = 0;
}
if(work_enable ==1)
{
STAT=1;
{ CLRWDT
canTxMessage.message_type=CAN_MSG_DATA;
canTxMessage.frame_type=CAN_FRAME_STD;
canTxMessage.buffer=0;
canTxMessage.id=0X01;
canTxMessage.data[0]=0x01;
canTxMessage.data[1]=0x02;
canTxMessage.data[2]=0x03;
canTxMessage.data[3]=0x04;
canTxMessage.data[4]=0x05;
canTxMessage.data[5]=0x06;
canTxMessage.data[6]=0x07;
canTxMessage.data[7]=0x08;
canTxMessage.data_length=8;
sendECAN(&canTxMessage);//发送报文,指示第n+1块板子返回采集值
DELAY(60000); // 600~9ms
DELAY(60000); // 600~9ms
}
for(n=0;n<BOARD_NUM;n++)//获取slave的采集数据
{
DELAY(800); // 600~9ms
CLRWDT
canTxMessage.message_type=CAN_MSG_DATA;
canTxMessage.frame_type=CAN_FRAME_STD;
canTxMessage.buffer=0;
canTxMessage.id=(0x002|(n+1)<<4);
canTxMessage.data[0]=0x01;
canTxMessage.data[1]=0x02;
canTxMessage.data[2]=0x03;
canTxMessage.data[3]=0x04;
canTxMessage.data[4]=0x05;
canTxMessage.data[5]=0x06;
canTxMessage.data[6]=0x07;
canTxMessage.data[7]=0x08;
canTxMessage.data_length=8;
sendECAN(&canTxMessage);//发送报文,指示第n+1块板子返回采集值
Slave_Message_Count[n] = 0;
while(Slave_Message_Count[n]<4) // 每个从板需要回复2帧
{
if(canRxMessage.buffer_status==CAN_BUF_FULL)
{
// if(canRxMessage.buffer==(n+1))
{
rxECAN(&canRxMessage);
canRxMessage.buffer_status=CAN_BUF_EMPTY;
Slave_Message_Count[n]++;
if(canRxMessage.id == (0x001|(n+1)<<8))
{
freq[n][0] = ((unsigned int)(canRxMessage.data[0])<<8)+canRxMessage.data[1];
temp[n][0] = ((unsigned int)(canRxMessage.data[2])<<8)+canRxMessage.data[3];
freq[n][1] = ((unsigned int)(canRxMessage.data[4])<<8)+canRxMessage.data[5];
temp[n][1] = ((unsigned int)(canRxMessage.data[6])<<8)+canRxMessage.data[7];
}
else if(canRxMessage.id == (0x002|(n+1)<<8))
{
freq[n][2] = ((unsigned int)(canRxMessage.data[0])<<8)+canRxMessage.data[1];
temp[n][2] = ((unsigned int)(canRxMessage.data[2])<<8)+canRxMessage.data[3];
freq[n][3] = ((unsigned int)(canRxMessage.data[4])<<8)+canRxMessage.data[5];
temp[n][3] = ((unsigned int)(canRxMessage.data[6])<<8)+canRxMessage.data[7];
}
else if(canRxMessage.id == (0x003|(n+1)<<8))
{
freq[n][4] = ((unsigned int)(canRxMessage.data[0])<<8)+canRxMessage.data[1];
temp[n][4] = ((unsigned int)(canRxMessage.data[2])<<8)+canRxMessage.data[3];
freq[n][5] = ((unsigned int)(canRxMessage.data[4])<<8)+canRxMessage.data[5];
temp[n][5] = ((unsigned int)(canRxMessage.data[6])<<8)+canRxMessage.data[7];
}
else if(canRxMessage.id == (0x004|(n+1)<<8))
{
freq[n][6] = ((unsigned int)(canRxMessage.data[0])<<8)+canRxMessage.data[1];
temp[n][6] = ((unsigned int)(canRxMessage.data[2])<<8)+canRxMessage.data[3];
freq[n][7] = ((unsigned int)(canRxMessage.data[4])<<8)+canRxMessage.data[5];
temp[n][7] = ((unsigned int)(canRxMessage.data[6])<<8)+canRxMessage.data[7];
}
}
}
}
CAN_FLAG=1;
}//
CLRWDT
error=0;
error+=s_measure((unsigned char*) &humi_val_i,&checksum,HUMI);
error+=s_measure((unsigned char*) &temp_val_i,&checksum,TEMP);
if(error!=0)
s_connectionreset();
else
{
error=0;
}
send_data[0]='S';
send_data[1]=1;
send_data[2]=2;
send_data[3] = 3;
send_data[4] =4;
send_data[5] = 5;
send_data[6] = 6;
send_data[7] = MAIN_ID;
s=8;
for(m=0;m<BOARD_NUM;m++)
{
send_data[s]=m;s++;
for(n=0;n<8;n++)
{
send_data[s] = freq[m][n]>>8;s++;
send_data[s] = freq[m][n];s++;
send_data[s] = temp[m][n]>>8;s++;
send_data[s] = temp[m][n];s++;
}
}
send_data[s] = 0x00;s++;
send_data[s] = 0x00;s++;
send_data[s] = humi_val_i>>8;s++;
send_data[s] = humi_val_i;s++;
send_data[s] = temp_val_i>>8;s++;
send_data[s] =temp_val_i;s++;
send_data[s]= 'E';s++;
if(flag_ascii_or_bin == 'a')
{
for(p = 0;p < s;p ++)
{
sprintf(&send_ascii[p * 2],"%02x",send_data[p]);
}
}
if(flag_ascii_or_bin == 'a')
{
if(uart2_enable ==1)
{
UART2_Send(send_ascii,s * 2);
}
if(uart1_enable ==1)
{
UART1_Send(send_ascii,s * 2);
}
}
else if(flag_ascii_or_bin == 'b')
{
if(uart2_enable ==1)
{
UART2_Send(send_data,s);
}
if(uart1_enable ==1)
{
UART1_Send(send_data,s);
}
}
STAT=0;
COMM = ~COMM;
work_enable = 0;
uart2_enable =0;
uart1_enable =0;
}
}
return 0;
}