|
@@ -23,6 +23,7 @@
|
23
|
23
|
#include <avr/io.h>
|
24
|
24
|
#include <avr/interrupt.h>
|
25
|
25
|
#include <stdlib.h>
|
|
26
|
+#include <util/atomic.h>
|
26
|
27
|
|
27
|
28
|
#include "uart.h"
|
28
|
29
|
#include "cube.h"
|
|
@@ -31,7 +32,11 @@
|
31
|
32
|
#define F_CPU 16000000L
|
32
|
33
|
#endif
|
33
|
34
|
|
34
|
|
-volatile uint8_t _isrCounter = 0;
|
|
35
|
+// Should be 41666
|
|
36
|
+#define FIRSTCOUNT 41666
|
|
37
|
+// Time one latch is active in ns, should be 63
|
|
38
|
+#define LATCHDELAY 63
|
|
39
|
+
|
35
|
40
|
volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
|
36
|
41
|
volatile uint8_t changedFlag = 0;
|
37
|
42
|
volatile uint8_t imgFlag = 0;
|
|
@@ -41,12 +46,27 @@ inline void delay_ns(int16_t ns);
|
41
|
46
|
inline void isrCall(void);
|
42
|
47
|
|
43
|
48
|
void setImage(uint8_t **img) {
|
44
|
|
- uint8_t i = 0, j;
|
|
49
|
+ uint8_t i, j;
|
|
50
|
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
|
51
|
+ changedFlag = 1;
|
|
52
|
+ imgFlag = 0;
|
|
53
|
+ for (i = 0; i < 8; i++) {
|
|
54
|
+ for (j = 0; j < 8; j++) {
|
|
55
|
+ imgBuffer[i][j] = img[i][j];
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+}
|
45
|
60
|
|
46
|
|
- changedFlag = 1;
|
47
|
|
- for (; i < 8; i++) {
|
48
|
|
- for (j = 0; j < 8; j++) {
|
49
|
|
- imgBuffer[i][j] = img[i][j];
|
|
61
|
+void fillBuffer(uint8_t val) {
|
|
62
|
+ uint8_t i, j;
|
|
63
|
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
|
64
|
+ changedFlag = 1;
|
|
65
|
+ imgFlag = 0;
|
|
66
|
+ for (i = 0; i < 8; i++) {
|
|
67
|
+ for (j = 0; j < 8; j++) {
|
|
68
|
+ imgBuffer[i][j] = val;
|
|
69
|
+ }
|
50
|
70
|
}
|
51
|
71
|
}
|
52
|
72
|
}
|
|
@@ -59,9 +79,9 @@ void init(void) {
|
59
|
79
|
uint8_t ctr = 0;
|
60
|
80
|
|
61
|
81
|
TCCR1A |= (1 << WGM12); // CTC Mode
|
62
|
|
- TCCR1B |= (1 << CS10); // No prescaler
|
63
|
|
- OCR1A = 3968;
|
64
|
|
- TIMSK = (1 << OCIE1A); // Enable Overflow Interrupt
|
|
82
|
+ TCCR1B |= (1 << CS10); // Prescaler: 1
|
|
83
|
+ OCR1A = FIRSTCOUNT;
|
|
84
|
+ TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
|
65
|
85
|
|
66
|
86
|
// We just assume this works, because after reset,
|
67
|
87
|
// enough Memory should be available...
|
|
@@ -84,21 +104,15 @@ void close(void) {
|
84
|
104
|
TIMSK &= ~(1 << OCIE1A); // Disable interrupt
|
85
|
105
|
}
|
86
|
106
|
|
87
|
|
-// Count to 3968 21 times...
|
|
107
|
+// Count to FIRSTCOUNT SECONDCOUNT times...
|
88
|
108
|
ISR(TIMER1_COMPA_vect) {
|
89
|
|
- if (_isrCounter < 20) {
|
90
|
|
- _isrCounter++;
|
91
|
|
- } else {
|
92
|
|
- _isrCounter = 0;
|
93
|
|
- isrCall();
|
94
|
|
- }
|
|
109
|
+ isrCall();
|
95
|
110
|
}
|
96
|
111
|
|
97
|
112
|
// Data is sent to 8 Fet bits...
|
98
|
113
|
inline void setFet(uint8_t data) {
|
99
|
114
|
PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
|
100
|
|
- PORTB &= ~(24);
|
101
|
|
- PORTB |= ((data << 3) & 24);
|
|
115
|
+ PORTB = (PORTB & ~(24)) | ((data << 3) & 24);
|
102
|
116
|
}
|
103
|
117
|
|
104
|
118
|
// Give id of latch, 0 - 7
|
|
@@ -110,12 +124,9 @@ inline void selectLatch(uint8_t latchNr) {
|
110
|
124
|
}
|
111
|
125
|
|
112
|
126
|
inline void setLatch(uint8_t latchNr, uint8_t data) {
|
113
|
|
- setFet(0); // All LEDs off
|
114
|
127
|
selectLatch(latchNr); // Activate current latch
|
115
|
128
|
PORTA = data; // Put latch data
|
116
|
129
|
delay_ns(LATCHDELAY); // Wait for latch
|
117
|
|
- selectLatch(8); // Deactivate all latches
|
118
|
|
- setFet(1 << latchNr); // Activate current plane
|
119
|
130
|
}
|
120
|
131
|
|
121
|
132
|
inline void isrCall(void) {
|
|
@@ -124,13 +135,13 @@ inline void isrCall(void) {
|
124
|
135
|
if (changedFlag != 0) {
|
125
|
136
|
// The picture changed. Restart!
|
126
|
137
|
layer = 0;
|
127
|
|
- imgFlag = 0;
|
128
|
138
|
changedFlag = 0;
|
129
|
139
|
}
|
130
|
|
-
|
|
140
|
+ setFet(0);
|
131
|
141
|
for (; latchCtr < 8; latchCtr++) {
|
132
|
142
|
setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
|
133
|
143
|
}
|
|
144
|
+ setFet(1 << layer);
|
134
|
145
|
|
135
|
146
|
// Select next layer
|
136
|
147
|
if (layer < 7) {
|