Simplifying my USART code [ char string]

Thread Starter

HappyC4mper

Joined Oct 13, 2017
47
Hey guys,
so i created this code
Code:
int main(void)
{
//    PLL_Config();
    SystemCoreClockUpdate();
    char string[]= "Hello World";
    int i=0;
       
    while(!(USART_MODULE->SR&USART_SR_RXNE))
    for (i=0; i<11; i++)
    {
       
        init_USART();
        send_usart(string[i]);
           
    }
    while (string[i]!=0);
ignoring the while loop and the other functions. I would like to simplify my char string and somehow turn it into a function. I'm using putty to read the usart code.
I want it so that i can write as many characters as I want without having to change the number of the integer [(i)(i<11)]. Eg. send out "Hello world, blah blah." without having to type down the how many characters there are (does/calculate it automatically). Hopefully that makes sense.
Thanks!
 

John P

Joined Oct 14, 2008
1,761
Well, that's easy. Write the for() loop as:

Code:
    for (i=0; ; i++)
    {
        init_USART();
        send_usart(string[i]);
        if (string[i] == '\0')
            break;
    }
or maybe
Code:
    i = 0;
    do
    {
        init_USART();
        send_usart(string[i]);
    } while (string[i++] != '\0');
 
Last edited:

AlbertHall

Joined Jun 4, 2014
8,513
Well, that's easy. Write the for() loop as:

Code:
    for (i=0; ; i++)
    {
        init_USART();
        send_usart(string[i]);
        if (string[i] == '/0')
            break;
    }
or maybe
Code:
    i = 0;
    do
    {
        init_USART();
        send_usart(string[i]);
    } while (string[i++] != '\0');
These will also transmit the '\0' - that might be desired behaviour but TS code doesn't do this.
 

John P

Joined Oct 14, 2008
1,761
Yes, that was deliberate. If the terminating \0 isn't sent, how will the computer know that the string has ended? If the string has a variable length, counting characters won't do it--there has to be a marker of some kind.
 

shteii01

Joined Feb 19, 2010
4,647
Assuming your string is properly nul terminated then you can use strlen(string) to get the length of the string.
Basically to add to what Albert said.
Your string is defined in several different places.
The first element of string is defined at: int i=0
The last element of string is defined in the for loop: i<11
Those two are the limits of your string. They are user defined, meaning "you" put them there because you knew how long your string is ahead of time.
You want to be more flexible. You may or may not know the length of the string ahead of time.
So.
You define first element.
Then you find the length of the string. This can be done with a count where you write code to count the number of characters in the string. Or. You do what Albert told you. Smart people has been dealing with this situation for 30? or 40? years, so they wrote a library function that finds the length of the string. Use it.
You would have:
int i=0; /first element
int j=strlen(string); /length of the string
for (i=0; i<=j; i++) {} /your for loop
 

Thread Starter

HappyC4mper

Joined Oct 13, 2017
47
Basically to add to what Albert said.
Your string is defined in several different places.
The first element of string is defined at: int i=0
The last element of string is defined in the for loop: i<11
Those two are the limits of your string. They are user defined, meaning "you" put them there because you knew how long your string is ahead of time.
You want to be more flexible. You may or may not know the length of the string ahead of time.
So.
You define first element.
Then you find the length of the string. This can be done with a count where you write code to count the number of characters in the string. Or. You do what Albert told you. Smart people has been dealing with this situation for 30? or 40? years, so they wrote a library function that finds the length of the string. Use it.
You would have:
int i=0; /first element
int j=strlen(string); /length of the string
for (i=0; i<=j; i++) {} /your for loop
thanks a lot!!
 
Top