Continue to Site

# sending back message with space using avr usart after capitalization

Status
Not open for further replies.

#### okbro

##### 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){
if(mychar >= 'a' && mychar <= 'z'){
mychar += ('A' - 'a');
usartsend(mychar);
_delay_ms(50);
}

thanks

#### Pommie

##### Well-Known 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

##### 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
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){
if(mychar >= 'a' && mychar <= 'z'){
mychar += ('A' - 'a');
}
usartsend(mychar);

#### Pommie

##### Well-Known Member
BTW, if usartreceive doesn't wait you'll get lots of garbage.

Mike.

#### KeepItSimpleStupid

##### Well-Known 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
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

##### 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){
if(mychar >= 'a' && mychar <= 'z'){
mychar += ('A' - 'a');
usartsend(mychar);
_delay_ms(50);
}
else if(mychar = ' '){
usartsend(' ');
_delay_ms(50);
}
}

#### upand_at_them

##### Active Member
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
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

##### 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

#### KeepItSimpleStupid

##### Well-Known Member
mistake here: else if(mychar = ' '){

I believe (C is rusty)

else if(mychar == ' '){

#### Nigel Goodwin

##### Super Moderator
mistake here: else if(mychar = ' '){

I believe (C is rusty)

else if(mychar == ' '){

It's a VERY, VERY, VERY, VERY easy mistake to make!

Particularly when you come from more civilised languages, like Pascal!.

#### Pommie

##### Well-Known 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
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 that's one mystery cleared up. I wonder what the chances are of me remembering to do it that way?.

Last edited:

#### djsfantasi

##### Member
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 that's one mystery cleared up. I wonder what the chances are of me remembering to do it that way?.

You’ll do it the wrong way for a few more times. Until you learn not to do it. Then, you’ll be ok, except for the occasional brain fart.

Status
Not open for further replies.

Replies
5
Views
5K
Replies
2
Views
4K
Replies
21
Views
6K
Replies
7
Views
3K
Replies
0
Views
3K