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
/
msp_main.c
524 lines (442 loc) · 15.8 KB
/
msp_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
//********************************************************
// $Id: msp_main.c,v 1.30 2004-05-13 14:45:46 peter Exp $
//********************************************************
//#include <msp430x11x1.h>
//#include <msp430x13x.h>
#include <msp430x14x.h>
//â¨¯ë ¯¥à¥¬¥ëå
#include "type_def.h"
#ifdef STEND
#include "uart_s.h"
#endif // STEND
// ìîäóëü äëÿ ðàáîòû ñ ïàêåòàìè
#include "uart_p.h"
#include "global.h"
#include "change.log"
static const char* rev=REVISION;
u16 temp_hold;
u16 timer_hold;
u16 timer_sum;
u16 timer_sum_int;
u16 timer_sum_sleep;
u16 sleep;
u16 timer_sum_display;
u16 timer_sum_serial;
u16 timer_sum_adc;
u16 timer_sum_stat;
u16 timer_diff_min;
u16 timer_diff_max;
u16 why_job;
extern u16 analog_on;
extern u16 temperature;
extern u16 stop_adc;
extern u16 send_correction_temperature;
extern unsigned int what_doing;
extern unsigned int stat_rcv_fifo_start; /* stat receive buffer start index */
extern volatile unsigned int stat_rcv_fifo_end; /* stat receive buffer end index */
extern unsigned int stat1_rcv_fifo_start; /* stat receive buffer start index */
extern volatile unsigned int stat1_rcv_fifo_end; /* stat receive buffer end index */
#define power_good() 1
// ÀÖÏ
extern int end_adc_conversion;
//extern unsigned int results[5]; // Needs to be global in this example
extern int error_adc;
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 */
//òàéìåð
extern int change_to_mode;
// 0 - ACLK 1 - SMCLK
extern int mode_timer;
//ýòî âðåìÿ ïî ãðèíâè÷ó
extern time_in GlobalTime;
//åñëè íå 0, òî çàïóñêàåìÿ íà ïîëíîé ñêîðîñòè
//extern int run_full_speed;
//ïåðåêëþ÷àåìñÿ íà ñêîðîñòíîé ðåæèì òàéìåðà
extern int switch_speed_timer;
// äèñïëåé
extern int mode_display;
extern int second_point;
extern int invert;
extern int symbl[4];
extern int update_display;
//ýòî âðåìÿ â ôîðìàòå long äëÿ ïîêàçà íà èíäèêàòîð
extern time_in time_to_show;
#ifdef DEBUG_SERIAL
extern u16 packet_in_fifo;
extern u16 fifo_trn_depth;
#endif //DEBUG_SERIAL
extern u16 received_packed;
#define ACLK_GO 250 //êîëè÷åñòâî äîñòàòî÷íûõ ïîïûòîê çàïóñêà
//âîçâðàùàåì íå íîëü, åñëè ÷àñîâîé êâàðö ðàáîòàåò
int test_run_LFXT1CLK(void){
int c=500; //îáùåå êîëè÷åñòâî ïîïûòîê çàïóñêà ACLK
int t=0; //ñ÷åò÷èê ïîïûòîê "óäà÷íîãî áåãà"
int last_att=9;
int i;
unsigned int to_compare,to_compare1;
while(c){
//çàõâàòûâàåì çíà÷åíèå òàéìåðà
to_compare=TAR; //TIMER_A_COUNTER;
//äåëàåì ïàóçó
for (i = 100; i>0; i--); // Delay
// \ 000C 3C406400 MOV #100,R12 //2
// \ 0010 ?0004:
// \ 0010 1C93 CMP #1,R12 //2
// \ 0012 0238 JL (?0003) //4
// \ 0014 3C53 ADD #-1,R12 //2
// 60 for (i = 100; i>0; i--); //
// \ 0016 FC3F JMP (?0004) //4
// ïîäñ÷åò ÷èñëà òàêòîâ ñäåëàí "ýìïèðè÷åñêè" 22.05.2003
// \ 0018 ?0003: //=2+(1+2+1+2)*100=602
// îò âíóòðåííåãî ãåíåðàòîðà âðåìÿ âûïîëíåíèÿ:
// 602/610.000=0,000986885
// 602/750.000=0,000802667
// 602/900.000=0,000668889
// íà ýìóëÿòîðå ýòî çàäåðæêà ïîêàçûâàåò 25-26 òàêòîâ 32768
// 25 òàêòîâ 32768 = 25/32768=0,000762939453125 ñåê
// 26 òàêòîâ 32768 = 26/32768=0,000793457 ñåê
// ìèíèìàëüíîå çíà÷åíèå òàéìåðà, êîòîðîå, âåðîÿòíî, ìîæíî ïîëó÷èòü
// 0,000668889*32768=21,91815475
//çàõâàòûâàåì çíà÷åíèå òàéìåðà îïÿòü
to_compare1=TAR; //TIMER_A_COUNTER;
//ðàññ÷èòûâàåì ðàçíèöó
if (to_compare1>to_compare)
to_compare=to_compare1-to_compare;
else
to_compare=0x2000+to_compare1-to_compare;
//åñëè òàéìåð áåæèò, òî ñ÷èòàåì, ÷òî êâàðö çàïóñòèëñÿ
// if (to_compare>16) t++; else t=0;
if (to_compare>2) t++; else t=0;
//ñáðîñ WatchDog
if (t>ACLK_GO) break; //ñ÷èòàåì, ÷òî ÷àñîâîé êâàðö çàïóñòèëñÿ
CLEAR_DOG();
if ((t>>5)!=last_att){
last_att=t>>5;
symbl[3]=0x0A; //'A'
symbl[2]=0x0C; //'C'
symbl[1]=0x15; //'L'
symbl[0]=last_att; //"ïñåâäîíîìåð" ïîïûòêè âûâîäèì íà ýðàí
update_diplay();
}
if ((c&0x3F)==0) show_display();
c--;
}
return c;
}
//Âêëþ÷àåì îñíîâíîé ÷àñòîòîé ÷àñîâîé òàéìåð
// åñëè ÷àñîâîé òàéìåð çàïóñòèëñÿ, òî:
// - îòêëþ÷àåì XT2 (åñëè mode&0x01)
// - îòêëþ÷àåì DC Generator (SCG0=1 åñëè mode&0x02)
// - îòêëþ÷àåì SMCLK (SCG1=1 åñëè mode&0x04)
// è âîçâðàùàåì 1 (True) èíà÷å ïðîäîëæàåì ðàáîòàòü íà âíóòðåííåì ãåíåðàòîðå
// è âîçâðàùàåì 0 (False)
int run_LFXT1CLK( int mode){
//XTS = 0: The low-frequency oscillator is selected.
if (mode&0x04) _BIS_SR(SCG1);
BCSCTL1&=~XTS;
// äåëèòåëü (ACLK/1)
// DIVA = 0: 1
// DIVA = 1: 2
// DIVA = 2: 4
// DIVA = 3: 8
// åñëè çàïðîøåíî îòêëþ÷åíèå XT2 êâàðöà - îòêëþ÷àåì
// XT2Off = 1: the oscillator is off if it is not used for MCLK or SMCLK.
BCSCTL1 = (BCSCTL1& (~(DIVA0|DIVA1))) | (mode&0x01 ? XT2OFF:0);
_BIC_SR(OSCOFF); // Âêëþ÷àåì ÷àñîâîé êâàðö
//ïðîâåðÿåì çàïóñê ÷àñîâîãî êâàðöà
if (!test_run_LFXT1CLK()){ //÷àñîâîé íå çàïóñòèëñÿ
return 0;
}
// Bit0, DCOR: The DCOR bit selects the resistor for injecting current into the
// dc generator. Based on this current, the oscillator operates if
// activated.
// DCOR = 0: Internal resistor on, the oscillator can operate. The failsafe
// mode is on.
// DCOR = 1: Internal resistor off, the current must be injected
// externally if the DCO output drives any clock using
// the DCOCLK.
// Bit1, Bit2: The selected source for SMCLK is divided by:
// DIVS.1 .. DIVS.0 DIVS = 0: 1
// DIVS = 1: 2
// DIVS = 2: 4
// DIVS = 3: 8
// Bit3, SELS: Selects the source for generating SMCLK:
// SELS = 0: Use the DCOCLK
// SELS = 1: Use the XT2CLK signal (in three-oscillator systems)
// or
// LFXT1CLK signal (in two-oscillator systems)
// Bit4, Bit5: The selected source for MCLK is divided by:
// DIVM.0 .. DIVM.1 DIVM = 0: 1
// DIVM = 1: 2
// DIVM = 2: 4
// DIVM = 3: 8
// Bit6, Bit7: Selects the source for generating MCLK:
// SELM.0 .. SELM.1 SELM = 0: Use the DCOCLK
// SELM = 1: Use the DCOCLK
// SELM = 2: Use the XT2CLK (x13x and x14x devices) or
// Use the LFXT1CLK (x11xx and x12xx devices)
// SELM = 3: Use the LFXT1CLK
// âêëþ÷àåì èñòî÷íèê îñíîâíîé ÷àñòîòû - LFXT1CLK
BCSCTL2=(BCSCTL2&(~(DIVM0|DIVM1|DIVS0|DIVS1)))|SELM0|SELM1|SELS;
if (mode&0x02) _BIS_SR(SCG0);
return 1;
}
void set_pin_directions(void){
#ifdef CABLE
P1DIR = 0xFF; // All P1.x outputs
P1OUT = 0; // All P1.x reset
P2DIR = 0xFF; // All P2.x outputs
P2OUT = 0; // All P2.x reset
P3SEL |= 0xC0; // P3.6,7 = USART1 option select
// P3DIR |= 0x20; // P3.6 = output direction
P3DIR = 0xFF; // All P3.x outputs
P3OUT = 0; // All P3.x reset
// P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
// P3DIR |= 0x10; // P3.4 output direction
P4DIR = 0xFF; // All P4.x outputs
P4OUT = 0; // All P4.x reset
P5DIR = 0xFF; // All P5.x outputs
P5OUT = 0; // All P5.x reset
// P5SEL = 0x0E; SPI1
P5SEL = 0x70; // MCLK, SMCLK,ACLK íà âûâîä
P6DIR = 0xFF; // All P6.x outputs
P6OUT = 0; // All P6.x reset
P6SEL = 0x0F; // Enable A/D channel inputs
#endif //CABLE
#ifdef STEND
P1DIR = BIT0+BIT1+BIT5+BIT6+BIT7;
P1OUT = 0; // All P1.x reset
P2DIR = BIT0+BIT1+BIT3+BIT4+BIT5+BIT6+BIT7;
P2OUT = 0; // All P2.x reset
P3SEL |= BIT4+BIT5; //USART0 select
P3DIR = 0xFF; // All P3.x outputs
P3OUT = 0; // All P3.x reset
P4DIR = BIT0+BIT1+BIT2+BIT3+BIT6+BIT7;
P4OUT = 0; // All P4.x reset
P5DIR = 0xFF; // All P5.x outputs
P5OUT = 0; // All P5.x reset
P5SEL = 0x70; // MCLK, SMCLK,ACLK íà âûâîä
P6DIR = 0x00; // All P6.x inputs
P6OUT = 0; // All P6.x reset
P6SEL = 0xFF; // Enable A/D channel inputs
#endif //STEND
analog_on=1;
}
int current_speed;
int run_xt2(void){
int i;
int count=10;
BCSCTL1 &= ~XT2OFF; // XT2 = HF XTAL
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
count--;
if (count==0) break;
}
while ((IFG1 & OFIFG) != 0); // OSCFault flag still set?
if ((IFG1 & OFIFG)==0){
BCSCTL2 = (BCSCTL2&(~SELM0))|SELM1; // MCLK = XT2 (safe) |DIVM0 äåëåíèå ïîïîëàì óáðàë
current_speed=1;
}
return count;
}
void switch_xt2(void){
switch_speed_timer=1;
}
long time_to_change;
int mode_work;
void main(void)
{
int i;
// îñòàíàâëèâàåì watchdog
WDTCTL=WDTPW|WDTHOLD; // Stop WDT
//ñáðîñ WatchDog
CLEAR_DOG();
// êîíôèãóðèðóåì íîãè ââîäà âûâîäà
set_pin_directions();
// run_full_speed=0;
stop_adc=0; //ÀÖÏ íå îñòàíàâëèâàëè
temperature=0; //ïðåîáðàçîâàíèå òåìïåðàòóðû âûêëþ÷åíî
current_speed=0;
switch_speed_timer=0;
mode_timer=0;
mode_display=0;
GlobalTime=0;
mode_work=0;
second_point=0;
invert=0;
time_to_show=1;
change_to_mode=0;
time_to_change=0;
stat_rcv_fifo_start=0;
stat_rcv_fifo_end=0;
what_doing=0;
if (IFG1&0x01){ //ñðàáîòàë WATCHDOG
//òî ñ÷èòàåì ïîêà ýòî àâàðèéíûì ðåæèìîì
//âûêëþ÷àåì íîãè îò áëîêà USART1
P3SEL &= ~0xC0; // P3.6,7 = USART1 option select
symbl[3]=0x0D; //'D'
symbl[2]=0x0D; //'D'
symbl[1]=0x0D; //'D'
symbl[0]=0x10; //' '
update_diplay();
while(1){
//ñáðîñ WatchDog
CLEAR_DOG();
//è íà÷èíàåì ýòîé íîãîé "ìèãàòü"
for (i = 1000; i>0; i--); // Delay
P3OUT^=BIT6;
//ñáðîñ WatchDog
CLEAR_DOG();
for (i = 40; i>0; i--); // Delay
P3OUT^=BIT6;
show_display();
}
}
//çàïóñêàåì òàéìåð A è ÷àñû îò íåãî (ACLK)
init_timer_a();
//ïåðåõîäèì íà ðàáîòó îò ÷àñîâîãî êâàðöà
//åñëè ÷àñîâîé êâàðö ïî êàêèì-òî ïðè÷èíàì íå çàïóñòèëñÿ
if (!run_LFXT1CLK(0x0)){
//òî ñ÷èòàåì ïîêà ýòî àâàðèéíûì ðåæèìîì
//âûêëþ÷àåì íîãè îò áëîêà USART1
P3SEL &= ~0xC0; // P3.6,7 = USART1 option select
symbl[3]=0x0A; //'A'
symbl[2]=0x0C; //'C'
symbl[1]=0x15; //'L'
symbl[0]=0x10; //' '
update_diplay();
while(1){
//ñáðîñ WatchDog
CLEAR_DOG();
//è íà÷èíàåì ýòîé íîãîé "ìèãàòü"
for (i = 1000; i>0; i--); // Delay
//ñáðîñ WatchDog
CLEAR_DOG();
P3OUT^=BIT6;
for (i = 1000; i>0; i--); // Delay
P3OUT^=BIT6;
show_display();
}
}
// init_wdt();
/* Watchdog mode -> reset after expired time */
/* WDT is clocked by fACLK (assumed 32KHz) */
// #define WDT_ARST_1000 (WDTPW+WDTCNTCL+WDTSSEL)
/* 1000ms " */
WDTCTL = WDT_ARST_1000;
//ñáðîñ WatchDog
CLEAR_DOG();
_BIS_SR(SCG0);
//ïåðåõîäèì íà ðàáîòó îò âòîðîãî êâàðöà
if (current_speed==0 && power_good() ){
//åñëè âòîðîé êâàðö ïî êàêèì-òî ïðè÷èíàì íå çàïóñòèëñÿ
if (!run_xt2()){
//òî ñ÷èòàåì ïîêà ýòî àâàðèéíûì ðåæèìîì
//âûêëþ÷àåì íîãè îò áëîêà USART1
P3SEL &= ~0xC0; // P3.6,7 = USART1 option select
symbl[3]=0x0C; //'C'
symbl[2]=0x15; //'L'
symbl[1]=0x02; //'2'
symbl[0]=0x10; //' '
update_diplay();
while(1){
//ñáðîñ WatchDog
CLEAR_DOG();
//è íà÷èíàåì ýòîé íîãîé "ìèãàòü"
for (i = 1000; i>0; i--); // Delay
P3OUT^=BIT6;
show_display();
}
}
switch_xt2();
}
init_adc();
init_uart();
CCR2=TAR+0x100; //workaround ïðåðûâàíèÿ ÷àñîâîãî
_EINT(); // Enable interrupts
while(1)
{
#ifdef CABLE
P1OUT &= ~0x01; // Reset P1.0 LED off
#endif
SUM_TIME(DISPLAY_JOB,1);
_BIS_SR(CPUOFF+GIE);
// SUM_TIME(DISPLAY_JOB,0);
// _BIS_SR(GIE);
#ifdef CABLE
P1OUT |= 0x01; // Set P1.0 LED on
#endif
//-----------------
// ðàáîòà ñ "äèñïëååì"
//-----------------
if (update_display){
update_display=0;
tick_timer();
work_with_display();
}
SUM_TIME(ADC_JOB,0);
_BIS_SR(GIE);
//-----------------
// ðàáîòà ñ äàííûìè èç ÀÖÏ
//-----------------
#ifdef CABLE
P1OUT |= 0x01; // Set P1.0 LED on
#endif //
while((adc_rcv_fifo_start& (ADC_FIFO_RCV_LEN-1))!=(adc_rcv_fifo_end& (ADC_FIFO_RCV_LEN-1))){ //îáÿçàòåëüíî íàïèñàòü îöåíêó ïåðåïîëíåíèÿ
if (work_with_adc_put()==0) break;
}
if (send_correction_temperature>100) put_packet_type1A();
#ifdef CABLE
P1OUT &= ~0x01; // Reset P1.0 LED off
#endif
SUM_TIME(STAT_JOB,0);
_BIS_SR(GIE);
//-----------------
//-----------------
// ðàáîòà ñ äàííûìè ñòàòèñòèêè
//-----------------
#ifdef CABLE
P1OUT |= 0x01; // Set P1.0 LED on
#endif
while ((stat_rcv_fifo_start& (STAT_FIFO_RCV_LEN-1))!=(stat_rcv_fifo_end& (STAT_FIFO_RCV_LEN-1))){
if (put_packet_type5()==0) break;
}
while ((stat1_rcv_fifo_start& (STAT1_FIFO_RCV_LEN-1))!=(stat1_rcv_fifo_end& (STAT1_FIFO_RCV_LEN-1))){
if (put_packet_type6()==0) break;
}
#ifdef CABLE
P1OUT &= ~0x01; // Reset P1.0 LED off
#endif
SUM_TIME(SERIAL_JOB,0);
_BIS_SR(GIE);
//-----------------
//-----------------
// ðàáîòà ñ ïîñëåäîâàòåëüíûì ïîðòîì
//-----------------
#ifdef CABLE
P1OUT |= 0x01; // Set P1.0 LED on
#endif
while(received_packed){
work_with_serial_rec();
}
while(fifo_trn_depth<(SERIAL_FIFO_TRN_LEN/2)){
if (packet_in_fifo==0) break;
work_with_serial();
}
#ifdef CABLE
P1OUT &= ~0x01; // Reset P1.0 LED off
#endif
// SUM_TIME(0);
// _BIS_SR(CPUOFF+GIE);
//-----------------
}
}
interrupt[NMI_VECTOR] void nmi(void)
{
}
//ðàññ÷åò ïîòðåáëåíèÿ
//ADC
// 200 mkA íà äâà ìëàäøèõ ðàçðÿäà âî âðåìÿ ïðåîáðàçîâàíèÿ
// 0.5 mA (max 0.8) - âíóòðåííåå îïîðíîå