MPM controlled 16ch RGB LED dimmer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.7KB

  1. /***************************************************************************
  2. * 16ch RGB 8bit PWM controller *
  3. * *
  4. * Copyright (C) 2011 - 2012 by Olaf Rempel *
  5. * razzor AT kopf MINUS tisch DOT de *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; version 2 of the License, *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. #include <stdio.h>
  24. #include "rgb16mpm.h"
  25. /*
  26. * using ATmega32 @8MHz:
  27. * Fuse H: 0xD9 (no bootloader, jtag disabled)
  28. * Fuse L: 0xD4 (int. 8MHz Osz, fast rising power, no BOD)
  29. *
  30. * PA0..7 -> COL1..8
  31. * PC0..7 -> COL9..16
  32. * PB0 / PD7(OC2) -> ROW2/GREEN (OC2 not used)
  33. * PB1 / PD5(OC1A) -> ROW1/RED (OC1A not used)
  34. * PB2 / PD4(OC1B) -> ROW4 (OC1B not used)
  35. * PB3(OC0) / PD6 -> ROW3/BLUE (OC0 not used)
  36. * PD0 -> RXD
  37. * PD1 -> TXD
  38. * PD2 -> /RX_TX
  39. * PD3 -> /LED
  40. */
  41. int main(void) __attribute__ ((noreturn));
  42. int main(void)
  43. {
  44. DDRD = (1<<LED);
  45. PORTD = (1<<LED);
  46. eeprom_read();
  47. rgb_init();
  48. mpm_init();
  49. sei();
  50. uint8_t x = 0;
  51. uint8_t xdir = 1;
  52. uint16_t ramp = 0;
  53. uint8_t step = 0;
  54. while (1) {
  55. /* wait for complete update */
  56. rgb_update(COLOR_MASK, 1);
  57. #if 1
  58. // _delay_ms(100);
  59. step++;
  60. if (step == 16) {
  61. step = 0;
  62. if (xdir) {
  63. x++;
  64. if (x == 0x05)
  65. x = 0x08;
  66. else if (x == 0x0C)
  67. xdir = 0;
  68. } else {
  69. x--;
  70. if (x == 0x00)
  71. xdir = 1;
  72. else if (x == 0x07)
  73. x = 0x04;
  74. }
  75. }
  76. uint8_t color[3];
  77. ramp++;
  78. switch (ramp >> 8) {
  79. case 6:
  80. ramp = 0x0000;
  81. /* no break */
  82. case 0: /* red: on, green: ramp up, blue: off */
  83. color[0] = 0xFF;
  84. color[1] = ramp & 0xFF;
  85. color[2] = 0x00;
  86. break;
  87. case 1: /* red: ramp down, green: on, blue:off */
  88. color[0] = 0xFF - (ramp & 0xFF);
  89. color[1] = 0xFF;
  90. color[2] = 0x00;
  91. break;
  92. case 2: /* red: off, green: on, blue: ramp up */
  93. color[0] = 0x00;
  94. color[1] = 0xFF;
  95. color[2] = (ramp & 0xFF);
  96. break;
  97. case 3: /* red: off, green: ramp down: blue: on */
  98. color[0] = 0x00;
  99. color[1] = 0xFF - (ramp & 0xFF);
  100. color[2] = 0xFF;
  101. break;
  102. case 4: /* red: ramp up, green: off, blue: on */
  103. color[0] = (ramp & 0xFF);
  104. color[1] = 0x00;
  105. color[2] = 0xFF;
  106. break;
  107. case 5: /* red: on, green: off, blue: ramp down */
  108. color[0] = 0xFF;
  109. color[1] = 0x00;
  110. color[2] = 0xFF - (ramp & 0xFF);
  111. break;
  112. }
  113. uint8_t i, j;
  114. for (i = 0; i < 16; i++) {
  115. for (j = 0; j < 3; j++) {
  116. #if 1
  117. if (x == i) {
  118. chan_value[j][i] = color[j];
  119. } else if (chan_value[j][i] > 0) {
  120. uint8_t tmp = (chan_value[j][i] >> 5);
  121. chan_value[j][i] -= (tmp > 0) ? tmp : 1;
  122. }
  123. #else
  124. chan_value[j][i] = color[j];
  125. #endif
  126. }
  127. }
  128. #endif
  129. }
  130. }