News:

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

Main Menu

16F18326 UART Rx not working

Started by Bravo, Today at 12:12 AM

Previous topic - Next topic

Bravo

Hi All,
I have been using 16F18346 for some texting projects which are working fine. Then I decided that I don't need that many pins, so I changed to 16F18326.
I cannot get the UART Rx to work which talks to a  4G module . I had to change the pins assigned for Tx & Rx using PPS commands which worked ok on the 16F18346. The ones on the 16F18326 are obviously different.
I even assembled a new board, with just enough parts to test this into an RSR232 to TTL board to my CF19 Toughbook which has an actual 9 pin RS232 socket on.
 I asked Chat GPT for help which produced the same PPS settings which I used. So I am wondering if there is an issue with the compiler for this chip.
The test board used a new chip which is DIP configuration, just in case the first chip is damaged. Unfortunately not even Proteus 9.1 SP3 includes these chips , so I cannot simulate this.
The Tx works OK but not the Rx. The baud rate is 115200, the clock frequency is 16Mhz

My PPS settings are as follows:
RC0PPS = %00010100 ' this makes port C.0 USART Tx
RXPPS =  %00000001 ' This makes port A.1 USART Rx

Here is the bottom part of my test code:

                Declare Hserial_Baud  = 115200
                Declare Hserial_RCSTA = %10010000   ' SPEN + CREN
                Declare Hserial_TXSTA = %00100100   ' BRGH + TXEN
                'Declare Hserial_BRG16 = True        ' 16-bit baud generator
                Declare Hserial_SPBRG = 34          ' 115200 @ 16MHz
               
                '========================================
                ' Main Loop
                '========================================
                Dim bData As Byte
               
               
Begin:               
                HSerOut ["Test", 13,]
               
                While 1 = 1
               
                    If HSerIn bData Then
                        HSerOut ["RX: ", bData, 13,10]
                    EndIf
               
                Wend


Any help would be greatly appreciated.
Retired RF Tech

RGV250

Hi,
I would just link the TX pin to the RX pin so that takes anything else out of the equation to start with.
Then just have a loop sending out an incrementing value.

I do not see where you are capturing the incoming data?

Regards,
Bob

Gamboa

Hi Bravo,

Try this:

                While 1 = 1
               
                    HSerIn [bData]
                    HSerOut ["RX: ", bData, 13,10]
                    bData = 0
               
                Wend

Regards,
Gamboa
Long live for you

top204

#3
Your syntax would give an error with the HSerin command inside a comparison.

Try the code listing below, for a PIC16F18346 transmitting and receiving via EUSART1. It has not been tested because I do not have that particular device, but looking at the assembler code that the compiler produces (which is not a lot), it all looks OK.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Setup a PIC16F18346 device to operate at 32MHz using its internal oscillator, and operate EUSART1.
'
' Written for the Positron8 compiler by Les Johnson.
' https://sites.google.com/view/rosetta-tech/positron-compilers-experimenters-notebook
'
    Device = 16F18346                           ' Tell the compiler what device to compile for
    Declare Xtal = 32                           ' 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 HSerout1_Pin   = PORTC.0            ' Set the TX pin for EUSART1
    Declare HSerin1_Pin    = PORTA.1            ' Set the RX pin for EUSART1
    Declare Hserial1_Clear = On                 ' Enable Error clearing on received bytes
'
' Create any global variables and constants here
'
    Dim Bytein As Byte
   
'----------------------------------------------------------------------
' The main program starts here.
' Receive data from EUSART1, and echo it back.
'
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_Osc32MHz()                              ' Set the internal oscillator to operate at 32MHz
'
' More setups here when required
'
EndProc

'----------------------------------------------------------------------
' Set the internal oscillator to operate at 32 MHz on a PIC16F18346 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Int_Osc32MHz()
    OSCCON1 = $60                               ' Use HFINTOSC. NDIV is 1
    OSCCON3 = $00
    OSCEN = $00   
    OSCFRQ = $07                                ' 32 MHz
    OSCTUNE = $00
    DelayMS 10                                  ' Wait for stability
EndProc

'----------------------------------------------------------------------
' Setup the fuses for internal oscillator on a PIC16F18346
' The OSC pins are set as general purpose I/O
'
    Config1 FEXTOSC_OFF,_                       ' FEXTOSC External Oscillator not enabled
            RSTOSC_HFINT32,_                    ' HFINTOSC with 2xPLL (32 MHz)
            CLKOUTEN_OFF,_                      ' CLKOUT function is disabled
            CSWEN_ON,_                          ' Writing to NOSC and NDIV is allowed
            FCMEN_ON                            ' Fail-Safe Clock Monitor is enabled

    Config2 MCLRE_ON,_                          ' MCLR/VPP pin function is MCLR. Weak pull-up enabled
            PWRTE_OFF,_                         ' Power-up Timer disabled
            WDTE_OFF,_                          ' Watchdog Timer disabled. SWDTEN is ignored
            LPBOREN_OFF,_                       ' Low-power BOR disabled
            BOREN_ON,_                          ' Brown-out Reset enabled. SBOREN bit ignored
            BORV_LOW,_                          ' Brown-out voltage (Vbor) set to 2.45V
            PPS1WAY_OFF,_                       ' The PPSLOCK bit can be cleared and set multiple times
            STVREN_ON,_                         ' Stack Overflow or Underflow will cause a reset
            DEBUG_OFF                           ' Background debugger disabled

    Config3 WRT_OFF,_                           ' User NVM Write protection off
            LVP_OFF                             ' High Voltage on MCLR/VPP must be used for programming

    Config4 CP_OFF,_                            ' User NVM code protection disabled
            CPD_OFF                             ' Data NVM code protection disabled

If you press the F2 button in the IDE, and look at the assembler code, you will see that the compiler sets up the EUSART1 SFRs and PPS (Peripheral Pin Select), based upon the Declares:

; UART1_ACTUALBAUD = 117647.06
; UART1_BAUDERROR = 02.12

    movlb 0x03
    bcf BAUD1CON,PP_BRG16
    movlw 0x10
    movwf SP1BRG
    clrf SP1BRGH
    movlw 0x24
    movwf TX1STA
    movlw 0x90
    movwf RC1STA
; CONFIGURE HRSOUT1 PPS
    movlb 0x1D
    movlw _PPS_FN_TX
    movwf RC0PPS
; CONFIGURE HRSIN1 PPS
    movlw 0x01
    movlb ((RXPPS)>>7)
    movwf RXPPS
    movlb 0x00


Regards
Les