mega168 with four 5x7 LEDs
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.

178 lines
3.7KB

  1. /***************************************************************************
  2. * Copyright (C) 03/2010 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/io.h>
  20. #include <avr/interrupt.h>
  21. #include <avr/pgmspace.h>
  22. #define F_CPU 8000000
  23. #include <util/delay.h>
  24. #include "6x8_vertikal_LSB_1.h"
  25. #define nop() asm volatile ("nop")
  26. #define nCLEAR PB0
  27. #define RCLK PB1
  28. #define nOE PB2
  29. #define MOSI PB3
  30. #define MISO PB3
  31. #define SCK PB5
  32. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  33. static const char text[] = " <<<atmega168:Scrolltext-Demo>>> ";
  34. /* scrollbuffer */
  35. static volatile uint8_t data[32];
  36. static volatile uint8_t offset;
  37. void setRow(uint8_t row)
  38. {
  39. uint8_t i;
  40. volatile uint8_t *p = data + offset + 19;
  41. row = (1 << row);
  42. /* bit-banging spi, 20 cols */
  43. for (i = 0; i < 20; i++) {
  44. PORTB &= ~(1<<MISO);
  45. if (*p-- & row)
  46. PORTB |= (1<<MOSI);
  47. PORTB |= (1<<SCK);
  48. PORTB &= ~(1<<SCK);
  49. /* insert extra cols (unused hc595 outputs) */
  50. if (i == 5 || i == 12 || i == 19) {
  51. PORTB |= (1<<SCK);
  52. PORTB &= ~(1<<SCK);
  53. }
  54. }
  55. /* disable outputs, update register */
  56. PORTB |= ((1<<nOE) | (1<<RCLK));
  57. /* select row */
  58. PORTC |= 0x07;
  59. PORTC &= ~(row & 0x07);
  60. PORTD |= 0x78;
  61. PORTD &= ~(row & 0x78);
  62. /* enable outputs again */
  63. PORTB &= ~((1<<nOE) | (1<<RCLK));
  64. }
  65. ISR(SIG_OVERFLOW0)
  66. {
  67. static uint8_t row;
  68. /* 2ms */
  69. TCNT0 = 0xFF - 64;
  70. setRow(row++);
  71. if (row == 7)
  72. row = 0;
  73. }
  74. int main(void)
  75. {
  76. /* row 1-3 */
  77. DDRC = 0x07;
  78. PORTC = 0x07;
  79. /* row 4-7 */
  80. DDRD = 0x78;
  81. PORTD = 0x78;
  82. /* ctrl signals */
  83. DDRB = (1<<nCLEAR) | (1<<RCLK) | (1<<nOE) | (1<<MOSI) | (1<<SCK);
  84. PORTB = (1<<nCLEAR) | (1<<nOE);
  85. /* F_CPU/256, Overflow Interrupt */
  86. TCCR0B = (1<<CS02);
  87. TIMSK0 = (1<<TOIE0);
  88. sei();
  89. uint8_t mode = 0;
  90. uint8_t pos = 0;
  91. uint8_t delay = 0;
  92. while (1) {
  93. switch (mode)
  94. {
  95. case 0:
  96. if (offset == 6) {
  97. offset = 0;
  98. if (text[pos +3] == '\0') {
  99. pos = 0;
  100. mode++;
  101. } else {
  102. pos++;
  103. }
  104. }
  105. if (offset == 0) {
  106. int i;
  107. for (i = 0; i < 5; i++) {
  108. memcpy_P((void *)&data[i * 6], font[(uint8_t)text[pos + i]], 5);
  109. }
  110. }
  111. offset++;
  112. delay = 6;
  113. break;
  114. case 1:
  115. if (offset == 6) {
  116. offset = 0;
  117. if (pos == (ARRAY_SIZE(font) -3)) {
  118. pos = 0;
  119. mode++;
  120. } else {
  121. pos++;
  122. }
  123. }
  124. if (offset == 0) {
  125. int i;
  126. for (i = 0; i < 5; i++) {
  127. memcpy_P((void *)&data[i * 6], font[(uint8_t)pos + i], 5);
  128. }
  129. }
  130. offset++;
  131. delay = 6;
  132. break;
  133. default:
  134. mode = 0;
  135. delay = 0;
  136. break;
  137. }
  138. while (delay--)
  139. _delay_ms(10);
  140. }
  141. return 0;
  142. }