Pārlūkot izejas kodu

Worked at Cube Firmware

Thomas Buck 13 gadus atpakaļ
vecāks
revīzija
d627eeec8b
4 mainītis faili ar 48 papildinājumiem un 41 dzēšanām
  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 Parādīt failu

@@ -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) {

+ 2
- 4
CubeFirmware/cube.h Parādīt failu

@@ -21,10 +21,6 @@
21 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 25
  * Call init(). A picture you set a new picture it will be displayed.
30 26
  * After the last part is applied to the latches, isFinished() will change
@@ -37,6 +33,8 @@ extern void setImage(uint8_t **img); // img[8][8]
37 33
 extern uint8_t isFinished(void);
38 34
 extern void close(void);
39 35
 
36
+extern void fillBuffer(uint8_t val);
37
+
40 38
 // For debugging, not normal operation:
41 39
 extern void setFet(uint8_t data);
42 40
 extern void setLatch(uint8_t latchNr, uint8_t data);

+ 11
- 13
CubeFirmware/main.c Parādīt failu

@@ -32,29 +32,27 @@
32 32
 #endif
33 33
 
34 34
 int main(void) {
35
-	uint8_t i, j = 1;
35
+	uint8_t i = 1, j = 1;
36 36
 
37 37
 	DDRD = 0xFF; // Mosfets as Output
38 38
 	DDRB = 0xFF;
39 39
 	DDRC = 0xFF; // Latch Enable
40 40
 	DDRA = 0xFF; // Latch Data
41 41
 
42
-	// init(); // Initialize Cube Low-Level Code
42
+	init(); // Initialize Cube Low-Level Code
43 43
 	uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
44 44
 
45
-
46
-	setFet(0x01);
45
+	//setFet(0x01);
47 46
 	// Blink led :)
48 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 58
 	close();

+ 1
- 1
CubeFirmware/makefile Parādīt failu

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

Notiek ielāde…
Atcelt
Saglabāt