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

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