Guest User

Untitled

a guest
Jan 8th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1.  
  2. //##############################################################################
  3. //##############################################################################
  4. // #
  5. // Glediator to WS2812 pixel converter #
  6. // by R. Heller #
  7. // V 1.0 - 07.01.2014 #
  8. // wwww.SolderLab.de #
  9. // #
  10. // Receives serial data in Glediator protocol format @ 1 MBit/s #
  11. // and distributes it to a connectect chain of WS2812 pixels #
  12. // #
  13. // Adjust the correct DATA PIN and the correct NUMBER OF PIXELS you are using #
  14. // in the definitions section below before uploading this sketch to your #
  15. // Arduino device. #
  16. // #
  17. // Maxiumim number of supported pixeles is 512 !!! #
  18. // #
  19. // In the Glediator software set output mode to "Glediator_Protocol", #
  20. // color order to "GRB" and baud rate to "1000000" #
  21. // #
  22. //##############################################################################
  23. //##############################################################################
  24.  
  25.  
  26. //##############################################################################
  27. // #
  28. // Definitions --> Make changes ONLY HERE #
  29. // #
  30. // To find out the correct port, ddr and pin name when you just know the #
  31. // Arduino's digital pin number just google for "Arduino pin mapping". #
  32. // In the present example digital Pin 6 is used which corresponds to "PORTD", #
  33. // "DDRD" and "6", respectively. #
  34. // #
  35. //##############################################################################
  36.  
  37. #define DATA_PORT PORTD
  38. #define DATA_DDR DDRD
  39. #define DATA_PIN 6
  40. #define NUMBER_OF_PIXELS 588
  41.  
  42.  
  43. //##############################################################################
  44. // #
  45. // Variables #
  46. // #
  47. //##############################################################################
  48.  
  49. unsigned char display_buffer[NUMBER_OF_PIXELS * 3];
  50. static unsigned char *ptr;
  51. static unsigned int pos = 0;
  52.  
  53. volatile unsigned char go = 0;
  54.  
  55.  
  56. //##############################################################################
  57. // #
  58. // Setup #
  59. // #
  60. //##############################################################################
  61.  
  62. void setup()
  63. {
  64. // Set data pin as output
  65. DATA_DDR |= (1 << DATA_PIN);
  66.  
  67. // Initialize UART
  68. UCSR0A |= (1<<U2X0);
  69. UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
  70. UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00) ;
  71. UBRR0H = 0;
  72. UBRR0L = 1; //Baud Rate 1 MBit (at F_CPU = 16MHz)
  73.  
  74. ptr=display_buffer;
  75.  
  76. //Enable global interrupts
  77. sei();
  78. }
  79.  
  80.  
  81. //##############################################################################
  82. // #
  83. // Main loop #
  84. // #
  85. //##############################################################################
  86.  
  87. void loop()
  88. {
  89. if (go==1)
  90. {
  91. cli();
  92. ws2812_sendarray(display_buffer, NUMBER_OF_PIXELS * 3);
  93. sei();
  94. go=0;
  95. }
  96. }
  97.  
  98.  
  99. //##############################################################################
  100. // #
  101. // UART-Interrupt-Prozedur (called every time one byte is compeltely received) #
  102. // #
  103. //##############################################################################
  104.  
  105. ISR(USART_RX_vect)
  106. {
  107. unsigned char b;
  108. b=UDR0;
  109.  
  110. if (b == 1) {pos=0; ptr=display_buffer; return;}
  111. if (pos == (NUMBER_OF_PIXELS*3)) {} else {*ptr=b; ptr++; pos++;}
  112. if (pos == ((NUMBER_OF_PIXELS*3)-1)) {go=1;}
  113. }
  114.  
  115.  
  116. //##############################################################################
  117. // #
  118. // WS2812 output routine #
  119. // Extracted from a ligh weight WS2812 lib by Tim (cpldcpu@gmail.com) #
  120. // Found on wwww.microcontroller.net #
  121. // Requires F_CPU = 16MHz #
  122. // #
  123. //##############################################################################
  124.  
  125. void ws2812_sendarray(uint8_t *data,uint16_t datlen)
  126. {
  127. uint8_t curbyte,ctr,masklo;
  128. uint8_t maskhi = _BV(DATA_PIN);
  129. masklo =~ maskhi & DATA_PORT;
  130. maskhi |= DATA_PORT;
  131.  
  132. while (datlen--)
  133. {
  134. curbyte = *data++;
  135.  
  136. asm volatile
  137. (
  138. " ldi %0,8 \n\t" // 0
  139. "loop%=:out %2, %3 \n\t" // 1
  140. "lsl %1 \n\t" // 2
  141. "dec %0 \n\t" // 3
  142. " rjmp .+0 \n\t" // 5
  143. " brcs .+2 \n\t" // 6l / 7h
  144. " out %2,%4 \n\t" // 7l / -
  145. " rjmp .+0 \n\t" // 9
  146. " nop \n\t" // 10
  147. " out %2,%4 \n\t" // 11
  148. " breq end%= \n\t" // 12 nt. 13 taken
  149. " rjmp .+0 \n\t" // 14
  150. " rjmp .+0 \n\t" // 16
  151. " rjmp .+0 \n\t" // 18
  152. " rjmp loop%= \n\t" // 20
  153. "end%=: \n\t"
  154. : "=&d" (ctr)
  155. : "r" (curbyte), "I" (_SFR_IO_ADDR(DATA_PORT)), "r" (maskhi), "r" (masklo)
  156. );
  157. }
  158.  
  159. }
  160.  
  161.  
  162. //##############################################################################
  163. // #
  164. // End of program #
  165. // #
  166. //##############################################################################
Add Comment
Please, Sign In to add comment