News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

how to tun off UART on 16F876A

Started by Yves, Nov 01, 2022, 01:24 PM

Previous topic - Next topic

Yves

Hello all,
How can I turn off the UART on 16F876A. I have serial RS23 display that is working fine on any port but PortC.6and I have to use portC.6. Hserial gives garbage.
the code below doesn't work I suspect UART is in the way
Declare RSOut_Pin = PORTC.6
Declare RSOut_Mode = 0'Inverted or True/False or 1, 0
Declare Serial_Baud = 9600
Many thanks.

Yves
Yves

RGV250

Hi,
Perhaps you should post your code for the hardware serial as I would have expected that to work.
The serial port (UART) is disabled by default, see RCSTA (bit 7)

Regards,
Bob

top204

#2
The USART is not brought into play and not enabled in a program unless the HRSout/HSerout or HRSin/HSerin commands are used in the program. So the I/O pins are as standard.

However, if they are used in the program as well, setting and clearing bits of the SFRs used by USART1 will enable and disable it, and bring the pins out for normal I/O use, and vice-versa.

It's been a while since I used the standard 14-bit core devices, and their latest datasheets are not as detailed as their original datasheets, but taking a quick look at it shows that some bits will do the job. Try the test program below:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
    Device = 16F876                              ' Tell the compiler what device is being compiled for
    Declare Xtal = 20                            ' Tell the compiler what speed the device will be operating at
'
' Setup USART1
'   
    Declare Hserial_Baud = 9600
    Declare HRSOut_Pin = PORTC.6
    Declare HRSIn_Pin = PORTC.7
'
' Setup RSin and RSout
'   
    Declare Serial_Baud = 9600
    Declare RSOut_Pin = PORTC.6
    Declare RSIn_Pin = PORTC.7
    Declare RSOut_Mode = 0 
    Declare RSIn_Mode = 0
'
' Disable USART1  
$define USART1_Disable() '
    TXSTAbits_TXEN = 0   '
    RCSTAbits_CREN = 0   '
    RCSTAbits_SPEN = 0   '
    DelayMS 100
'
' Enable USART1  
$define USART1_Enable() '
    TXSTAbits_TXEN = 1  '
    RCSTAbits_CREN = 1  '
    RCSTAbits_SPEN = 1  '
    DelayMS 100
    
'-----------------------------------------------------------------------
' The main program starts here
'
Main:
    Do                                  ' Create a loop
        USART1_Enable()                 ' Enable USART1 so the pins become dedicated
        HRSOutLn "TX from HRsout"       ' Transmit from USART1
        DelayMS 255
             
        USART1_Disable()                ' Disable USART1 so the pins become normal I/O
        RsOutLn "TX from Rsout"         ' Transmit from software RSout
        DelayMS 255      
    Loop                                ' Do it forever

In simulation, the serial terminal shows:
TX from HRsout
TX from Rsout
TX from HRsout
TX from Rsout
TX from HRsout
TX from Rsout
TX from HRsout
TX from Rsout


So it is enabling and disabling USART1.

Yves

Many thanks Les for giving clear explanation to all the subject we come across.

Best Regards,

Yves
Yves