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
/
main.c
467 lines (404 loc) · 12.7 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
#include <msp430x11x1.h>
#include "includes.h"
#define FOSC 2000000
#define FREQ 7000
#define TICKS 30 //êîëè÷åñòâî ïðåðûâàíèé òàéìåðà ïî ïåðåïîëíåíèþ/ñåê
#define FREQ_BEEP 2500 //÷àñòîòà çâóêà
#define DARK_0 1 //âðåìÿ, â òèêàõ, ãîðåíèÿ ñâåòîäèîäà
#define FLASH_0 (TICKS*2) //ïåðèîä ìèãàíèÿ ðåæèìà 0
#define DARK_1 1 //âðåìÿ, â òèêàõ, ãîðåíèÿ ñâåòîäèîäà
#define FLASH_1 (TICKS*2) //ïåðèîä ìèãàíèÿ ðåæèìà 0
#define DARK_2 1 //âðåìÿ, â òèêàõ, ãîðåíèÿ ñâåòîäèîäà
#define FLASH_2 (TICKS*2) //ïåðèîä ìèãàíèÿ ðåæèìà 0
#define NUMBER_OF_MODES 3
#define GO_TO_SLEEP (3*TICKS)
#define CONVERSION_SPEED 8
#define WAIT_CONV_TIME (1+1) //âñåãäà äîëæíî áûòü +1
unsigned char BitCnt;
unsigned int RXTXData;
unsigned int time_to_compare;
unsigned int hold_time;
// Conditions for 9600 Baud SW UART,
//#define Bitime_5 104 // ~ 0.5 bit length
//#define Bitime 208 // ~ 9615 baud
//#define Bitime_5 56 // ~ 0.5 bit length
//#define Bitime 112 // ~ 9615 baud
// Conditions for 9600 Baud SW UART, 2 Mhz
#define Bitime_5 101 // ~ 0.5 bit length
#define Bitime 202 // ~ 9615 baud
#define BUTTON_BIT BIT0
#define ESCAPE 0x7D
#define EOFPACKET 0x7E //êîä ïðèçíàêà êîíöà êàäðà
//íîìåð ïîðòà íàïðàâëåíèÿ, íà êîòîðîì ñòîèò RC-öåïî÷êà
#define PORT_DIR_CAPACITOR P2DIR
//íîìåð ïîðòà âûâîäà, íà êîòîðîì ñòîèò RC-öåïî÷êà
#define PORT_OUT_CAPACITOR P2OUT
//ïîðÿäêîâûé íîìåð ïèíà ïîðòà, íà êîòîðîì ñòîèò RC-öåïî÷êà
#define CAPACITOR_PIN BIT4
#define DIODE_PIN BIT3
#define PORT_DIR_REZISTOR P2DIR
#define PORT_OUT_REZISTOR P2OUT
#define REZISTOR_PIN BIT1
int divider;
volatile unsigned int c_beep;
volatile unsigned char button_pressed;
volatile unsigned char button_release;
volatile unsigned char go_sleep;
volatile unsigned char sleep_beeped;
volatile unsigned char mode_collect;
volatile unsigned char post_charge;
volatile unsigned int time_pressed;
volatile unsigned int counter_flash;
volatile unsigned int counter_dark;
volatile unsigned int value_convert;
volatile unsigned int counter2begin;
volatile unsigned char wait_end_convert;
volatile unsigned char value_placed;
volatile unsigned char in_sleep_mode;
volatile unsigned char knopka_otpushena;
volatile unsigned char pusk_komparator;
//1 - ãîòîâèìñÿ êî ñíó
/****************************************/
//âêëþ÷åíèå çàðÿäà âíåøíåãî êîíäåíñàòîðà
/****************************************/
#pragma inline=forced
inline void on_charge(void){
// CAPD&=~CAPACITOR_PIN; // ïîäêëþ÷àåì âõîäíîé áóôåð
// P2SEL&=~CAPACITOR_PIN; // ïîäêëþ÷àåì âõîäíîé áóôåð
// PORT_DIR_CAPACITOR|=CAPACITOR_PIN; // íàïðàâëåíèå íà âûõîä
// PORT_OUT_CAPACITOR|=CAPACITOR_PIN; // âûñîêèé óðîâåíü
//îòêëþ÷àåì (íà âñÿêèé ñëó÷àé) âûõîä p2.4 (êîíäåíñàòîð)
CAPD|=CAPACITOR_PIN; //îòêëþ÷àåì âõîäíîé áóôåð
PORT_DIR_CAPACITOR&=~CAPACITOR_PIN; // íàïðàâëåíèå íà âõîä
PORT_OUT_CAPACITOR|=CAPACITOR_PIN; // âûñîêèé óðîâåíü
PORT_DIR_REZISTOR|=REZISTOR_PIN; //âêëþ÷àåì ïèí ðåçèñòîðà íà âûõîä
PORT_OUT_REZISTOR|=REZISTOR_PIN; // âûñîêèé óðîâåíü
}
void init_timer_a(int freq,int delay){
P1SEL |= BIT2|BIT3;
divider=(FOSC/freq)>>1;
c_beep=(freq>>2)*delay;
CCTL1= OUTMOD_1 ; // set
CCTL2= OUTMOD_5 ; // reset
TACCR1=(divider)+TAR;
TACCR2=TACCR1;
CCTL1|=CCIE /* ðàçðåøàåì ïðåðûâàíèÿ */;
}
// Timer_A3 Interrupt Vector (TAIV) handler
#if __VER__ < 200
interrupt[TIMERA1_VECTOR] void Timer_A (void)
#else
#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A(void)
#endif
{
switch( TAIV )
{
case 2: // CCR1
{
CCTL1 ^= OUTMOD2; // Toggle timer pin set
CCTL2 ^= OUTMOD2; // Toggle timer
TACCR1+=divider;
TACCR2+=divider;
c_beep--;
if (c_beep==0){
CCTL1&=~CCIE /* çàïðåùàåì ïðåðûâàíèÿ */;
P1SEL&= ~(BIT2|BIT3);
}
}
break;
case 4:
break; // CCR2 not used
case 10:
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)
switch(post_charge){
case 0:
break;
case 1:
fast_charge();
post_charge++;
break;
case 2:
on_charge();
post_charge=0;
break;
default:
post_charge=0;
}
counter_flash++;
if (counter_dark){
if (--counter_dark==0){
P2OUT|=0x1;
P1OUT|=0x80;
}
}
if (in_sleep_mode==0){
counter2begin++;//æä¸ì íà÷àëà ïðåîáðàçîâàíèÿ
if (counter2begin>CONVERSION_SPEED){
counter2begin=0;
wait_end_convert=WAIT_CONV_TIME;
on_comparator_external();
pusk_komparator=1; //ïðè âûõîäå èç ïðåðûâàíèÿ çàïóñòèì âñ¸
}
if (wait_end_convert){
if (--wait_end_convert==0){ //ïðåîáðàçîâàíèå çàêîí÷èëîñü
on_charge();
post_charge=1;
value_convert=(time_to_compare-hold_time)>>2;
// if (value_convert>90) value_convert-=90; else value_convert=0;
value_convert=value_convert>255 ? 255:value_convert;
value_placed=1;
}
}
switch(mode_collect){
case 0:
if (counter_flash>FLASH_0){
P2OUT=(P2OUT&(0xFE));
counter_flash=0;
counter_dark=DARK_0;
}
break;
case 1:
if (counter_flash>FLASH_1){
P2OUT=(P2OUT&(0xFD));
counter_flash=0;
counter_dark=DARK_1;
}
break;
case 2:
if (counter_flash>FLASH_2){
P1OUT=(P1OUT&(0x7F));
counter_flash=0;
counter_dark=DARK_2;
}
break;
default:
init_timer_a(2000,18);
}
}
if ((P1IN&BUTTON_BIT)==0){
if (button_pressed<10)
button_pressed++;
time_pressed++; //óâåëè÷èâàåì âðåìÿ íàæàòèÿ êíîïêè
button_release=0;
}
else {
if (button_release<10)
button_release++;
}
if ((sleep_beeped==0) && (time_pressed>(GO_TO_SLEEP))){
init_timer_a(3000,8);
if (in_sleep_mode==2){ //ìû â ñïÿùåì ðåæèìå
in_sleep_mode=0;
sleep_beeped=1;
}
else{
sleep_beeped=1;
in_sleep_mode=1;
}
}
if (button_release>2){ //êíîïêó îòïóñòèëè
time_pressed=0;
sleep_beeped=0;
if (knopka_otpushena==0){
knopka_otpushena=1;
if (in_sleep_mode==0) mode_collect++;
if (mode_collect>=NUMBER_OF_MODES){
mode_collect=0;
}
if (in_sleep_mode) in_sleep_mode=2;
init_key();
}
}
break; // overflow not used
}
if (pusk_komparator){
pusk_komparator=0;
CACTL1&=~CAIFG;
CACTL1|=CAIE; //ðàçðåøàåì ïðåðûâàíèå îò êîìïàðàòîðà
/****************************************/
//âûêëþ÷åíèå çàðÿäà âíåøíåãî êîíäåíñàòîðà
/****************************************/
// off_charge();
//void off_charge(void){
PORT_DIR_REZISTOR|=REZISTOR_PIN; //âêëþ÷àåì ïèí ðåçèñòîðà íà âûõîä
P2SEL|=CAPACITOR_PIN; // îòêëþ÷àåì âõîäíîé áóôåð
PORT_DIR_CAPACITOR&=~CAPACITOR_PIN; // íàïðàâëåíèå íà âõîä
CAPD|=CAPACITOR_PIN; // îòêëþ÷àåì âõîäíîé áóôåð
PORT_OUT_REZISTOR&=~REZISTOR_PIN; // íèçêèé óðîâåíü
//}
hold_time=TAR;
time_to_compare=hold_time;
}
}
#define RXD 0x04 // RXD on P2.2
#define TXD 0x02 // TXD on P1.1
void init_soft_uart(void){
_BIS_SR(OSCOFF); // XTAL not used
CCTL0 = OUT; // TXD Idle as Mark
TACTL = TASSEL_2 + MC_2+TAIE; // SMCLK, continuous mode
P1SEL|= TXD; // P1.1/TA0 for TXD function
P1DIR|= TXD; // TXD output on P1
// P2SEL|= RXD; // P2.2/TA0 as RXD input
}
// Function Transmits Character from RXTXData Buffer
void TX_Byte (void)
{
BitCnt = 0xA; // Load Bit counter, 8data + ST/SP
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
RXTXData |= 0x100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
// CCTL0 = OUTMOD0+OUTMOD2 + CCIE; // TXD = mark = idle
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while ( CCTL0 & CCIE ); // Wait for TX completion
}
void init_key(void){
P1IES |= 0x01; // P2.0 Hi/lo edge
P1IFG &= ~BUTTON_BIT; // P2.0 IFG cleared
P1IE |= BUTTON_BIT; // P2.0 interrupt enabled
}
// Port 1 interrupt service routine
#if __VER__ < 200
interrupt[PORT1_VECTOR] void Port_1 (void)
#else
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
#endif
{
P1IE &= ~BUTTON_BIT; // P2.0 interrupt disabled
if (in_sleep_mode==0) init_timer_a(3000,8);
button_pressed=1;
time_pressed=0; //âðåìÿ íàæàòèÿ êíîïêè
knopka_otpushena=0;
}
void transmit(unsigned char transfer){
if (transfer==EOFPACKET||transfer==ESCAPE){
RXTXData=ESCAPE;
TX_Byte(); // TX Back RXed Byte Received
RXTXData=transfer^0x40;
TX_Byte(); // TX Back RXed Byte Received
}
else{
RXTXData=transfer;
TX_Byte(); // TX Back RXed Byte Received
}
}
void main(void)
{
volatile unsigned int y=0;
unsigned char transfer;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P2DIR |= 0x03; // Set P2.0, P2.1 to output direction
P1DIR |= BIT2|BIT3|BIT7;
P2DIR|=BIT2;
P2SEL|=BIT2;
P2OUT|=0x3;
P1OUT|=0x80;
P1OUT &= ~(BIT2|BIT3);
P1SEL |=BIT4;
P1DIR |=BIT4;
button_pressed=0;
button_release=0;
sleep_beeped=0;
time_pressed=0;
mode_collect=0;
counter2begin=0;
post_charge=0;
go_sleep=0;
knopka_otpushena=0;
init_params();
init_soft_uart();
init_key();
on_charge();
_EINT(); // Enable interrupts
for (;;)
{
if (value_placed){
value_placed=0;
transfer=value_convert;
transmit(transfer);
transmit(~transfer);
RXTXData=EOFPACKET;
// RXTXData=0xF7;
TX_Byte(); // TX Back RXed Byte Received
}
_BIS_SR(CPUOFF); // LPM0
}
}
void fast_charge(void){
CAPD&=~CAPACITOR_PIN; // ïîäêëþ÷àåì âõîäíîé áóôåð
P2SEL&=~CAPACITOR_PIN; // ïîäêëþ÷àåì âõîäíîé áóôåð
PORT_OUT_CAPACITOR|=CAPACITOR_PIN; // âûñîêèé óðîâåíü
PORT_DIR_CAPACITOR|=CAPACITOR_PIN; // íàïðàâëåíèå íà âûõîä
}
// COMPARATORA_VECTOR interrupt service routine
#if __VER__ < 200
interrupt[COMPARATORA_VECTOR] void comparator (void)
#else
#pragma vector=COMPARATORA_VECTOR
__interrupt void comparator (void)
#endif
{
time_to_compare=TAR;
on_charge();
CACTL1&=~CAIE; //çàïðåùàåì ïðåðûâàíèå îò êîìïàðàòîðà
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
// Timer A0 interrupt service routine
#if __VER__ < 200
interrupt[TIMERA0_VECTOR] void Timer_A0 (void)
#else
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A0 (void)
#endif
{
CCR0 += Bitime; // Add Offset to CCR0
if (!(CCTL0 & CCIS0)) // RX on CCI0B?
{
if ( BitCnt == 0)
CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt
else
{
if ((RXTXData & 0x01)==0)
// if ((RXTXData & 0x01)==1)
CCTL0 |= OUTMOD2; // TX Space
else
CCTL0 &= ~ OUTMOD2; // TX Mark
RXTXData = RXTXData >> 1;
BitCnt --;
}
}
}
/****************************************/
//èíèöèàëèçàöèÿ ïàðàìåòðîâ
/****************************************/
void init_params(void){
CACTL1=0; //
CAPD=DIODE_PIN; //îòêëþ÷àåì äèîä îò âõîäíûõ áóôåðîâ ìèêðîñõåìû
BCSCTL1=(BCSCTL1&~(RSEL0|RSEL1|RSEL2))|RSEL2;
BCSCTL2|=DCOR;
}
/****************************************/
//âêëþ÷åíèå êîìïàðàòîðà è îïîðíèêà íà 0.25Vcc
/****************************************/
void on_comparator(void){
//ê äèîäó êîìïàðàòîð+ ïîäêëþ÷åí
//êîìïàðàòîð- îò âíåøíåãî âõîäà îòêëþ÷åí
CACTL2=(CACTL2&~(P2CA0|P2CA1|CAF))|P2CA0|P2CA1|CAF;
//!!! êàêîå âðåìÿ âêëþ÷åíèÿ (çàðàíåå) êîìïàðàòîðà?
//âûáðàíî 0.25Vcc è ïîäêëþ÷åíî íà êîìïàðàòîð-
CACTL1=(CACTL1&~(CAREF0|CAREF1))|CAON|CAIE; //âêëþ÷àåì êîìïàðàòîð
}
/****************************************/
//âêëþ÷åíèå êîìïàðàòîðà è âíåøíåãî âûõîäà
/****************************************/
void on_comparator_external(void){
//ê äèîäó êîìïàðàòîð+ ïîäêëþ÷åí
//êîìïàðàòîð- ïîäêëþ÷åí ê âíåøíåìó âõîäó
CACTL2=(CACTL2&~(P2CA0|P2CA1|CAF))|P2CA1|P2CA0|CAF;
//!!! êàêîå âðåìÿ âêëþ÷åíèÿ (çàðàíåå) êîìïàðàòîðà?
//âêëþ÷àåì êîìïàðàòîð
//âûêëþ÷åí âíóòðåííèé îïîðíèê
CACTL1=(CACTL1&~(CAREF0|CAREF1))|CAON; //âêëþ÷àåì êîìïàðàòîð
}