• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

sending back message with space using avr usart after capitalization

okbro

New Member
hi,
I got a code where one avr send message via usart and another avr receives and send back the same message after capitalization. So far capitalization works but I don't know how to include the space between the words. The received message is capitalized but there is no space between the words. Below is example code:

C:
 while (1){
        mychar = usartreceive();
        if(mychar >= 'a' && mychar <= 'z'){
            mychar += ('A' - 'a');
            usartsend(mychar);
            _delay_ms(50);
        }
thanks
 

Pommie

Well-Known Member
Most Helpful Member
Could it be something to do with the fact that spaces doesn't get the 50mS delay? BTW, is that the send or receive end?

Mike.
Edit, or perhaps it's that spaces never get processed and therefore NEVER sent back.
 

okbro

New Member
hi,
i removed delay but same effect. i just included delay because the words appeared so fast over the screen. actually i don't understand this part of the code:
mychar += ('A' - 'a');
 

Pommie

Well-Known Member
Most Helpful Member
Your problem is that the send only gets executed is it's not a space. Try moving the usartsend outside of the if construct.

Mike.
Edit, I.E.
Code:
 while (1){
        mychar = usartreceive();
        if(mychar >= 'a' && mychar <= 'z'){
            mychar += ('A' - 'a');
        }
        usartsend(mychar);
 

KeepItSimpleStupid

Well-Known Member
Most Helpful Member
mychar += ('A' - 'a');
It's probably -32, I think without looking it up. 'A' is 65 from memory. Lower case characters are a fixed distance from upper case. ASCII chart. It subtracts 32, thus converting LC to UC. It has to be done within a specific range,

65-32 is 1, or Control-A

BTW, 32 is the ASCII space.
 
Last edited:

KeepItSimpleStupid

Well-Known Member
Most Helpful Member
Your original code basically says if it's LC convert to UC and send. If it's UC it won't get sent. Neither will numbers and control characters like CR and LF.

So a12 3Xa would only return AA
 
Last edited:

okbro

New Member
hi,
- putting usartsend(mychar); outside the while loop didn't do the trick.
- i now understand mychar += ('A' - 'a'); line, and i tried to use if and else statement for character and space, but how to detect space and send back the space? for example the following didn't work,

Code:
while (1){
        mychar = usartreceive();
        if(mychar >= 'a' && mychar <= 'z'){
                mychar += ('A' - 'a');
                usartsend(mychar);
                _delay_ms(50);
        }
        else if(mychar = ' '){
            usartsend(' ');
            _delay_ms(50);
        }
    }
 
Put the "uartsend()" outside the IF tests. You want to call it no matter what. You should be using the IF tests just to alter the character from lowercase to uppercase.
 

KeepItSimpleStupid

Well-Known Member
Most Helpful Member
So, now with the new code "
So "a12 3Xa" would only return "A XA"

I have no idea what you really want to do.
Some things you might want to do is convert LC to UC; make multiple spaces 1 space unless it's within quotes.

There is also stuff like carriage returns, line feeds, bells and even form feed. form feed sometimes erases the screen.

I did a neat program back in the mid 70's that would "pretty-print" a file in BASIC. We also had sources for the interpreter, but that was mostly immaterial. it was almost like writing a compiler.

order mattered. There was a token called "forinputasfile" which was replaced by " for input as file " to pretty print.

I did not handle adding or printing strings across line boundries. You could also interchange single and double quotes,.

The program helped make programs readable, though.
 
Last edited:

okbro

New Member
- i don't know why usartsend() should be outside the if statement, i want to send capitalized letter if i receive lower letters
- i am actually not sending any numbers only alphabets
 

Pommie

Well-Known Member
Most Helpful Member
As mentioned above, using a single equals is a very common mistake. To combat this I try to replace else if(mychar = ' '){ with else if(' ' = mychar){ as it will always throw an error at compile time is double equals isn't used.

Mike.
 

Nigel Goodwin

Super Moderator
Most Helpful Member
As mentioned above, using a single equals is a very common mistake. To combat this I try to replace else if(mychar = ' '){ with else if(' ' = mychar){ as it will always throw an error at compile time is double equals isn't used.
That's really interesting to know, I've seen it done and always wondered why?.

I'll have to go and try it now!.

Edit: well so it does :D that's one mystery cleared up. I wonder what the chances are of me remembering to do it that way?.
 
Last edited:

EE World Online Articles

Loading
Top