CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to use int16

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nbt1987



Joined: 28 May 2010
Posts: 3

View user's profile Send private message

how to use int16
PostPosted: Fri May 28, 2010 11:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 29, 2010 12:16 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat May 29, 2010 12:22 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat May 29, 2010 12:46 am     Reply with quote

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'? Confused

Rohit
Ttelmah



Joined: 11 Mar 2010
Posts: 19563

View user's profile Send private message

PostPosted: Sat May 29, 2010 2:43 am     Reply with quote

#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

View user's profile Send private message

PostPosted: Sat May 29, 2010 5:51 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat May 29, 2010 10:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 29, 2010 10:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 29, 2010 10:49 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group