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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Should be 41666
  37. #define COUNT 41666
  38. /*
  39. * We have:
  40. * Fosc / prescaler / fps / interruptsPerFrame = interruptCount
  41. * 16000000 / 1 / 24 / 8 = 83333 (round-a-bout)
  42. * That means, 192 Interrupts per second, 1920 after 10 secs!
  43. *
  44. * Small Study: Interrupt count after 10 seconds
  45. *
  46. * |-------------------------------|
  47. * | COUNT | 1 | 41666 | |
  48. * |----------|------|-------|-----|
  49. * | intcount | 2451 | 2451 | |
  50. * |-------------------------------|
  51. *
  52. * Haha, what's up with that?
  53. */
  54. volatile uint8_t imgBuffer[8][8];
  55. volatile uint8_t changedFlag = 0;
  56. volatile uint8_t imgFlag = 0;
  57. volatile uint8_t layer = 0;
  58. inline void isrCall(void);
  59. volatile uint32_t timesTriggered = 0;
  60. volatile uint8_t warningDisplayed = 0;
  61. uint32_t getTriggerCount(void) {
  62. return timesTriggered;
  63. }
  64. void setImage(uint8_t *img) {
  65. uint8_t i, j;
  66. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  67. changedFlag = 1;
  68. imgFlag = 0;
  69. for (i = 0; i < 8; i++) {
  70. for (j = 0; j < 8; j++) {
  71. imgBuffer[i][j] = ~(img[j + (i * 8)]);
  72. }
  73. }
  74. }
  75. }
  76. void fillBuffer(uint8_t val) {
  77. uint8_t i, j;
  78. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  79. changedFlag = 1;
  80. imgFlag = 0;
  81. for (i = 0; i < 8; i++) {
  82. for (j = 0; j < 8; j++) {
  83. imgBuffer[i][j] = ~(val);
  84. }
  85. }
  86. }
  87. }
  88. uint8_t isFinished(void) {
  89. return imgFlag;
  90. }
  91. void initCube(void) {
  92. TCCR1B |= (1 << CS10) | (1 << WGM12); // Prescaler: 1, CTC Mode
  93. OCR1A = COUNT;
  94. TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
  95. }
  96. void close(void) {
  97. TIMSK &= ~(1 << OCIE1A); // Disable interrupt
  98. }
  99. ISR(TIMER1_COMPA_vect) {
  100. isrCall();
  101. timesTriggered++;
  102. #ifdef DEBUG
  103. if (TIFR & TOV1) {
  104. if (warningDisplayed) {
  105. serialWrite('!');
  106. } else {
  107. serialWriteString(getString(27));
  108. warningDisplayed++;
  109. }
  110. TIFR |= (1 << TOV1); // Clear flag
  111. }
  112. #endif
  113. }
  114. // Data is sent to 8 Fet bits...
  115. inline void setFet(uint8_t data) {
  116. PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
  117. PORTB = (PORTB & ~(24)) | ((data << 3) & 24);
  118. }
  119. // Give id of latch, 0 - 7
  120. inline void selectLatch(uint8_t latchNr) {
  121. PORTC = 0;
  122. if (latchNr < 8) {
  123. PORTC = 1 << latchNr;
  124. }
  125. }
  126. inline void setLatch(uint8_t latchNr, uint8_t data) {
  127. selectLatch(latchNr); // Activate current latch
  128. PORTA = data; // Put latch data
  129. asm volatile("nop"::); // Wait for latch
  130. }
  131. inline void isrCall(void) {
  132. uint8_t latchCtr = 0;
  133. if (changedFlag != 0) {
  134. // The picture changed. Restart!
  135. layer = 0;
  136. changedFlag = 0;
  137. }
  138. setFet(0);
  139. for (; latchCtr < 8; latchCtr++) {
  140. setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
  141. }
  142. setFet(1 << layer);
  143. // Select next layer
  144. if (layer < 7) {
  145. layer++;
  146. } else {
  147. layer = 0;
  148. imgFlag++; // Finished
  149. }
  150. }