Main
Main
Main
cpp
1/6
04.04.2011
#include <inttypes.h>
//#include <msp430f2013.h>
#include <msp430g2211.h>
#define DIGIT_1 BIT0 //
Common Cathode LEDs
#define DIGIT_2 BIT1 // +1AF23B+
#define DIGIT_3 BIT2 // | 4 x 7 Segment + DP |
#define DIGIT_4 BIT3 // +EDDPCG4+
#define SR_A BIT4 // shift register serial data pin
#define SR_CLK BIT5 // shift register clock pin
#define BUTTON_2 BIT6
#define BUTTON_1 BIT7 //
A
//
// |
|
#define _A ( 1 << 7 ) // F
B
#define _B ( 1 << 6 ) // |
|
#define _C ( 1 << 5 ) //
G
#define _D ( 1 << 4 ) // |
|
#define _E ( 1 << 3 ) // E
C
#define _F ( 1 << 2 ) // |
|
#define _G ( 1 << 1 ) //
D .DP
#define DP ( 1 << 0 ) // decimal point
static const uint8_t glyph_pattern[ 14 ] = {
_A | _B | _C | _D | _E | _F,
// 0
_B | _C,
// 1
_A | _B | _D | _E | _G,
// 2
_A | _B | _C | _D | _G,
// 3
_B | _C | _F | _G,
// 4
_A | _C | _D | _F | _G,
// 5
_A | _C | _D | _E | _F | _G,
// 6
_A | _B | _C,
// 7
_A | _B | _C | _D | _E | _F | _G, // 8
_A | _B | _C | _D | _F | _G,
// 9
0,
// none
_C | _D | _E | _G,
// o
_C | _E | _G,
// n
_A | _E | _F | _G,
// F
};
#define GLYPH_none 10
#define GLYPH_o
11
#define GLYPH_n
12
#define GLYPH_O
0 // reuse 0 as O
#define GLYPH_F
13
//
^ VCC
^ VCC
//
|
|
//
\ Button_2
\ Button_1
//
|
|
// P1.6 +
P1.7 +
//
|
|/|
|
// 220 nF ===
= | Piezo
220 nF ===
//
|
|\|
|
//
//
//
RST 47 k pullup (2.2 nF pulldown)
// TEST leave open
//
//
[ ACTUAL CONFIGURATION ]
//
^ VCC
^ VCC
//
|
|
//
\ Button_2
\ Button_1
//
|
|
// P1.6 +
//
|
|
100 R | |
// 220 nF ===
|
//
|
|
// Coil
/|
\|/ \ LED
P1.7 +
// Speaker | | |
\v
|
// (40 R)
\|
|
v
220 nF ===
//
|
|
|
//
#define CORRECTION 2
static const uint8_t glyph_delay[ 14 ] = {
6 + CORRECTION, // 0
2 + CORRECTION, // 1
5 + CORRECTION, // 2
5 + CORRECTION, // 3
4 + CORRECTION, // 4
5 + CORRECTION, // 5
6 + CORRECTION, // 6
3 + CORRECTION, // 7
7 + CORRECTION, // 8
6 + CORRECTION, // 9
0 + CORRECTION, // none
4 + CORRECTION, // o
3 + CORRECTION, // n
4 + CORRECTION, // F
};
static
static
static
static
static
static
static
static
volatile
volatile
volatile
volatile
volatile
volatile
volatile
volatile
main.cpp
c:/Dokumente und Einstellungen/Markus/Eigene Dateien/Embedded/workspace/clock/
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
2/6
04.04.2011
F#5
G5
G#
main.cpp
c:/Dokumente und Einstellungen/Markus/Eigene Dateien/Embedded/workspace/clock/
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
3/6
04.04.2011
main.cpp
4/6
04.04.2011
main.cpp
c:/Dokumente und Einstellungen/Markus/Eigene Dateien/Embedded/workspace/clock/
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
5/6
04.04.2011
break;
case TIME_MINUTE_EDITING:
minutes = ++minutes % 60;
seconds = 0;
ticks = 0;
hour_point = DP;
break;
case ALARM_IS_ACTIVE_EDITING:
alarm_is_active ^= 1;
break;
case ALARM_HOUR_EDITING:
alarm_hour = ++alarm_hour % 24;
break;
case ALARM_MINUTE_EDITING:
alarm_minute = ( alarm_minute + 5 ) % 60;
break;
case SHINING:
case BUZZING:
p_1_6_input_mode();
go_to_sleep = true;
break;
}
ticks_at_button_press = ticks;
P1IFG &= ~BUTTON_1; // clear the interrupt flag
} else if ( P1IFG & BUTTON_2 ) {
switch ( state ) {
case SLEEPING:
// no reaction
break;
case TIME_DISPLAYING:
state = TIME_HOUR_EDITING;
break;
case TIME_HOUR_EDITING:
state = TIME_MINUTE_EDITING;
break;
case TIME_MINUTE_EDITING:
state = TIME_DISPLAYING;
break;
case ALARM_IS_ACTIVE_DISPLAYING:
case ALARM_DISPLAYING:
state = ALARM_IS_ACTIVE_EDITING;
break;
case ALARM_IS_ACTIVE_EDITING:
state = alarm_is_active ? ALARM_HOUR_EDITING : ALARM_IS_ACTIVE_DISPLAYING;
break;
case ALARM_HOUR_EDITING:
state = ALARM_MINUTE_EDITING;
break;
case ALARM_MINUTE_EDITING:
state = ALARM_IS_ACTIVE_DISPLAYING;
break;
}
ticks_at_button_press = ( ticks + 48 ) % 64;
P1IFG &= ~BUTTON_2; // clear the interrupt flag
} else {
P1IFG = 0; // Probably unnecessary, but if another flag occurs in P1, this will clear it.
}
}
#pragma vector = WDT_VECTOR
__interrupt void WATCHDOG_INTERVAL_TIMER_ISR(void)
{
// Flag is cleared automatically.
tick_happened = true;
++ticks;
switch ( ticks ) {
case 32:
hour_point = 0;
break;
case 64:
hour_point = DP;
ticks = 0;
++seconds;
if ( state == TIME_DISPLAYING || state == ALARM_IS_ACTIVE_DISPLAYING || state == ALARM_DISPLAYING )
{
++displaying_watchdog;
//if ( displaying_watchdog > ( hours >= 22 || hours < 7 ? 3 : 60 ) ) {
if ( displaying_watchdog > 3 ) {
go_to_sleep = true;
}
}
main.cpp
c:/Dokumente und Einstellungen/Markus/Eigene Dateien/Embedded/workspace/clock/
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
end
if ( seconds == 60 ) {
seconds = 0;
++minutes;
if ( minutes == 60 ) {
minutes = 0;
++hours;
if ( hours == 24 ) {
hours = 0;
//seconds = 2; // correction for too slow quarz
//++days;
}
}
if ( alarm_is_active && hours == alarm_hour && minutes == alarm_minute ) {
state = SHINING; // Let your geek (and the LED) shine.
p_1_6_pwm_mode();
TACCR1 = 2; // 2 is permanent on, 0 is off when TACCR0 is 1.
TACCR0 = 1;
LPM3_EXIT;
} else if ( state == SHINING ) {
state = BUZZING; // Play a delightful tune.
tune_index = remaining_duration = 0;
} else if ( state == BUZZING ) {
p_1_6_input_mode(); // All attempts were in vain.
go_to_sleep = true;
}
}
break;
}
}
//if ( days == 32 ||
//
days == 31 && ( months == 4 || months == 6 || months == 9 || months == 11 ) ||
//
days == 30 && months == 2 && leap_year != 0 ||
//
days == 29 && months == 2 && leap_year == 0 )
//{
// days = 1;
// ++months;
// if ( months == 13 ) {
//
months = 1;
//
++years;
//
if ( years % 400 == 0 ||
//
years % 4 == 0 && years % 100 != 0 )
//
{
//
leap_year = 1;
//
} else {
//
leap_year = 0;
//
}
// }
//}
6/6
04.04.2011