There is also a problem at high baudrates, because the SPBRG calculation takes just the integer part of the result, and it should be rounded to +1 when the decimal > 0.5.
For example at 115200 teoretical SPBRG should be 9.85, but 9 is parsed, then it does not work, but with SPBRG=10 it works ok. For lower speeds the error is small enought to not create problems.
This works for me (usart.h):
SPBRG_TEMP = ChipMHz / USART_BAUD_RATE / 16 * 1000000
if int(SPBRG_TEMP + 0.5) = int(SPBRG_TEMP) then SPBRG_TEMP -= 1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Now is:
sub HSerSend(In SerData)
#ifdef PIC
#ifdef USART_BLOCKING
Wait While TXIF = On '...................HERE
#endif
TXREG = SerData
#endif
it should be:
sub HSerSend(In SerData)
#ifdef PIC
#ifdef USART_BLOCKING
Wait While TXIF = Off
#endif
TXREG = SerData
#endif
And... Just an optimisation in InitUsart. This save two unneeded banksels:
Sub InitUSART
#ifdef PIC
'Set baud rate
SPBRG = SPBRGL_TEMP
#ifdef Bit(BRG16)
SPBRGH = SPBRGH_TEMP
BRG16 = BRG16_TEMP
#endif
BRGH = BRGH_TEMP
'Enable async mode
Set SYNC Off
'Enable TX and RX
Set TXEN On
Set CREN On
'Enable Serial Port
Set SPEN On
#endif
Just first set all bits in bank1 registers , then the bank0 ones.
There is also a problem at high baudrates, because the SPBRG calculation takes just the integer part of the result, and it should be rounded to +1 when the decimal > 0.5.
For example at 115200 teoretical SPBRG should be 9.85, but 9 is parsed, then it does not work, but with SPBRG=10 it works ok. For lower speeds the error is small enought to not create problems.
This works for me (usart.h):
SPBRG_TEMP = ChipMHz / USART_BAUD_RATE / 16 * 1000000
if int(SPBRG_TEMP + 0.5) = int(SPBRG_TEMP) then SPBRG_TEMP -= 1