Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cube.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #endif
  31. #include "cube.h"
  32. #ifndef F_CPU
  33. #define F_CPU 16000000L
  34. #endif
  35. // Should be 41666
  36. #define FIRSTCOUNT 41666
  37. // Time one latch is active in ns, should be 63
  38. #define LATCHDELAY 63
  39. volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
  40. volatile uint8_t changedFlag = 0;
  41. volatile uint8_t imgFlag = 0;
  42. volatile uint8_t layer = 0;
  43. inline void delay_ns(int16_t ns);
  44. inline void isrCall(void);
  45. void setImage(uint8_t *img) {
  46. uint8_t i, j;
  47. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  48. changedFlag = 1;
  49. imgFlag = 0;
  50. for (i = 0; i < 8; i++) {
  51. for (j = 0; j < 8; j++) {
  52. imgBuffer[i][j] = img[j + (i * 8)];
  53. }
  54. }
  55. }
  56. }
  57. void fillBuffer(uint8_t val) {
  58. uint8_t i, j;
  59. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  60. changedFlag = 1;
  61. imgFlag = 0;
  62. for (i = 0; i < 8; i++) {
  63. for (j = 0; j < 8; j++) {
  64. imgBuffer[i][j] = val;
  65. }
  66. }
  67. }
  68. }
  69. uint8_t isFinished(void) {
  70. return imgFlag;
  71. }
  72. void initCube(void) {
  73. uint8_t ctr = 0;
  74. TCCR1A |= (1 << WGM12); // CTC Mode
  75. TCCR1B |= (1 << CS10); // Prescaler: 1
  76. OCR1A = FIRSTCOUNT;
  77. TIMSK = (1 << OCIE1A); // Enable Output Compare Interrupt
  78. // We just assume this works, because after reset,
  79. // enough Memory should be available...
  80. imgBuffer = malloc(8 * sizeof(uint8_t*));
  81. if (imgBuffer == NULL) {
  82. #ifdef DEBUG
  83. serialWriteString("initCube: No memory!\nHalting!");
  84. #endif
  85. while(1);
  86. }
  87. for(ctr = 0; ctr < 8; ctr++) {
  88. // Same reasoning here...
  89. imgBuffer[ctr] = malloc(8 * sizeof(uint8_t));
  90. if (imgBuffer[ctr] == NULL) {
  91. #ifdef DEBUG
  92. serialWriteString("initCube: No memory!\nHalting!");
  93. #endif
  94. while(1);
  95. }
  96. }
  97. }
  98. void close(void) {
  99. uint8_t ctr = 0;
  100. for (; ctr < 8; ctr++) {
  101. free((uint8_t *)imgBuffer[ctr]);
  102. }
  103. free(imgBuffer);
  104. TIMSK &= ~(1 << OCIE1A); // Disable interrupt
  105. }
  106. // Count to FIRSTCOUNT SECONDCOUNT times...
  107. ISR(TIMER1_COMPA_vect) {
  108. isrCall();
  109. }
  110. // Data is sent to 8 Fet bits...
  111. inline void setFet(uint8_t data) {
  112. PORTD = (data & ~(3)); // Doesn't interfere with serial communication...
  113. PORTB = (PORTB & ~(24)) | ((data << 3) & 24);
  114. }
  115. // Give id of latch, 0 - 7
  116. inline void selectLatch(uint8_t latchNr) {
  117. PORTC = 0;
  118. if (latchNr < 8) {
  119. PORTC = 1 << latchNr;
  120. }
  121. }
  122. inline void setLatch(uint8_t latchNr, uint8_t data) {
  123. selectLatch(latchNr); // Activate current latch
  124. PORTA = data; // Put latch data
  125. delay_ns(LATCHDELAY); // Wait for latch
  126. }
  127. inline void isrCall(void) {
  128. uint8_t latchCtr = 0;
  129. if (changedFlag != 0) {
  130. // The picture changed. Restart!
  131. layer = 0;
  132. changedFlag = 0;
  133. }
  134. setFet(0);
  135. for (; latchCtr < 8; latchCtr++) {
  136. setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
  137. }
  138. setFet(1 << layer);
  139. // Select next layer
  140. if (layer < 7) {
  141. layer++;
  142. } else {
  143. layer = 0;
  144. imgFlag++; // Finished
  145. }
  146. }
  147. inline void delay_ns(int16_t ns) {
  148. // minimum 63 nanoseconds (= 1 tick)
  149. for (;ns > 0; ns -= 63)
  150. asm volatile("nop"::);
  151. }