This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

EK-TM4C1294XL: How can I print a new line when Using UARTCharPut()?

Part Number: EK-TM4C1294XL

I am using multiple UART ports, I was not able to modify the UARTstdio.c or .h file with any success to allow me to use the UARTprintf function with multiple ports, so I am creating my own. Not sure if it will work properly for my end use but its headed in the right direction for now. I was wondering though, how could I add some type of formatting, like a new line, spaces etc. This is just prints an array of chars.

I am not sure how to pass something like "\n" to UARTCharPut()?

Also if you think I am headed in the wrong direction please tell me also, I am very new to this.

Any suggestions or pointing in the right direction would be highly appreciated.

Thank you!

void UARTprint(const char* text)
{
    const char* p;

    for (p = text; *p != '\0'; p++)
    {
        UARTCharPut(UART0_BASE, *p);
    }
}

  • The problem with "\n" is you create a C string with array size 2 and a 0 (NULL) character at the end. Its like writing char str[2] = {10, 0};

    The hacky way to prove my point is write UARTCharPut(UART0_BASE, "\n"[0]);

    Don't actually release code like this.

    Edit: after contemplating your code, you seem to understand this. Something else is wrong with the setup. I would expect your code to send the new line character. Windows sometimes expects a new line feed "\r\n"

  • Hey, so strange thing happens, if I pass the "\n" directly in UARTCharPut, it doesn't work and I get \0x0 printed instead. Which is what I think you were alluding to in your response.
    However, when I pass this char array:

    char Line[] = "\n";

    to the function I have posted above, it ends up creating a new line no problem.
    I was wondering if you could give me any insight into why that is so? I was thinking it may be passing in as line[] = [\,n,0]. However, I am not sure. I was going to try to pass each of those individually to UARTCharPut(base, "\") then UARTCharPut(base, "n") but I am unable to write the first part.

    I think its interesting because if I am unable to just pass "\" then how is my line[] being passed in? If it is passed in as such line[]=[\n,0} then why would UARTCharPut(base, "\n") fail?

    I am struggling to explain what my question is (if you don't understand it I will try to clarify again), I did find a solution of sort but I wanted to understand a little more of what could be happening under the hood, I think it could really be useful down the road.
  • You are running into a tricky detail of C. The meaning of "\n" changes based on context.

    Passing "\n" into a function passes a pointer the first character in the string array. Passing "\n"[0] passes the value at the 0th (first) element ie 10.

    Initially assigning "\n" to a string sets the first to characters to 10 and 0. Note I said "initially". You may not assign a string literal to an character array past initialization. That would be attempting to assign a pointer the first character to a const char pointer (which can't happen because its const). You can do this which a char pointer.

    You would benefit from reading K&R 2nd edition cover to cover.
  • Maybe I am missing something here, but can't you just pass '\n' (NOT "\n") to UARTCharPut? UARTCharPut wants a character, not a string.

    Regards,

    Dave