The problems in the serial port of PIC18F67K22

  • Thread starter Thread starter whatry109
  • Start date Start date
  • Tags Tags
    Serial
Click For Summary
SUMMARY

The discussion centers on issues encountered while using the serial port of the PIC18F67K22 microcontroller. The user initially faced problems sending complete strings when the serial port functions were moved to a different .C file. The solution involved correcting the register reference from TXSTA2bits.TRMT to TXSTA1bits.TRMT in the Usart1_Send_Byte function. This highlights the importance of precise register handling in embedded programming.

PREREQUISITES
  • Understanding of PIC18F67K22 microcontroller architecture
  • Familiarity with C programming for embedded systems
  • Knowledge of serial communication protocols
  • Experience with debugging embedded software
NEXT STEPS
  • Review the PIC18F67K22 datasheet for detailed register descriptions
  • Learn about UART configuration and operation in embedded systems
  • Explore best practices for debugging embedded C code
  • Investigate the use of interrupts in serial communication
USEFUL FOR

Embedded systems developers, microcontroller programmers, and anyone troubleshooting serial communication issues in PIC microcontrollers.

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

  • · Replies 2 ·
Replies
2
Views
15K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 6 ·
Replies
6
Views
4K