The problems in the serial port of PIC18F67K22

  • Thread starter Thread starter whatry109
  • Start date Start date
  • Tags Tags
    Serial
AI Thread Summary
The issue with the PIC18F67K22 serial port was identified as a mistake in the code where the wrong register, TXSTA2bits.TRMT, was checked instead of TXSTA1bits.TRMT. This error caused only partial strings to be sent when the sending functions were moved to another .C file. The solution involved correcting the register reference to ensure proper operation of the serial port. Additionally, it was noted that waiting for the shift register to be empty before sending the next byte is crucial for efficient data transmission. Attention to detail in coding is essential for successful implementation.
whatry109
Messages
2
Reaction score
0

Homework Statement


Hello~

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:
/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
19200pbs
}

//the serial port send the first byte data
void Usart1_Send_Byte(char dat)
{
    TXREG1 = dat;
    while(!TXSTA2bits.TRMT);   
}//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 ++)
    {
       Usart1_Send_Byte(array[i]);
       __delay_us(500);
    }
}

//the serial port send the string data
void Usart1_Send_String(char *str)
{
    while(*str != '\0')
    {
                Usart1_Send_Byte(*str++);
       __delay_us(500);
    }
}//******************************

Homework Equations


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

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:
Physics news on Phys.org
whatry109 said:
//the serial port send the first byte data
void Usart1_Send_Byte(char dat)
{
TXREG1 = dat;
while(!TXSTA2bits.TRMT);
}
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 ?
 
Baluncore said:
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 ?
Hi,friend
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!
 

Similar threads

Back
Top