News:

;) This forum is the property of Proton software developers

Main Menu

Uart settings for 18F87K90

Started by Dave-S, Feb 16, 2026, 11:38 AM

Previous topic - Next topic

Dave-S

Uart settings for 18F87K90

Trying to set HSerOut/HSerIn on a 18F87K90 so as to send AT commands to a WizFi380 wifi unit.
I have it working ok on a 18F47K40 using Uart2,
Device = 18F47K40
Declare Xtal = 64
Declare Hserial2_Baud = 115200 'Set the Baud rate for USART1
Declare HRSOut2_Pin  = PORTD.6 'Set the pin used for USART1 TX
Declare HRSIn2_Pin   = PORTD.7 'Set the pin used for USART1 RX
Declare Hserial2_RCSTA = %10010000 'Enable serial port and continuous receive
Declare Hserial2_Clear = On 'Enable Error clearing on received characters      ' Clear the buffer before receiving
 TRISD.6 = 0
 TRISD.7 = 1
I need to use a larger I/O pin pic so installed it onto a 18F87K90 but cannot get the UART1 to work,
Device = 18F87K90
Declare Xtal = 64
OSCCON = %11111100    'for 64
OSCTUNE = %11000000 'PLL on
Declare Hserial1_Baud = 9600   '115200    ' Set the Baud rate for USART1
Declare HSerOut1_Pin  = PORTC.6        ' Set the pin used for USART1 TX
Declare HSerIn1_Pin   = PORTC.7
Declare Hserial1_RCSTA = %10010000   ' Enable serial port and continuous receive
Declare Hserial1_TXSTA = %10101100 'enable transmit and asynchronous mode
Declare Hserial2_Clear = On
BAUDCON1 = %00001000
Declare Access_Upper_64K = True
TRISC.6 = 0
TRISC.7 = 1
Tried setting the Hserial1_RCSTA, Hserial1_TXSTA and BAUDCON1 but cannot get it to send, it appears that HSerOut1 is not functioning.  Using a serial terminal on PC HSerIn1 receives data but sending data using HSerOut1 i get nothing.

Obviously my settings are not correct and need some help. I did try using Uart2, PORTG1/PORTG2.

Any help Appreciated.
Thanks David





Stephen Moss

I may well be wrong as I don't often use serial commands but have you tried removing the channel number qualifier (1), so that instead of...
 
Declare HSerOut1_Pin  = PORTC.6        ' Set the pin used for USART1 TX
you use
Declare HSerOut_Pin  = PORTC.6        ' Set the pin used for USART1 TX.

Back in the day devices with such capabilities will originally only had one channel, so there was no need to quantify it with a 1 designator in the original command. I think it was only when multi-channel device started coming out and the command was subsequently expanded to accommodate that that you have to then specify a channel number if not using the default channel (1).
I cannot recall whether or not the commands for channel 1 without the channel qualifier (HSerOut_Pin) was subsequently aliased to that with the qualifier (HSerOut1_Pin), if so then removing the channel designator for the first channel will make no difference.


John Lawton

There are two RCSTA registers, RCSTA1 and RCSTA2, (and TXSTA1/2) which are you setting?

See: https://www.farnell.com/datasheets/1669477.pdf

page 349.
-----------------------------------------------------------------------------------------
Amicus 8 and 16A/16B dev boards
Especially created for Positron development
https://www.easy-driver.co.uk/Amicus

Dave-S

As in above Code
Declare Hserial1_RCSTA = %10010000   ' Enable serial port and continuous receive
Declare Hserial1_TXSTA = %10101100 'enable transmit and asynchronous mode
Have tried
Hserial_RCSTA
Hserial_TXSTA

John Lawton

I'm a believer in setting the registers directly rather than using the declares.

John
-----------------------------------------------------------------------------------------
Amicus 8 and 16A/16B dev boards
Especially created for Positron development
https://www.easy-driver.co.uk/Amicus

top204

#5
The Hserialx_RCSTA and Hserialx_TXSTA declares have not been required for quite a few years now, since I made the compiler automatically setup a USART's SFRs based upon the Baud required. They will actiully over-write some of the setups made by the compiler, and were left in place for backward compatability, or if a user wants to manipulate the RCSTAx or TXSTA SFRs from them, instead of by the SFRs themselves in a code listing.

Below is a tested code listing that sets up EUSART1 and EUSART2, and receives a byte and re-transmits it using EUSART1. Please note that the Proteus Isis simulator does not simulate the EUSART2 correctly on these devices, but EUSART1 works in the simulator.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Setup a PIC18F87K90 device to operate at 64 MHz using its internal oscillator and 4xPLL enabled.
' And setup EUSART1 for TX and RX
'
' Written for the Positron8 compiler by Les Johnson.
' https://sites.google.com/view/rosetta-tech/positron-compilers-experimenters-notebook

    Device = 18F87K90                           ' Tell the compiler what device to compile for
    Declare Xtal = 64                           ' Tell the compiler what frequency the device will be operating at (in MHz)
    Declare Auto_Heap_Strings = On              ' Tell the compiler to create Strings above standard variables, so assembler code is more efficient
    Declare Auto_Variable_Bank_Cross = On       ' Tell the compiler to create any multi-byte variables in the same RAM bank. For more efficiency
'
' Setup EUSART1
'
    Declare Hserial1_Baud  = 115200             ' Set the Baud rate for EUSART1
    Declare HRSOut1_Pin    = PORTC.6            ' Set the TX pin for EUSART1
    Declare HRSIn1_Pin     = PORTC.7            ' Set the RX pin for EUSART1
    Declare Hserial1_Clear = On                 ' Enable Error clearing on received bytes
'
' Setup EUSART2
'
    Declare Hserial2_Baud  = 115200             ' Set the Baud rate for EUSART2
    Declare HRSOut2_Pin    = PORTG.1            ' Set the TX pin for EUSART2
    Declare HRSIn2_Pin     = PORTG.2            ' Set the RX pin for EUSART2
    Declare Hserial2_Clear = On                 ' Enable Error clearing on received bytes
'
' Create any global variables and constants here
'
    Dim Bytein As Byte
   
'----------------------------------------------------------------------
' The main program starts here
' Echo bytes received from EUSART1, and jump to a timeout routine if no bytes received within approx 2 seconds.
'
Main:
    Setup()                                     ' Setup the program and any peripherals
    HRSOut1Ln "Hello World"                     ' Transmit from EUSART1 text to a serial terminal
    HRSOut1Ln "Type characters and see them echoed back"

    Do                                          ' Create a loop
Again:
        HSerIn1 2000, TimedOut, [Bytein]        ' Receive a byte into Bytein from EUSART1 (time out after approx 2 seconds)
        HRSOut1 Bytein                          ' Transmit what was received
    Loop                                        ' Do it forever
'
' Jump here if EUSART1 timed out
'
TimedOut:
    HRSOut1Ln "\rTimed Out"                     ' Transmit text to a serial terminal
    GoTo Again                                  ' Jump back to receive more data
   
'----------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Int_Osc64MHz()                              ' Set the internal oscillator to operate at 64MHz
'
' More setups here if required
'
EndProc

'----------------------------------------------------------------------
' Set the internal oscillator to operate at 64MHz on a PIC18F87K90 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Int_Osc64MHz()
    OSCCON  = %01110000                         ' 16 MHz
    OSCCON2 = %00000000
    OSCTUNE = %01000000                         ' INTRC. PLLEN enabled
    REFOCON = %00000000
    DelayMS 10                                  ' Wait for stability
EndProc

'----------------------------------------------------------------------
' Setup the fuses for internal oscillator on a PIC18F87K90
' The OSC pins are set as general purpose I/O
'
Config_Start
    RETEN = On                                  ' VREG Sleep enabled
    INTOSCSEL = High                            ' LF-INTOSC in High-power mode during Sleep
    SOSCSEL = Dig                               ' Digital (SCLKI) mode
    XINST = Off                                 ' Extended Instruction Set disabled
    FOSC = INTIO2                               ' Internal RC oscillator
    PLLCFG = Off                                ' PLL x4 disabled
    FCMEN = Off                                 ' Fail-Safe Clock Monitor disabled
    IESO = Off                                  ' Internal External Oscillator Switch Over Mode disabled
    PWRTEN = Off                                ' Power Up Timer disabled
    BOREN = SBORDIS                             ' Brown Out Detect enabled in hardware. SBOREN disabled
    BORV = 3                                    ' Brown-out Reset Voltage is 1.8V
    BORPWR = ZPBORMV                            ' ZPBORMV instead of BORMV is selected
    WDTEN = Off                                 ' Watchdog Timer disabled in hardware. SWDTEN bit disabled
    WDTPS = 128                                 ' Watchdog Postscaler is 1:128
    RTCOSC = SOSCREF                            ' RTCC uses SOSC
    CCP2MX = PORTC                              ' CCP2 Mux is RC1
    ECCPMX = PORTE                              ' Enhanced CCP1/3 [P1B/P1C/P3B/P3C] muxed with RE6/RE5/RE4/RE3
    MSSPMSK = MSK7                              ' MSSP address is 7 Bit address masking mode
    MCLRE = On                                  ' MCLR enabled. RG5 disabled
    STVREN = On                                 ' Stack Overflow Reset enabled
    BBSIZ = BB2K                                ' 2K word Boot Block size
    Debug = Off                                 ' Background Debug disabled
    Cp0 = Off                                   ' Code Protect 00800-03FFF disabled
    CP1 = Off                                   ' Code Protect 04000-07FFF disabled
    CP2 = Off                                   ' Code Protect 08000-0BFFF disabled
    CP3 = Off                                   ' Code Protect 0C000-0FFFF disabled
    CP4 = Off                                   ' Code Protect 10000-13FFF disabled
    CP5 = Off                                   ' Code Protect 14000-17FFF disabled
    CP6 = Off                                   ' Code Protect 18000-1BFFF disabled
    CP7 = Off                                   ' Code Protect 1C000-1FFFF disabled
    CPB = Off                                   ' Code Protect Boot disabled
    CPD = Off                                   ' Data EE Read Protect disabled
    WRT0 = Off                                  ' Table Write Protect 00800-03FFF disabled
    WRT1 = Off                                  ' Table Write Protect 04000-07FFF disabled
    WRT2 = Off                                  ' Table Write Protect 08000-0BFFF disabled
    WRT3 = Off                                  ' Table Write Protect 0C000-0FFFF disabled
    WRT4 = Off                                  ' Table Write Protect 10000-13FFF disabled
    WRT5 = Off                                  ' Table Write Protect 14000-17FFF disabled
    WRT6 = Off                                  ' Table Write Protect 18000-1BFFF disabled
    WRT7 = Off                                  ' Table Write Protect 1C000-1FFFF disabled
    WRTC = Off                                  ' Config. Write Protect disabled
    WRTB = Off                                  ' Table Write Protect Boot disabled
    WRTD = Off                                  ' Data EE Write Protect disabled
    EBRT0 = Off                                 ' Table Read Protect 00800-03FFF disabled
    EBRT1 = Off                                 ' Table Read Protect 04000-07FFF disabled
    EBRT2 = Off                                 ' Table Read Protect 08000-0BFFF disabled
    EBRT3 = Off                                 ' Table Read Protect 0C000-0FFFF disabled
    EBRT4 = Off                                 ' Table Read Protect 10000-13FFF disabled
    EBRT5 = Off                                 ' Table Read Protect 14000-17FFF disabled
    EBRT6 = Off                                 ' Table Read Protect 18000-1BFFF disabled
    EBRT7 = Off                                 ' Table Read Protect 1C000-1FFFF disabled
    EBRTB = Off                                 ' Table Read Protect Boot disabled
Config_End

If you press the F2 key in the IDE, you will see the assembler code for the declares:

; UART1_ACTUALBAUD = 114285.71
; UART1_BAUDERROR = 0.79

    bsf BAUDCTL,PP_BRG16
    movlw 0x22
    movwf SPBRG1
    clrf SPBRG1H
    movlw 0x20
    movwf TXSTA1
    movlw 0x90
    movwf RCSTA1


And, as can be seen, the relevant SFRs are altered to suit the Baud rate required, depending on the operating frequency of the device. On devices with PPS (Peripheral Pin Select), the Positron8 compiler will also automatically manipulate these SFRs to match the pins required in the Declares.

Below is a screenshot of the above program simulating using EUSART1:

PIC18F87K90_Serial_Coms.jpg