Willem Ellis
Willem Ellis

Reputation: 5016

How would I send this string one character at a time?

Here is the code that I have. I am unable to test it right now, so I'm just wondering if someone can verify that this will take the full str string and send each character individually to TXREG

char str [10];
int n;
n = sprintf(str, "%u%% %u\n", Duty_Cycle, DAC_Output);
transmit_uart(str);                    // Send the value

And here is the transmit_uart() method.

void transmit_uart(const char *value) {
    for(int i = 0; value[i] != '\0'; i++) {
        while(TXIF == 0) {}
        TXREG = value[i];
    }
}

So this should send something like

50% 128

Every time I call transmit_uart() with a string formatted the way I have it up there.

UPDATE: I was able to test it yesterday, and this did in fact work! Thanks for all the help!

Upvotes: 0

Views: 623

Answers (2)

Arsenii Fomin
Arsenii Fomin

Reputation: 3346

If you want to send string of 5-9 characters:

1 or 2 or 3 symbols - Duty_Cycle value,
1 symbol - % symbol,
1 symbol - space,
1 or 2 or 3 symbols - DAC_Output value,
1 symbol - \n symbol,

you are doing right.

Upvotes: 1

Potatoswatter
Potatoswatter

Reputation: 137830

Although I haven't personally loaded it onto an MCU and tested it, yes, that looks fine as long as TXIF does what it looks like.

You really should use snprintf or a larger buffer. This is one case where overflowing the integer (as in simply having too large a value, or any negative value) would cascade into buffer overflow.

Upvotes: 1

Related Questions