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.5KB

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