View previous topic :: View next topic |
Author |
Message |
trabk1234
Joined: 12 Mar 2013 Posts: 1
|
help transfer data from slave to master |
Posted: Tue Mar 12, 2013 2:30 am |
|
|
for example I want to pass the number 5 from the master to the slave, then the slave to send the number 5 for the master and the owner is the value that the port B, write code and simulation on protues it all fails.
your code is correct for me with:
code:
//master
#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=20000000)
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
int8 value_re1,n;
void write_I2C(int8 value1,int8 slave_addr)
{
i2c_start();
i2c_write(slave_addr);
i2c_write(value1);
i2c_stop();
}
void read_I2C()
{
i2c_start();
n=i2c_write(0x11);
while(n==1)
{
i2c_start();
n=i2c_write(0x11);
}
value_re1 = i2c_read();
i2c_read(1,0);
i2c_stop();
}
void main()
{
const int8 slave_addr = 0x10;
set_tris_b(0x00);
while(1)
{
write_I2C(5,slave_addr);
delay_ms(10);
read_I2C();
output_b(value_re1);
}
}
// slave
#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(Clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10)
BYTE addr;
#INT_SSP
void i2c_isr()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
addr = incoming;
}
if(state ==0x80)
{
i2c_write(addr);
}
}
void main()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
set_tris_b(0x00);
while(1)
{
output_b(addr);
}
}
[/code] |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Mar 12, 2013 5:50 am |
|
|
also...
hmm...
"simulation on protues it all fails."
PIC101.
WHO says 'Proteus' is 100% working correctly?
Your PIC code could be 100% correct in the real world and fail in teh Proteus simulation......100s of others have found that out!!! |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Tue Mar 12, 2013 8:10 am |
|
|
However the manual should really be the starting point.
For instance, what is this meant to do?:
i2c_read(1,0);
When does I2C read use two arguments?. What then are the arguments?. Does the setup you are using support this?.
Some basic study of the manual, versus the lines posted would help a lot. Then look at the I2C examples.
It is like at least taking the time to work out what a steering wheel is, and where it is in a car, _before_ trying to drive.... |
|
|
|