How can i get 2 bytes of an int? please help

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
underworldman
Posts: 270
Joined: 29 Jan 2010 16:36

How can i get 2 bytes of an int? please help

#1 Post by underworldman » 07 Jul 2010 23:28

Hi there. This may sound like a simple question but i'm unable to find an easy way of seperating two bytes of an unsigned int variable. I want to store each byte into an unsigned char so that i can store this value into the EEPROM or can i simply store the whole unsigned int into the EEPROM's two successive locations?

underworldman
Posts: 270
Joined: 29 Jan 2010 16:36

Re: How can i get 2 bytes of an int? please help

#2 Post by underworldman » 07 Jul 2010 23:36

Just found that i can use Lo() and Hi() of the built_in.h to separate the bytes. which took me a long time to figure out.

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: How can i get 2 bytes of an int? please help

#3 Post by Sparky1039 » 08 Jul 2010 06:03

Here is another method to achieve the same result... by using a Union. This has the benefit of allowing access to high or low bytes independently and to the full word when needed. It also has the added benefit of not having to rely on bit shifting as found in other methods.

Create the Union template somwhere before "void main ()":

Code: Select all

union U16_
  {
     unsigned int word;      // For accessing the whole 16-bit unsigned int
     unsigned char byte[2];  // For accessing 16-bits as individual bytes
  };
Next create the instance where you need to use the Union in a function and load it with data:

Code: Select all

void WriteMyEE_Bytes (unsigned int MyInWord)
{
union U16_ Int2Bytes; 
...
	Int2Bytes.word = MyInWord;
		
             EEPROM_WR (Int2Bytes.byte[1]);  // write the hi byte
             EEPROM_WR (Int2Bytes.byte[0]);  // write the lo byte to eeprom
...
}
This same kind of union can also work the other way around when you want to combine 2-bytes into 1 word (16-bits). This technique is handy for reading 16-bit timer registers or ADC registers... ect... basically anytime two bytes are read out and are needed as a combined word.

Code: Select all

unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
...
...		
             Bytes2Int.byte[1] = HiInbyte;  //  combine bytes
             Bytes2Int.byte[0] = LoInbyte;  // 
...
	
	return (Bytes2Int.word); // return the combined bytes as a 16-bit word
}

underworldman
Posts: 270
Joined: 29 Jan 2010 16:36

Re: How can i get 2 bytes of an int? please help

#4 Post by underworldman » 08 Jul 2010 13:08

Thanks Sparky. Will sure try your approach as well. But don't you think just using lo(), hi(), higher() and highest() is fastest possible way? its like you don't need more than one line of code to get a byte.

User avatar
ranko.rankovic
Posts: 433
Joined: 11 Jun 2010 09:22

Re: How can i get 2 bytes of an int? please help

#5 Post by ranko.rankovic » 08 Jul 2010 14:24

Hello underworldman,

These Built-in Routines are made for easier and faster code writing. So in fact, for your need this is right and fast solution. All of these routines are detail explained in our help file.

Best regards
Ranko Rankovic
mikroElektronika [Support Department]

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: How can i get 2 bytes of an int? please help

#6 Post by Sparky1039 » 08 Jul 2010 19:23

In programming there are always different ways to arrive at the same solution. Using a union is one way, Hi/Lo commands are another. As Ranko said using the Hi/Lo commands are a fast and easy solution to do what you want and a perfect solution for programmers with less experience.

However this does come at a cost, that being the Hi/Lo commands are compiler specific. So if you ever decided to port this code over to another processor platform which uses a different tool set, all Hi/Lo commands will have to be re-coded to work correctly. From a code size or execution speed perspective I don't know which is better because I never did a comparison. Might be an interesting test to try.

The real benefit of the union approach is the ability to perform two processes with one command. That is the ability to combine 2-bytes into 1-word, or take 1-word and split it into 2-bytes. No compiler specific commands are needed. The only portability issue that one may encounter with the union technique is the byte order in which the data is stored in memory. In the case of PIC's it's little endian first i.e byte[0] = lowbyte, byte[1]= highbyte. With some compiler and processor platforms this order is reversed. From the programmers perspective this is easily accomodated by assigning the data to the correct locations.

underworldman
Posts: 270
Joined: 29 Jan 2010 16:36

Re: How can i get 2 bytes of an int? please help

#7 Post by underworldman » 09 Jul 2010 15:51

Thanks for your explanation Sparky and Ranko :shock:

Post Reply

Return to “mikroC PRO for PIC General”