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 support@ccsinfo.com

12c_write

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



Joined: 19 Nov 2003
Posts: 45
Location: Oxford

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

12c_write
PostPosted: Wed Mar 03, 2004 3:26 am     Reply with quote

I was wondering what each line in the following actually does. I know that both are used to set the actual byte address to be written but I am not sure what the two lines actually do.

Code:
i2c_write((char)(addr >> 8));
i2c_write((char)addr);


thanks for your help!!! Idea
Ttelmah
Guest







Re: 12c_write
PostPosted: Wed Mar 03, 2004 4:08 am     Reply with quote

homfray wrote:
I was wondering what each line in the following actually does. I know that both are used to set the actual byte address to be written but I am not sure what the two lines actually do.

Code:
i2c_write((char)(addr >> 8));
i2c_write((char)addr);


thanks for your help!!! Idea

The address is a 16 bit value. The >>8 operation, rotates the 16bit value right 8bits, so that the top eight bits are now in the low part of the number. The (char) 'cast', converts the result to an 8bit number (thereby returning the 'high' 8bits.
Then the cast in the second line, returns just the low 8 bits.
So the two writes send the 16bit address, in two sequential bytes, MSB first, and then LSB.
There are probably about a dozen other ways of doing the same thing!. For instance:

(char)(addr/256)

gives the same result as rotating the character right eight times in this context (and will produce the same compiler code).

The construct being used, is a pretty 'generic' one (will work with 99% of C compilers). In CCS C, there is also this version:

code]
i2c_write(make8(addr,1));
i2c_write(make8(addr,0));
[/code]

This form is 'CCS specific' (the 'make8' function doesn't exist in most compilers), but has the advantage that the first line will code as a single byte move (just fetching the high byte), whereas the 'rotation' version will involve quite a few machine instructions. The second line generates the same code for either version.

Best Wishes
homfray



Joined: 19 Nov 2003
Posts: 45
Location: Oxford

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

PostPosted: Wed Mar 03, 2004 4:39 am     Reply with quote

thats great stuff, it's what I pretty much figured, but as I am quite new to C, guessing and knowing are two very seperate things

thanks
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