-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleddisplay.c
executable file
·134 lines (108 loc) · 2.82 KB
/
leddisplay.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
#include <stdio.h>
#include <stdlib.h>
#include <p24FJ64GB002.h>
#define DELAY 3000
#define DS_low() LATB &=~0x8000
#define DS_high() LATB |=0x8000
#define ST_CP_low() LATB &=~0x4000
#define ST_CP_high() LATB |=0x4000
#define SH_CP_low() LATB &=~0x2000
#define SH_CP_high() LATB |=0x2000
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define DIG1 0x8000
#define DIG2 0x4000
#define DIG3 0x2000
#define DIG4 0x1000
#define ZERO 0x0003
#define ONE 0x009F
#define TWO 0x0025
#define THREE 0x000D
#define FOUR 0x0099
#define FIVE 0x0049
#define SIX 0x00C1
#define SEVEN 0x001F
#define EIGHT 0x0001
#define NINE 0x0019
#define DECIMAL 0x00FE
#define APOSTROPHE 0x0200
#define COLON 0x0800
#define CLEAR 0x0000
unsigned int characters[] = {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
unsigned int punctuation[] = {DECIMAL, APOSTROPHE, COLON};
unsigned int index = 0;
void setLEDs(unsigned int pattern);
void selfTest();
void iterateAllDigits(unsigned int dig1Pattern, unsigned int dig2Pattern, unsigned int dig3Pattern, unsigned int dig4Pattern, unsigned int punctuation);
void setupTimer();
/*
*
*/
int main(int argc, char** argv) {
TRISB = 0x0000;
T1CON = 0x8030;
// reset everything
LATB = 0x0000;
setupTimer();
TMR1 = 0;
while (1) {
} // main loop
return (EXIT_SUCCESS);
}
void selfTest() {
int i;
for (i = 0; i < 10; i++) {
iterateAllDigits(characters[i], characters[i], characters[i], characters[i], punctuation[i / 3]);
}
iterateAllDigits(CLEAR, CLEAR, CLEAR, CLEAR, CLEAR);
}
void iterateAllDigits(unsigned int dig1Pattern, unsigned int dig2Pattern, unsigned int dig3Pattern, unsigned int dig4Pattern, unsigned int punctuation) {
TMR1 = 0;
while (TMR1 < DELAY) {
setLEDs(DIG1 | dig1Pattern);
setLEDs(DIG2 | dig2Pattern);
setLEDs(DIG3 | dig3Pattern);
setLEDs(DIG4 | dig4Pattern);
setLEDs(punctuation);
}
}
void setLEDs(unsigned int pattern) {
ST_CP_low();
SH_CP_low();
int i;
for (i = 0; i < 16; i++) {
if (CHECK_BIT(pattern, i))
DS_high();
else
DS_low();
SH_CP_high();
SH_CP_low();
}
ST_CP_high();
}
void setupTimer() {
// Set timer 2
T2CON = 0x8000;
TMR2 = 0x00;
PR2 = 0xFF;
_T2IE = 1;
_T2IF = 0;
// Set timer 4 to tick about four times a second
T4CON = 0x8000;
TMR4 = 0x00;
PR4 = 64000;
_T4IE = 1;
_T4IF = 0;
}
void _ISRFAST __attribute__((interrupt, auto_psv)) _T2Interrupt(void) {
setLEDs(DIG1 | characters[index]);
setLEDs(DIG2 | characters[index]);
setLEDs(DIG3 | characters[index]);
setLEDs(DIG4 | characters[index]);
_T2IF = 0;
}
void _ISRFAST __attribute__((interrupt, auto_psv)) _T4Interrupt(void) {
if (index++ >= 10) {
index = 0;
}
_T4IF = 0;
}