Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * twislave.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 <stdint.h>
  26. uint8_t data[7] = { 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42 };
  27. uint8_t dataSent = 0;
  28. ISR(TWI_vect) {
  29. static uint8_t dataPos = 0;
  30. switch (TWSR & ~(7)) {
  31. case 0xB0: case 0xA8: // We've been called
  32. dataPos = 1; // Prepare next position
  33. TWDR = data[0]; // Transmit first byte
  34. TWCR |= (1 << TWEA); // More follows
  35. dataSent = 0;
  36. break;
  37. case 0xB8: // We're doing fine
  38. if (dataPos >= 6) {
  39. // whoops, this shouldn't happen
  40. // we transmit the last byte again and stop as if nothing happened
  41. // we do this also if we reached the last byte
  42. dataPos = 0;
  43. TWDR = data[6];
  44. TWCR &= ~(1 << TWEA); // Nothing follows
  45. dataSent = 1;
  46. } else {
  47. TWDR = data[dataPos++]; // Transmit next byte
  48. TWCR |= (1 << TWEA); // More follows
  49. dataSent = 0;
  50. }
  51. break;
  52. case 0xC0: case 0xC8: // We're done
  53. TWCR |= (1 << TWEA);
  54. dataSent = 1;
  55. break;
  56. }
  57. TWCR |= (1 << TWINT); // Continue with bus operation
  58. }
  59. void twiInit(uint8_t address) {
  60. if (address == 0) { // set slave address
  61. TWAR = 1;
  62. } else {
  63. TWAR = address & ~(1);
  64. }
  65. TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // Enable twi, interrupt and acknowledge bit
  66. }
  67. void twiSetDataToSend(uint8_t *d) { // 7 bytes
  68. // We don't care if it interrupts while we update the data.
  69. // It will cause a single faulty frame. This will not be visible while showing many
  70. // readouts in a short time.
  71. uint8_t i;
  72. for (i = 0; i < 7; i++) {
  73. data[i] = d[i];
  74. }
  75. dataSent = 0;
  76. }
  77. uint8_t twiDataWasSent(void) {
  78. if (dataSent != 0) {
  79. return 1;
  80. } else {
  81. return 0;
  82. }
  83. }