This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc_s.c
463 lines (388 loc) · 16 KB
/
adc_s.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
// $Id: adc_s.c,v 1.13 2004-05-13 14:45:45 peter Exp $
#include <msp430x14x.h>
//òèïû ïåðåìåííûõ
#include "type_def.h"
#ifdef STEND
#include "uart_s.h"
#endif // STEND
#include "global.h"
// "inline" ôóíêöèè äëÿ 149 ïðîöåññîðà
#include "m149.h"
extern u16 temp_hold;
extern u16 timer_sum_sleep;
extern u16 timer_sum_int;
extern u16 timer_hold;
extern u16 timer_sum;
extern u16 sleep;
extern u16 timer_sum_display;
extern u16 timer_sum_serial;
extern u16 timer_sum_adc;
extern u16 timer_sum_stat;
extern u16 why_job;
extern u16 jitter_adc;
extern u16 jitter_adc_max;
extern u16 jitter_pusk;
extern u16 stop_transmit;
extern u16 analog_on;
extern u16 temperature;
//unsigned int results0[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//unsigned int results1[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//unsigned int results2[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//unsigned int results3[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//unsigned int results4[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//unsigned int results5[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//unsigned int results6[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
unsigned int results[ADC_FIFO_RCV_LEN];
unsigned int one_count0[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
unsigned int one_count1[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
unsigned int multi_count0[NUM_MULTICHANNEL][ADC_FIFO_RCV_LEN];
unsigned int multi_count1[NUM_MULTICHANNEL][ADC_FIFO_RCV_LEN];
unsigned int current_level;
unsigned int what_doing;
unsigned int timer1;
extern int error_adc;
extern int end_adc_conversion;
u16 chanel;
u16 chanel_convert;
extern u16 dac[NUM_CHANEL];
extern int rotate_channel;
extern int first_channel;
extern volatile unsigned int asp_trn_fifo_start; /* serial transmit buffer start index */
extern unsigned int asp_trn_fifo_end; /* serial transmit flash buffer end index */
extern unsigned int adc_rcv_fifo_start; /* ADC receive buffer start index */
extern volatile unsigned int adc_rcv_fifo_end; /* ADC receive buffer end index */
interrupt[ADC_VECTOR] void ADC12ISR (void)
{
//int p=( & (ADC_FIFO_RCV_LEN-1))<<3;
int sh;
unsigned int sum,sum1;
u16 savecctl1,savecctl2,savectl,saveie1,saveadcie;
//ïðèõîäèòñÿ ñîêðàùàòü HOLD_TIME_IRQ()
//ïðèõîäèòñÿ ñîêðàùàòü jitter_adc=CCR1-TAR;
//ïðèõîäèòñÿ ñîêðàùàòü if (jitter_adc_max<jitter_adc) jitter_adc_max=jitter_adc;
savectl=TACTL;
savecctl1=TACCTL1;
savecctl2=TACCTL2;
saveadcie=ADC12IE;
TACTL&= ~(TAIE);
TACCTL1&= ~(CCIE); //çàïðåùàåì ïðåðûâàíèÿ òàéìåðà
TACCTL2&= ~(CCIE); //çàïðåùàåì ïðåðûâàíèÿ òàéìåðà
ADC12IE=0; //çàïðåùàåì ïðåðûâàíèå ÀÖÏ
#ifdef STEND
saveie1=IE1;
IE1 &= ~UTXIE0; // òî çàïðåùàåì ïðåðûâ. ïåðåäà÷è
#endif //STEND
enable_int_no_interrupt(); //ðàçðåøàåì ãëîáàëüíûå ïðåðûâàíèÿ
// jitter_adc_max++;
// jitter_adc=TAR;
// if (jitter_pusk>jitter_adc) jitter_adc=jitter_pusk-jitter_adc;
// else jitter_adc=jitter_adc-jitter_pusk;
// if (jitter_adc_max<jitter_adc) jitter_adc_max=jitter_adc;
if (temperature==1){
sum=ADC12MEM0;
sum+=ADC12MEM1;
sum+=ADC12MEM2;
sum+=ADC12MEM3;
sum+=ADC12MEM4;
sum+=ADC12MEM5;
sum+=ADC12MEM6;
sum+=ADC12MEM7;
sum+=ADC12MEM8;
sum+=ADC12MEM9;
sum+=ADC12MEM10;
sum+=ADC12MEM11;
sum+=ADC12MEM12;
sum+=ADC12MEM13;
sum+=ADC12MEM14;
sum+=ADC12MEM15;
sh=(adc_rcv_fifo_end & (ADC_FIFO_RCV_LEN-1));
multi_count0[0][sh]=sum;
results[sh]=0x4000;
if (stop_transmit==0)
adc_rcv_fifo_end++;
ADC12CTL0 |= ADC12SC; // Start conversion
_BIC_SR_IRQ(CPUOFF); // Clear LPM0, SET BREAKPOINT HERE
//ïðèõîäèòñÿ ñîêðàùàòü SUM_TIME_IRQ();
}
else
if (chanel_convert&0x40){
sh=(adc_rcv_fifo_end & (ADC_FIFO_RCV_LEN-1));
sum=ADC12MEM0;
if (rotate_channel==0)
current_level=sum;
sum+=ADC12MEM2;
sum+=ADC12MEM4;
sum+=ADC12MEM6;
sum+=ADC12MEM8;
sum+=ADC12MEM10;
sum+=ADC12MEM12;
sum+=ADC12MEM14;
sum1=ADC12MEM1;
sum1+=ADC12MEM3;
sum1+=ADC12MEM5;
sum1+=ADC12MEM7;
sum1+=ADC12MEM9;
sum1+=ADC12MEM11;
sum1+=ADC12MEM13;
sum1+=ADC12MEM15;
if (rotate_channel<=NUM_MULTICHANNEL){
multi_count0[rotate_channel][sh]=sum;
multi_count1[rotate_channel][sh]=sum1;
}
else rotate_channel=0; ///îøèÁÎ×ÍÀß ÑÈÒÓÀÖÈß
rotate_channel++;
// if (rotate_channel==(NUM_MULTICHANNEL)){ //äëÿ ñëåäóþùåãî öèêëà óñòàíàâëèâàåì òåìïåðàòóðó
// set_adc_temperature(); //òåìïåðàòóðà áóäåò ñóììîé sum è sum1
// SUM_TIME_IRQ_NOSLEEP();
// }
// else
if (rotate_channel==(NUM_MULTICHANNEL)){ //äëÿ ñëåäóþùåãî öèêëà óñòàíàâëèâàåì öèêë ñ íà÷àëà
// è âûñòàâëÿåì äàííûå äëÿ çàíåñåíèÿ â î÷åðåäü ïåðåäà÷è
results[sh]=0x8000|(first_channel&0x1F);
if (stop_transmit==0)
adc_rcv_fifo_end++;
rotate_channel=0;
//çäåñü áûëà óñòàíîâêà ÀÖÏ - ïåðåíåñåíî â òàéìåðíîå ïðåðûâàíèå
_BIC_SR_IRQ(CPUOFF); // Clear LPM0, SET BREAKPOINT HERE
//ïðèõîäèòñÿ ñîêðàùàòü SUM_TIME_IRQ();
}
else { //îáû÷íûé öèêë äëÿ ñëåäóþùåãî êàíàëà
//âûõîäèì èç ñïÿ÷êè â "äâà ðàçà ÷àùå"
//çäåñü áûëà óñòàíîâêà ÀÖÏ - ïåðåíåñåíî â òàéìåðíîå ïðåðûâàíèå
if (rotate_channel==(NUM_MULTICHANNEL/2)){
_BIC_SR_IRQ(CPUOFF); // Clear LPM0, SET BREAKPOINT HERE
//ïðèõîäèòñÿ ñîêðàùàòü SUM_TIME_IRQ();
}
else{
//ïðèõîäèòñÿ ñîêðàùàòü SUM_TIME_IRQ_NOSLEEP();
}
}
}
else{ //"îáûêíîâåííûé" ïàðíûé ðåæèì
sh=(adc_rcv_fifo_end & (ADC_FIFO_RCV_LEN-1))*SIZE_OF_ADC_DUMP;
one_count0[sh] = ADC12MEM0;
current_level=one_count0[sh];
one_count1[sh++] = ADC12MEM1;
one_count0[sh] = ADC12MEM2;
one_count1[sh++] = ADC12MEM3;
one_count0[sh] = ADC12MEM4;
one_count1[sh++] = ADC12MEM5;
one_count0[sh] = ADC12MEM6;
one_count1[sh++] = ADC12MEM7;
one_count0[sh] = ADC12MEM8;
one_count1[sh++] = ADC12MEM9;
one_count0[sh] = ADC12MEM10;
one_count1[sh++] = ADC12MEM11;
one_count0[sh] = ADC12MEM12;
one_count1[sh++] = ADC12MEM13;
one_count0[sh] = ADC12MEM14;
one_count1[sh++] = ADC12MEM15;
results[adc_rcv_fifo_end & (ADC_FIFO_RCV_LEN-1)]=chanel_convert&0x1F;
if (stop_transmit==0)
adc_rcv_fifo_end++;
ADC12CTL0 |= ADC12SC; // Start conversion
_BIC_SR_IRQ(CPUOFF); // Clear LPM0
//ïðèõîäèòñÿ ñîêðàùàòü SUM_TIME_IRQ();
}
disable_int_no_interrupt(); //çàïðåùàåì ãëîáàëüíûå ïðåðûâàíèÿ
if (savectl&TAIE)
TACTL|=TAIE;
if (CCIE&savecctl1)
TACCTL1|=CCIE; //âîññòàíàâëèâàåì ðåãèñòð òàéìåðà
if (CCIE&savecctl2)
TACCTL2|=CCIE; //âîññòàíàâëèâàåì ðåãèñòð òàéìåðà
#ifdef STEND
if (saveie1&UTXIE0)
IE1 |= UTXIE0; // ïðåðûâ. ïåðåäà÷è
#endif //STEND
ADC12IE=saveadcie;
end_adc_conversion=1;
}
//äëÿ òåìïåðàòóðíîãî ñåíñîðà sample period áîëåå 30 mkS
void init_adc(void){
//ADC12ON - ADC12 On
//REFON - Reference generator
//MSC - Multiple sample and convertion
//SHT0_xx - Sample-and-hold time. These bits define the number
// of ADC12CLK cycles in the sampling period for
// registers ADC12MEM0 to ADC12MEM7
//SHT1_xx - Sample-and-hold time. These bits define the number
// of ADC12CLK cycles in the sampling period for
// registers ADC12MEM8 to ADC12MEM15
ADC12CTL0 = ADC12ON+REFON+MSC+SHT0_6+SHT1_6; // Turn on ADC12, set sampling time
//ADC12CLK=3 686 400
//SHT0_8 t_sample=4*ADC12CLK*64=14400 æ 0.00006944
//REF2_5V
//ADC12 SSELx Bits 4-3
// ADC12 clock source select
// 00 ADC12OSC
// 01 ACLK
// 10 MCLK
// 11 SMCLK -ADC12SSEL_3
//SHS bits 10-11 Source select for the sample-input signal.
//0: Control bit ADC12SC is selected.
//1: Timer_A.OUT1
//2: Timer_B.OUT0
//3: Timer_B.OUT1
ADC12CTL1 = ADC12SSEL_3+ADC12DIV_1+SHP+CONSEQ_1+SHS_1; // Use sampling timer, single sequence
// ADC12DIV_1 - ¤¥«¥¨¥ 2
//SREFx Bits 6-4 Select reference
// 000 VR+ = A VCC and VR- = A VSS - ®â 0 ¤® ¯¨â ¨ï
// 001 VR+ = V REF+ and VR- = A VSS - ®â 0 ¤® ¢ãâà. ®¯®à.
// 010 VR+ = Ve REF+ and VR- = A VSS - ®â 0 ¤® ¢¥è. ®¯®à.
// 011 VR+ = Ve REF+ and VR- = A VSS ???
// 100 VR+ = A VCC and VR- = V REF- / Ve REF-
// 101 VR+ = V REF+ and VR- = V REF- / Ve REF-
// 110 VR+ = Ve REF+ and VR- = V REF- / Ve REF-
// 111 VR+ = Ve REF+ and VR- = V REF- / Ve REF-
ADC12MCTL0 = INCH_1+SREF_2; // ref+=Ve REF, channel = A0
ADC12MCTL1 = INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL2 = INCH_1+SREF_2; // ref+=AVcc, channel = A2
ADC12MCTL3 = INCH_1+SREF_2; // ref+=AVcc, channel = A3,
ADC12MCTL4 = INCH_1+SREF_2; // ref+=AVcc, channel = A3,
ADC12MCTL5 = INCH_1+SREF_2; // ref+=AVcc, channel = A3,
ADC12MCTL6 = INCH_1+SREF_2; // ref+=AVcc, channel = A3,
ADC12MCTL7 = INCH_1+SREF_2; // ref+=AVcc, channel = A3,
ADC12MCTL8 = INCH_10+SREF_1+EOS; // ref+=AVcc, channel = A10, end seq.
ADC12IE = 1<<8; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
error_adc=0;
end_adc_conversion=0;
adc_rcv_fifo_end=0;
adc_rcv_fifo_start=0;
chanel=0x0;
chanel_convert=0xFF;
// dac[0]=0x800;
}
void off_adc(void){
int z;
ADC12CTL0=0;
ADC12CTL0=0;
z=ADC12MEM15;
}
int counter;
u16 work_with_adc_put(void){
if (counter++>=1000){
counter=0;
// put_packet_type4();
}
if (results[adc_rcv_fifo_start& (ADC_FIFO_RCV_LEN-1)]&0x8000){
if (put_packet_typeA(adc_rcv_fifo_start& (ADC_FIFO_RCV_LEN-1))){
adc_rcv_fifo_start++;
return 1;
}
}
else
if (results[adc_rcv_fifo_start& (ADC_FIFO_RCV_LEN-1)]&0x4000){
if (put_packet_type19(adc_rcv_fifo_start& (ADC_FIFO_RCV_LEN-1))){
adc_rcv_fifo_start++;
return 1;
}
}
else
if (put_packet_type7(adc_rcv_fifo_start& (ADC_FIFO_RCV_LEN-1))){
adc_rcv_fifo_start++;
return 1;
}
return 0;
}
void set_adc_temperature(void){
ADC12CTL0 &= ~ENC; // Enable conversions
ADC12CTL0 = ADC12ON+REFON+MSC+SHT0_15+SHT1_15; // Turn on ADC12, set sampling time
ADC12MCTL0 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL1 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL2 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL3 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL4 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL5 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL6 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL7 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL8 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL9 = INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL10= INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL11= INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL12= INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL13= INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL14= INCH_10+SREF_1; // ref+=AVcc, channel = A10, end seq.
ADC12MCTL15= INCH_10+SREF_1+EOS; // ref+=AVcc, channel = A10, end seq.
ADC12IE = 1<<15; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
}
void set_adc(int ch,char b){
ADC12CTL0 &= ~ENC; // Enable conversions
ADC12CTL0 = ADC12ON+REFON+MSC+SHT0_6+SHT1_6; // Turn on ADC12, set sampling time
set_dac(dac[ch&((NUM_CHANEL>>1)-1)]); //
if (analog_on){
P3OUT&=~(BIT0|BIT1|BIT2|BIT3);
if (ch&0x01) P3OUT|=BIT0;
if (ch&0x08) P3OUT|=BIT2;
if (ch&0x10) P3OUT|=BIT1;
if ((ch&0x20)==0) P3OUT|=BIT3;
}
switch((ch>>1)&0x03){
case 0:
ADC12MCTL1 = INCH_6+SREF_2;
ADC12MCTL3 = INCH_6+SREF_2;
ADC12MCTL5 = INCH_6+SREF_2;
ADC12MCTL7 = INCH_6+SREF_2;
ADC12MCTL9 = INCH_6+SREF_2;
ADC12MCTL11= INCH_6+SREF_2;
ADC12MCTL13= INCH_6+SREF_2;
ADC12MCTL15= INCH_6+SREF_2+EOS;
break;
case 1:
ADC12MCTL1 = INCH_5+SREF_2;
ADC12MCTL3 = INCH_5+SREF_2;
ADC12MCTL5 = INCH_5+SREF_2;
ADC12MCTL7 = INCH_5+SREF_2;
ADC12MCTL9 = INCH_5+SREF_2;
ADC12MCTL11= INCH_5+SREF_2;
ADC12MCTL13= INCH_5+SREF_2;
ADC12MCTL15= INCH_5+SREF_2+EOS;
break;
case 2:
ADC12MCTL1 = INCH_4+SREF_2;
ADC12MCTL3 = INCH_4+SREF_2;
ADC12MCTL5 = INCH_4+SREF_2;
ADC12MCTL7 = INCH_4+SREF_2;
ADC12MCTL9 = INCH_4+SREF_2;
ADC12MCTL11= INCH_4+SREF_2;
ADC12MCTL13= INCH_4+SREF_2;
ADC12MCTL15= INCH_4+SREF_2+EOS;
break;
case 3:
ADC12MCTL1 = INCH_3+SREF_2;
ADC12MCTL3 = INCH_3+SREF_2;
ADC12MCTL5 = INCH_3+SREF_2;
ADC12MCTL7 = INCH_3+SREF_2;
ADC12MCTL9 = INCH_3+SREF_2;
ADC12MCTL11= INCH_3+SREF_2;
ADC12MCTL13= INCH_3+SREF_2;
ADC12MCTL15= INCH_3+SREF_2+EOS;
break;
}//ᢨâç
if (b)
if (ch&0x80){ //१¥à¢ë© ª «
ADC12MCTL0 = INCH_1+SREF_2; // ref+=Ve REF, channel = A1
ADC12MCTL2 = INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL4 = INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL6 = INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL8 = INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL10= INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL12= INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
ADC12MCTL14= INCH_1+SREF_2; // ref+=Ve REF+, channel = A1
}
else{// ®á®¢®© ª «
ADC12MCTL0 = INCH_7+SREF_2; // ref+=Ve REF, channel = A7
ADC12MCTL2 = INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
ADC12MCTL4 = INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
ADC12MCTL6 = INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
ADC12MCTL8 = INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
ADC12MCTL10= INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
ADC12MCTL12= INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
ADC12MCTL14= INCH_7+SREF_2; // ref+=Ve REF+, channel = A7
}
ADC12IE = 1<<15; // Enable ADC12IFG.3
// ADC12IE = 1<<7; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
}