Browse Source

Worked at Cube Firmware

Thomas Buck 12 years ago
parent
commit
d627eeec8b
4 changed files with 48 additions and 41 deletions
  1. 34
    23
      CubeFirmware/cube.c
  2. 2
    4
      CubeFirmware/cube.h
  3. 11
    13
      CubeFirmware/main.c
  4. 1
    1
      CubeFirmware/makefile

+ 34
- 23
CubeFirmware/cube.c View File

23
 #include <avr/io.h>
23
 #include <avr/io.h>
24
 #include <avr/interrupt.h>
24
 #include <avr/interrupt.h>
25
 #include <stdlib.h>
25
 #include <stdlib.h>
26
+#include <util/atomic.h>
26
 
27
 
27
 #include "uart.h"
28
 #include "uart.h"
28
 #include "cube.h"
29
 #include "cube.h"
31
 #define F_CPU 16000000L
32
 #define F_CPU 16000000L
32
 #endif
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
 volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
40
 volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
36
 volatile uint8_t changedFlag = 0;
41
 volatile uint8_t changedFlag = 0;
37
 volatile uint8_t imgFlag = 0;
42
 volatile uint8_t imgFlag = 0;
41
 inline void isrCall(void);
46
 inline void isrCall(void);
42
 
47
 
43
 void setImage(uint8_t **img) {
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
 	uint8_t ctr = 0;
79
 	uint8_t ctr = 0;
60
 
80
 
61
 	TCCR1A |= (1 << WGM12); // CTC Mode
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
 	// We just assume this works, because after reset,
86
 	// We just assume this works, because after reset,
67
 	// enough Memory should be available...
87
 	// enough Memory should be available...
84
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
104
 	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
85
 }
105
 }
86
 
106
 
87
-// Count to 3968 21 times...
107
+// Count to FIRSTCOUNT SECONDCOUNT times...
88
 ISR(TIMER1_COMPA_vect) {
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
 // Data is sent to 8 Fet bits...
112
 // Data is sent to 8 Fet bits...
98
 inline void setFet(uint8_t data) {
113
 inline void setFet(uint8_t data) {
99
 	PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
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
 // Give id of latch, 0 - 7
118
 // Give id of latch, 0 - 7
110
 }
124
 }
111
 
125
 
112
 inline void setLatch(uint8_t latchNr, uint8_t data) {
126
 inline void setLatch(uint8_t latchNr, uint8_t data) {
113
-	setFet(0); // All LEDs off
114
 	selectLatch(latchNr); // Activate current latch
127
 	selectLatch(latchNr); // Activate current latch
115
 	PORTA = data; // Put latch data
128
 	PORTA = data; // Put latch data
116
 	delay_ns(LATCHDELAY); // Wait for latch
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
 inline void isrCall(void) {
132
 inline void isrCall(void) {
124
 	if (changedFlag != 0) {
135
 	if (changedFlag != 0) {
125
 		// The picture changed. Restart!
136
 		// The picture changed. Restart!
126
 		layer = 0;
137
 		layer = 0;
127
-		imgFlag = 0;
128
 		changedFlag = 0;
138
 		changedFlag = 0;
129
 	}
139
 	}
130
-
140
+	setFet(0);
131
 	for (; latchCtr < 8; latchCtr++) {
141
 	for (; latchCtr < 8; latchCtr++) {
132
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
142
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
133
 	}
143
 	}
144
+	setFet(1 << layer);
134
 	
145
 	
135
 	// Select next layer
146
 	// Select next layer
136
 	if (layer < 7) {
147
 	if (layer < 7) {

+ 2
- 4
CubeFirmware/cube.h View File

21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
21
  * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
  */
22
  */
23
 
23
 
24
-
25
-// Time one latch is active in ns
26
-#define LATCHDELAY 63
27
-
28
 /*
24
 /*
29
  * Call init(). A picture you set a new picture it will be displayed.
25
  * Call init(). A picture you set a new picture it will be displayed.
30
  * After the last part is applied to the latches, isFinished() will change
26
  * After the last part is applied to the latches, isFinished() will change
37
 extern uint8_t isFinished(void);
33
 extern uint8_t isFinished(void);
38
 extern void close(void);
34
 extern void close(void);
39
 
35
 
36
+extern void fillBuffer(uint8_t val);
37
+
40
 // For debugging, not normal operation:
38
 // For debugging, not normal operation:
41
 extern void setFet(uint8_t data);
39
 extern void setFet(uint8_t data);
42
 extern void setLatch(uint8_t latchNr, uint8_t data);
40
 extern void setLatch(uint8_t latchNr, uint8_t data);

+ 11
- 13
CubeFirmware/main.c View File

32
 #endif
32
 #endif
33
 
33
 
34
 int main(void) {
34
 int main(void) {
35
-	uint8_t i, j = 1;
35
+	uint8_t i = 1, j = 1;
36
 
36
 
37
 	DDRD = 0xFF; // Mosfets as Output
37
 	DDRD = 0xFF; // Mosfets as Output
38
 	DDRB = 0xFF;
38
 	DDRB = 0xFF;
39
 	DDRC = 0xFF; // Latch Enable
39
 	DDRC = 0xFF; // Latch Enable
40
 	DDRA = 0xFF; // Latch Data
40
 	DDRA = 0xFF; // Latch Data
41
 
41
 
42
-	// init(); // Initialize Cube Low-Level Code
42
+	init(); // Initialize Cube Low-Level Code
43
 	uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
43
 	uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
44
 
44
 
45
-
46
-	setFet(0x01);
45
+	//setFet(0x01);
47
 	// Blink led :)
46
 	// Blink led :)
48
 	while (1) {
47
 	while (1) {
49
-		for (i = 0; i < 8; i++) {
50
-			setLatch(i, j);
51
-			_delay_ms(100);
52
-		}
53
-		if (j < 128) {
54
-			j *= 2;
55
-		} else {
56
-			j = 1;
57
-		}
48
+		fillBuffer(0x00);
49
+		_delay_ms(500);
50
+		fillBuffer(0xFF);
51
+		_delay_ms(500);
52
+		fillBuffer(0x00);
53
+		_delay_ms(500);
54
+		fillBuffer(0xFF);
55
+		_delay_ms(500);
58
 	}
56
 	}
59
 
57
 
60
 	close();
58
 	close();

+ 1
- 1
CubeFirmware/makefile View File

88
 #  -Wall...:     warning level
88
 #  -Wall...:     warning level
89
 #  -Wa,...:      tell GCC to pass this to the assembler.
89
 #  -Wa,...:      tell GCC to pass this to the assembler.
90
 #    -adhlns...: create assembler listing
90
 #    -adhlns...: create assembler listing
91
-CFLAGS = -g$(DEBUG)
91
+#CFLAGS = -g$(DEBUG)
92
 CFLAGS += $(CDEFS) $(CINCS)
92
 CFLAGS += $(CDEFS) $(CINCS)
93
 CFLAGS += -O$(OPT)
93
 CFLAGS += -O$(OPT)
94
 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
94
 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums

Loading…
Cancel
Save