UART does not do what I want

I have development which sends a UART command which my logic analyzer recognized. Now I tried to duplicate this to integrate in my PCB, but I can’t get it to work.

This is my code:
byte buf[8];
buf[0] = 0;
buf[1] = 170;
buf[2] = 0;
buf[3] = 3;
buf[4] = 0;
buf[5] = 2;
buf[6] = 0;
buf[7] = 251;
Serial.write(buf, sizeof(buf));

But it does not give the values I programmed? (see attachment). I repeated it a couple of times but everytime a couple of bits seem off.

Details:
AtMega328p 3.3v 8Mhz, burned bootloader and programmed with USBASP, Saleae USB logic analyzer

waveform.PNG

Well, you declared buf to have 4 bytes. You tried to store 8 bytes into it and then only wrote the first four of them because sizeof(buf) is 4.

Pete

Oops, that needed to change. However, the problem still persists...

You didn't fix it correctly.

Pete

But it does not give the values I programmed? (see attachment). I repeated it a couple of times but everytime a couple of bits seem off.

Do you want that the Serial Monitor should show the values which you have exactly stored into the array members? If so, execute the following codes:

void setup()
{
  Serial.begin(9600);
  byte buf[8];
  buf[0] = 0;
  buf[1] = 170;
  buf[2] = 0;
  buf[3] = 3;
  buf[4] = 0;
  buf[5] = 2;
  buf[6] = 0;
  buf[7] = 251;

  for (int i = 0; i < sizeof(buf); i++)
  {
    Serial.println(buf[i]);
  }
}

void loop() 
{

}

I really don’t understand what’s happening here. If I use Coolterm to send a hex string which should turn on the bluetooth module it works, but the waveform it sends contains completely different data.

For example, the string I send is “AA 00 03 02 00 51 AA”
And then my logic analyzer shows “00 AA 00 03 00 02 00 FB 00 AA 00 03 2D 05 08 C3” with a small pause in the middle. I think it would be logical to replicate this command on my PCB (Atmega328p) however I can’t seem to replicate this command (or the original hex string for that matter) even if I write it byte for byte.

The whole documentation package can be downloaded here: http://ww1.microchip.com/downloads/en/DeviceDoc/DSPK%20v2.1%20Package.zip

I also attached a screenshot of the UART protocol from the related document. Maybe that helps

uart protocol.PNG