PDA

View Full Version : 16F18446 - setting baudrate for HW EUSART



flotulopex
- 14th December 2025, 17:54
Hi All,

I had an example from Richard lately where he configured my 16F18446 HW serial's port.

I didn't pay that much attention to it...

Now that I'm looking closer on "how he did it", I can't figure it out.

So I went to "visit" the DS and after lots of calculations and trials, I'm still stuck.

This is an extract from Richard's code (as said from another thread/project), the piece of code I'm working on.


' ====== PIC 16F18446 FUSES ================================================== =====================
' Internal oscillator
#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTS_PWRT_64 & _LPBOREN_OFF & _BOREN_SBOREN & _BORV_LO & _ZCDDIS_OFF & _PPS1WAY_OFF & _STVREN_ON
__config _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_SWDTEN & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _BBSIZE_BB512 & _BBEN_OFF & _SAFEN_OFF & _WRTAPP_OFF & _WRTB_OFF & _WRTC_OFF & _WRTD_OFF & _WRTSAF_OFF & _LVP_OFF
__config _CONFIG5, _CP_OFF
#ENDCONFIG

@ ERRORLEVEL -306
@ ERRORLEVEL -202

' ====== DEFINES / REGISTERS ================================================== ====================
DEFINE OSC 32
DEFINE ADC_BITS 12 'Number of bits in ADCIN result
DEFINE HSER_TXSTA 24h 'Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 'Clear overflow automatically

BAUDCON.3 = 1 'Enable 16 bit baudrate generator

' Baudrates @ 32MHz
' 9600
'DEFINE HSER_SPBRG 832
'SP1BRGH = 51

' 19200
''DEFINE HSER_SPBRG 416
'SP1BRGH = 25

' 57600
'DEFINE HSER_SPBRG 138
SP1BRGH = 0

' 115200
'DEFINE HSER_SPBRG 68
'SP1BRGH = 0

' ====== VARIABLES ================================================== ==============================
RC3PPS = 15 'TX PORTC.3 - set RC3 to EUSART TX

' ====== TEST PROGRAM ================================================== ===========================
HW_SERIAL:
HSerout ["Hello World!",13,10]
HSerout ["Testing...",13,10]
PAUSE 1000
GOTO HW_SERIAL:


While the settings for baudrates 57600 and 115200 work perfectly, I can't make it work for slower baudrates like 19200 or even 9600.


I tried to follow the DS and, even if in Richard's example some values don't chage like for instance the SP1BRGH on, I thought I should use the following formula.

10031


It doesn't work.


But looking at the baudrate examples table, I can't find the difference for all proposed baudrates.

10032


Can anyone please explain whre I'm missing the point (or points)?

richard
- 14th December 2025, 23:41
when you get a result like
DEFINE HSER_SPBRG 832 you realize something is very wrong since an 8 bit register can not accept that value ever

you have enabled the 16 bit baud rate generator in every case


BAUDCON.3 = 1 'Enable 16 bit baudrate generator

for 19200
SP1BRGL = 160:SP1BRGH = 1

for 9600
SP1BRGL = 64:SP1BRGH = 3


BAUDCON.3 = 0 '8 bit baudrate generator
for 9600
SP1BRGH = 51

for 19200
SP1BRGH = 25

richard
- 15th December 2025, 09:00
its probably better to say

there are two baudrate clock source dividers brgh=0 ; fosc/64 and brgh=1 ; fosc/16 and
and you have the choice of 8 or 16 bit baud rate generators, so 4 options for every baud rate


BAUDCON.3 = 1 'Enable 16 bit baudrate generator
brgh=1
for 19200
SP1BRGL = 160:SP1BRGH = 1

for 9600
SP1BRGL = 64:SP1BRGH = 3

brgh=0
for 19200
SP1BRGL = 103:SP1BRGH =0


for 9600
SP1BRGL = 207:SP1BRGH = 0





BAUDCON.3 = 0 '8 bit baudrate generator
brgh=1
for 9600
SP1BRG = 207


for 19200
SP1BRG = 103

brgh=0
for 9600
SP1BRG = 51

for 19200
SP1BRG = 25