News:

Let's find out together what makes a PIC Tick!

Main Menu

Serin vs HSerin

Started by JackB, Sep 29, 2025, 07:22 PM

Previous topic - Next topic

JackB

Hi everyone,

I posted some code about an LCD Backpack using a HD44780 IC from Hitachi

and I've got a comment on 'why not using the Hserin command instead of Serin'

first I used Serin from start and it's worked fine from 300 to 19200 baud rate either 8Mhz or 20Mhz

then I added the 'Hserin' and the display show up random characters as if the baud rate is not the same between

the sender (12F683) and receiver chip (16F690).

I did alternate from the Serin and Hserin, and the Serin work fine, but not the Hserin.

I'm sure I'm missing something here,,,,

if someone as any guess on how to fix this I'll appreciate a lot.

Thank's to all.


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Project : Backpack
' Name    : 16F690 LCD BACKPACK for Hitachi HD44780 
' Date    : Sep 29-2025
' Note    : 383 words used  of 4096
' Notes   : Serin  Baud Rate 300 ~ 19200
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Device = 16F690

'Declare Xtal = 8
'Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF

Declare Xtal = 20
Config HS_OSC, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF

ANSEL = 0           ' Set all pins digital
ANSELH = 0          ' Set all pins digital
TRISA = 0 : TRISB = %00100000 : TRISC = 0
PORTA = 0 : PORTB = 0 : PORTC = 0
OSCCON = $70 

Declare Hserial_Baud = 9600 ' Set Baud rate to 9600 for HSerin
Declare Hserial_Clear = On ' Clear the buffer before receiving

Dim En As PORTB.4 : Low En
Dim RS As PORTB.6 : Low RS
Dim B1 As Byte : B1 = 0

' 300=3313  600=1646  1200=813  2400=396  4800=188  9600=84 14400=49 19200=32
Dim BaudR As Word : BaudR = 84

High PORTA.0              ' Backlight ON
GoSub LCDINIT             ' initialise LCD

'Serin
main:

'    SerIn PORTB.5,BaudR,3,main,[B1]
HSerIn 3, main, [B1]
If B1 < 253 Then     ' Characters
        PORTC = B1
        High En : DelayUS 2 : Low En
GoTo main
ElseIf B1 = 254 Then  ' LCD Command
Low RS           

'        SerIn PORTB.5,BaudR,3,main,[B1]
HSerIn 3, main, [B1]
PORTC = B1   ' output the data
        High En : DelayUS 2 : Low En
High RS           ' character mode
GoTo main         
ElseIf B1 = 255 Then 

'        SerIn PORTB.5,BaudR,3,main,[B1]
HSerIn 3, main, [B1]
        If B1 = 0 Then
            Low PORTA.0   ' Back Light OFF
        EndIf
        If B1 = 8 Then
            High PORTA.0  ' Back Light ON
        EndIf
    GoTo main             ' loop back to top
    EndIf

LCDINIT:   ' Standard LCD Module Initialisation
PORTC = %00000001   ' 2, Clear Display
    High En : DelayUS 300 : Low En ' Send data
    DelayMS 4
PORTC = %00000010   ' 4, Return Home
    High En : DelayUS 300 : Low En
    DelayMS 4
PORTC = %00111000      ' 1, 8 bit, 2 line, 5x8 Characters
    High En : DelayUS 300 : Low En
    DelayMS 4
' PORTC = %00001100   ' Display on, no cursor, no blink
PORTC = %00001110   ' 1, Display on, cursor, blink
    High En : DelayUS 300 : Low En
    DelayMS 4
PORTC = %00000110   ' 1, Entry mode
    High En : DelayUS 300 : Low En
    DelayMS 4
    High RS          ' character mode
Return




















Pepe

I don't know why, if you set it to 2400 baud instead of 9600, it works.

JackB

#2
Hello Pepe,

you're right, you find it !

very strange
just reload both pic, 12F683 sender at 9600 baud, and receiver 16F690 at 2400 baud and it work
maybe the Hserin instruction mix baud rate 2400 for 9600.
here's both code

Many thank's Pepe for your help,





'-----------------------------------------------------------------------------
'  Project : Serial Sender 12F683
'  Name    : Serial LCD 300-19200.bas  for Hitachi HD44780
'  File    : "D:\Positron Code\12F683\Serial LCD\Serial LCD 300-19200.bas"
'  Date    : 9/28/2025
'  Notes   : Serial send
'          : 298 words used of 2048
'-----------------------------------------------------------------------------
Device = 12F683

'Declare Xtal = 20  ' HS_OSC
'Config  HS_OSC, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOD_OFF, IESO_OFF, FCMEN_OFF

Declare Xtal = 8  ' INTOSCIO
Config  INTOSCIO, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOD_OFF, IESO_OFF, FCMEN_OFF

Dim LCD As GPIO.0
Dim BaudR As Word
' 300=3313  600=1646  1200=813  2400=396  4800=188  9600=84  14400=49  19200=32
BaudR = 84
TRISIO = %00001000
OSCCON = $70
SerOut LCD, BaudR,[254,1] ' Clear LCD
DelayMS 40                ' LCD clear delay
Do
    SerOut LCD, BaudR,[254,128,"1234567890&*()-="]   ' Line 1, 16 Characters
    SerOut LCD, BaudR,[254,192,"1234567890abcdef"]   ' Line 2, 16 Characters
    DelayMS 1000
    SerOut LCD, BaudR,[255,0] ' Backlight Off
    DelayMS 100
    SerOut LCD, BaudR,[255,8] ' Backlight On
    DelayMS 2000
    SerOut LCD, BaudR,[254,1] ' Clear LCD
    DelayMS 2000
Loop







'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Project : Backpack
' Name    : 16F690 LCD BACKPACK for Hitachi HD44780 
' Date    : Sep 29-2025
' Note    : 383 words used  of 4096
' Notes   : Serin  Baud Rate 300 ~ 19200
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Device = 16F690

'Declare Xtal = 8
'Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF

Declare Xtal = 20
Config HS_OSC, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF

ANSEL = 0           ' Set all pins digital
ANSELH = 0          ' Set all pins digital
TRISA = 0 : TRISB = %00100000 : TRISC = 0
PORTA = 0 : PORTB = 0 : PORTC = 0
OSCCON = $70 

Dim En As PORTB.4 : Low En
Dim RS As PORTB.6 : Low RS
Dim B1 As Byte : B1 = 0

Declare Hserial_Baud = 2400 ' Set Baud rate to 9600 for HSerin
Declare Hserial_Clear = On ' Clear the buffer before receiving

' 300=3313  600=1646  1200=813  2400=396  4800=188  9600=84 14400=49 19200=32
Dim BaudR As Word : BaudR = 84

High PORTA.0              ' Backlight ON
GoSub LCDINIT             ' initialise LCD
 
'Serin
main:

'    SerIn PORTB.5,BaudR,1,main,[B1]
HSerIn 5, main,[B1]
If B1 < 253 Then     ' Characters
    PORTC = B1
        High En : DelayUS 1 : Low En
GoTo main
ElseIf B1 = 254 Then  ' LCD Command
Low RS           
'        SerIn PORTB.5,BaudR,1,main,[B1]
HSerIn 5, main, [B1]
PORTC =  B1   ' output the data
        High En : DelayUS 1 : Low En
High RS           ' character mode
GoTo main         
ElseIf B1 = 255 Then 
'       SerIn PORTB.5,BaudR,1,main,[B1]
HSerIn 5, main, [B1]
        If B1 = 0 Then
            Low PORTA.0   ' Back Light OFF
        EndIf
        If B1 = 8 Then
            High PORTA.0  ' Back Light ON
        EndIf
    GoTo main             ' loop back to top
    EndIf

LCDINIT:   ' Standard LCD Module Initialisation
PORTC = %00000001   ' 2, Clear Display
    High En : DelayUS 10 : Low En ' Send data
    DelayMS 4
PORTC = %00000010   ' 4, Return Home
    High En : DelayUS 10 : Low En
    DelayMS 4
PORTC = %00111000      ' 1, 8 bit, 2 line, 5x8 Characters
    High En : DelayUS 10 : Low En
    DelayMS 4
' PORTC = %00001100   ' Display on, no cursor, no blink
PORTC = %00001110   ' 1, Display on, cursor, blink
    High En : DelayUS 10 : Low En
    DelayMS 4
PORTC = %00000110   ' 1, Entry mode
    High En : DelayUS 10 : Low En
    DelayMS 4
    High RS          ' character mode
Return






Pepe

If the BRG16 bit in BAUDCTL is cleared, it works correctly; this is something Les needs to check.


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Project : Backpack
' Name    : 16F690 LCD BACKPACK for Hitachi HD44780
' Date    : Sep 29-2025
' Note    : 383 words used  of 4096
' Notes   : Serin  Baud Rate 300 ~ 19200
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F690

Config FOSC_INTRCIO, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOREN_OFF, IESO_OFF, FCMEN_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Declare Xtal = 8
Declare Create_Coff On
Declare Watchdog off

TRISA = 0 : TRISB = %00100000 : TRISC = 0
PORTA = 0 : PORTB = 0 : PORTC = 0
OSCCON = $70

Declare Hserial_Baud = 9600 ' Set Baud rate to 2400 for HSerin
Declare Hserial_Clear = On ' Clear the buffer before receiving
Declare HSerin_Pin = PORTB.5

Dim En As PORTB.4 : Low En
Dim RS As PORTB.6 : Low RS
Dim B1 As Byte  = 0

High PORTA.0              ' Backlight ON
GoSub LCDINIT             ' initialise LCD

BAUDCTL.3 = 0

main:

B1 = HRSIn, {3, main}
If B1 < 253 Then     ' Characters
        PORTC = B1
        High En : DelayUS 2 : Low En
GoTo main
ElseIf B1 = 254 Then  ' LCD Command
Low RS           

B1 = HRSIn, {3, main}
PORTC = B1   ' output the data
        High En : DelayUS 2 : Low En
High RS           ' character mode
GoTo main         
ElseIf B1 = 255 Then

B1 = HRSIn, {3, main}
        If B1 = 0 Then
            Low PORTA.0   ' Back Light OFF
        EndIf
        If B1 = 8 Then
            High PORTA.0  ' Back Light ON
        EndIf
    GoTo main             ' loop back to top
    EndIf

LCDINIT:   ' Standard LCD Module Initialisation
PORTC = %00000001   ' 2, Clear Display
    High En : DelayUS 300 : Low En ' Send data
    DelayMS 4
PORTC = %00000010   ' 4, Return Home
    High En : DelayUS 300 : Low En
    DelayMS 4
PORTC = %00111000      ' 1, 8 bit, 2 line, 5x8 Characters
    High En : DelayUS 300 : Low En
    DelayMS 4
' PORTC = %00001100   ' Display on, no cursor, no blink
PORTC = %00001110   ' 1, Display on, cursor, blink
    High En : DelayUS 300 : Low En
    DelayMS 4
PORTC = %00000110   ' 1, Entry mode
    High En : DelayUS 300 : Low En
    DelayMS 4
    High RS          ' character mode
Return

JackB

#4
Hello,

just added BAUDCTL.3 = 0, and as you mention it's working fine

does the BAUDCTL.3 is for 16 bit or 8 bit receiving mode, ?

Many thank's Pepe for your help, does Les will be aware of this issue?

was able to run the 16F690 Hserin with theses baud rate :   '9600, 14400, 19200,  28800,  38400.

and the Serin at theses rate:  300, 600, 1200, 2400, 4800, 9600,14400, 19200, 28800, 38400




here's the Backpack 16F690 latest update.



'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Project : LCD Backpack
' Name    : 16F690 LCD BACKPACK for Hitachi HD44780
' File    : "D:\Positron Code\16F690\LCD Backpack\16F690 LCD Backpack 300-38400.bas"
' Date    : Sep 30 2025
' Notes   : 334 words used  of 4096
'         : Serin  Baud Rate 300 ~ 38400
'         : Hserin Baud rate 9600,14400,19200,28800,38400
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Device = 16F690

'Declare Xtal = 8
'Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF

Declare Xtal = 20
Config HS_OSC, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF

ANSEL = 0           ' Set all pins digital
ANSELH = 0          ' Set all pins digital
TRISA = 0 : TRISB = %00100000 : TRISC = 0
PORTA = 0 : PORTB = 0 : PORTC = 0
OSCCON = $70 

Dim En As PORTB.4 : Low En
Dim RS As PORTB.6 : Low RS
Dim B1 As Byte : B1 = 0
                     
'BAUDCTL = %00000000           'x.3 must be 0
BAUDCTL.3 = 0                ' must be set for baud rate

Declare Hserial_Baud = 38400 ' 9600,14400,19200,28800,38400
Declare Hserial_Clear = On   ' Clear the buffer before receiving

' 300=3313  600=1646  1200=813  2400=396  4800=188  9600=84  14400=49  19200=32  28800=15  38400=6
Dim BaudR As Word : BaudR = 6

High PORTA.0                 ' Backlight ON
GoSub LCDINIT                ' initialise LCD
 
'Serin
main:

'    SerIn PORTB.5,BaudR,3,main,[B1]  ' Timeout 3 minimum for slow baud rate
HSerIn 5, main,[B1]
   If B1 < 253 Then     ' Characters
       PORTC = B1
        High En : DelayUS 10 : Low En
      GoTo main      
   ElseIf B1 = 254 Then  ' LCD Command
      Low RS           
'        SerIn PORTB.5,BaudR,3,main,[B1]
HSerIn 5, main, [B1]
      PORTC =  B1     ' output the data
        High En : DelayUS 10 : Low En
      High RS           ' character mode
      GoTo main         
   ElseIf B1 = 255 Then 
'       SerIn PORTB.5,BaudR,3,main,[B1]
HSerIn 5, main, [B1]
        If B1 = 0 Then
            Low PORTA.0   ' Back Light OFF
        EndIf
        If B1 = 8 Then
            High PORTA.0  ' Back Light ON
        EndIf
      GoTo main             ' loop back to top
    EndIf

LCDINIT:             
PORTC = %00000001 : High En : DelayUS 1 : Low En : DelayMS 4  ' Clear display
PORTC = %00000010 : High En : DelayUS 1 : Low En : DelayMS 1  ' Return Home     
PORTC = %00111000 : High En : DelayUS 1 : Low En : DelayMS 1  ' 5x8 Character 8 Bit, 2 Line
' PORTC = %00001100    ' Display on, no cursor, no blink
PORTC = %00001110 : High En : DelayUS 1 : Low En : DelayMS 1  ' Display on, cursor, blink
PORTC = %00000110 : High En : DelayUS 1 : Low En : DelayMS 1  ' Entry mode
High RS           
Return


Pepe

With BRG16 enabled, you get a much wider range of baud rates and reduce error because the divisor can reach up to 65535 instead of just 255.
This is critical when using uneven clock frequencies (e.g., 8 MHz, 20 MHz, 48 MHz) and requiring a standard baud rate (e.g., 9600, 115200).

JackB

#6
is this mean with BRG16 on, my code should have worked, and Les will have to tweek the compiler
also can it mean I can use auto baud detect ABDEN bit

If I understand correctly , the ABDEN will be cleared when auto-baud is complete, from datasheet

then for the next different baud rate received, it will not work

eg:

receiving at  2400 baud           auto-baud complete then the bit is clear
receiving at  4800 baud           auto-baud is disable then no detection.

my atempt to use ABDEN was to send at different baud rate to the backpack (16F690) so it can auto detect and display the right data.


quick update for the Backpack code:  High RS line should be remove to avoid random character on screen when booting.

PORTC = %00000110 : High En : DelayUS 1 : Low En : DelayMS 1  ' Entry mode
' High RS           
Return

'correct bit order for Display control

PORTC = %00001100 : High En : DelayUS 1 : Low En : DelayMS 1  ' %00001DCB Display on/off, Cursor on/off, Blink y/n




JackB

Oct 2 2025,

update on 16F690 Backpack, new LCDINT DelayMS to accomodate 28800,38400 baud rate for (Xtal = 20 only)

Declare Xtal = 20
Config HS_OSC, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF


LCDINIT:             
PORTC = %00000001 : High En : DelayUS 1 : Low En : DelayMS 4  ' Clear display
PORTC = %00000010 : High En : DelayUS 1 : Low En : DelayMS 2  ' Return Home     
PORTC = %00111000 : High En : DelayUS 1 : Low En : DelayMS 2  ' 5x8 Character 8 Bit, 2 Line
PORTC = %00001100 : High En : DelayUS 1 : Low En : DelayMS 2  ' %00001DCB Display on/off, cursor on/off, blink y/n
PORTC = %00000110 : High En : DelayUS 1 : Low En : DelayMS 2  ' Entry mode
Return

JackB

Here's a result of different baud rate test on the 16F690.

dr-zin

JackB,

Your 1st photo above of the Hserin display looks like a cool game.  :D

JackB

Good idea,

maybe I should call it

Battles of the Characters !



JackB

Here's an shrink down version of the 16F690 backpack
no serial involved just internal array's for displaying messages
for the LCD 1601, 1602, 2004 Hitachi display controller.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Project : LCD Backpack Self Text  (Write text on LCD no serial port)
' Name    : 16F690 LCD BACKPACK for Hitachi HD44780  1602 / 2004
' File    : "D:\Positron Code\16F690\LCD Backpack\16F690 LCD Backpack Self Text PORT.bas"
' Date    : Oct 3 2025
' Notes   : 320 words used of 4096
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Device = 16F690
'Declare Xtal = 8
'Config INTRC_OSC_NOCLKOUT, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF
Declare Xtal = 20
Config HS_OSC, WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, IESO_OFF, FCMEN_OFF
ANSEL = 0           ' Set all pins digital
ANSELH = 0          ' Set all pins digital
TRISA = 0 : TRISB = %00100000 : TRISC = 0 : PORTA = 0 : PORTB = 0 : PORTC = 0
OSCCON = $70 

Dim I As Byte

' 1601 1602 16 Char 1/2 Lines
Dim MyArrayA[16] As Byte = "0123456789ABCDEF"
Dim MyArrayB[16] As Byte = "abcdefghijklmnop"

' 2004  20 Char 4 Lines
'Dim MyArrayC[20] As Byte = "ABCDDE1234567890ABCD"
'Dim MyArrayD[20] As Byte = "abcdefghijklmnop1234"
'Dim MyArrayE[20] As Byte = "ABCDDE1234567890abcd"
'Dim MyArrayF[20] As Byte = "abcdefghijklmnop1234"

GoSub LCDINIT                ' initialise LCD
PORTA = 1                    ' Backlight ON

main:
' 1601 / 1602   
PORTB = 0 : PORTC = 128 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 3 ' Line 1
For I = 0 To 15
    PORTC = MyArrayA[I] : PORTB = %01010000 : DelayUS 1 : PORTB = %01000000 : DelayMS 1
Next I

PORTB = 0 : PORTC = 192 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 3 ' Line 2
For I = 0 To 15
    PORTC = MyArrayB[I] : PORTB = %01010000 : DelayUS 1 : PORTB = %01000000 : DelayMS 1
Next I

' 2004
'PORTB = 0 : PORTC = 128 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 3 ' Line 1
'For I = 0 To 19
'    PORTC = MyArrayC[I] : PORTB = %01010000 : DelayUS 1 : PORTB = %01000000 : DelayMS 1
'Next I
'PORTB = 0 : PORTC = 192 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 3 ' Line 2
'For I = 0 To 19
'    PORTC = MyArrayD[I] : PORTB = %01010000 : DelayUS 1 : PORTB = %01000000 : DelayMS 1
'Next I
'PORTB = 0 : PORTC = 148 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 3 ' Line 3
'For I = 0 To 19
'    PORTC = MyArrayE[I] : PORTB = %01010000 : DelayUS 1 : PORTB = %01000000 : DelayMS 1
'Next I
'PORTB = 0 : PORTC = 212 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 3 ' Line 4
'For I = 0 To 19
'    PORTC = MyArrayF[I] : PORTB = %01010000 : DelayUS 1 : PORTB = %01000000 : DelayMS 1
'Next I

DelayMS 2000
PORTB = 0 : PORTC = 1 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : PORTB = %01000000 : DelayMS 4  ' Clear display
DelayMS 255
PORTA = 0             ' Bacck Light off
DelayMS 255
PORTA = %00000001     ' Bacck Light on
DelayMS 255

GoTo main

LCDINIT:  
PORTC = %00000001 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : DelayMS 4  ' Clear display
PORTC = %00000010 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : DelayMS 2  ' Return Home     
PORTC = %00111000 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : DelayMS 2  ' 5x8 Character 8 Bit, 2 Line
PORTC = %00001100 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : DelayMS 2  ' %00001DCB Display on/off, Cursor on/off, Blink y/n
PORTC = %00000110 : PORTB = %00010000 : DelayUS 1 : PORTB = 0 : DelayMS 2  ' Entry mode
Return