News:

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

Main Menu

Uart of 16F15213

Started by shantanu@india, Oct 26, 2025, 12:43 PM

Previous topic - Next topic

shantanu@india

Hi,
I am not able to make the UART receive & reflect back the byte. This is new devive for me. Maybe I am doing something wrong. Can someone help? Thanks.

Device = 16F15213
Declare Xtal = 32
'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config1 FEXTOSC_OFF, RSTOSC_HFINTOSC_32MHZ, CLKOUTEN_OFF, VDDAR_HI
Config2 MCLRE_INTMCLR, PWRTS_PWRT_64, WDTE_ON, BOREN_ON, BORV_LO, PPS1WAY_ON, STVREN_ON
Config4 BBSIZE_BB512, BBEN_OFF, SAFEN_OFF, WRTAPP_OFF, WRTB_OFF, WRTC_ON, WRTSAF_OFF, LVP_OFF
Config5 CP_ON

'**** End of Fuse Configurator Settings ****
'-------------------------------------------------------------------------------
'25.10.25 This program reflects the character received from the UART terminal
'adding the baud rate control commands
'configuring the pps registers to assign Rx & Tx to RA0 & RA1 respectively
'**********************************************************************************
dim inbyte  as byte
dim outbyte as byte
dim rx_flag as bit
'**********************************************************************************
symbol led lata.5
symbol dir lata.2
'**********************************************************************************
goto main
'**********************************************************************************
on_hardware_interrupt  goto interrupt_routine        'uart receive interrupt

interrupt_routine:

context save
if pir1.4=1 then    'check the RC1IF flag
    inbyte=RC1REG
    rx_flag=1
EndIf
context restore
'*********************************************************************************
main:
    oscfrq=%00000101        '32MHz
    osctune=%00000000       'tuned to center frequency
    wdtcon=%00001100        'watchdog CS=31kHz , time 4 sec
    porta=0
    lata=0
    ansela=0
    trisa.5=0
    trisa.2=0
    RC1STA=%10010000       'Enable serial port , continuous receive
    SP1BRGH=0x03
    sp1brgl=0x40

    TX1STA=%10100100      'baud source internal,asynchronous mode , transmit enabled , high speed baud generator
    BAUD1CON.3=1          '16 bit baud rate generator(BRG16=1)
    RC1STA.7=0
    Nop
    Nop
    RC1STA.7=1            'reset UART1 to clear flags
    dir=0                 'default in receive mode
    led=1
    PPSLOCK = 0x55
    PPSLOCK = 0xAA
    ppslock.0 = 0          'PPS unlocking(not required but still done)
    ra1pps= %00001001        'Uart Tx output assigned to RA1
    ra0pps= %00010001        'uart Rx input assigned to RA0
    PIE1=%00010000         'unmask USART receive  interrupt
    INTCON=%11000000       'Peripheral interrupts enabled
    dir=1
    hserout["Ultratech"]
    While TX1STA.1=0 : Clrwdt : Wend
    nop
    dir=0                                                           
    while 1=1
        clrwdt
        if rx_flag=1 then
            rx_flag=0
            gosub Reflect_byte
            led=0
            delayms 100
            led=1
        EndIf
    Wend

Reflect_byte:
    dir=1
    hserout[inbyte]
    While TX1STA.1=0 : Clrwdt : Wend
    dir=0
    Nop
    return



Regards
Shantanu

Pepe

#1
Device = 16F15213
Declare Xtal = 32

Config1 FEXTOSC_OFF, RSTOSC_HFINTOSC_32MHZ, CLKOUTEN_OFF, VDDAR_HI
Config2 MCLRE_INTMCLR, PWRTS_PWRT_64, WDTE_ON, BOREN_ON, BORV_LO, PPS1WAY_ON, STVREN_ON
Config4 BBSIZE_BB512, BBEN_OFF, SAFEN_OFF, WRTAPP_OFF, WRTB_OFF, WRTC_ON, WRTSAF_OFF, LVP_OFF
Config5 CP_ON

Dim inbyte  As Byte
Dim rx_flag As Bit

Symbol led = LATA.5
Symbol dir = LATA.2

On_Hardware_Interrupt GoTo interrupt_routine

'---------------------------------------------------------
interrupt_routine:
Context Save
If PIR1.4 = 1 Then
    inbyte = RC1REG
    rx_flag = 1
    PIR1.4 = 0
EndIf
Context Restore
'---------------------------------------------------------
main:
    OSCFRQ = %00000101
    OSCTUNE = %00000000
    WDTCON = %00001100
    ANSELA = 0
    PORTA = 0
    LATA = 0
    TRISA = %00000001    ' RA0 RX entrada, resto salidas

    RC1STA = %10010000
    TX1STA = %00100100
    BAUD1CON.3 = 1
    SP1BRGH = 0x03
    SP1BRGL = 0x40

    PPS_Unlock()
    RA1PPS = 0x09
    RX1DTPPS = 0x00
    PPS_Lock()

    PIE1 = %00010000
    INTCON = %11000000

    dir = 1
    HSerOut ["Ultratech"]
    While TX1STA.1 = 0 : ClrWDT : Wend
    dir = 0

    led = 1
    While 1 = 1
        ClrWDT
        If rx_flag = 1 Then
            rx_flag = 0
            GoSub Reflect_byte
            led = 0
            DelayMS 100
            led = 1
        EndIf
    Wend

Reflect_byte:
    dir = 1
    HSerOut [inbyte]
    While TX1STA.1 = 0 : ClrWDT : Wend
    dir = 0
Return

shantanu@india

Thanks Pepe. I could not understand the meaning of xxxPPS for input. Now it is clear.
Regards
Shantanu

shantanu@india

Still not working.
I think TX1STA = %10100100 was correct since the internal baud generator is being used. I changed it but no result.
PPS_Unlock() & PPS_Lock()....how are they getting compiled?
Is it working in Proteus?
Regards
Shantanu

Pepe

test this

Device = 16F15213
Declare Xtal = 32

Config1 FEXTOSC_OFF, RSTOSC_HFINTOSC_32MHZ, CLKOUTEN_OFF, VDDAR_HI
Config2 MCLRE_INTMCLR, PWRTS_PWRT_64, WDTE_ON, BOREN_ON, BORV_LO, PPS1WAY_ON, STVREN_ON
Config4 BBSIZE_BB512, BBEN_OFF, SAFEN_OFF, WRTAPP_OFF, WRTB_OFF, WRTC_ON, WRTSAF_OFF, LVP_OFF
Config5 CP_ON

Dim inbyte  As Byte
Dim rx_flag As Bit

Symbol led = LATA.5
Symbol dir = LATA.2
  Declare HRSOut_Pin = PORTA.1
    Declare HRSIn_Pin  = PORTA.0
   Declare Hserial_Baud = 9600
On_Hardware_Interrupt GoTo interrupt_routine

'---------------------------------------------------------
interrupt_routine:
Context Save
If PIR1.4 = 1 Then
    inbyte = RC1REG
    rx_flag = 1
    PIR1.4 = 0
EndIf
Context Restore
'---------------------------------------------------------
main:
    OSCEN = $00
    OSCFRQ = %00000101                                      ' FRQ is 32MHz
    OSCTUNE = $00
    WDTCON = %00001100
    ANSELA = 0
    PORTA = 0
    LATA = 0
    TRISA = %00000001     ' RA0 RX entrada, resto salidas

    PIE1 = %00010000
    INTCON = %11000000

    dir = 1
    HSerOut ["Ultratech"]
   
    dir = 0

    led = 1
    While 1 = 1
        Clrwdt
        If rx_flag = 1 Then
            rx_flag = 0
            GoSub Reflect_byte
            led = 0
            DelayMS 100
            led = 1
        EndIf
    Wend

Reflect_byte:
    dir = 1
    HSerOut [inbyte]
    dir = 0
Return