MC9S08JM60

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

MC9S08JM60

1,354件の閲覧回数
Koolmelee
Contributor I

I am relatively new to embedded systems, so please forgive me if this is a stupid question.

I am currently programming an 8 bit microcontroller, model MC9S08JM60, and I just want to know how 32 bit numbers are handled on an 8 bit microcontroller.

On this processor UINT32 is a typedef of unsigned long...which is 32 bit.

How is a 32 bit integer stored in memory?
My assumption is that is is stored as follows:

ADDR DATA(8 bits wide)
0x0000 D31...D24(MSB)
0x0001 D23...D16
0x0002 D15...D8
0x0003 D7...D0(LSB)

Can I do bitshifts such as:

 

UINT8 var1 = 5;

UINT32 var2 = 0;

 

  var2 = (UINT32)(var1 << 18); //and any other arithmetic on UINT32's as long as it does not overflow beyond 32 bits?

 

 

I am creating a simple protocol over USB and I need to be able to send in 32 bit values(for large delays etc...)

The 32 bit value is recieved into a 64 bit buffer which I have access to as a char array.

Can I simply cast the value to a UINT32 by doing the following(assume Buffer[1] contains the most significant byte of the integer value):

UINT32 var = *((UINT32*)&Buffer[1])

ラベル(1)
0 件の賞賛
返信
4 返答(返信)

719件の閲覧回数
kef
Specialist I

           UINT8 var1 = 5;

           UINT32 var2 = 0;

 

                  var2 = (UINT32)(var1 << 18); //and any other arithmetic on UINT32's as long as it does not overflow beyond 32 bits?

 

Due to integer promotion code above will work on 32 bit machines, but will fail on targets with 16bits int'egers. Before shifting left, shorter than int var1 has to be promoted to int. Shifting 16bits int to the left by >=16 bit positions will give zero. Finally zero will be converted to UINT32. You should typecast var1 before shifting it left.   var2 = (UINT32)var1 << 18; should work.

 

 

            Can I simply cast the value to a UINT32 by doing the following(assume Buffer[1] contains the most significant

            byte of the integer value):

                        UINT32 var = *((UINT32*)&Buffer[1])

 

This will copy UINT32 from the (char?) buffer. Buffer[1] to Buffer[4] will be copied to var byte by byte. If you do the same on little endian PC and send buffer to your MCU, then you will need to reverse byte order. You can reverse byte order either on PC or on your micro (unless it's standard protocol and byte order is defined).

0 件の賞賛
返信

719件の閲覧回数
Koolmelee
Contributor I

Thanks to all that helped me!

 

I settled with using unions to make the code more readable than using all that pointer notation.

0 件の賞賛
返信

719件の閲覧回数
bigmac
Specialist III

Hello,

 

The 9S08 devices are big endian, so your assumption for a 32-bit integer is correct.  You can perform all basic arithmetic operations, as you would for smaller integers.

 

To handle the individual bytes of a 32-bit integer, I would create a union between an unsigned long and a four element char array.  The same variable can then be referenced in either format.  The first element of the array will have an index of zero., which will contain the MS byte. 

 

Regards,

Mac

 

0 件の賞賛
返信

719件の閲覧回数
Koolmelee
Contributor I

Thanks for the quick response.

 

I have not learned unions yet...will be reading up on them today.

 

From what I have read so far, just something like this:

 

union {

UINT32 myInt32;

UINT8[4] myBytes;

} var;

 

var.myBytes[0] = Buffer[0];

...

var.myBytes[3] = Buffer[3];

 

someFunction(var.myInt32);//function expects 32 bit int parameter

 

myInt32 and myBytes share same memory, but at compile, they allow different ways to access memory?

 

My question still remains, will what I did by casting to a UINT32 pointer still work?

 

Thanks.

0 件の賞賛
返信