0

Translate Arduino UNO USART to Arduino MEGA USART
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Thu Jul 19, 2012 11:51 am

Trying to get this sketch that works great with the UNO to send data to the LED pixels to work with the MEGA. I know it has something to do with the UARTs since the UNO has only one available and the MEGA has four, I've just exhausted all my efforts attempting. I'm trying to do this because with the UNO I'm experiencing frame rate issues at anything above about 250 pixels. The display buffer is (I'm assuming) stored in RAM and is three times the size of the amount of pixels, so this is what (also assuming) is damaging my frame rate at higher values.

Here is the complete sketch:

Code: Select all | TOGGLE FULL SIZE

#define Num_Pixels 300


#define CMD_NEW_DATA 1


int SDI = 13;
int CKI = 12;

unsigned char display_buffer[Num_Pixels * 3];

static unsigned char *ptr;
static unsigned int pos = 0;

volatile unsigned char go = 0;

void setup()
{
  pinMode(SDI, OUTPUT);
  pinMode(CKI, OUTPUT);
 
  //UART Initialization
  UCSR0A |= (1<<U2X0);                               
  UCSR0B |= (1<<RXEN0)  | (1<<TXEN0) | (1<<RXCIE0);   
  UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00)             ;
  UBRR0H = 0;
  UBRR0L = 3; //Baud Rate 0.5 MBit   --> 0% Error at 16MHz :-)
 
  ptr=display_buffer;
 
  //Enable global interrupts
  sei();
}

void loop()
{
  if (go==1) {shift_out_data(); go=0;}
}


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

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


// Shift out Data

void shift_out_data()
{

  for (int i=0; i<Num_Pixels; i++)
  {
    byte r = display_buffer[i*3+0];
    byte g = display_buffer[i*3+1];
    byte b = display_buffer[i*3+2];
   
    for (byte j=0; j<8; j++)
    {
       digitalWrite(CKI, LOW);
       if (b & (byte)(1<<(7-j))) {digitalWrite(SDI, HIGH);} else {digitalWrite(SDI, LOW);}     
       digitalWrite(CKI, HIGH);
    }
   
    for (byte j=0; j<8; j++)
    {
       digitalWrite(CKI, LOW);
       if (r & (1<<(7-j))) {digitalWrite(SDI, HIGH);} else {digitalWrite(SDI, LOW);}     
       digitalWrite(CKI, HIGH);
    }
   
    for (byte j=0; j<8; j++)
    {
       digitalWrite(CKI, LOW);
       if (g & (1<<(7-j))) {digitalWrite(SDI, HIGH);} else {digitalWrite(SDI, LOW);}     
       digitalWrite(CKI, HIGH);
    }
   
  }
 
  digitalWrite(CKI, LOW);
  delayMicroseconds(800); //Latch Data

}


If my assumptions hold true, I think the problem lies in this part of the code:
Code: Select all | TOGGLE FULL SIZE
ISR(USART_RX_vect)
{
  unsigned char b;
 
  b=UDR0;
 
  if (b == CMD_NEW_DATA)  {pos=0; ptr=display_buffer; return;}   
  if (pos == (Num_Pixels*3)) {} else {*ptr=b; ptr++; pos++;} 
  if (pos == ((Num_Pixels*3)-1)) {go=1;}
}


Where the declarations for the UARTs would be different for the MEGA. I've tried changing the ISR(USART_RX_vect) to
ISR(USART0_RX_vect) so it knows which to use, but still seeing no lights come up.

Any suggestions?
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by adafruit_support_bill on Thu Jul 19, 2012 12:01 pm

Are these LED pixels from Adafruit?

adafruit_support_bill
 
Posts: 73519
Joined: Sat Feb 07, 2009 10:11 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Thu Jul 19, 2012 12:08 pm

Indeed they are! They're the 36mm squares.
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by adafruit_support_bill on Thu Jul 19, 2012 12:18 pm

We do have a WS2801 library that for these that works on the Mega. https://github.com/adafruit/Adafruit-WS2801-Library
The library do not rely on the hardware UARTs on either the UNO or the MEGA.

adafruit_support_bill
 
Posts: 73519
Joined: Sat Feb 07, 2009 10:11 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Thu Jul 19, 2012 12:24 pm

Right, that works great, however I'm trying to get something aside from what has been provided to work. That's why I posted in the Arduino section and not the glowy things section...
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by adafruit_support_bill on Thu Jul 19, 2012 12:31 pm

OK. Hacking is an approved activity around here :D

adafruit_support_bill
 
Posts: 73519
Joined: Sat Feb 07, 2009 10:11 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by adafruit_support_rick on Thu Jul 19, 2012 12:37 pm

The UART settings look OK - comparing the datasheets, I don't see any difference between the 328 and 2560 UART0 register definitions.

The USART0 Rx interrupt vector is different on the 2560. It uses Vector 26 (0x0032). On the 328, it's at Vector 19 (0x0024)

Are you sure you've got the right board configuration?

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Thu Jul 19, 2012 12:45 pm

Board configuration as in....
Tools > Board > Arduino Mega 2560 or Mega ADK
in the IDE? Yes. I'll double check and retest shortly.
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Thu Jul 19, 2012 1:21 pm

Still no dice. I've tried about every variation of the USART0_RX_vect variable I can think of.
USART0_RXC_vect, USART_RXC_vect, I've even tried the other USARTs 1, 2 and 3.
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by mtbf0 on Thu Jul 19, 2012 5:45 pm

the correct vector for the receive interrupt on usart0 on the m2560 is USART0_RX_vect.

see arduino-1.0.1/hardware/tools/avr/lib/avr/include/iomxx0_1.h

couple of questions.

why not use hardware spi? much faster. much cleaner code.

why wait for all of the data to begin output? an uno really ought to have plenty of ram for this 2k - 900 still leaves 1148 bytes. my thought is while you are displaying one frame, the data for the next starts coming in and just gets placed in the buffer. soon you are all out of sync. you can start loading the next frame's data at anytime and it won't display until the ws2801 sees that 800us pause. (if i recall correctly isn't only 500us required?) of course, i have no idea what your host software is up to so i may be blowing smoke.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Fri Jul 20, 2012 2:26 pm

@mtbf0: Meanwhile, on the host side...
http://www.solderlab.de/index.php/software/glediator
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by mtbf0 on Sat Jul 21, 2012 12:22 am

it's a tad late for this question, but what error exactly are you getting? compilation failure? runtime failure?
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Sun Jul 22, 2012 11:52 pm

The only "error" is that the lights aren't getting the data being pushed to them for some reason and therefore don't light up. The IDE isn't reporting anything fishy. Compiles and uploads fine. Moreover, I can see the arduinos busy light so I know it's receiving something, the pixel lights just don't turn on. Works flawlessly on the UNO so its gotta be some config change with the mega. No word back from the host programs dev team yet.
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Re: Translate Arduino UNO USART to Arduino MEGA USART

by mtbf0 on Mon Jul 23, 2012 1:55 am

which led is flashing? the one on the rx line, the pin 13 led or both?

got a picture of your setup?
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Translate Arduino UNO USART to Arduino MEGA USART

by anstone on Wed Oct 24, 2012 11:51 am

ALAS! I've found the answer...

Along with changing the interrupt vector to USART0 since the Mega has 4 of them, you must also define the serial data output ports and serial data clock ports for the mega.
Everything else in the ISR stays the same as long as you're using USART0, otherwise you'd change the registers as well.

Code: Select all | TOGGLE FULL SIZE
#define SDO_Port          PORTH
#define SDO_DDR           DDRH
#define SDO_Pin           4

#define CLK_Port          PORTH
#define CLK_DDR           DDRH
#define CLK_Pin           5


Just thought I'd update...Cheers! :D
anstone
 
Posts: 34
Joined: Tue Mar 06, 2012 11:23 am

Please be positive and constructive with your questions and comments.