Advertisement
Guest User

Untitled

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