Serial losing sync after a few bytes??

Hi all

So the evolution of my project has been an Arduino Nano until I ran out of memory, and I moved to a Mega. However for something different, I have moved to a mega1284p to take advantage of lazy-mode RAM utilisation, and the plethora of PCINT ports it has. I was always going to move to a bare chip, but the plan was originally a mega328p.

The project interfaces with a module over UART at 57600 baud rate. I can’t change that as the module is fixed.

I have been tearing my hair out lately as the communications have been breaking down to the module sparodically. After some stuffing about, I have observed the following:

Most commands are 7 - 8 bytes long and are transmitted over serial, with a reply expected and checked.

It has no issues reading / writing commands in that 7 - 8 byte length. Once we start to go over 10 bytes, the module will probably not acknowledge and reply… I say probably not because sometimes it works fine first go, and other times, it takes a few retries to get the message across. Sometimes 2 - 3, but often upwards of 6 - 10.

The arduino boards didn’t have this problem at all.

I have gotten around the problem by making the following code changes:
Old:

i = Serial1.write( Buffer, BufferLength );

New:

  for( i = 0; i < BufferLength; i++ ){
    Serial1.write( Buffer[i] );
    delay(1);
  }
  i = BufferLength;

In this new scenario, it goes through first time, no problems.

My gut feel is that the 1284p serial comms are slightly out of sync with external module… By a bee’s proverbial. The length of the communication is compounding the inaccuracy and causing the loss of sync. Adding the delay is just enough to keep it in sync while not enough to cause the module to time out.

The 1284p is a DIL version, mounted on a breadboard. I have a 16mhz crystal with trimmed leads as close as practical to the processor pins. I also have 2 x 22uF ceramic caps (with legs trimmed) between the clock legs and GND. Specifically for the crystal, it has the following specs:

  • Tolerance +/- 30 PPM
  • Load capacitance - 20uF
  • Series resistance - 40 ohms

Is this a suitable crystal? I’ve always just used arduino boards in the past, so I’m not sure if there’s a gotcha that I’ve missed. One of the goals of the project is to use a bare chip as I will make a PCB after I finish the prototyping.

(deleted)

Which 1284 core are you using? Do you have the fuses set properly to use the 16MHz external crystal?

57600 is a particularly problematic bitrate for a 16MHz AVR, though with a crystal you should be within specs, usually. Try using "Serial.begin(57601);"

spycatcher2k > Sorry, a typo.

westfw > That's just the ticket. Problem solved - this is the sort of gotcha that I'm talking about.

westfw:
Which 1284 core are you using? Do you have the fuses set properly to use the 16MHz external crystal?

57600 is a particularly problematic bitrate for a 16MHz AVR, though with a crystal you should be within specs, usually. Try using "Serial.begin(57601);"

Problematic for a 16MHz avr? Looks like "problematic for a 16MHz Arduino" is more accurate - from those comments, it looks like a "backwards compatibility with a screwup that shipped" thing?

I’m using a mightycore…

From the looks of it, I would be better off moving to a 18.432mhz crystal.