AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
loveoflife
PostPosted: Jun 12, 2006 - 04:36 PM
Wannabe


Joined: Jun 04, 2006
Posts: 77


Hi everyone.

Have a nice day.

Now I want to communicate Atmega128 and PC via UART.

Data save in DataFlash is Hex code.
Data display in PC terminal is Ascii.

So I would like you to give me some instruction about this conversation. And I am very happy If you give me some code about this.

Many thanks in advance.
 
 View user's profile Send private message  
Reply with quote Back to top
vstana
PostPosted: Jun 12, 2006 - 04:59 PM
Wannabe


Joined: Mar 25, 2003
Posts: 64


a decent terminal show received data in hex
try Bray's terminal (http://bray.velenje.cx/avr/terminal)
 
 View user's profile Send private message  
Reply with quote Back to top
MrJoe82
PostPosted: Jun 12, 2006 - 05:02 PM
Rookie


Joined: Mar 13, 2006
Posts: 45
Location: Helsinki, Finland

maybe you should again look at how data is represented. the data in the dataflash is like (almost) always stored binary, so it's just a matter of how to represent the data. you can convert your data for example by using itoa, or sprintf and then send it over the serial connection
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 12, 2006 - 05:52 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 13547
Location: Orlando Florida

Using c or assembler?
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
lfmorrison
PostPosted: Jun 12, 2006 - 06:23 PM
Raving lunatic


Joined: Dec 08, 2004
Posts: 4502
Location: Nova Scotia, Canada

bobgardner wrote:
Using c or assembler?

The OP's work so far has been in C.

Judging by the include files in previous posts:
Quote:
Code:
#include <ioavr.h>
#include <inavr.h>

I'd guess that it's IAR specifically.

sprintf() is almost guaranteed to be present in every C compiler's library, so it may be a good starting point.

If size matters, then switching to a smaller alternative like itoa() might make sense. But itoa() isn't part of ISO C, so some compilers may not offer it.


Last edited by lfmorrison on Jun 12, 2006 - 06:27 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
Bo1986
PostPosted: Jun 12, 2006 - 06:25 PM
Newbie


Joined: Apr 24, 2003
Posts: 5


Hex is a way to look at data like binary, decimal, octal and so on. By looking at a piece of data as hex instead of decimal doesn't change the actual data it only changes how it's represented. You only have these different kinds of data-representations to make it easier for you to program your controller.

ASCII on the other hand is a specific way of encoding characters. Look here for an ASCII table:

http://en.wikipedia.org/wiki/Ascii

-so you can represent ASCII characters as you want (hex, bin, dec, oct).

You need to know whats on your flash. What/who made the data? Find out what encoding is beeing used.

I hope you understand.
 
 View user's profile Send private message  
Reply with quote Back to top
stevech
PostPosted: Jun 12, 2006 - 06:29 PM
Raving lunatic


Joined: Dec 18, 2001
Posts: 3011


You can purchase a good used HEX to ASCII transliteration device on eBay. They're just a few thousand dollars.

Rolling Eyes
 
 View user's profile Send private message  
Reply with quote Back to top
Bo1986
PostPosted: Jun 12, 2006 - 08:58 PM
Newbie


Joined: Apr 24, 2003
Posts: 5


If you refer to hexadecimal when you say hex, then it's only a way to represent information. Here i will give you a free hex -> bin -> dec converter. Wink
Code:

Hex    Bin    Dec
0    0000    0
1    0001    1
2    0010    2
3    0011    3
4    0100    4
5    0101    5
6    0110    6
7    0111    7
8    1000    8
9    1001    9
A    1010    10
B    1011    11
C    1100    12
D    1101    13
E    1110    14
F    1111    15

If you have any information as hex and you want to know what it means as ASCII, you just look up in the table here:

http://en.wikipedia.org/wiki/Ascii

If you meen something else by saying hex, then say so.
 
 View user's profile Send private message  
Reply with quote Back to top
loveoflife
PostPosted: Jun 13, 2006 - 04:33 PM
Wannabe


Joined: Jun 04, 2006
Posts: 77


Thank all of you.

I understand now and I hope I can program to convert Hexa to Ascii.

Again, many thanks to you.
 
 View user's profile Send private message  
Reply with quote Back to top
awneil
PostPosted: Jun 13, 2006 - 08:30 PM
Hangaround


Joined: Jul 02, 2005
Posts: 499
Location: Basingstoke, Hampshire, UK

loveoflife wrote:
I understand now and I hope I can program to convert Hexa to Ascii..

That statement suggests that you have not understood at all! Rolling Eyes

Your program would convert numbers to ASCII-coded text

Here we go again:

The number 23 (decimal) is the same number as 17 (hex) is the same number as 27 (octal) is the same number as 10111 (binary).

If you want these as ASCII-coded text, that is when you need to do conversions:

23 as ASCII-coded text is a '2' character followed by a '3' character;
17 as ASCII-coded text is a '1' character followed by a '7' character;
27 as ASCII-coded text is a '2' character followed by a '7' character;
10111 as ASCII-coded text is a '1' character followed by a '0' character, followed by three more '1' characters.

(and for 'C' strings, you'd also need a NUL terminator)

The ASCII code for a '0' character has a value of 0x30;
The ASCII code for a '1' character has a value of 0x31;
The ASCII code for a '2' character has a value of 0x32;
The ASCII code for a '3' character has a value of 0x33;
The ASCII code for a '7' character has a value of 0x37.

You should be able to see a pattern there...


Note that the ASCII code for a NUL character is zero - which is not to be confused with a '0' character... Cool
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
tristram_
PostPosted: Jun 13, 2006 - 10:36 PM
Wannabe


Joined: Dec 02, 2005
Posts: 52


I agree with awneil that your response does not indicate understanding, so suppose I may as well stick my 2p worth in...

ASCII stands for "American Standard Code for Information Interchange". It was designed to give a standard interpretation of characters that could be input/output into a computer from a teletypes or other character oriented input device or output to a printer. The code set gives meanings to the values 0 through (originally) 127. The value 0 is the NUL character, the value 7 is the BEL character (you can guess what that did on a teletype!), 27 the ESC character - these are all non-printing values. The values 32 to 127 represent the printable characters.Of interest here are the chacater representations of 48 to 67 (which print as the decimal numbers '0' to '9') and the values 65 to 70, which are the letters 'A' through 'F'.

Why these? Because they represnt the hexidecimal digit set (0 to 15 is usually written as 0 to F)

In a computer the only raw number system is usually binary and everything is converted to it for storage and processing. Humans find binary a bit tedious to process so we break down the data into managable portions (usually of 8 bits). However, even 8 bits is a bit tedious for humans to work out so we use other numeric bases to make it easier for us to understand and manipulate. The usual bases we use are base 8 and base 16 (octal and hexxidecimal respectively).

Sticking with hex... a byte with 10110011 is the decimal value 179 but that's not easy to see but, using hex, we'd represent that as B3. So if we look at a portion of memory/disk and see the binary value 10110011 we could convert it to B3 in our heads. But, wouldn't it be better if it was displayed in human readable form? That's where the ASCII stuff comes in - first we decide on our number system and then we convert to ASCII output - 1 byte for each digit. (If we decided to output it as binary that'd be 8 ASCII characters, 3 as octal or 2 as hex.

In this case B3 is the ASCII character 66 followed by the ASCII character 51.

When I was a system programmer I could do octal and hex sums dead easy in my head - now I just use the scientific function of the windows calculator - lazy or what? Smile
 
 View user's profile Send private message  
Reply with quote Back to top
loveoflife
PostPosted: Jun 14, 2006 - 05:10 PM
Wannabe


Joined: Jun 04, 2006
Posts: 77


Hi awneil and tristram.

I am sorry for late reply. These days I am busy with USART.

Thank you very much for so detail explanation.

Awneil, you are so right. I want to program as you said. But I don't know how to express.

Quote:

Your program would convert numbers to ASCII-coded text


And below is my draft code to convert hexa number to ASCII-coded text.

Code:

//For example, hex number contain 3 bytes.
Dec_H='0';
Dec_M='0';
Dec_L='0';
while(hex>100)
{
Dec_H++;
hex-=100;
}
while (hex>10)
{
Dec_M++;
hex-=10;
}
Dec_L+=hex;
// so hex number is converted to 3 ASCII-coded characters.


I hope this is correct way.

Again, many thanks to you.

P/S: I am so happy to join AVR forum. In my opionion, It is really a good environment for a new programmer like me. Wishing all of you healthy and happy.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 14, 2006 - 05:20 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 34381
Location: (using avr-gcc in) Finchingfield, Essex, England

If you want to say x=ascii_to_hex("1234") so that x=1234 then look at using sscanf() from the C library - they've already done all the tricky conversion work for you.

Use something like:
Code:
{
  int x;
  char digit_string[] = { "1234" };

  sscanf(digit_string, "%d", &x);
}


Only downside of using the pre-made sscanf() is that it's a very complicated function that can read LOADS of different number formats and using it will add considerably to the size of your program - but it's the easy answer unless space is tight.

Cliff

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
awneil
PostPosted: Jun 15, 2006 - 12:41 AM
Hangaround


Joined: Jul 02, 2005
Posts: 499
Location: Basingstoke, Hampshire, UK

loveoflife wrote:
And below is my draft code to convert hexa number to ASCII-coded text.

You are still missing the point.

You are converting a number to an ASCII-coded string.

The number is just a number: it is not hex; it is not binary - it is just a number

Quote:
//For example, hex number contain 3 bytes.

Remember, it takes two hex digits to represent each byte;
Thus, If a number contains 3 bytes, it would be 0x000000...0xFFFFFF - so you need more than just three decimal digits for that!
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Jun 15, 2006 - 10:35 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 34381
Location: (using avr-gcc in) Finchingfield, Essex, England

I can't help thinking that the Tutorial forum needs a good tutorial on number representation / conversions - this subject seems to be very recurrent!

(now where's Dean when you need him?)

Cliff

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
abcminiuser
PostPosted: Jun 15, 2006 - 10:46 AM
Moderator


Joined: Jan 23, 2004
Posts: 7014
Location: Melbourne, Victoria, Australia

You raaaannngggg?

I never knew people liked my tutorials. I always cringe when I re-read them Confused.

- Dean Twisted Evil

_________________

 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: Jun 15, 2006 - 10:52 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 34381
Location: (using avr-gcc in) Finchingfield, Essex, England

Au contraire! I think they're great - just wish I had time to sit down and do one justice on the subject but if anyone can explain this well I think you could.

A lot of folks seem to have problems getting their head around binary/hex/decimal/BCD/printf+itoa conversion to ASCII/sscanf conversion from ASCII/etc/etc

Cliff

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
abcminiuser
PostPosted: Jun 15, 2006 - 10:57 AM
Moderator


Joined: Jan 23, 2004
Posts: 7014
Location: Melbourne, Victoria, Australia

Hmmm, perhaps i'll give it a go then. Tomorrow is my last day of term, so I'll have two whole weeks of semi-relaxation (whoever invented holiday homework should have been shoved into a hot fire) in which I'll have plenty of free time. My aim is to finally get the release version of BL1.4 out (almost done bugfixing, the latest/final? version has some neat new features) and I guess if people email suggestions I could pick the most popular subjects and display my total ignorance on then Smile.

[/derail]

- Dean Twisted Evil

_________________

 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
awneil
PostPosted: Jun 15, 2006 - 11:54 AM
Hangaround


Joined: Jul 02, 2005
Posts: 499
Location: Basingstoke, Hampshire, UK

Here's a tutorial on binary representation:

http://mathforum.org/library/drmath/view/54311.html

And here's something I wrote earlier:

http://www.8052.com/forum/read.phtml?id=94478


although I've just spotted an error:
I wrote:
BCD = Binary-Coded Decimal; therefore, by definition the digits can only ever go from 0-9.
In binary, that's 0000-1010 - so four bits are required for each BCD digit.

That should, of course, be 0000-1001 Embarassed
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
djoshi
PostPosted: Oct 15, 2009 - 01:27 PM
Posting Freak


Joined: Dec 04, 2007
Posts: 1741


Hi All i need to send specfic commands to a device atttached to my AVR UART port.

I can send any char over the UART, but i need to send 0xD5, which is 208 in decimal, What char would that be?

Regards

DJ
 
 View user's profile Send private message  
Reply with quote Back to top
decode
PostPosted: Oct 15, 2009 - 02:07 PM
Newbie


Joined: Sep 16, 2009
Posts: 19


Hi,

I had a similar problem because I had hooked up a sensor that gave me BCDs and I needed to show the information through a serial led that only accepted ASCII so here's what I did:


1 - use asciitable.com

People usually say you must add '0' to turn your value into ascii but as you can see from the table it doesn't quite work for the value of 0xA which I would want to represent as "A" and not ":"

So here's a little thing I'm using that worked for me in converting hex bytes to ascii:

Code:

if(value <=9) {
            value += 0x30; //0x0 turns into 0x30 which is "0"
        } else {
            value += 0x37; //0xA turns into 0x41 which is ascii "A"
        }


Hope it helps you. And sorry to the more experienced programmers if I made some terrible mistake.

Have fun!
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Oct 15, 2009 - 02:12 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 34381
Location: (using avr-gcc in) Finchingfield, Essex, England

Did Brays Termninal not work for you then?

http://braypp.googlepages.com/terminal

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
djoshi
PostPosted: Oct 15, 2009 - 02:25 PM
Posting Freak


Joined: Dec 04, 2007
Posts: 1741


HI Cliff

Sorry what does this software do for me?

DJ
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Oct 15, 2009 - 04:08 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 9270
Location: Vancouver, BC

Quote:
can send any char over the UART, but i need to send 0xD5, which is 208 in decimal, What char would that be?

0xD5 is not a standard ASCII character, but you don't need to have it be a character, just send 0xD5:
Code:
UDR = 0xD5;

_________________
Regards,
Steve A.

The Board helps those that help themselves.
wylfwt?
 
 View user's profile Send private message  
Reply with quote Back to top
djoshi
PostPosted: Oct 15, 2009 - 04:19 PM
Posting Freak


Joined: Dec 04, 2007
Posts: 1741


Ahh, i just found some convert on a site and found out that 0xD5 is Õ in chars, and it seems to work.
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Oct 15, 2009 - 04:40 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 19075
Location: Wisconsin USA

Why don't you just use the value directly? I do it on my LCDs for a degree symbol, for example.

Code:
putchar ('A');
putchar (0x41); // equivalent
...
char mystring[] = "ABCD";
char mystring[] = "\x41\x42\x43\x44"; // equivalent

 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Oct 15, 2009 - 04:58 PM
Raving lunatic


Joined: Nov 17, 2004
Posts: 9270
Location: Vancouver, BC

Quote:
and found out that 0xD5 is Õ in chars

On some systems, maybe. But again, this is not standard ASCII. ASCII only goes up to 0x7f.

_________________
Regards,
Steve A.

The Board helps those that help themselves.
wylfwt?
 
 View user's profile Send private message  
Reply with quote Back to top
ce
PostPosted: Oct 16, 2009 - 10:30 AM
Hangaround


Joined: Mar 11, 2005
Posts: 109
Location: Bocholt, Germany

@djoshi
0xD5 is 213 in decimal (not 205).

I am not sure but I guess it depends on your codepage what char this would be. In older ASCII tables decimal 213 belongs to the 'non-printable part'.
 
 View user's profile Send private message  
Reply with quote Back to top
adwaitsdeshpande
PostPosted: Oct 17, 2009 - 06:01 AM
Wannabe


Joined: May 02, 2008
Posts: 69
Location: Mumbai, India

Look Here:

http://www.idevelopment.info/data/Progr ... able.shtml
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits