Browse Source

Worked at cube low level code

Thomas Buck 12 years ago
parent
commit
fb9b3d09c2
3 changed files with 86 additions and 50 deletions
  1. 70
    10
      cube.c
  2. 11
    8
      cube.h
  3. 5
    32
      main.c

+ 70
- 10
cube.c View File

24
 #include <avr/interrupt.h>
24
 #include <avr/interrupt.h>
25
 #include <stdlib.h>
25
 #include <stdlib.h>
26
 
26
 
27
+#include "uart.h"
27
 #include "cube.h"
28
 #include "cube.h"
28
 
29
 
29
 #ifndef F_CPU
30
 #ifndef F_CPU
32
 
33
 
33
 volatile uint8_t _isrCounter = 0;
34
 volatile uint8_t _isrCounter = 0;
34
 volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
35
 volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
36
+volatile uint8_t changedFlag = 0;
35
 volatile uint8_t imgFlag = 0;
37
 volatile uint8_t imgFlag = 0;
38
+volatile uint8_t layer = 0;
36
 
39
 
37
-// Wir zählen 21 mal bis 3968
40
+inline void delay_ns(int16_t ns);
41
+inline void isrCall(void);
42
+
43
+void setImage(uint8_t **img) {
44
+	uint8_t i = 0, j;
45
+
46
+	changedFlag = 1;
47
+	for (; i < 8; i++) {
48
+		for (j = 0; j < 8; j++) {
49
+			imgBuffer[i][j] = img[i][j];
50
+		}
51
+	}
52
+}
53
+
54
+uint8_t isFinished(void) {
55
+	return imgFlag;
56
+}
57
+
58
+void init(void) {
59
+	uint8_t ctr = 0;
60
+	DDRD = 0xFF; // Mosfets as Output
61
+	DDRB = 0xFF;
62
+	DDRC = 0xFF; // Latch Enable
63
+	DDRA = 0xFF; // Latch Data
64
+
65
+	TCCR1A |= (1 << WGM12); // CTC Mode
66
+	TCCR1B |= (1 << CS10); // No prescaler
67
+	OCR1A = 3968;
68
+	TIMSK = (1 << OCIE1A); // Enable Overflow Interrupt
69
+
70
+	// We just assume this works, because after reset,
71
+	// enough Memory should be available...
72
+	imgBuffer = malloc(8 * sizeof(uint8_t*));
73
+
74
+	for(ctr = 0; ctr < 8; ctr++) {
75
+		// Same reasoning here...
76
+		imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
77
+	}
78
+
79
+	sei(); // Enable Interrupts
80
+}
81
+
82
+void close(void) {
83
+	uint8_t ctr = 0;
84
+	for (; ctr < 8; ctr++) {
85
+		free((uint8_t *)imgBuffer[ctr]);
86
+	}
87
+	free(imgBuffer);
88
+	TIMSK &= ~(1 << OCIE1A); // Disable interrupt
89
+}
90
+
91
+// Count to 3968 21 times...
38
 ISR(TIMER1_COMPA_vect) {
92
 ISR(TIMER1_COMPA_vect) {
39
 	if (_isrCounter < 20) {
93
 	if (_isrCounter < 20) {
40
 		_isrCounter++;
94
 		_isrCounter++;
44
 	}
98
 	}
45
 }
99
 }
46
 
100
 
101
+// Data is sent to 8 Fet bits...
47
 inline void setFet(uint8_t data) {
102
 inline void setFet(uint8_t data) {
48
-	data &= ~((1 << 1) | 1);
49
-	PORTD = data;
50
-	data &= ~(3);
51
-	data = data << 3;
52
-	PORTB |= data;
103
+	PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
104
+	PORTB &= ~(24);
105
+	PORTB |= ((data << 3) & 24);
53
 }
106
 }
54
 
107
 
55
-
108
+// Give id of latch, 0 - 7
56
 inline void selectLatch(uint8_t latchNr) {
109
 inline void selectLatch(uint8_t latchNr) {
57
 	PORTC = 0;
110
 	PORTC = 0;
58
 	if (latchNr < 8) {
111
 	if (latchNr < 8) {
70
 }
123
 }
71
 
124
 
72
 inline void isrCall(void) {
125
 inline void isrCall(void) {
73
-	static uint8_t layer = 0;
74
 	uint8_t latchCtr = 0;
126
 	uint8_t latchCtr = 0;
75
-	
127
+
128
+	if (changedFlag != 0) {
129
+		// The picture changed. Restart!
130
+		layer = 0;
131
+		imgFlag = 0;
132
+		changedFlag = 0;
133
+	}
134
+
76
 	for (; latchCtr < 8; latchCtr++) {
135
 	for (; latchCtr < 8; latchCtr++) {
77
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
136
 		setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
78
 	}
137
 	}
82
 		layer++;
141
 		layer++;
83
 	} else {
142
 	} else {
84
 		layer = 0;
143
 		layer = 0;
144
+		imgFlag = 1; // Finished
85
 	}
145
 	}
86
 }
146
 }
87
 
147
 
88
 inline void delay_ns(int16_t ns) {
148
 inline void delay_ns(int16_t ns) {
89
 	// minimum 63 nanoseconds (= 1 tick)
149
 	// minimum 63 nanoseconds (= 1 tick)
90
-	for (ns > 0; ns -= 63)
150
+	for (;ns > 0; ns -= 63)
91
 		asm volatile("nop"::);
151
 		asm volatile("nop"::);
92
 }
152
 }

+ 11
- 8
cube.h View File

20
  * You should have received a copy of the GNU General Public License
20
  * You should have received a copy of the GNU General Public License
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
-#include <stdint.h>
23
+
24
 
24
 
25
 // Time one latch is active in ns
25
 // Time one latch is active in ns
26
 #define LATCHDELAY 63
26
 #define LATCHDELAY 63
27
 
27
 
28
-extern volatile uint8_t **imgBuffer; // imgBuffer[8][8]
29
-extern volatile uint8_t imgFlag;
28
+/*
29
+ * 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
31
+ * to 1. Now you can place a new picture here.
32
+ */
33
+extern void init(void);
30
 
34
 
31
-extern void setFet(uint8_t data);
32
-extern void selectLatch(uint8_t latchNr);
33
-extern void setLatch(uint8_t latchNr, uint8_t data);
34
-extern void isrCall(void);
35
-extern void delay_ns(uint16_t ns);
35
+// Copies the data in img into its own buffer, so free img afterwards...
36
+extern void setImage(uint8_t **img); // img[8][8]
37
+extern uint8_t isFinished(void);
38
+extern void close(void);

+ 5
- 32
main.c View File

23
 #include <stdlib.h>
23
 #include <stdlib.h>
24
 #include <stdint.h>
24
 #include <stdint.h>
25
 #include <avr/io.h>
25
 #include <avr/io.h>
26
-#include <avr/interrupt.h>
27
 #include <util/delay.h>
26
 #include <util/delay.h>
28
 #include "uart.h"
27
 #include "uart.h"
29
 #include "cube.h"
28
 #include "cube.h"
32
 #define F_CPU 16000000L
31
 #define F_CPU 16000000L
33
 #endif
32
 #endif
34
 
33
 
35
-void init(void) {
36
-	uint8_t ctr = 0;
37
-	DDRD = 0xFF; // Mosfets as Output
38
-	DDRB = 0xFF;
39
-	DDRC = 0xFF; // Latch Enable
40
-	DDRA = 0xFF; // Latch Data
41
-	
42
-	uart_init(UART_BAUD_SELECT(19200, 16000000L));
43
-
44
-	TCCR1A |= (1 << WGM12); // CTC Mode
45
-	TCCR1B |= (1 << CS10); // No prescaler
46
-	OCR1A = 3968;
47
-	TIMSK = (1 << OCIE1A); // Enable Overflow Interrupt
48
-
49
-	imgBuffer = malloc(8 * sizeof(uint8_t*));
50
-	if (imgBuffer == NULL) {
51
-		// TO-DO:
52
-		// error!
53
-	}
54
-	for(ctr = 0; ctr < 8; ctr++) {
55
-		imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
56
-		if (imgBuffer[ctr] == NULL) {
57
-			// TO-DO:
58
-			// error!
59
-		}
60
-	}
61
-
62
-	sei(); // Enable Interrupts
63
-}
64
-
65
 int main(void) {
34
 int main(void) {
66
 	
35
 	
67
-	init();
36
+	init(); // Initialize Cube Low-Level Code
37
+	uart_init(UART_BAUD_SELECT(19200, 16000000L)); // Initialize Serial
68
 
38
 
39
+	// Blink led :)
69
 	while (1) {
40
 	while (1) {
70
 		PORTB |= (1 << PB0);
41
 		PORTB |= (1 << PB0);
71
 		_delay_ms(1000);
42
 		_delay_ms(1000);
72
 		PORTB &= ~(1 << PB0);
43
 		PORTB &= ~(1 << PB0);
73
 		_delay_ms(1000);
44
 		_delay_ms(1000);
74
 	}
45
 	}
46
+
47
+	close();
75
 	return 0;
48
 	return 0;
76
 }
49
 }

Loading…
Cancel
Save