EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Converting ADC value to ASCII

Started by shred444 January 9, 2008
Hey there, I'm new to these forums and I've got a bit of an odd question.

I'm working with an MSP430 microcontroller and using an ADC to check battery
voltage on my device. The ADC10MEM register returns a value somewhere
between 0x0000 and 0x03FF depending on battery levels.

I'm displaying the result by outputing via USART to hyperterminal. I've got
a function that allows me to send a character, but if I send ADC10MEM, it's
sending a number that doesnt reflect it's ASCII equivalent.
Is there a neat way to convert a byte like 0x02F8 to an equivelant ASCII
value of sending 2, then F, then 8?
--

Beginning Microcontrollers with the MSP430

----- Original Message -----
From: "shred444"
To:
Sent: Tuesday, January 08, 2008 10:28 PM
Subject: [msp430] Converting ADC value to ASCII
>
> Hey there, I'm new to these forums and I've got a bit of an odd question.
>
> I'm working with an MSP430 microcontroller and using an ADC to check
> battery
> voltage on my device. The ADC10MEM register returns a value somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to hyperterminal. I've
> got
> a function that allows me to send a character, but if I send ADC10MEM,
> it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant ASCII
> value of sending 2, then F, then 8?

C or assembler?

Leon
--
Leon Heller
Amateur radio call-sign G1HSM
Yaesu FT-817ND and FT-857D transceivers
Suzuki SV1000S motorcycle
l...@btinternet.com
http://webspace.webring.com/people/jl/leon_heller/
Hi Shred444,
Easy way to get string, containig anything You want -- use (s)printf
functions from standart libraries. For more information consult
documentation ;)
Another approach -- convert value by hand, in Your case it is rather
simple. Algorith can look like this (convert lowest 12 bits into
string with trailing zero):

unsigned int value = ADC10MEM;
unsigned char byte;
unsigned char string[4];
for (int i = 2; i >= 0; i--)
{
var = value & 0x0F;
value >>= 4;
if (var <= 9) // 0..9
string[i] = var + 0x30; // place ascii code of var
else // A..F
string[i] = var + 0x41 - 10;
}
string[3] = '\0';

So if ADC10MEM = 0x02F8 string will contain "2F8\0"

I hope my post is helpful.

Best regards,
Max

--- In m..., shred444 wrote:
> Hey there, I'm new to these forums and I've got a bit of an odd
question.
>
> I'm working with an MSP430 microcontroller and using an ADC to
check battery
> voltage on my device. The ADC10MEM register returns a value
somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to hyperterminal.
I've got
> a function that allows me to send a character, but if I send
ADC10MEM, it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant
ASCII
> value of sending 2, then F, then 8?
> --
>
>
Hi Shred444,
Easy way to get string, containig anything You want -- use (s)printf
functions from standart libraries. For more information consult
documentation ;)
Another approach -- convert value by hand, in Your case it is rather
simple. Algorith can look like this (convert lowest 12 bits into
string with trailing zero):

unsigned int value = ADC10MEM;
unsigned char low4bits;
unsigned char string[4];
for (int i = 2; i >= 0; i--)
{
low4bits = value & 0x0F;
value >>= 4;
if (low4bits <= 9) // 0..9
string[i] = low4bits + 0x30; // place ascii code of low4bits
else // A..F
string[i] = low4bits + 0x41 - 10;
}
string[3] = '\0';

So if ADC10MEM = 0x02F8 string will contain "2F8\0"

I hope my post is helpful.

Best regards,
Max

--- In m..., shred444 wrote:
> Hey there, I'm new to these forums and I've got a bit of an odd
question.
>
> I'm working with an MSP430 microcontroller and using an ADC to
check battery
> voltage on my device. The ADC10MEM register returns a value
somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to
hyperterminal. I've got
> a function that allows me to send a character, but if I send
ADC10MEM, it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant
ASCII
> value of sending 2, then F, then 8?
> --
>
>
Hi Shred444,
Easy way to get string, containig anything You want -- use (s)printf
functions from standart libraries. For more information consult
documentation ;)
Another approach -- convert value by hand, in Your case it is rather
simple. Algorith can look like this (convert lowest 12 bits into
string with trailing zero):

unsigned int value = ADC10MEM;
unsigned char byte;
unsigned char string[4];
for (int i = 0; i < 3; i++)
{
var = value & 0x0F;
value <<= 4;
if (var <= 9) // 0..9
string[i] = var + 0x30;
else // A..F
string[i] = var + 0x41 - 10;
}
string[3] = '\0';

I hope my post is helpful.

Best regards,
Max

--- In m..., shred444 wrote:
> Hey there, I'm new to these forums and I've got a bit of an odd
question.
>
> I'm working with an MSP430 microcontroller and using an ADC to
check battery
> voltage on my device. The ADC10MEM register returns a value
somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to hyperterminal.
I've got
> a function that allows me to send a character, but if I send
ADC10MEM, it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant
ASCII
> value of sending 2, then F, then 8?
> --
>
>
Hi Shred444,
Easy way to get string, containig anything You want -- use (s)printf
functions from standart libraries.
Another approach -- convert value by hand, in Your case it is rather
simple. Algorith can look like this (convert lowest 12 bits into
string with trailing zero):

unsigned int value = ADC10MEM;
unsigned char low4bits;
unsigned char string[4];
for (int i = 2; i >= 0; i--)
{
low4bits = value & 0x0F;
value >>= 4;
if (low4bits <= 9) // 0..9
string[i] = low4bits + 0x30; // place ascii code of low4bits
else // A..F
string[i] = low4bits + 0x41 - 10;
}
string[3] = '\0';

So if ADC10MEM = 0x02F8 string will contain "2F8\0"

I hope my post is helpful.

Best regards,
Max

--- In m..., shred444 wrote:
> Hey there, I'm new to these forums and I've got a bit of an odd
question.
>
> I'm working with an MSP430 microcontroller and using an ADC to
check battery
> voltage on my device. The ADC10MEM register returns a value
somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to hyperterminal.
I've got
> a function that allows me to send a character, but if I send
ADC10MEM, it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant
ASCII
> value of sending 2, then F, then 8?
> --
>
>
In our applications, which are very tight on time and code space, we
don't use any of the printf-like functions. In my applications, I use
created a very simple library of conversions very similar to those
described by g0tty0 in a previous response.

The library has functions like these which call each other hierarchically.
char getNibbleChar(uint8 nib) which conversions a byte (less than 16)
to a character.
byteToHexChars(uint8 byte, char hexChars[]) which converts a byte to a
string of to 2 characters.
word16ToChars(uint16 word16, char hexChars[]) which converts a 16-bit
word to 4 characters.
debugMessgeString(char msgTex[], uint16 value, char fmtdMsg[]) which
takes some text string, tacks-on a value which is converted to hex,
and builds an English-readable message (fmtdMsg[]).

Taking the "lean" route, you just have to be careful about who is
alocating what memory, etc. You also need to be a little careful
about endianness if you think you'll every re-use this code on another
processor.

I say, go lean!

Stuart

--- In m..., shred444 wrote:
> Hey there, I'm new to these forums and I've got a bit of an odd
question.
>
> I'm working with an MSP430 microcontroller and using an ADC to check
battery
> voltage on my device. The ADC10MEM register returns a value somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to hyperterminal.
I've got
> a function that allows me to send a character, but if I send
ADC10MEM, it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant ASCII
> value of sending 2, then F, then 8?
> --
>
>
Hi Shred,
(s)printf function will do, what You want, even more
Example:
char str[10];
example sprintf(str,"%04X",ADC10MEM);
str will contian "02F8\0".

Another way - use printf function. It will send Your formatted string
to UART.

For more details read EW430 Compiler Reference Guide (page 60,77).

Best regards,
Max

--- In m..., shred444 wrote:
> Hey there, I'm new to these forums and I've got a bit of an odd
question.
>
> I'm working with an MSP430 microcontroller and using an ADC to
check battery
> voltage on my device. The ADC10MEM register returns a value
somewhere
> between 0x0000 and 0x03FF depending on battery levels.
>
> I'm displaying the result by outputing via USART to hyperterminal.
I've got
> a function that allows me to send a character, but if I send
ADC10MEM, it's
> sending a number that doesnt reflect it's ASCII equivalent.
> Is there a neat way to convert a byte like 0x02F8 to an equivelant
ASCII
> value of sending 2, then F, then 8?
> --
>
>
Instead of "convert value by hand", you may also "convert value by
fingers (which we have 10 of them)".

unsigned int value = ADC10MEM;
const unsigned int digits[4] = {1000, 100, 10,1};
unsigned char string[5];
for (int i = 0; i<4; i++)
{
string[i] = '0';
while (value >= digits[i]) {value -= digits[i]; string[i]++;}
}
string[4] = '\0';

--- In m..., "Max Salov" wrote:
>
> Hi Shred444,
> Easy way to get string, containig anything You want -- use (s)printf
> functions from standart libraries.
> Another approach -- convert value by hand, in Your case it is rather
> simple. Algorith can look like this (convert lowest 12 bits into
> string with trailing zero):
>
> unsigned int value = ADC10MEM;
> unsigned char low4bits;
> unsigned char string[4];
> for (int i = 2; i >= 0; i--)
> {
> low4bits = value & 0x0F;
> value >>= 4;
> if (low4bits <= 9) // 0..9
> string[i] = low4bits + 0x30; // place ascii code of low4bits
> else // A..F
> string[i] = low4bits + 0x41 - 10;
> }
> string[3] = '\0';
>
> So if ADC10MEM = 0x02F8 string will contain "2F8\0"
>
> I hope my post is helpful.
>
> Best regards,
> Max
>
> --- In m..., shred444 wrote:
> >
> >
> > Hey there, I'm new to these forums and I've got a bit of an odd
> question.
> >
> > I'm working with an MSP430 microcontroller and using an ADC to
> check battery
> > voltage on my device. The ADC10MEM register returns a value
> somewhere
> > between 0x0000 and 0x03FF depending on battery levels.
> >
> > I'm displaying the result by outputing via USART to hyperterminal.
> I've got
> > a function that allows me to send a character, but if I send
> ADC10MEM, it's
> > sending a number that doesnt reflect it's ASCII equivalent.
> >
> >
> > Is there a neat way to convert a byte like 0x02F8 to an equivelant
> ASCII
> > value of sending 2, then F, then 8?
> > --
> >
>

Memfault Beyond the Launch