News:

;) This forum is the property of Proton software developers

Main Menu

Help me? 16F18855 async serial

Started by Cassio_Franquini, Oct 31, 2022, 03:55 AM

Previous topic - Next topic

Cassio_Franquini

Hi friends, can anyone help me? I am not able to transmit and receive data by serial, many errors in the output and in the RX/TX input. I had my project working very well with 16f886 but due to the lack of this component I had to replace it with 16F18855. Due to my lack of experience with the enhanced series I'm having difficulties. please someone help me with this task. Thank you very much.

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F18855


Config1 FEXTOSC_HS, RSTOSC_EXT1X, CLKOUTEN_OFF, CSWEN_OFF, FCMEN_OFF
Config2 MCLRE_ON, PWRTE_ON, LPBOREN_OFF, BOREN_ON, BORV_LO, ZCD_OFF, PPS1WAY_ON, STVREN_ON, DEBUG_OFF
Config3 WDTCPS_WDTCPS_31, WDTE_ON, WDTCWS_WDTCWS_7, WDTCCS_SC
Config4 WRT_OFF, SCANE_available, LVP_ON
Config5 CP_OFF, CPD_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
 
  '9600 8 N 1
 
          Xtal = 32  'I use an external crystal of 32mhz I would also like to use 20mhz that I have a lot of parts.
                      'no other clock source needed
                      '
 
          Declare LCD_DTPin PORTB.4
          Declare LCD_RSPin PORTB.2
          Declare LCD_ENPin PORTB.3
          Declare LCD_Interface 4
          Declare LCD_Lines 4
       
          Declare Hserial_Baud = 9600
          Declare HRSOut_Pin = PORTC.6 '
          Declare HRSIn_Pin = PORTC.7

          ' Declare  Hserial_TXSTA = %00100100 ' ??
          ' Declare  Hserial_RCSTA = %10010000 '??
          Declare Hserial_Clear = On

          Declare Watchdog = On

          Dim SERDATA As Word
          Dim checksum As Byte
         
          TRISC.7 = 1
          TRISC.6 = 0
          Symbol RTS = PORTB.0 : TRISB.0 = 0  '

INICIO:

          Low RTS
          DelayMS 10
          HSerIn 1500,TIMEOUTS,[SERDATA,checksum] '                 
          DelayMS 10
          Print At 1,1, Dec SERDATA,Dec checksum, "  "


          Set RTS
          DelayMS 10
          HSerOut [ SERDATA.LowByte,SERDATA.HighByte,Dec checksum]
          DelayMS 10
          Low RTS
          DelayMS 1000

          GoTo INICIO


TIMEOUTS:
            Cls
            Print At 2,17,"-TIMEOUTS- "
            DelayMS 800
            Cls
            GoTo INICIO


top204

#1
The compiler takes care of the USARTx Baud rate SFR manipulations, and the PPS for the these devices, so a simple program listing below works:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A simple tester of USART1 working on a PIC16F18855 device.
' Receive data from USART1 and re-transmit it to a serial terminal.
' Also display the characters received on an alphanumeric LCD.
'
' Written for the Positron8 Compiler by Les Johnson.
'
    Device = 16F18855                                   ' Tell the compiler what device to compile for
    Declare Xtal = 16                                   ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART1 and its pins, so that the PPS can be configured
'
    Declare HSerial1_Baud = 9600                        ' Set USART1 Baud rate to 9600
    Declare HRsout1_Pin   = PORTC.6                     ' Set the pin to use for USART1 TX
    Declare HRSin1_Pin    = PORTC.7                     ' Set the pin to use for USART1 RX
'
' Setup the alphanumeric LCD
'
    Declare LCD_DTpin     = PORTA.0                     ' Set the pins to use for the LCD's RT4 to DT7 lines
    Declare LCD_RSpin     = PORTA.4                     ' Set the pin to use for the LCD's RS line
    Declare LCD_ENpin     = PORTA.5                     ' Set the pin to use for the LCD's EN line
    Declare LCD_Interface = 4                           ' Set for a 4 pin interface
    Declare LCD_Lines     = 2                           ' Set for a 2 line LCD
    Declare LCD_Type      = Alphanumeric                ' Tell the compiler what LCD is being used
'
' Create some variables
'
    Dim bChar As Byte                                   ' Holds the character received and transmitted
    Dim bXpos As Byte                                   ' Holds the X position on the LCD's line

'--------------------------------------------------------------
' The main program starts here
' Receive characters for USART1 and re-transmit them
' Also display the characters received on line 2 of the alphanumeric LCD
'
Main:
    Cls                                                 ' Clear the LCD's display
    HRsoutLn "Type some characters"                     ' Transmit some characters to the serial terminal
    Print at 1,1, "Char:"                               ' Display on the LCD
    bXpos = 1                                           ' Reset the LCD X positrion

    Do                                                  ' Create a loop
        bChar = HRsin1                                  ' Receive a byte from USART1
        HRsout1 bChar                                   ' Transmit the byte received
        If bXpos < 17 Then                              ' Is the LCD X position within a line length?
            Print at 2, bXpos, bChar                    ' Yes. So display the character received on the LCD
        Else                                            ' Otherwise... The end of line has been reached
            bXpos = 1                                   ' So. Reset the X position
            Print at 2, 1, bChar, "               "     ' Display the character received and clear the rest of the line
        EndIf
        Inc bXpos                                       ' Increment the X position
    Loop                                                ' Do it forever


'--------------------------------------------------------------
' Setup the config fuses for an external crystal
'
    Config1 FEXTOSC_HS, CLKOUTEN_OFF, CSWEN_ON, FCMEN_OFF
    Config2 MCLRE_ON, PWRTE_ON, LPBOREN_OFF, BOREN_OFF, ZCD_OFF, PPS1WAY_OFF, STVREN_OFF
    Config3 WDTCPS_WDTCPS_2, WDTE_OFF
    Config4 WRT_OFF, SCANE_NOT_AVAILABLE, LVP_OFF
    Config5 CP_OFF, CPD_OFF

If using an external crystal of 16MHz or above, you can remove the config fuses from the listing and use the compiler's defaults, but it is always better to have them in place, so they can be easily changed if required.

The above program can be seen working in the Proteus simulator, below:
PIC18855 USART1 Tester.jpg

Stephen Moss

Quote from: Cassio_Franquini on Oct 31, 2022, 03:55 AMDeclare Watchdog = On
If you are going to enable the WatchDog timer (WDT) you need it periodically clear it using the clrwdt command, usually at the beginning or end of your main program loop, otherwise the PIC will be reset when the WDT overflows which cause problems. Its primary purpose is to automatically restart a PIC when code execution stops for any reason.

Quote from: Cassio_Franquini on Oct 31, 2022, 03:55 AMTIMEOUTS:
            Cls
            Print At 2,17,"-TIMEOUTS- "
            DelayMS 800
            Cls
            GoTo INICIO
Either something is missing from your code or this will never execute as there is no GoTo TIMEOUTS to execute it.

 

Cassio_Franquini

Ok thanks, I have watdog disabled as I isolated I didn't worry about it. thanks for the tip.