View previous topic :: View next topic |
Author |
Message |
nbt1987
Joined: 28 May 2010 Posts: 3
|
how to use int16 |
Posted: Fri May 28, 2010 11:01 pm |
|
|
I use CCS, PIC8F4431. I declare int16 variable as follow:
Code: |
int16 tong;
#byte tong = 0x058
#byte tongH = 0x058
#byte tongL = 0x059
|
I do that becase I think "the high byte of tong is in 0x058 and the low byte of tong is in 0x059"
But it's wrong because only high byte is in 0x058.
I want to know where's lowbyte??
Sorry for my bad english and thanks for your help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 29, 2010 12:16 am |
|
|
The CCS manual says this, on page 44 (page 56 in the Acrobat reader):
Quote: |
Integers are stored in little endian format. The LSB is in the lowest address.
|
|
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Sat May 29, 2010 12:22 am |
|
|
The first problem I see in your code is 'tong' is not a byte, its is a word.
I gather from your code snippet that you want to access the high and low byte of 'tong'. Try this:
Code: | int16 tong;
int8* tong_ptr=&(tong);
int8 tongH;
int8 tongL; |
In your code: Code: | tongH=*(tptr+1);
tongL=*tptr; |
Rohit |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Sat May 29, 2010 12:46 am |
|
|
Hmm, was just checking out #byte and #word.
This is my code:
Code: | #byte t=0x1FE
int16* ptr=&t;
*ptr=260; |
Compared it with:
Code: | #word t=0x1FE
int16* ptr=&t;
*ptr=260; |
Both give the value of *ptr as 0x0104 (=260). Is this correct? I mean technically, shouldn't ptr be 0x04 in the first case and 0x0104 in the second? According to the CCS help for #byte: Quote: | If the id (ie 't') is not known a new C variable is created and placed as address x (ie0x1FE). |
Is this just a case of the CCS compiler being 'nice and accommodating'?
Rohit |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19562
|
|
Posted: Sat May 29, 2010 2:43 am |
|
|
#byte, creates a 'byte' variable at an address _or_ if given the name of an existing variable, locates that variable 'at' the address.
#word, creates a 'word' variable, or again if given the name of an existing variable, locates that variable 'at' an address.
So, in the case of 'tong', it is perfectly OK, to declare an int16, and use #byte to locate it, or to just use:
#word tong=0x58
Rohit. ptr, is a pointer to an int16 value. It doesn't care whether the value has been declared earlier to be an 8bit value, or a 16bit value.
The original code posted by nbt1987, is OK, _except_ that the order of the bytes is reversed.
Best Wishes |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat May 29, 2010 5:51 am |
|
|
I usually use structs and unions to access multi-byte values. Maybe it's way easier on the eyes and a little more portable.
one variation might be:
struct {
int8 low;
int8 hi;
} val;
val.hi = 0xff;
val.lo = 0x44; _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Sat May 29, 2010 10:21 am |
|
|
Ttelmah, the reason I'm still confused is because the CCS help explicitly states that if the variable does not exist, then #byte creates an int8, and #word creates an int16. Did the manual mean 'locate' instead of 'create'?
Rohit |
|
|
nbt1987
Joined: 28 May 2010 Posts: 3
|
|
Posted: Sat May 29, 2010 10:39 am |
|
|
Thanks for all!
When i change the address as follow:
int16 tong;
#word tong = 0x201
#byte tongH = 0x202
#byte tongL = 0x201
It's true. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat May 29, 2010 10:49 am |
|
|
I still don't understand why you are explicitly laying out a variable in memory when the compiler (is supposed to and) does it for you?
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|