Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cube.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * cube.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. // Uses Timer 1!
  22. #include <avr/io.h>
  23. #include <avr/interrupt.h>
  24. #include <stdlib.h>
  25. #include <util/atomic.h>
  26. #include <avr/pgmspace.h>
  27. #include "cube.h"
  28. #ifndef F_CPU
  29. #define F_CPU 16000000L
  30. #endif
  31. /*
  32. * 24 fps, 8 layers => 192 Interrupts per second
  33. * We are at 16MHz, so we count to:
  34. * 16000000 / 192 = 83333,33...
  35. * We can only count to 2^16, therefore we have a second counter
  36. * COUNT * (COUNT2 + 1) = Realcount
  37. * 41666 * 2 = 83332
  38. * This is flickering really hard.
  39. *
  40. * COUNT2 set to 0 on the other hand, thus producing 48fps,
  41. * works flicker-free. Now we just return imgFlag / 2 as isFinished(),
  42. * so we still fake 24fps for the main program...
  43. */
  44. #define COUNT 41666
  45. #define COUNT2 0
  46. volatile uint8_t imgBuffer[8][8];
  47. volatile uint8_t changedFlag = 0;
  48. volatile uint8_t imgFlag = 0;
  49. volatile uint8_t layer = 0;
  50. volatile uint8_t toggleFlag = 0;
  51. volatile uint32_t timesTriggered = 0;
  52. volatile uint8_t warningDisplayed = 0;
  53. // A fix for our mirrored 64pin cable.
  54. uint8_t lookUp[256] PROGMEM = { 0, 64, 128, 192, 16, 80, 144, 208, 32, 96, 160,
  55. 224, 48, 112, 176, 240, 4, 68, 132, 196, 20,
  56. 84, 148, 212, 36, 100, 164, 228, 52, 116, 180,
  57. 244, 8, 72, 136, 200, 24, 88, 152, 216, 40,
  58. 104, 168, 232, 56, 120, 184, 248, 12, 76, 140,
  59. 204, 28, 92, 156, 220, 44, 108, 172, 236, 60,
  60. 124, 188, 252, 1, 65, 129, 193, 17, 81, 145,
  61. 209, 33, 97, 161, 225, 49, 113, 177, 241, 5,
  62. 69, 133, 197, 21, 85, 149, 213, 37, 101, 165,
  63. 229, 53, 117, 181, 245, 9, 73, 137, 201, 25,
  64. 89, 153, 217, 41, 105, 169, 233, 57, 121, 185,
  65. 249, 13, 77, 141, 205, 29, 93, 157, 221, 45,
  66. 109, 173, 237, 61, 125, 189, 253, 2, 66, 130,
  67. 194, 18, 82, 146, 210, 34, 98, 162, 226, 50,
  68. 114, 178, 242, 6, 70, 134, 198, 22, 86, 150,
  69. 214, 38, 102, 166, 230, 54, 118, 182, 246, 10,
  70. 74, 138, 202, 26, 90, 154, 218, 42, 106, 170,
  71. 234, 58, 122, 186, 250, 14, 78, 142, 206, 30,
  72. 94, 158, 222, 46, 110, 174, 238, 62, 126, 190,
  73. 254, 3, 67, 131, 195, 19, 83, 147, 211, 35,
  74. 99, 163, 227, 51, 115, 179, 243, 7, 71, 135,
  75. 199, 23, 87, 151, 215, 39, 103, 167, 231, 55,
  76. 119, 183, 247, 11, 75, 139, 203, 27, 91, 155,
  77. 219, 43, 107, 171, 235, 59, 123, 187, 251, 15,
  78. 79, 143, 207, 31, 95, 159, 223, 47, 111, 175,
  79. 239, 63, 127, 191, 255 };
  80. ISR(TIMER1_COMPA_vect) {
  81. uint8_t latchCtr;
  82. if (toggleFlag >= COUNT2) {
  83. if (changedFlag != 0) {
  84. // The picture changed. Restart!
  85. layer = 0;
  86. changedFlag = 0;
  87. }
  88. PORTD = 0;
  89. PORTB &= ~(24); // Disable all Fets
  90. for (latchCtr = 0; latchCtr < 8; latchCtr++) {
  91. // Disable all latches
  92. PORTC &= ~(0xFC); // 2 - 7
  93. PORTB &= ~(6); // 0 & 1
  94. PORTA = imgBuffer[layer][latchCtr]; // Put latch data
  95. // Enable desired latch
  96. if (latchCtr > 1) {
  97. PORTC |= (1 << latchCtr);
  98. } else {
  99. PORTB |= (1 << (latchCtr + 1));
  100. }
  101. }
  102. // Set FET
  103. PORTD = (1 << layer); // PD2 to 7
  104. PORTB = (PORTB & ~(24)) | (((1 << layer) << 3) & 24); // PB3 & 4
  105. // Select next layer
  106. if (layer < 7) {
  107. layer++;
  108. } else {
  109. layer = 0;
  110. imgFlag++; // Finished
  111. }
  112. toggleFlag = 0;
  113. timesTriggered++;
  114. } else {
  115. toggleFlag++;
  116. }
  117. }
  118. uint32_t getTriggerCount(void) {
  119. return timesTriggered;
  120. }
  121. void setImage(uint8_t *img) {
  122. uint8_t i, j;
  123. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  124. changedFlag = 1;
  125. imgFlag = 0;
  126. toggleFlag = 0;
  127. for (i = 0; i < 8; i++) {
  128. for (j = 0; j < 8; j++) {
  129. imgBuffer[j][7 - i] = ~(pgm_read_byte(&(lookUp[img[j + (i * 8)]])));
  130. }
  131. }
  132. }
  133. }
  134. void fillBuffer(uint8_t val) {
  135. uint8_t i, j;
  136. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  137. changedFlag = 1;
  138. imgFlag = 0;
  139. toggleFlag = 0;
  140. for (i = 0; i < 8; i++) {
  141. for (j = 0; j < 8; j++) {
  142. imgBuffer[i][j] = ~(val);
  143. }
  144. }
  145. }
  146. }
  147. uint8_t isFinished(void) {
  148. return (imgFlag / 2);
  149. }
  150. void initCube(void) {
  151. uint8_t x, y;
  152. TCCR1B |= (1 << CS10) | (1 << WGM12); // Prescaler: 1, CTC Mode
  153. OCR1A = COUNT;
  154. TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
  155. fillBuffer(0); // Clear memory
  156. sei(); // Enable interrupts
  157. // Show test animation
  158. for (x = 0; x < 8; x++) {
  159. for (y = 0; y < 8; y++) {
  160. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  161. imgBuffer[x][y] &= ~(0xFF); // Set a pixel
  162. changedFlag = 1;
  163. imgFlag = 0;
  164. toggleFlag = COUNT2; // Ensure next interrupts starts displaying
  165. }
  166. while(imgFlag < 1); // Wait for frame to display
  167. }
  168. }
  169. cli(); // Disable interrupts
  170. timesTriggered = 0;
  171. imgFlag = 0;
  172. toggleFlag = 0;
  173. }