-
Notifications
You must be signed in to change notification settings - Fork 0
/
nixie.c
481 lines (404 loc) · 13.8 KB
/
nixie.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
#include "driver.h"
#include "uart.h"
//#include "stdio.h"
#include "string.h"
#define INC_START_RATE 3 /* rate at which start when setting button is continuously pushed down */
#define INC_MAX_RATE 100 /* max rate of increasing/decreasing minutes/days when setting new time */
#define REFRESH_RATE 1000 /* refresh rate for multiplexing 1 ms = 1000 Hz */
#define BLANK_RATE 10000 /* blanking interval 100 us = 10000 (2*100 us; first turn off anode, wait 100us, set cathodes, wait 100us, turn on anode */
#define ROLL_RATE 15 /* roll numbers in 15 Hz when changing from TIME to DATE */
#define LEAVE_SET_MODE_IN 4 /* leave set mode in 4 seconds when no button is pushed */
#define SHOW_TIME 90 /* Show time for 90 seconds */
#define SHOW_DATE 10 /* Show date for 10 seconds */
#define SHOW_USER_DATA 10 /* Show user data for 30 seconds */
#define NOT_IN_SET_MODE 0
#define PRE_SET_MODE 1
#define SET_MODE_BLINK 2
#define SET_MODE_INC 3
volatile time_t my_time;
volatile uint8_t anode_ON = 0;
volatile uint8_t set_mode = NOT_IN_SET_MODE;
volatile display_t to_display;
volatile display_t user_data;
volatile bool blink = FALSE;
volatile int8_t set_value;
volatile uint8_t leave_set_mode = 0;
void SysTick_Handler(void)
{
switch (set_mode)
{
case NOT_IN_SET_MODE:
time_inc_dec(&my_time, +1, SECONDS);
blink = FALSE;
my_time.change_display_timeout++;
if ((my_time.curr_displayed != USER_DATA))
{
if ((my_time.curr_displayed == TIME) && (my_time.change_display_timeout > my_time.show_time)) /* LOCK equals 0 => unlocked, if locked, there is no change between date & time */
{
my_time.curr_displayed = DATE | LOCK;
my_time.change_display_timeout = 0;
}
else if ((my_time.curr_displayed == DATE) && (my_time.change_display_timeout > my_time.show_date))
{
my_time.curr_displayed = TIME | LOCK;
my_time.change_display_timeout = 0;
}
}
else if (my_time.change_display_timeout > my_time.show_user_data)
{
my_time.curr_displayed = TIME | LOCK;
my_time.change_display_timeout = 0;
}
break;
case PRE_SET_MODE:
blink ^= TRUE;
time_inc_dec(&my_time, +1, SECONDS);
break;
case SET_MODE_BLINK:
blink ^= TRUE;
time_inc_dec(&my_time, +1, SECONDS);
leave_set_mode++;
if (leave_set_mode > LEAVE_SET_MODE_IN) /* leave set mode after elapsing X sec */
{
leave_set_mode = 0;
set_mode = NOT_IN_SET_MODE;
my_time.change_display_timeout = 0;
/* disable interrupt for +/- buttons handlers */
NVIC_DisableIRQ(PININT0_IRQn);
NVIC_DisableIRQ(PININT1_IRQn);
/* Clear pending IRQs for buttons */
NVIC_ClearPendingIRQ(PININT2_IRQn);
/* enable interrupt for change displayed data (time/date) */
NVIC_EnableIRQ(PININT2_IRQn);
}
break;
case SET_MODE_INC:
leave_set_mode = 0;
break;
}
}
volatile bool turn_anode_on = FALSE;
void MRT_IRQHandler(void)
{
uint32_t int_pend;
uint32_t interval_val;
/* Get interrupt pending status for all timers */
int_pend = Chip_MRT_GetIntPending();
Chip_MRT_ClearIntPending(int_pend);
if (set_mode == NOT_IN_SET_MODE)
{
/* Channel 3 - speed of numbers rolling*/
if (int_pend & MRTn_INTFLAG(3))
{
roll_numbers(&my_time, &user_data, &to_display);
}
}
else if (my_time.curr_displayed == TIME) /* if in SET MODE and time to be displayed */
{
to_display.seconds = to_BCD(my_time.seconds);
to_display.minutes = to_BCD(my_time.minutes);
to_display.hours = to_BCD(my_time.hours);
}
else /* in SET MODE and date to be displayed */
{
to_display.seconds = to_BCD(year_to_number(my_time.years));
to_display.minutes = to_BCD(my_time.months);
to_display.hours = to_BCD(my_time.days);
}
/* Channel 0 - base period for multiplexing */
if (int_pend & MRTn_INTFLAG(0))
{
/* Enable timer 1 in single one mode to limit blanking interval */
setupMRT(1, MRT_MODE_ONESHOT, BLANK_RATE);
turn_anode_on = FALSE;
/* Blanking interval */
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, SEC_TENS);
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, SEC);
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, MINUT_TENS);
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, MINUT);
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, HOUR_TENS);
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 0, HOUR);
anode_ON++;
if (anode_ON > 5)
{
anode_ON = 0;
}
}
/* Channel 1 is single shot - limits blanking interval */
if ((int_pend & MRTn_INTFLAG(1)) && !blink)
{
if (turn_anode_on == FALSE)
{
/* Enable timer 1 in single one mode to limit blanking interval */
setupMRT(1, MRT_MODE_ONESHOT, BLANK_RATE);
turn_anode_on = TRUE;
/* Set number (cathode) for the nixie anode which will be turned ON in next interrupt */
switch(anode_ON)
{
case 0:
set_number(to_display.seconds & 0x0F);
break;
case 1:
set_number(to_display.seconds >> 4);
break;
case 2:
set_number(to_display.minutes & 0x0F);
break;
case 3:
set_number(to_display.minutes >> 4);
break;
case 4:
set_number(to_display.hours & 0x0F);
break;
case 5:
set_number(to_display.hours >> 4);
break;
}
}
else /* turn anode ON */
{
turn_anode_on = FALSE;
switch(anode_ON)
{
case 0:
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, SEC);
break;
case 1:
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, SEC_TENS);
break;
case 2:
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, MINUT);
break;
case 3:
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, MINUT_TENS);
break;
case 4:
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, HOUR);
break;
case 5:
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 0, HOUR_TENS);
break;
}
}
}
/* Channel 2 - to enter setting and to increase setting speed when in set mode*/
if (int_pend & MRTn_INTFLAG(2))
{
switch(set_mode)
{
case SET_MODE_INC:
if (my_time.curr_displayed == TIME)
{
time_inc_dec(&my_time, set_value, MINUTES | TIME_ONLY);
}
else
{
time_inc_dec(&my_time, set_value, DAYS);
}
interval_val = Chip_MRT_GetInterval(Chip_MRT_GetRegPtr(2));
interval_val -= interval_val/6;
if (interval_val < (Chip_Clock_GetSystemClockRate() / INC_MAX_RATE))
{
interval_val = (Chip_Clock_GetSystemClockRate() / INC_MAX_RATE);
}
Chip_MRT_SetInterval(Chip_MRT_GetRegPtr(2), interval_val);
break;
case NOT_IN_SET_MODE:
set_mode = PRE_SET_MODE;
break;
}
}
}
Chip_PININT_BITSLICE_CFG_T Chip_PININT_GetBitsliceConfig(LPC_PIN_INT_T *pPININT, Chip_PININT_BITSLICE_T slice)
{
uint32_t pmcfg_reg;
/* Configure bit slice configuration */
pmcfg_reg = pPININT->PMCFG & (PININT_SRC_BITCFG_MASK << (PININT_SRC_BITCFG_START + (slice * 3)));
pmcfg_reg = pmcfg_reg >> (PININT_SRC_BITCFG_START + (slice * 3));
return (Chip_PININT_BITSLICE_CFG_T)pmcfg_reg;
}
/* Clock setting - decrement (-)*/
void PININT0_IRQHandler(void)
{
set_value = -1;
if (Chip_PININT_GetBitsliceConfig(LPC_PININT, PININTBITSLICE0) == PININT_PATTERNLOW)
{
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE0, PININT_PATTERNHIGH, TRUE);
if (my_time.curr_displayed == TIME)
{
my_time.seconds = 0;
time_inc_dec(&my_time, set_value, MINUTES | TIME_ONLY);
}
else
{
time_inc_dec(&my_time, set_value, DAYS);
}
set_mode = SET_MODE_INC;
blink = FALSE;
leave_set_mode = 0;
setupMRT(2, MRT_MODE_REPEAT, INC_START_RATE);/* start at x Hz */
}
else if (Chip_PININT_GetBitsliceConfig(LPC_PININT, PININTBITSLICE0) == PININT_PATTERNHIGH)
{
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE0, PININT_PATTERNLOW, TRUE);
set_mode = SET_MODE_BLINK;
Chip_MRT_SetInterval(Chip_MRT_GetRegPtr(2), 0 | MRT_INTVAL_LOAD); /* stop the timer */
Chip_MRT_SetDisabled(Chip_MRT_GetRegPtr(2));
}
}
/* Clock setting - increment (+) */
void PININT1_IRQHandler(void)
{
set_value = +1;
if (Chip_PININT_GetBitsliceConfig(LPC_PININT, PININTBITSLICE1) == PININT_PATTERNLOW)
{
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE1, PININT_PATTERNHIGH, TRUE);
if (my_time.curr_displayed == TIME)
{
my_time.seconds = 0;
time_inc_dec(&my_time, set_value, MINUTES | TIME_ONLY);
}
else
{
time_inc_dec(&my_time, set_value, DAYS);
}
set_mode = SET_MODE_INC;
blink = FALSE;
leave_set_mode = 0;
setupMRT(2, MRT_MODE_REPEAT, INC_START_RATE);/* start at x Hz */
}
else if (Chip_PININT_GetBitsliceConfig(LPC_PININT, PININTBITSLICE1) == PININT_PATTERNHIGH)
{
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE1, PININT_PATTERNLOW, TRUE);
set_mode = SET_MODE_BLINK;
Chip_MRT_SetInterval(Chip_MRT_GetRegPtr(2), 0 | MRT_INTVAL_LOAD); /* stop the timer */
Chip_MRT_SetDisabled(Chip_MRT_GetRegPtr(2));
}
}
void PININT3_IRQHandler(void)
{
/* if user data is displayed, setting cannot be entered and display cannot be changed between time/date */
if (my_time.curr_displayed == USER_DATA)
{
return;
}
if (LPC_PININT->PMCTRL & PININT_PMCTRL_PMATCH_SEL)
{
//my_time.seconds = 33;
//Chip_PININT_DisablePatternMatch(LPC_PININT);
}
if (Chip_PININT_GetBitsliceConfig(LPC_PININT, PININTBITSLICE2) == PININT_PATTERNLOW)
{
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE2, PININT_PATTERNHIGH, FALSE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE3, PININT_PATTERNHIGH, TRUE);
setupMRT(2, MRT_MODE_ONESHOT, 1);
}
else if (Chip_PININT_GetBitsliceConfig(LPC_PININT, PININTBITSLICE2) == PININT_PATTERNHIGH)
{
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE2, PININT_PATTERNLOW, FALSE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE3, PININT_PATTERNLOW, TRUE);
/* stop the timer */
Chip_MRT_SetInterval(Chip_MRT_GetRegPtr(2), 0 | MRT_INTVAL_LOAD);
Chip_MRT_SetDisabled(Chip_MRT_GetRegPtr(2));
if (set_mode == PRE_SET_MODE)
{
/* enable set mode only after two buttons go high */
set_mode = SET_MODE_BLINK;
/* Clear pending IRQs for buttons */
NVIC_ClearPendingIRQ(PININT0_IRQn);
NVIC_ClearPendingIRQ(PININT1_IRQn);
/* enable interrupt for +/- buttons handlers */
NVIC_EnableIRQ(PININT0_IRQn);
NVIC_EnableIRQ(PININT1_IRQn);
/* disable interrupt for change displayed data (time/date) */
NVIC_DisableIRQ(PININT2_IRQn);
}
if (set_mode == NOT_IN_SET_MODE)
{
if (my_time.curr_displayed == TIME) /* LOCK equals 0 => unlocked */
{
my_time.curr_displayed = DATE | LOCK;
}
else if (my_time.curr_displayed == DATE)
{
my_time.curr_displayed = TIME | LOCK;
}
my_time.change_display_timeout = 0;
}
}
}
int main(void)
{
uint8_t mrtch = 0;
/* Initialize system clock */
SystemInit();
Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
/* Initialize bord, I/O port setting, systick, etc. */
board_init();
UART_init();
/*------------*/
//Chip_PININT_Init (LPC_PIN_INT_T *pPININT)
/* Configure interrupt channel 0 for the GPIO pin in SysCon block */
Chip_SYSCTL_SetPinInterrupt(0, SW1);
/* Configure channel 0 as wake up interrupt in SysCon block */
/*Chip_SYSCTL_EnablePINTWakeup(0);*/
/* Configure channel 0 interrupt as edge sensitive and raising/falling edge interrupt */
/* Chip_PININT_SetPinModeEdge(LPC_PININT, PININTCH0);
Chip_PININT_EnableIntLow(LPC_PININT, PININTCH0);
Chip_PININT_EnableIntHigh(LPC_PININT, PININTCH0);*/
/* Configure interrupt channel 1 for the GPIO pin in SysCon block */
Chip_SYSCTL_SetPinInterrupt(1, SW2);
/* Configure channel 0 as wake up interrupt in SysCon block */
/*Chip_SYSCTL_EnablePINTWakeup(0);*/
/* Configure channel 1 interrupt as edge sensitive and raising/falling edge interrupt */
/*Chip_PININT_SetPinModeEdge(LPC_PININT, PININTCH1);
Chip_PININT_EnableIntLow(LPC_PININT, PININTCH1);
Chip_PININT_EnableIntHigh(LPC_PININT, PININTCH1);*/
/*------------*/
Chip_PININT_SetPatternMatchSrc(LPC_PININT, 0, PININTBITSLICE0);
Chip_PININT_SetPatternMatchSrc(LPC_PININT, 1, PININTBITSLICE1);
Chip_PININT_SetPatternMatchSrc(LPC_PININT, 0, PININTBITSLICE2);
Chip_PININT_SetPatternMatchSrc(LPC_PININT, 1, PININTBITSLICE3);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE0, PININT_PATTERNLOW, TRUE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE1, PININT_PATTERNLOW, TRUE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE2, PININT_PATTERNLOW, FALSE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE3, PININT_PATTERNLOW, TRUE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE4, PININT_PATTERCONST0, FALSE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE5, PININT_PATTERCONST0, FALSE);
Chip_PININT_SetPatternMatchConfig(LPC_PININT, PININTBITSLICE6, PININT_PATTERCONST0, FALSE);
Chip_PININT_EnablePatternMatch(LPC_PININT);
NVIC_EnableIRQ(PININT3_IRQn);
/* MRT Initialization and disable all timers */
Chip_MRT_Init();
for (mrtch = 0; mrtch < MRT_CHANNELS_NUM; mrtch++) {
Chip_MRT_SetDisabled(Chip_MRT_GetRegPtr(mrtch));
}
/* Enable the interrupt for the MRT */
NVIC_EnableIRQ(MRT_IRQn);
/* Enable timer 0 in repeat mode - main period for multiplexing*/
setupMRT(0, MRT_MODE_REPEAT, REFRESH_RATE);
/* Enable timer 3 in repeat mode - */
setupMRT(3, MRT_MODE_REPEAT, ROLL_RATE);
/* Playground starts here */
my_time.seconds = 0;
my_time.minutes = 1;
my_time.hours = 12;
my_time.days = 31;
my_time.months = 12;
my_time.years = 2017;
my_time.curr_displayed = TIME;
my_time.change_display_timeout = 0;
my_time.show_time = SHOW_TIME;
my_time.show_date = SHOW_DATE;
my_time.show_user_data = SHOW_USER_DATA;
#ifdef BOARD_REV1 /* REV1 uses RN42, the below commands are compatible with it only */
while(my_time.seconds == 0); /* wait one second prior setting BT to give it enough time to startup */
while (!set_BT_power_save (&my_time));
#endif
while(1)
{
UART_commands_exec(&my_time, &user_data);
//buttonishi=!Chip_GPIO_ReadPortBit(LPC_GPIO_PORT, 0, SW1); //button is hi
//buttonishioneshot=buttonishi && !buttonishil; //button just went hi
}
}