|
|
View previous topic :: View next topic |
Author |
Message |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
18f46j50 i2c master and slave mode |
Posted: Sat Jan 25, 2014 9:40 am |
|
|
hi
I want to use 18f46j50 in master and slave mode i2c.
In master mode read from device(such as compass) and in slave mode send data to other micro.
Can you help me for config code and wire conection?
thanx |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Sun Jan 26, 2014 1:45 am |
|
|
Read the data sheet.
This tells you what pins SDA1, SDA2, SCL1, and SCL2 are on.
Use these pins.
Then just have two #use I2C statements. One setting up a master (with the clock rate your peripheral requires), using the pins for one of the ports. Give this a stream name of perhaps 'MASTER'. Then a second #use I2C setting up a slave on the other port. Perhaps called 'SLAVE'. In each case have FORCE_HW. Remember the slave address will need to be what the other 'master' wants to talk to.
Then code is exactly the same as the many examples supplied with the compiler, in the code library, and posted here, except that the slave code, must have the stream designator added to each I2C statement (SLAVE), and the the master code must have it's stream designator added to it's statements.
You can't generally 'switch modes' with normal I2C, so just use the two ports. Trying to change modes can easily lead to bus collisions, and should only be done with devices 'built' to handle this on the bus (multi-master). Unless your other 'master' device, has the bus arbitration to handle multi-master operations, then this is a recipe for disaster. Much safer and easier to use the two ports.
Best Wishes |
|
|
tems
Joined: 26 Nov 2008 Posts: 8
|
I2C master and slave |
Posted: Mon Jan 27, 2014 8:17 am |
|
|
If you are using version 5.16 and possibly earlier you must setup the #use I2C as given below. Version 5.17 does not exhibit this problem.
From CCS
The problem is due to an error in the #use i2c() directive. I have fixed the problem and it will be available in the next compiler release. However the problem only occurs if the #use i2c() directive setting up the HW slave is above the #use i2c() directive setting up the HW master. So the quick solution to your problem is to move your #use i2c(Master,Fast,I2C2,restart_wdt,force_hw,stream=eemem) line above your #use i2c(Slave,Fast,I2C1,restart_wdt,force_hw,address=0x20,stream=com) line. |
|
|
|
|
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
|