-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
479 lines (412 loc) · 8.57 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
#if defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#elif defined(__PIC24H__)
#include <p24hxxxx.h>
#endif
#include "timer.h"
#include "adc.h"
#include "sci.h"
#include "AD7176.h"
#include "stdio.h"
#include "collect.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
typedef union
{
struct
{
unsigned long AD_value;
};
struct
{
unsigned char AD_1;
unsigned char AD_2;
unsigned char AD_3;
unsigned char AD_4;
};
struct
{
unsigned int AD_11;
unsigned int AD_12;
};
} AD_type;
AD_type AD_buffer[4][4];
AD_type AD_value[4][4];
unsigned char flag_data=0,flag1=0;
unsigned char work_enable=0;
unsigned char capture_enable=0;
unsigned char MAIN_ID=17;
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';
volatile unsigned int Tick_1S=0;
unsigned char uart2_enable = 0;//串口使能位
unsigned char uart1_enable = 0;//网口使能位
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");}
}
}
unsigned int Tick_sys = 0;
void __attribute__((interrupt,no_auto_psv)) _T6Interrupt(void) // 1ms interrupt
{
IFS2bits.T6IF = 0;
Tick_sys++;
if(Tick_sys >= 1000)
{
Tick_sys = 0;
FAIL = ~FAIL;
WORK = FAIL;
Tick_1S ++;
}
if(Tick_sys % 123 == 1)
{
capture_enable = 1;
}
if(Tick_1S >= 1920) //32min
{
Tick_1S = 0;
Nrest=1;
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); //饿狗,让狗来重启
}
else if(Tick_1S == 1918)
{
Nrest=0;
}
else if(Tick_1S == 1860)
{
InitSCI();
work_enable = 1;
uart1_enable = 1;
uart2_enable = 1;
}
}
/******
网口中断
**********/
void __attribute__((interrupt,no_auto_psv)) _U1RXInterrupt(void)
{
IFS0bits.U1RXIF = 0;
unsigned char data[17];
unsigned int UART_Timeout = 0;
unsigned char i = 0;
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;
}//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';
}
}
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';
}
}
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;
}
int main()
{
int i;
int k,j;
int n,m,p;
int s=0;
int temp1;
unsigned int humi_val_i,temp_val_i;
unsigned char error,checksum;
unsigned char read_temp;
unsigned char adc_temp[3];
unsigned char test[7] = {0x55,0x55,0x55,'t','e','s','t'};
unsigned int wait;
unsigned int flag_break = 0;
CLRWDT
OSCCON = 0x2200;
SPI_Init();
DELAY(5000);
AD7176set();
InitTimer6(); //// Timer6 提供0.1s中断定时
StartTimer6();
InitSCI();
s_connectionreset();
TRISCbits.TRISC13 = 0;
TRISDbits.TRISD0 = 0;
TRISFbits.TRISF6 = 0;
TRISCbits.TRISC14 = 0;
TRISDbits.TRISD11 = 0;
AD1PCFGLbits.PCFG7 = 1;
AD2PCFGLbits.PCFG7 = 1;
Nrest=1;
WORK = 1;
FAIL = 1;
STAT = 0;
COMM = 0;
CLRWDT
//UART2_Send(test,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(capture_enable)
{
for(p = 0;p < 4;p ++)
{
for(i = 0;i < 4;i ++)
{
cs_low(i);
DELAY(100);
read_write_byte(0x10 + p);
read_write_byte(0x80);
read_write_byte(0x20 * p + 0x16);
cs_high(i);
DELAY(100);
}
DELAY(1000);
for(i = 0;i < 4;i ++)
{
cs_low(i);
DELAY(100);
read_write_byte(0x01);
read_write_byte(0x01);
read_write_byte(0x18);
DELAY(9000);
read_write_byte(0x44);
adc_temp[2]=read_write_byte(0x00);
adc_temp[1]=read_write_byte(0x00);
adc_temp[0]=read_write_byte(0x00);
flag_data=read_write_byte(0x00);
if(!(flag_data&0x80)) //有数据转化成功
{
flag_data = flag_data&0x03;////通道0 返回通道号
//if(adc_temp[0] != 0 && adc_temp[1] != 0 && adc_temp[2] != 0)
{
AD_buffer[i][flag_data].AD_3=adc_temp[2];
AD_buffer[i][flag_data].AD_2=adc_temp[1];
AD_buffer[i][flag_data].AD_1=adc_temp[0];
AD_buffer[i][flag_data].AD_4=0;
}
}
cs_high(i);
DELAY(100);
CLRWDT
}
for(i = 0;i < 4;i ++)
{
cs_low(i);
DELAY(100);
read_write_byte(0x10 + p);
read_write_byte(0x00);
read_write_byte(0x20 * p + 0x16);
cs_high(i);
DELAY(100);
}
DELAY(1000);
}
capture_enable = 0;
}
if(work_enable)
{
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;
/*for(i = 0;i < 4;i ++)
{
test[i] = check(i);
}
UART1_Send(test,4);*/
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(k = 0;k < 4;k ++)
{
send_data[s] = k;s++;
for(m = 0;m < 4;m ++)
{
send_data[s] = AD_buffer[k][m].AD_3;s++;
send_data[s] = AD_buffer[k][m].AD_2;s++;
send_data[s] = AD_buffer[k][m].AD_1;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(uart2_enable ==1)
{
UART2_Send(send_data,s);
}
uart2_enable = 0;
UART1_Send(send_data,s);
STAT = ~STAT;
COMM = ~COMM;
work_enable = 0;
Tick_1S = 0;
}
}
return 0;
}