-
Notifications
You must be signed in to change notification settings - Fork 0
/
dave.c
376 lines (338 loc) · 7.68 KB
/
dave.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
/*
* A very simple C program for an 8x8x8 monochrome cube such as icubesmart 3D8S
* or possibly others with compatible stc12/8051 based controllers.
*
* Based on tomazas 888.c on github, but ported for the 3D8S and SDCC/Linux.
* it also supports the 3D8S's three buttons for pause and mode selection.
* it also shows how to scroll your name easily.
*
* Compile with sdcc-sdcc --std-c99 -mmcs51 dave.c
* translate to hex with sdcc-packihx dave.ihx > dave.hex
* Program the controller with stcgal -P stc12 -p /dev/ttyUSB0 dave.hex
* Note: I had to switch the ground rather than 5V (from the USB-TTY
* device to the cube) because of parasitic drain. Read the stcgal
* documentation for details.
*
* Looking from the front (LEDs all point to the front) the cube axes are:
* X is left to right
* Y is front to back
* Z is bottom to top
*/
#include <stc12.h>
/* led state storage for the entire cube.
* (Each 'x' is the 8 bit value in the display[z][y] array,
* most significant bit on the left.)
* For each bit, a '0' sinks or turns on the led.
* This is kept in the extended ram due to its size.
*/
__xdata volatile unsigned char display[8][8];
static int mode = 0;
/*
********** timing, interrrupt and I/O functions ***********************
*/
/* configure and start interrupts */
void sinter(void)
{
IE = 0x82; // EA | ET0
TCON = 0x01; // IT0
TH0 = 0xc0; // timer 0 high byte ~.05 sec
TL0 = 0; // timer 0 low byte
TR0 = 1; // Turn on the timer
}
/* simple software timer */
void delay5us(void)
{
unsigned char a, b;
for(b=7; b>0; b--)
for(a=2; a>0; a--)
;
}
/* simple longer delay for main or interrupt threads */
void delay(unsigned int i)
{
while (i--) {
delay5us();
}
}
/* Main thread software delay with support for pause and mode buttons.
*
* Button 1, 2, and 3 are connected to P4_1, P4_2, and P4_3, active low.
* Button 1 is pause on/off
* Button 2 selects mode 0
* Button 3 sequences through modes one at a time
*
* We poll the buttons in here for simplicity and fast response.
* Pause takes effect immediately, and mode changes return true
* immediately to the caller for handling there.
*/
int pause(unsigned int i)
{
static unsigned char pause = 0, p41key = 0, p42key = 0, p43key = 0;;
while (i--) {
delay5us();
/* handle pause (button 1) */
while (1) {
if (!P4_1){ // button 1 pressed
if (!p41key){ // new press
p41key = 1;
pause = !pause; // toggle pause
delay(100); //debounce
}
} else
p41key = 0;
if (!pause) // stay in loop until unpaused
break;
}
/* handle button 2 (set mode to zero) */
if (!P4_2)
if (!p42key) {
p42key = 1;
mode = 0;
delay(100); //debounce
return(1);
}
else
p42key = 0;
/* handle button 3 (increment mode) */
if (!P4_3){
if (!p43key) {
p43key = 1;
mode = (mode +1) % 5;
delay(100); //debounce
return(1);
}
} else
p43key = 0;
}
return 0;
}
/*
****************** display primitives ************************
*/
/* turn all leds in the whole cube on or off */
void set_all(unsigned char v)
{
unsigned char i, j;
for (j=0; j<8; j++) {
for (i=0; i<8; i++)
display[j][i] = v ? 0 : 0xff;
}
}
/* turn all leds in an x plane on or off */
void set_x_plane(unsigned char x, unsigned char v)
{
unsigned char i, j;
for (j=0; j<8; j++) {
for (i=0; i<8; i++)
if (v)
display[j][i] &= ~(128>>x);
else
display[j][i] |= (128>>x);
}
}
/* turn all leds in an y plane on or off */
void set_y_plane(unsigned char y, unsigned char v)
{
unsigned char j;
for (j=0; j<8; j++) {
if (v)
display[j][y] = 0;
else
display[j][y] = 0xff;
}
}
/* turn all leds in an z plane on or off */
void set_z_plane(unsigned char z, unsigned char v)
{
unsigned char i;
for (i=0; i<8; i++) {
if (v)
display[z][i] = 0;
else
display[z][i] = 0xff;
}
}
void set_point(unsigned char x, unsigned char y, unsigned char z,
unsigned char v)
{
if (v)
display[z][y] &= ~(128>>x);
else
display[z][y] |= (128>>x);
}
/* display a 64 led y plane from (positive logic bit array */
void character_on_y(unsigned char y, unsigned char *c)
{
int z;
for (z=0; z<8; z++)
display[z][y] = ~c[z]; //make negative
}
/*
******************** display routines ***********************
*/
/* blink the whole cube
* Return (1) if pause indicates a button pressed
*/
int all(void)
{
/* turn on all leds */
set_all(1);
if (pause(60000))
return(1);
/* turn off all leds */
set_all(0);
if (pause(60000))
return(1);
return(0);
}
/* sweep the x, y, and z planes
* Return (1) if pause indicates a button pressed
*/
int planes(void)
{
int x, y, z;
set_all(0);
/* cycle through all planes */
for (x=0; x<8; x++){
set_x_plane(x, 1);
if (pause(10000))
return(1);
set_x_plane(x, 0);
}
for (y=0; y<8; y++){
set_y_plane(y, 1);
if (pause(10000))
return(1);
set_y_plane(y, 0);
}
for (z=0; z<8; z++){
set_z_plane(z, 1);
if (pause(10000))
return(1);
set_z_plane(z, 0);
}
return(0);
}
/* sweep each individual led in the whole cube
* Return (1) if pause indicates a button pressed
*/
int points(void)
{
int x, y, z;
set_all(0);
for (x=0; x<8; x++) {
for (y=0; y<8; y++) {
for (z=0; z<8; z++) {
set_point(x,y,z,1);
if (pause(1000))
return(1);
set_point(x,y,z,0);
}
}
}
return(0);
}
/* display "DAVE" scrolled on the y planes front to back
* Return (1) if pause indicates a button pressed
*/
int dave(void)
{
/* Bit mapped characters for "DAVE".
* These are positive logic ('1' means on)
* Since this is read-only, keep in text space to save RAM
*/
__code unsigned char dave[4][8] = {
{0xf8, 0xfc, 0xc6, 0xc3, 0xc3, 0xc6, 0xfc, 0xf8}, // D
{0xc3, 0xc3, 0xff, 0xff, 0xc3, 0x66, 0x3c, 0x18}, // A
{0x18, 0x3c, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, // V
{0xff, 0xff, 0xc0, 0xf8, 0xf8, 0xc0, 0xff, 0xff} // E
};
int c, y;
set_all(0);
for (c=0; c<4; c++) {
for (y=0; y<8; y++) {
character_on_y(y, dave[c]);
if (pause(5000))
return(1);
set_y_plane(y, 0);
}
}
set_all(1);
if (pause(30000))
return(1);
return(0);
}
/* initialize and loop display routines forever */
void main()
{
sinter(); // turn on interrupts
P4 = 0xff; // enable button input port
while(1) {
switch (mode) {
case 0:
dave();
break;
case 1:
points();
break;
case 2:
planes();
break;
case 3:
all();
break;
case 4:
if(dave())
break;;
if(points())
break;
if(planes())
break;
all();
break;
}
}
}
/*
* The timer interrupt service routine.
*
* On each interrupt, we turn on the next whole anode (Z) layer,
* and it remains on until the next timer interrupt.
* Each layer is on only one out of eight interrupts,
* but the multiplexing is fast enough that the eye
* does not see any flickering.
*
* For each layer, the latches hold the bits for all 64
* vertical cathode lines, so each led in the layer is
* individually controlled.
*
* P1 selects the horizontal anode layer (one at a time)
* NOTE! icubesmart 3D8S uses inverted drivers, so 1 is off.
* P2 selects a cathode latch chip to load a new data byte.
* P0 sets the cathode latch data (a '1' bit turns off the LED).
*
* The timing in here is sensitive, so don't add a lot.
*/
void timer_isr (void) __interrupt (1)
{
static unsigned char layer = 0;
unsigned char i;
/* turn off all layers while loading new data */
P1 = 255;
/* load the 8 bytes for this layer into latches */
for (i=0; i<8; i++) {
P2 = 1<<i; // select latch
delay(3);
P0 = display[layer][i]; // write its data byte
delay(3);
}
/* turn on this layer */
P1 = ~(1<<layer);
/* increment layer for next time */
layer++;
if (layer > 7)
layer = 0;
/* reload timer */
TH0 = 0xc0;
TL0 = 0;
}