Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] How to configure an UART to PIC16F18877

Status
Not open for further replies.

sajay

Newbie level 4
Joined
Apr 11, 2015
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
64
Hi Everyone,
Does anyone here know how to configure a PIC16F18877 for the UART communication, I can't get output when using this.



Thank you,
Sjy.
 

Hi,

The datasheet information should be sufficient.

Did you try online calculators, documents, video tutorials?

If you need further help:
Show what you did so far.
Show your code.
Show how you tested it.
Show the test results.

Klaus
 

Hi below is my code

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
fosc = 20.000000
void UART_Init(void) 
{    
 
    RC1STAbits.SPEN = 0;     // begin of setup disable serial port.
    // transmitter
    TX1STAbits.TXEN     = 1;    // continues transmit enable bit
    TX1STAbits.TX9      = 0;    // 8 bit transmission
    TX1STAbits.SYNC = 0;        // asynchronous operation
    ANSELCbits.ANSC6    = 0;    // digital
    TRISCbits.TRISC6    = 0;    // output
 
    // receiver
    RC1STAbits.CREN    = 1;   // continues receive enable bit
    RC1STAbits.RX9     = 0;
    // there is only one sync
    ANSELCbits.ANSC7   = 0;   // digital
    TRISCbits.TRISC7   = 1;   // input
 
    // baudrate.
    TX1STAbits.BRGH    = 1;
    BAUD1CONbits.BRG16 = 1;
    SPBRG = 520;               // baudrate 9600
 
    RC1STAbits.SPEN   = 1;     // end of setup enable serial port
}
 
void UART_TxChar(uchar ch) 
{ 
     while(TXIF==0);    
     TXREG=ch;
}
 
uchar UART_RxChar()
{  
    uchar Discard;
    while(1){
        while(RCIF==0);       // Wait till the data is received 
        if (RCSTAbits.FERR)
        {    
            Discard = RCREG;   Read the register and wait for the next byte
            break;
        }    
        if (RCSTAbits.OERR)
        {    
            RCSTAbits.CREN = 0;
            break;
        }
        return RCREG;   // Return the received data 
    }
}




Thank you,
Sjy.
 
Last edited by a moderator:

Hi,

Please don't let us ask for every item repeatedly. Don't waste your (and our) time.
Show how you tested it. --> what did you expect?
Show the test results.--> what happened instead?

Klaus
 

I would recommend that you use the TRMT bit instead of the TXIF bit. I know this chip is a bit different from most other PIC MCUs in that the TXIF is set when the Tx side is idle (rather than at the end of a transmission or on initialisation) and your code should work, but it will lead you into trouble when you try to use this technique on other MCUs.
Can you please show us the config settings?
Also I assume that the 'fosc = 20.00000' is just to tell us the crystal frequency and not a valid instruction (which it isn't).
Also which compiler are you using?
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top