Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cube.c 5.8KB

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