-
Notifications
You must be signed in to change notification settings - Fork 2
/
dmd.c
226 lines (181 loc) · 4.38 KB
/
dmd.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
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/cpufunc.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "matrixdata.h"
#define HEIGHT 16
#define WIDTH 64
#define SHIFTPORT PORTB
#define SHIFTPORTDDR DDRB
#define OUTPUTENABLE 6
#define SELA 1
#define SELB 2
#define CLK 3
#define LATCH 4
#define DATA 5
#define setpinhigh(pin) SHIFTPORT = SHIFTPORT | (1<<pin)
#define setpinlow(pin) SHIFTPORT = SHIFTPORT & ~(1<<pin)
unsigned char display[HEIGHT][WIDTH/8] = {
0xff, 0xff, 0xff,0Xff, 0xff, 0xff, 0xff,0Xff,
0xff, 0xff, 0xff,0Xff, 0xff, 0xff, 0xff,0Xff,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0Xff, 0xff, 0xff, 0xff,0Xff,
0xff, 0xff, 0xff,0Xff, 0xff, 0xff, 0xff,0Xff,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF,
0xff, 0xff, 0xff,0XFF, 0xff, 0xff, 0xff,0XFF
};
void setline(char line, char bool){
if (bool)
setpinhigh(line);
else
setpinlow(line);
}
void disableoutput(){
setline(OUTPUTENABLE, 0);
}
void enableoutput(){
setline(OUTPUTENABLE, 1);
}
/*
Sends a high followed by a low in the pin specified by outpin
____
___(low)__|high|__(low)___
*/
void sendpulse(char outpin){
setpinhigh(outpin);
setpinlow(outpin);
}
/*
inputs one bit into the shift register
1. first, data line is set to the bit
2. second, the clock is pulsed
*/
void clockbit(char bit){
if (bit)
setpinhigh(DATA);
else
setpinlow(DATA);
sendpulse(CLK);
setpinlow (DATA);
}
/*
sends one byte of data to dot matrix
*/
void clockbyte(char byte){
for (unsigned char i=0x80; i != 0; i = i >> 1){
if (byte & i)
setpinlow(DATA);
else
setpinhigh(DATA);
sendpulse (CLK);
}
setpinlow(DATA);
}
//sets the selector pins A and B
void setselector(char select){
//if (select > 3) return;
setline(SELA, select & 0x01);
setline(SELB, select & 0x02);
}
/*
clocks in one full multiplex unit from the display
sel is the multiplex selector(0,1,2,3)
void clocksel(char sel){
for (int i = 0 ; i < (WIDTH>>3);i++){ //4
for (int j = (HEIGHT>>2) - 1; j >= 0; j--){ //4
clockbyte(display[j<<2 + sel][ i ]);
}
}
}*/
void clocksel(char sel){
disableoutput();
for (int i = 0 ; i < (WIDTH/8); i++){ //4
for (int j = 3; j >= 0; j--){ //4
clockbyte(display[j*4 + sel ][ i ]);
}
}
setselector(sel);
sendpulse(LATCH);
enableoutput();
}
//clears screen to black BY shifting in zero
//as opposed to simply turning on the OE line
//which achieves the same effect
void clearscreen(){
int count = 0;
setpinlow(DATA);
while (count < (HEIGHT * WIDTH/4)){
sendpulse(CLK);
count++;
}
}
void init(){
//set ddr for PORT
SHIFTPORTDDR = 0XFF;
//set all pins to default states
setpinhigh(OUTPUTENABLE);
setpinlow(SELA);
setpinlow(SELB);
setpinlow(CLK);
setpinlow(LATCH);
setpinlow(DATA);
//clear once
clearscreen();
}
/*
void assigndisplay(const unsigned char newdisp[HEIGHT][WIDTH/8]){
unsigned char byte;
for (unsigned char i = 0; i < HEIGHT; i++){
for (unsigned char j = 0; j < WIDTH/8; j++){
byte = pgm_read_byte(&(newdisp[i][j]));
display[i][j] = byte;
}
}
}
*/
// reads frame data from PROGMEM in the Header
// and puts it into RAM
void newassigndisplay(int frameno){
unsigned char byte;
for (unsigned char i = 0; i < HEIGHT; i++){
for (unsigned char j = 0; j < WIDTH/8; j++){
byte = pgm_read_byte(&(animation[frameno][i][j]));
display[i][j] = byte;
}
}
}
int main(){
init();
OCR0 = 0xff;
TIMSK = 2;
TCCR0 = 0X0b;
sei();
int frame = 0;
while (1){
cli();
disableoutput();
newassigndisplay(frame++);
enableoutput();
sei();
if (frame >= framecount) frame = 0;
_delay_ms(6);
}
}
int rowcount_internal = 0;
ISR(TIMER0_COMP_vect ){
clocksel(rowcount_internal++);
if (rowcount_internal > 3){
rowcount_internal = 0;
}
}