0
votes

everyone.

I am using PIC18F452 micro-controller. I receive data on UART, frame it by attaching extra sync words and transmit it, I face problems in sending the sync words in sequence. What I want is to send words say... EA 09 C3 with the data. Now I have this code written in MikroC:

int j=0;
SPBRG = 129;
TXSTA = 0b00100110;
RCSTA = 0b10010000;
while(1)
{
   if(j == 0)
   {
     TXREG = 0xEA;
   }
   else if(j == 1)
   {
     TXREG = 0x09;
   }
   else if(j == 2)
   {
     TXREG = 0xC3;
   }
   else
   {
    TXREG = RCREG;
   }
   while(!TRMT); // wait for whole data frame to be ready for transmission
  if(j == 100)
     j = 0; // reset j after 100 bytes
  else
     j++;
   }

Now what happens here is that sync words are transmitted but out of sequence and sometimes byte duplication also occurs. What I want is that j should only increment after one byte is transmitted, here I think j increments independently of the transmission.

Thanking in anticipation.

Regards, Hassan

1

1 Answers

0
votes

The hidden roule of uart transmission is:

_ _ Put a little delay between each byte you send _ _

Note that this is a baud rate independant secret ;).

For instance 50 micro seconds.

Since sender and receiver are working async, they use too much effort to sync per byte while receiving and you keep stuffing them... They simply stall. Sometimes drop the byte, sometimes dropping it without clearing receive regs so duplicating the bytes.

So if speed is critical, first give a sending delay like 50ms or more per byte, then if it works narrow it, take it down to 10 micro seconds for instance. Keep narrowing it until it starts to stall again and preferably set the delay to two times of that limit for failsafe.

Not In a hurry or starving for a speed I hope, else use another communication channel not usart.