View previous topic :: View next topic |
Author |
Message |
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
Questions on I2C bus - problem |
Posted: Wed Aug 23, 2006 4:15 pm |
|
|
hi forum!
i am using 2 PIC18f4525 and communicating via i2c.
i can receive data correctly when sending from master to slave.
when i try to read from slave, it doesnt work. i have some code i had tried in 16f877 and it worked.
at that time i was using an older version of ccs.
at this moment i have 3.235 version.
i made some tests. what's happening is that
slave is receiving the write adress and entering the
condition.
can someone help me? what can be the problem?
the other questions i have is:
- do i always have to make the i2c_stop command after a sequence of i2c_write?
- when reading from the slave, can i send the address once and make successiv reads? or do i need to write address before each i2_read comand?
thanks!!! |
|
|
henry
Joined: 05 Feb 2005 Posts: 10
|
Re: Questions on I2C bus - problem |
Posted: Thu Aug 24, 2006 12:42 am |
|
|
I don't know so much about the 'i2cpoll()' function, so i can't help you with that...........
Quote: |
the other questions i have is:
- do i always have to make the i2c_stop command after a sequence of i2c_write?
|
No, you don't...
this is my routine to write in to the SLAVE, maybe it can help you:
Code: |
void write_i2c_slave(int address)
{
while( !i2c_ready() ); //Espera hasta que el dispositivo i2c al que se
//quiere contactar este listo
i2c_start(); //bit de inicio i2c
delay_cycles( 10 );
i2c_write( address ); //escribe direccion de dispositivo a contactar y
delay_cycles( 10 ); //comando de escritura ( Lsb = 0 ) Ej:0xa0
i2c_write( dato1_i2c ); //escribe primer byte de datos
delay_cycles( 10 );
i2c_write( dato2_i2c ); //escribe segundo byte de datos
delay_cycles( 10 );
i2c_stop(); //bit de parada de i2c
delay_cycles( 10 );
}
|
Quote: |
- when reading from the slave, can i send the address once and make successiv reads? or do i need to write address before each i2_read comand?
|
After sending the 'address', you can do sucessives reads as you want......in the last read you must use the function:
your_register = i2c_read(0); for sending the NACK bit to the slave
like you can see in my code for reading the SLAVE, i do 2 sucessives readings,
the first one:
dato1_i2c = i2c_read(); for sending the ACK bit to the slave
the second one:
dato2_i2c = i2c_read(0); for sending the NACK bit to the slave
Code: |
read_i2c_slave(int address)
{
while( !i2c_ready() ); //Espera hasta que el dispositivo i2c al que se
//quiere contactar este listo
i2c_start(); //bit START
delay_cycles( 10 );
i2c_write( address ); //Envia direccion de dispositivo a contactar y
delay_cycles( 10 ); //comando de lectura ( Lsb = 1 ) Ej:0xa1
dato1_i2c = i2c_read(); //generacion de clock + lectura de dato + ACK
delay_cycles( 10 ); //asi el SLAVE continua enviando datos
dato2_i2c = i2c_read(0);//generacion de clock + lectura de dato + NACK
delay_cycles( 10 ); //asi el SLAVE parar de enviar datos
i2c_stop(); //Bit STOP
delay_cycles( 10 );
}
|
The delays used in the routines are for giving enough time to the slave to handle de I2C interruption, otherwise my code doesn't works....
Regards
Henry |
|
|
cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
|
Posted: Thu Aug 24, 2006 3:51 pm |
|
|
olá Henry!!
thanks for your answer!!
it helped me a lot.
the problem was the last i2c_read(). i wasn't doing the NACK.
when sending it was ok. when receiving i forgot that point.
the i2c_poll(), as i understand, when using the ssp interrupt, if we enter the interrupt, and there is data arriving the bus it is 1, otherwise it means that the master wants to read data from the slave. it is explained in some examples and in help.
this part i resolved, now i am using the i2c_isr_state() function.
why do you use delay_cycles() instead of delay_us() ?
is there any special reason? i usually use delay_ms and delay_us
thanks!!! |
|
|
henry
Joined: 05 Feb 2005 Posts: 10
|
|
Posted: Fri Aug 25, 2006 12:18 am |
|
|
Hi cjusto,
There is no special reason for using delay_cycles()...........it is just a test routine.....I think that in the definitive routine i will use a delay_us()...
Saludos and thanks for the i2c_poll() info.........
henry |
|
|
|