The problems in the serial port of PIC18F67K22

  • Thread starter whatry109
  • Start date
  • Tags
    Serial
In summary, 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. I don't understand why? Can you help me?
  • #1
whatry109
3
1

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
  • #2
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 ?
 
  • #3
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!
 

Related to The problems in the serial port of PIC18F67K22

1. What is a serial port and why is it important in a PIC18F67K22?

A serial port is a hardware interface that allows data to be transferred in a serial manner, meaning one bit at a time. It is important in a PIC18F67K22 as it allows communication between the microcontroller and other devices, such as sensors and actuators.

2. What are some common problems that can occur in the serial port of PIC18F67K22?

Some common problems in the serial port of PIC18F67K22 include incorrect baud rate settings, noise interference, and faulty connections.

3. How can I troubleshoot and fix serial port problems in PIC18F67K22?

You can troubleshoot serial port problems in PIC18F67K22 by checking the baud rate settings, ensuring proper grounding, and checking for loose connections. If these do not resolve the issue, you may need to replace the hardware.

4. Can a damaged serial port in PIC18F67K22 be repaired?

In most cases, a damaged serial port in PIC18F67K22 cannot be repaired. It is recommended to replace the microcontroller or use an external serial to USB converter.

5. How can I prevent serial port problems in PIC18F67K22?

To prevent serial port problems in PIC18F67K22, make sure to use proper grounding techniques, use shielded cables to reduce noise interference, and regularly check for loose connections. It is also important to follow the manufacturer's guidelines for hardware connections and baud rate settings.

Similar threads

  • Programming and Computer Science
Replies
5
Views
917
  • Electrical Engineering
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
14K
  • Programming and Computer Science
Replies
6
Views
2K
  • Electrical Engineering
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Electrical Engineering
Replies
19
Views
7K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top