View previous topic :: View next topic |
Author |
Message |
wistonkee
Joined: 23 Nov 2016 Posts: 2
|
(beginner) i2c coding |
Posted: Thu Nov 24, 2016 1:19 am |
|
|
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: 19591
|
|
Posted: Thu Nov 24, 2016 2:12 am |
|
|
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
|
|
Posted: Thu Nov 24, 2016 3:42 am |
|
|
Thank you Ttelmah! You helped me to understand what the code is trying to do. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9271 Location: Greensville,Ontario
|
|
Posted: Thu Nov 24, 2016 8:38 am |
|
|
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 |
|
|
|