EmbeddedRelated.com
Forums

Best way in the IAR compiler to convert 2 bytes into 16-bit int

Started by Nick Alexeev January 18, 2007
I'm really new to MSP430 and IAR complier, but not new to embedded C
programming. Under IAR compiler, what's the best way of conevrting 2
bytes into 16-bit int?

Cheers,
Nick

P.S. I have used CCS compiler for PICs before.
It had a function make16(hibyte, lobyte)

Beginning Microcontrollers with the MSP430

char a;
char b;
int c;

*((char*) (&c)) = a;
*((char*) (&c + 1)) = b;

Maybe is a little better Idea.

_____

From: m... [mailto:m...] On Behalf Of
Nick Alexeev
Sent: Jueves, 18 de Enero de 2007 08:40 a.m.
To: m...
Subject: [msp430] Best way in the IAR compiler to convert 2 bytes into
16-bit int

I'm really new to MSP430 and IAR complier, but not new to embedded C
programming. Under IAR compiler, what's the best way of conevrting 2
bytes into 16-bit int?

Cheers,
Nick

P.S. I have used CCS compiler for PICs before.
It had a function make16(hibyte, lobyte)

--


--
#define make16(H, L) (((unsigned char)(H) << 8) | (unsigned char)(L))

> -----Original Message-----
> From: m... [mailto:m...]
> On Behalf Of Nick Alexeev
> Sent: 18 January 2007 11:40
> To: m...
> Subject: [msp430] Best way in the IAR compiler to convert 2
> bytes into 16-bit int
>
> I'm really new to MSP430 and IAR complier, but not new to
> embedded C programming. Under IAR compiler, what's the best
> way of conevrting 2 bytes into 16-bit int?
>
> Cheers,
> Nick
>
> P.S. I have used CCS compiler for PICs before.
> It had a function make16(hibyte, lobyte)
>
>
> Yahoo! Groups Links
Nick
This is pretty basic C. Try

unsigned int result;
unsigned char hibyte, lobyte;

result = (unsigned int)(((unsigned int)hibyte << 8) | lobyte);

or as a function

unsigned int make16(unsigned char hibyte, unsigned char lobyte)
{
return (unsigned int)(((unsigned int)hibyte << 8) | lobyte);
}

Regards
-Bill Knight
R O SoftWare

Nick Alexeev wrote:
> I'm really new to MSP430 and IAR complier, but not new to embedded C
> programming. Under IAR compiler, what's the best way of conevrting 2
> bytes into 16-bit int?
>
> Cheers,
> Nick
>
> P.S. I have used CCS compiler for PICs before.
> It had a function make16(hibyte, lobyte)
I tried those you propose but I got less code using

char a;
char b;
int c;

*((char*) (&c)) = a;
*((char*) (&c + 1)) = b;

Maybe is a little better Idea.

bye
FB

_____

From: m... [mailto:m...] On Behalf Of
Bill Knight
Sent: Jueves, 18 de Enero de 2007 11:14 a.m.
To: m...
Subject: Re: [msp430] Best way in the IAR compiler to convert 2 bytes into
16-bit int

Nick
This is pretty basic C. Try

unsigned int result;
unsigned char hibyte, lobyte;

result = (unsigned int)(((unsigned int)hibyte << 8) | lobyte);

or as a function

unsigned int make16(unsigned char hibyte, unsigned char lobyte)
{
return (unsigned int)(((unsigned int)hibyte << 8) | lobyte);
}

Regards
-Bill Knight
R O SoftWare

Nick Alexeev wrote:
> I'm really new to MSP430 and IAR complier, but not new to embedded C
> programming. Under IAR compiler, what's the best way of conevrting 2
> bytes into 16-bit int?
>
> Cheers,
> Nick
>
> P.S. I have used CCS compiler for PICs before.
> It had a function make16(hibyte, lobyte)

--


--
FB
Be a bit careful here. (&c + 1) will return the address of the next
int not the address of the second byte of the int c. The code also
specifically assumes the the processor is BIG_ENDIAN (if a is the high
order byte) or LITTLE_ENDIAN (if a is the low order byte). Either way,
the code is not portable.

Regards
-Bill Knight
R O SoftWare
FB wrote:
> I tried those you propose but I got less code using
>
> char a;
> char b;
> int c;
>
>
> *((char*) (&c)) = a;
> *((char*) (&c + 1)) = b;
>
> Maybe is a little better Idea.
>
> bye
> FB
>
> _____
>
> From: m... [mailto:m...] On Behalf Of
> Bill Knight
> Sent: Jueves, 18 de Enero de 2007 11:14 a.m.
> To: m...
> Subject: Re: [msp430] Best way in the IAR compiler to convert 2 bytes into
> 16-bit int
>
> Nick
> This is pretty basic C. Try
>
> unsigned int result;
> unsigned char hibyte, lobyte;
>
> result = (unsigned int)(((unsigned int)hibyte << 8) | lobyte);
>
> or as a function
>
> unsigned int make16(unsigned char hibyte, unsigned char lobyte)
> {
> return (unsigned int)(((unsigned int)hibyte << 8) | lobyte);
> }
>
> Regards
> -Bill Knight
> R O SoftWare
>
> Nick Alexeev wrote:
>> I'm really new to MSP430 and IAR complier, but not new to embedded C
>> programming. Under IAR compiler, what's the best way of conevrting 2
>> bytes into 16-bit int?
>>
>> Cheers,
>> Nick
>>
>> P.S. I have used CCS compiler for PICs before.
>> It had a function make16(hibyte, lobyte)
That is true.
Talking about (&c+1) it was just the idea (I write my code correctly in my
software)

Talking about portable, you are right but I've just mentioned that solution
only for MSP430.
thanks.
FB

_____

From: m... [mailto:m...] On Behalf Of
Bill Knight
Sent: Jueves, 18 de Enero de 2007 05:19 p.m.
To: m...
Subject: Re: [msp430] Best way in the IAR compiler to convert 2 bytes into
16-bit int

FB
Be a bit careful here. (&c + 1) will return the address of the next
int not the address of the second byte of the int c. The code also
specifically assumes the the processor is BIG_ENDIAN (if a is the high
order byte) or LITTLE_ENDIAN (if a is the low order byte). Either way,
the code is not portable.

Regards
-Bill Knight
R O SoftWare

FB wrote:
> I tried those you propose but I got less code using
>
> char a;
> char b;
> int c;
> *((char*) (&c)) = a;
> *((char*) (&c + 1)) = b;
>
> Maybe is a little better Idea.
>
> bye
> FB
>
> _____
>
> From: HYPERLINK "mailto:msp430%40yahoogroups.com"msp430@yahoogroups.-com
[mailto:HYPERLINK "mailto:msp430%40yahoogroups.com"msp430@yahoogroups.-com]
On Behalf Of
> Bill Knight
> Sent: Jueves, 18 de Enero de 2007 11:14 a.m.
> To: HYPERLINK "mailto:msp430%40yahoogroups.com"msp430@yahoogroups.-com
> Subject: Re: [msp430] Best way in the IAR compiler to convert 2 bytes into
> 16-bit int
>
> Nick
> This is pretty basic C. Try
>
> unsigned int result;
> unsigned char hibyte, lobyte;
>
> result = (unsigned int)(((unsigned int)hibyte << 8) | lobyte);
>
> or as a function
>
> unsigned int make16(unsigned char hibyte, unsigned char lobyte)
> {
> return (unsigned int)(((unsigned int)hibyte << 8) | lobyte);
> }
>
> Regards
> -Bill Knight
> R O SoftWare
>
> Nick Alexeev wrote:
>> I'm really new to MSP430 and IAR complier, but not new to embedded C
>> programming. Under IAR compiler, what's the best way of conevrting 2
>> bytes into 16-bit int?
>>
>> Cheers,
>> Nick
>>
>> P.S. I have used CCS compiler for PICs before.
>> It had a function make16(hibyte, lobyte)

--


--
Nick, I would try this:

unsigned int result;
result = (hiByte << 8) | lowByte;

If you want the result to be unsigned as shown above, then the state
of the highest bit in hiByte won't matter. Otherwise, you might have
to think about the consequences.
What's your position on portability?

There is always:

typedef unsigned char OneByte;
typedef unsigned short TwoBytes;

struct HiLoByte {
OneByte loByte;
OneByte hiByte;
};

union Number {
TwoBytes n;
HiLoByte bytes;
};