![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
homfray
Joined: 19 Nov 2003 Posts: 45 Location: Oxford
|
12c_write |
Posted: Wed Mar 03, 2004 3:26 am |
|
|
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](images/smiles/icon_idea.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: 12c_write |
Posted: Wed Mar 03, 2004 4:08 am |
|
|
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](images/smiles/icon_idea.gif) |
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 |
|
![](templates/subSilver/images/spacer.gif) |
homfray
Joined: 19 Nov 2003 Posts: 45 Location: Oxford
|
|
Posted: Wed Mar 03, 2004 4:39 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|