3

I'm programming a few libraries for a board with a PIC18F25K80 built-in. Right now I'm trying to program the UART library and I've tried everything but I cannot make it work when it comes to send a string of chars.

I'm using the XC8 compiler and I have the following code in the library: (I haven't programmed the interruptions yet, that's why you can't see anything related to that in the code.

void UARTsend(char data){
    UARTbusy();
    TXREG1 = data;
}

void UARTsendTEXT(unsigned char *text){

    while(*text != '\0'){
        UARTsend(*(text++));
    }
}

char UARTbusy(){
return TXSTA1bits.TRMT;
}

And this code in the main file:

int main() {

    UARTconfig(_BR_19200, _RxINT_OFF, _TxINT_OFF, _8BIT);

    unsigned char data[20] = "ABCDEFGHIJ";

    while(1){

        if(UARTrxREAD() == 'a'){
            UARTsendTEXT(data);
        }
    }

    return 0;
}

So, when I hit 'a', the string should be sent but instead of seeing the string, I receive the first and last letter, and sometimes just the first letter.

Honestly, I believe it is a really basic problem with the code, but I tried everything I could think about and nothing worked, so maybe someone here can help me with that.

EDIT: just in case you need to check it out:

void UARTconfig(unsigned int BaudRate, unsigned int RxINT, unsigned int         TxINT, unsigned int BIT){

int br_data;

//Clock configuration at 16MHz
OSCCONbits.IRCF = 0b111;

//9-bit transmit enable bit.
if(BIT == 1) {
  RCSTA1bits.RX9 = 1; //Reception 9-bit.
  TXSTA1bits.TX9 = 1; //Transmission 9-bit.
}
else {
  RCSTA1bits.RX9 = 0; //Reception 8-bit. 
  TXSTA1bits.TX9 = 0; //Transmission 8-bit.
}


//Enable serial port.
RCSTA1bits.SPEN = 1;                       

//Enable continuous reception.
RCSTA1bits.CREN = 1;

//Setting asynchronous mode.
TXSTA1bits.SYNC = 0;

//Enable transmission
TXSTA1bits.TXEN = 1;                         

//Setting Rx/Tx pins as output.
TRISC7 = 0;                                  
TRISC6 = 0;    

//Baud rate configuration
BAUDCON1bits.BRG16 = 1;                       //16bit Baud Rate generator
TXSTA1bits.BRGH = 0;                          //Low speed BR.

switch(BaudRate){
    case 0:
        br_data=415;
        break;
    case 1:
        br_data=103;
        break;
    case 2:
        br_data=51;
        break;
    case 3:
        br_data=16;
        break;
    case 4:
        br_data=8;         
} 
SPBRGH1:SPBRG1 = br_data;

}

1 Answers1

1

The function UARTbusy should be waiting for status, or its caller should be. But the transmit register status is read once and ignored.

There is typically a two-stage buffer for transmitted data. Values written to the interface register are held until the shift register is empty, and the device remains 'busy' until then.

When you write the first data, it is transferred immediately to the shift register, so writing the second data immediately is harmless. But you then keep on over-writing the contents of the output register until it holds the last character of your message. Then when the shift register becomes empty, the last character of your message is sent.

Remember that the shift register is shifting bits out relatively slowly - at the baud rate. That is why the status bit needs to be checked.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56