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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #ifdef DEBUG
  29. #include "serial.h"
  30. #include "strings.h"
  31. #endif
  32. #include "cube.h"
  33. #ifndef F_CPU
  34. #define F_CPU 16000000L
  35. #endif
  36. /*
  37. * 24 fps, 8 layers => 192 Interrupts per second
  38. * We are at 16MHz, so we count to:
  39. * 16000000 / 192 = 83333,33...
  40. * We can only count to 2^16, therefore we have a second counter
  41. * COUNT * (COUNT2 + 1) = Realcount
  42. * 41666 * 2 = 83332
  43. * This is flickering really hard.
  44. *
  45. * COUNT2 set to 0 on the other hand, thus producing 48fps,
  46. * works flicker-free. Now we just return imgFlag / 2 as isFinished(),
  47. * so we still fake 24fps for the main program...
  48. */
  49. #define COUNT 41666
  50. #define COUNT2 0
  51. volatile uint8_t imgBuffer[8][8];
  52. volatile uint8_t changedFlag = 0;
  53. volatile uint8_t imgFlag = 0;
  54. volatile uint8_t layer = 0;
  55. volatile uint8_t toggleFlag = 0;
  56. inline void isrCall(void);
  57. volatile uint32_t timesTriggered = 0;
  58. volatile uint8_t warningDisplayed = 0;
  59. ISR(TIMER1_COMPA_vect) {
  60. if (toggleFlag >= COUNT2) {
  61. isrCall();
  62. toggleFlag = 0;
  63. timesTriggered++;
  64. } else {
  65. toggleFlag++;
  66. }
  67. }
  68. uint32_t getTriggerCount(void) {
  69. return timesTriggered;
  70. }
  71. void setImage(uint8_t *img) {
  72. uint8_t i, j;
  73. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  74. changedFlag = 1;
  75. imgFlag = 0;
  76. toggleFlag = 0;
  77. for (i = 0; i < 8; i++) {
  78. for (j = 0; j < 8; j++) {
  79. imgBuffer[j][i] = ~(img[j + (i * 8)]);
  80. }
  81. }
  82. }
  83. }
  84. void fillBuffer(uint8_t val) {
  85. uint8_t i, j;
  86. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  87. changedFlag = 1;
  88. imgFlag = 0;
  89. toggleFlag = 0;
  90. for (i = 0; i < 8; i++) {
  91. for (j = 0; j < 8; j++) {
  92. imgBuffer[i][j] = ~(val);
  93. }
  94. }
  95. }
  96. }
  97. uint8_t isFinished(void) {
  98. return (imgFlag / 2);
  99. }
  100. void initCube(void) {
  101. uint8_t x, y;
  102. TCCR1B |= (1 << CS10) | (1 << WGM12); // Prescaler: 1, CTC Mode
  103. OCR1A = COUNT;
  104. TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
  105. fillBuffer(0); // Clear memory
  106. sei(); // Enable interrupts
  107. // Show test animation
  108. for (x = 0; x < 8; x++) {
  109. for (y = 0; y < 8; y++) {
  110. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  111. imgBuffer[x][y] &= ~(0xFF); // Set a pixel
  112. changedFlag = 1;
  113. imgFlag = 0;
  114. toggleFlag = COUNT2; // Ensure next interrupts starts displaying
  115. }
  116. while(imgFlag < 1); // Wait for frame to display
  117. /* ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  118. imgBuffer[x][y] |= (0xFF); // Clear pixel
  119. } */
  120. }
  121. }
  122. cli(); // Disable interrupts
  123. timesTriggered = 0;
  124. imgFlag = 0;
  125. toggleFlag = 0;
  126. }
  127. void close(void) {
  128. TIMSK &= ~(1 << OCIE1A); // Disable interrupt
  129. }
  130. // Data is sent to 8 Fet bits...
  131. inline void setFet(uint8_t data) {
  132. PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
  133. PORTB = (PORTB & ~(24)) | ((data << 3) & 24);
  134. }
  135. // Give id of latch, 0 - 7
  136. inline void selectLatch(uint8_t latchNr) {
  137. // Disable all latches
  138. PORTC &= ~(0xFC); // 2 - 7
  139. PORTB &= ~(6); // 0 & 1
  140. // Enable desired latch
  141. if (latchNr > 1) {
  142. PORTC |= (1 << latchNr);
  143. } else {
  144. PORTB |= (1 << (latchNr + 1));
  145. }
  146. }
  147. inline void setLatch(uint8_t latchNr, uint8_t data) {
  148. selectLatch(latchNr); // Activate current latch
  149. PORTA = data; // Put latch data
  150. asm volatile("nop"::); // Wait for latch
  151. }
  152. inline void isrCall(void) {
  153. uint8_t latchCtr = 0;
  154. if (changedFlag != 0) {
  155. // The picture changed. Restart!
  156. layer = 0;
  157. changedFlag = 0;
  158. }
  159. setFet(0);
  160. for (; latchCtr < 8; latchCtr++) {
  161. setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
  162. }
  163. setFet(1 << layer);
  164. // Select next layer
  165. if (layer < 7) {
  166. layer++;
  167. } else {
  168. layer = 0;
  169. imgFlag++; // Finished
  170. }
  171. }