1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The problems in the serial port of PIC18F67K22

  1. Apr 12, 2017 #1
    1. The problem statement, all variables and given/known data

    When using the serial port sends data,the Configuration function in the serial port and the sending function were put in the .C document of mian function.The serial port can send strings normally,but when I put them into another .C ,the serial port just can send a part of strings.(the begin and the end) I don’t understand why? Can you help me?

    This is my origin code:

    Code (Text):
    /A serial port initialization
    void Usart1_Init()
            TRISC7 = 1;
            TRISC6 = 0;
            //TXSTAx:Sending states and controlling register
            TXSTA1bits.TX9        = 0;                //choosing eight number to send
            TXSTA1bits.TXEN        = 1;                //enabled sending
            TXSTA1bits.SYNC = 0;                //asynchronous mode
            TXSTA1bits.BRGH = 1;                //High speed mode

            //RCSTAx:receive states and controlling register
            RCSTA1bits.SPEN        = 1;                //enabled seriel port
            RCSTA1bits.RX9  = 0;                //choosing eight number to receive
            RCSTA1bits.CREN = 1;                //0=baned receiver ,1= allowing receiver
            RC1IE = 1;                                        //receiving the interruption
            INTCONbits.GIE  = 1;
            INTCONbits.PEIE = 1;
            RC1IF = 0;

            //BAUDCONx:register is controlled by Baud rate
            BAUDCON1bits.BRG16  = 1;                //  16 number of Baud rate ―SPBRGHx 和 SPBRGx

            SPBRG = 95;                                //Baud rater


    //the serial port send the first byte data
    void Usart1_Send_Byte(char dat)
        TXREG1 = dat;

    //the serial port send the Hexadecimal array
    void Usart1_Send_Array(char array[],char n)
            unsigned char i=0;
        for(i = 0; i < n; i ++)

    //the serial port send the string data
    void Usart1_Send_String(char *str)
        while(*str != '\0')

    2. Relevant equations
    This is the PIC18F67K22's datasheet.Please have a look if needed.

    3. The attempt at a solution
    I have adjusted it and I found that “oid Usart1_Send_Byte(char dat)” has no influence at anywherejust this two function of sending (void Usart1_Send_String(F6ar *str)和void Usart1_Send_Array(char array[],char n))can’t operate!

    Thank you in advance!
    Last edited by a moderator: Apr 14, 2017
  2. jcsd
  3. Apr 12, 2017 #2


    User Avatar
    Science Advisor

    When transmitting data you should wait for the shift register to be empty before writing in the next byte of the string.
    I think you are testing for full after writing data to the UART. That should be OK, but you are wasting time waiting when you could be getting the next byte that is to be transmitted, or doing something else useful.
    while(TXSTA2bits.TRMT); // wait here until UART is empty, TRMT = 1.
    TXREG1 = dat; // load UART then go and get next one ready

    Should you send the zero at the end of the string to flag the end of string ?
  4. Apr 14, 2017 #3
    Thank you for you help,
    I have found the troubles,I review the origin program again. And I found that the resigister has read wrong (while(!TXSTA2bits.TRMT),it should be TXSTA1bits.TRMT,but I write it to be TXSTA2bits.TRMT. It's so right that the success depends on details. It's my lesson.
    Thank you for you help again!
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: The problems in the serial port of PIC18F67K22
  1. Serializing A Type (Replies: 2)

  2. 2 port network (Replies: 5)