1
\$\begingroup\$

I am trying to set up a communication with the EUSART of the pic16F18877 but sofar no succes. After receiving some comments I made some changes to the RxChar() function.

From the computer I am sending 0xFE05 to the controller by means of a usb to ttl interface (USB To RS232 TTL PL2303HX). A serial monitor in between shows no errors. The send char is going out correctly. On the micro controller side I keep receiving frame errors time after time.

Still trying to make the EUSART working. Sofar without succes. To find out where the problem comes from I returned to the Original hex file (code in basic) to reduce the possible error sources. The Original program is written for the pic16F887A and after programming the PIC16F877A the serial communication operates correctly. The serial communication for the PIC16F877A must be in a library since I can not find it in the basic code. Only the port and baud definitions are given.

This above makes me believe that the problem must be somewhere in the settings or code of my project with the PIC16F18877. But sofar I have no clue. Any assistance in how to proceed is welcome.

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 to calling function  
    }
}
\$\endgroup\$
12
  • \$\begingroup\$ Your 1st line of code doesn't "disable" the port as your comment says it should ... \$\endgroup\$
    – brhans
    Jun 5 '17 at 19:03
  • \$\begingroup\$ @brhans Copy mistake. Result unchanged \$\endgroup\$
    – Decapod
    Jun 5 '17 at 20:26
  • \$\begingroup\$ Why are you setting the interrupt flags to 0? You should not do that. And you haven't shown how all this is used \$\endgroup\$
    – Eugene Sh.
    Jun 5 '17 at 20:35
  • \$\begingroup\$ Make sure you clear the UART's error flags - particularly OERR. Also, as Eugene has noted, you shouldn't/don't-need-to clear the interrupt flags - they're read-only anyway. \$\endgroup\$
    – brhans
    Jun 5 '17 at 20:38
  • \$\begingroup\$ I go look into it and make the changes and come with a response. Sofar I only try to receive a char. In the external serial monitor the character shows. In RCREG it does not. \$\endgroup\$
    – Decapod
    Jun 5 '17 at 20:41
1
\$\begingroup\$

After a carefull re-reading the code and recompiling I discovered where the error was made. I was misled by the information in table 33-4 where SPBRG = 520 in combination with SYNC = 0; BRGH=1 and BRG16= 1; This decimal value can not be used in this format.

//Therefore not: 
 SPRG = 520;
//but:
 SPBRGH = 0x02;
 SPBRGL = 0x08;

After recompilation and testing I found that the information came through correctly; So time to move on with the rest of the program.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.