Help with definitions

Hi everybody, could someone please help me with definitions. Board: Arduino Leonardo, Code: Glediator interface for ws2811 LED’s.

//##############################################################################
//##############################################################################
//                                                                             #
// Glediator to WS2812 pixel converter                                         #
// by R. Heller                                                                #
// V 1.0 - 07.01.2014                                                          #            
// wwww.SolderLab.de                                                           #
//                                                                             #
// Receives serial data in Glediator protocol format @ 1 MBit/s                #
// and distributes it to a connectect chain of WS2812 pixels                   #
//                                                                             #
// Adjust the correct DATA PIN and the correct NUMBER OF PIXELS you are using  # 
// in the definitions section below before uploading this sketch to your       #
// Arduino device.                                                             #
//                                                                             #
// Maxiumim number of supported pixeles is 512 !!!                             #
//                                                                             #
// In the Glediator software set output mode to "Glediator_Protocol",          #
// color order to "GRB" and baud rate to "1000000"                             #
//                                                                             #
//##############################################################################
//##############################################################################


//##############################################################################
//                                                                             #
// Definitions --> Make changes ONLY HERE                                      #
//                                                                             #
// To find out the correct port, ddr and pin name when you just know the       #
// Arduino's digital pin number just google for "Arduino pin mapping".         #
// In the present example digital Pin 6 is used which corresponds to "PORTD",  #
// "DDRD" and "6", respectively.                                               #
//                                                                             #
//##############################################################################

#define DATA_PORT          PORTD
#define DATA_DDR           DDRD						
#define DATA_PIN           6							
#define NUMBER_OF_PIXELS   144
//##############################################################################
//                                                                             #
// Variables                                                                   #
//                                                                             #
//##############################################################################

unsigned char display_buffer[NUMBER_OF_PIXELS * 3];
static unsigned char *ptr;
static unsigned int pos = 0;

volatile unsigned char go = 0;


//##############################################################################
//                                                                             #
// Setup                                                                       #
//                                                                             #
//##############################################################################

void setup()
{
  // Set data pin as output
  DATA_DDR |= (1 << DATA_PIN);
  
  // Initialize UART
  UCSR1A |= (1<<U2X0);                                
  UCSR1B |= (1<<RXEN0)  | (1<<TXEN0) | (1<<RXCIE0);   
  UCSR1C |= (1<<UCSZ01) | (1<<UCSZ00)             ; 
  UBRR0H = 0;
  UBRR0L = 1; //Baud Rate 1 MBit (at F_CPU = 16MHz)
  
  ptr=display_buffer;
  
  //Enable global interrupts
  sei();
}


//##############################################################################
//                                                                             #
// Main loop                                                                   #
//                                                                             #
//##############################################################################

void loop()
{  	
  if (go==1) 
  {
    cli();
    ws2812_sendarray(display_buffer, NUMBER_OF_PIXELS * 3); 
    sei();
    go=0;
  }
}


//##############################################################################
//                                                                             #
// UART-Interrupt-Prozedur (called every time one byte is compeltely received) #
//                                                                             #
//##############################################################################

ISR(USART_RX_vect) 
{
  unsigned char b;
  b=UDR0;
  
  if (b == 1)  {pos=0; ptr=display_buffer; return;}    
  if (pos == (NUMBER_OF_PIXELS*3)) {} else {*ptr=b; ptr++; pos++;}  
  if (pos == ((NUMBER_OF_PIXELS*3)-1)) {go=1;}
}


//##############################################################################
//                                                                             #
// WS2812 output routine                                                       #
// Extracted from a ligh weight WS2812 lib by Tim (cpldcpu@gmail.com)          #
// Found on wwww.microcontroller.net                                           #
// Requires F_CPU = 16MHz                                                      #
//                                                                             #
//##############################################################################

void ws2812_sendarray(uint8_t *data,uint16_t datlen)
{
  uint8_t curbyte,ctr,masklo;
  uint8_t maskhi = _BV(DATA_PIN);
  masklo =~ maskhi & DATA_PORT;
  maskhi |= DATA_PORT;

  while (datlen--) 
  {
    curbyte = *data++;

    asm volatile
    (
      "		ldi %0,8	\n\t"		// 0
      "loop%=:out %2, %3	\n\t"		// 1
      "lsl	%1		\n\t"		// 2
      "dec	%0		\n\t"		// 3
      "		rjmp .+0	\n\t"		// 5
      "		brcs .+2	\n\t"		// 6l / 7h
      "		out %2,%4	\n\t"		// 7l / -
      "		rjmp .+0	\n\t"		// 9
      "		nop		\n\t"		// 10
      "		out %2,%4	\n\t"		// 11
      "		breq end%=	\n\t"		// 12      nt. 13 taken
      "		rjmp .+0	\n\t"		// 14
      "		rjmp .+0	\n\t"		// 16
      "		rjmp .+0	\n\t"		// 18
      "		rjmp loop%=	\n\t"		// 20
      "end%=:			\n\t" 
      :	"=&d" (ctr)
      :	"r" (curbyte), "I" (_SFR_IO_ADDR(DATA_PORT)), "r" (maskhi), "r" (masklo)
    );
  }

}


//##############################################################################
//                                                                             #
// End of program                                                              #
//                                                                             #
//##############################################################################
// To find out the correct port, ddr and pin name when you just know the       #
// Arduino's digital pin number just google for "Arduino pin mapping".         #
// In the present example digital Pin 6 is used which corresponds to "PORTD",  #
// "DDRD" and "6", respectively.

Why not just stick with Pin 6. Or use Google, as suggested.

johnwasser:
Why not just stick with Pin 6. Or use Google, as suggested.

i could do that, but i dont know the correct port and serial for pin 6 if i am using leonardo. I googled pin mapping and found something, but i dont understand what have i found :smiley: Where are the ports and where are the serials. Here’s a link: [Arduino - PinMapping32u4]Arduino - PinMapping32u4]Arduino - PinMapping32u4](Arduino - PinMapping32u4).

A quick Google Image search for "Arduino Leonardo pin mapping" turned up this diagram:

When you look at the diagram you will see that Pin 6 is marked "PD7" which means PORTD, bit 7.

johnwasser:
A quick Google Image search for "Arduino Leonardo pin mapping" turned up this diagram:

When you look at the diagram you will see that Pin 6 is marked "PD7" which means PORTD, bit 7.

OOOOOKKKK, so DATA_PORT i should leave PORTD? DATA_DDR also?

The example shows:

#define DATA_PORT          PORTD
#define DATA_DDR           DDRD						
#define DATA_PIN           6

You want port D pin 7. You can leave PORTD and DDRD the same since they already point to port D. Change ‘6’ to ‘7’ because the bit number on the Leonardo is 7.

ok and i still have this error:
Arduino: 1.8.3 (Windows Store 1.8.6.0) (Windows 10), Board: “Arduino Leonardo”

C:\Users\Mantas\Downloads\ws2812_glediator\WS2812_Glediator\WS2812_Glediator.ino: In function ‘void setup()’:

WS2812_Glediator:66: error: ‘U2X0’ was not declared in this scope

UCSR1A |= (1<<U2X0);

^

WS2812_Glediator:67: error: ‘RXEN0’ was not declared in this scope

UCSR1B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);

^

WS2812_Glediator:67: error: ‘TXEN0’ was not declared in this scope

UCSR1B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);

^

WS2812_Glediator:67: error: ‘RXCIE0’ was not declared in this scope

UCSR1B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);

^

WS2812_Glediator:68: error: ‘UCSZ01’ was not declared in this scope

UCSR1C |= (1<<UCSZ01) | (1<<UCSZ00) ;

^

WS2812_Glediator:68: error: ‘UCSZ00’ was not declared in this scope

UCSR1C |= (1<<UCSZ01) | (1<<UCSZ00) ;

^

WS2812_Glediator:69: error: ‘UBRR0H’ was not declared in this scope

UBRR0H = 0;

^

WS2812_Glediator:70: error: ‘UBRR0L’ was not declared in this scope

UBRR0L = 1; //Baud Rate 1 MBit (at F_CPU = 16MHz)

^

In file included from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.6.0_x64__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Arduino.h:30:0,

from sketch\WS2812_Glediator.ino.cpp:1:

C:\Users\Mantas\Downloads\ws2812_glediator\WS2812_Glediator\WS2812_Glediator.ino: In function ‘void USART_RX_vect()’:

C:\Users\Mantas\Downloads\ws2812_glediator\WS2812_Glediator\WS2812_Glediator.ino:103:5: warning: ‘USART_RX_vect’ appears to be a misspelled signal handler, missing __vector prefix [-Wmisspelled-isr]

ISR(USART_RX_vect)

^

WS2812_Glediator:106: error: ‘UDR0’ was not declared in this scope

b=UDR0;

^

exit status 1
‘U2X0’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

  // Initialize UART
  UCSR1A |= (1<<U2X0);                                
  UCSR1B |= (1<<RXEN0)  | (1<<TXEN0) | (1<<RXCIE0);   
  UCSR1C |= (1<<UCSZ01) | (1<<UCSZ00)             ; 
  UBRR0H = 0;
  UBRR0L = 1; //Baud Rate 1 MBit (at F_CPU = 16MHz)

Looks like the processor on the Leonardo (32u4) does not have the same serial port hardware as the processor for which the software was designed. Looks like the interrupt on the arrival of a byte is adding that byte to a buffer and setting a flag when the buffer is filled. You may want to change the code to use the built-in USB port or use an external USB-to_TTL_Serial module to allow you to use a UART (Serial1). That’s going to be some tricky work. Good luck.