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

(beginner) i2c coding

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



Joined: 23 Nov 2016
Posts: 2

View user's profile Send private message

(beginner) i2c coding
PostPosted: Thu Nov 24, 2016 1:19 am     Reply with quote

Hi, I am new to embedded programming language here. I am currently trying to understand some existing coding.
Can someone explain to me what does the following code mean please:
1. i2c_write((0xa0|(BYTE)(addr>>7))&0xfe);
2. i2c_write((0xa0|(BYTE)(addr>>7))|1);
3. i2c_write((((0xff00)&addr)>>8));

Appreciate if anyone could help me out. Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 2:12 am     Reply with quote

Nothing as posted.....

You need to show the whole I2C 'transaction'. A single line is meaningless without the context. Just like on RS232, when using some sort of 'protocol', exactly the same byte has different meanings depending on 'where' it is in the packet.
I can guess slightly, however one looks potentially rather silly...

I'd guess that the code has the I2C chip address, low four bits stored in the top byte of a 16bit word. Quite a few I2C chips like memories have the low three or four bits of their bus address adjustable using pins on the chip. So chips like the 24xx256 EEPROM's, have an address of 0b1010 aaad where 'aaa' is set by three bits on the outside of the chip. The 'd' bit is the transaction direction bit. It looks as if somebody has stored the three bits that are adjustable in the word 'addr' as something like:

addr = 0x0100;
(values from 0 to 7 in the second byte would be legitimate for a chip like this)

Then rotating this seven times gives:

0x0002

This is then 'anded' or'ed with 0xA0 to give:

0x00A2

and then anded with 0xFE (this ensures the bottom bit is zero).

This would then be the byte that needs to be sent to the chip as the first byte after an I2C_start.

The second line is for exactly the same purpose, but for a read transaction (would normally only be sent immediately after a bus restart).

The third is the one that looks silly. Given I2C only ever uses the bus address in the seven bit form handled by the previous two transactions, this doesn't work....

It almost looks as if somebody has perhaps misunderstood I2C addressing, and is then trying to use the same address for the register address. That makes no sense.
wistonkee



Joined: 23 Nov 2016
Posts: 2

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 3:42 am     Reply with quote

Thank you Ttelmah! You helped me to understand what the code is trying to do.
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 8:38 am     Reply with quote

Whenever you get code like this, you need a piece of paper, pencil and 'think' like a computer.
You write down the code, use 'square' paper to write out the variables, decide which order the operations occour and write down each step of the process.
By doing this 'playing computer' you'll see what is happening and hopefully why. It may also show you a better way of doing whatever the code is doing. If you know the PIC instruction set, how to access registers, etc. you can 'tweak' any code to be either better or faster.

Us, 'Old guys' who did assembler have done this for decades as it shows you what is going on.

Jay
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