Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cube.c 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <stdlib.h>
  4. #include "cube.h"
  5. #ifndef F_CPU
  6. #define F_CPU 16000000L
  7. #endif
  8. volatile uint8_t _isrCounter = 0;
  9. volatile uint8_t **imgBuffer = NULL; // imgBuffer[8][8]
  10. volatile uint8_t imgFlag = 0;
  11. // Wir zählen 21 mal bis 3968
  12. ISR(TIMER1_COMPA_vect) {
  13. if (_isrCounter < 20) {
  14. _isrCounter++;
  15. } else {
  16. _isrCounter = 0;
  17. isrCall();
  18. }
  19. }
  20. inline void setFet(uint8_t data) {
  21. data &= ~((1 << 1) | 1);
  22. PORTD = data;
  23. data &= ~(3);
  24. data = data << 3;
  25. PORTB |= data;
  26. }
  27. inline void selectLatch(uint8_t latchNr) {
  28. PORTC = 0;
  29. if (latchNr < 8) {
  30. PORTC = 1 << latchNr;
  31. }
  32. }
  33. inline void setLatch(uint8_t latchNr, uint8_t data) {
  34. setFet(0); // All LEDs off
  35. selectLatch(latchNr); // Activate current latch
  36. PORTA = data; // Put latch data
  37. delay_ns(LATCHDELAY); // Wait for latch
  38. selectLatch(8); // Deactivate all latches
  39. setFet(1 << latchNr); // Activate current plane
  40. }
  41. inline void isrCall(void) {
  42. static uint8_t layer = 0;
  43. uint8_t latchCtr = 0;
  44. for (; latchCtr < 8; latchCtr++) {
  45. setLatch(latchCtr, imgBuffer[layer][latchCtr]); // Put out all the data
  46. }
  47. // Select next layer
  48. if (layer < 7) {
  49. layer++;
  50. } else {
  51. layer = 0;
  52. }
  53. }
  54. inline void delay_ns(uint16_t ns) {
  55. // minimum 63 nanoseconds (= 1 tick)
  56. uint16_t i = ns;
  57. if (ns != 0) {
  58. if (ns < 63) {
  59. ns = 63;
  60. }
  61. for (; i > 0; i -= 63) {
  62. asm volatile("nop"::);
  63. }
  64. }
  65. }