I think this will help and improve the USART library.
Something like
procedure Usart_Write_Buf(var Buf : array [20] of byte)
or something that sends an array of bytes, and not just one byte. It will be better than using a cycle and in its body to call Usart_Write N times.
The reason I'm posting this is that if you have an array of char or a string and fill it with some bytes and after that use Usart_Write_Text if there's a $00 byte somewhere in the string the text is sent up to the $00 byte. The procedure Usart_Write_Text stops sending when it meets a $00 byte. Other ideas are welcome to solve this problem
Also - what I saw (as a problem maybe) was on a 18F4620 with a 10MHz, using PLL, i.e. 40MHz, using the UART on 115200 calling 10 times Usart_Write sends incorrect bytes. Putting a delay of 1ms between each call solved the problem, but that's not the idea.
Usart_Write_Buf
Look at this fragments of my network protocol and you may write what you need based on them:
Code: Select all
function Usart_Write_Byte(data_to_send : byte) : boolean;
begin
if TXSTA.TRMT = 1 then
begin //send
TXSTA.TXEN := 0;
TXREG := data_to_send;
TXSTA.TXEN := 1;
Result := True;
end
else
begin //don't send
//TXSTA.TXEN := 0;
Result := False;
end;
end;
Code: Select all
ns_SEND_FRAME :
begin
buffer_addr := @Tx_Buffer + frame_offset; //send current byte
if Usart_Write_Byte(buffer_addr^) then
Inc(frame_offset); //prepare for next byte
if (frame_offset >= (FRAME_LENGTH_MIN + databytes)) and
(frame_offset <= FRAME_LENGTH_MAX - 2) then
frame_offset := FRAME_LENGTH_MAX - 2; //to send EOF and BCC
if frame_offset >= FRAME_LENGTH_MAX then
NETWORK_STATUS := ns_SEND_FRAME_END; //goto wait for last byte to be sended
end;