|
|
View previous topic :: View next topic |
Author |
Message |
Mortal
Joined: 15 Dec 2010 Posts: 2
|
i2c communication doesn't work... |
Posted: Wed Dec 15, 2010 2:41 am |
|
|
Thanks to this forum i have master and slave codes. But i cannot communicate the master with slave. I put some LEDs to test the circuit and i guess the interrupt in the slave is not triggered.
My master is 18F452, and slave is 16F877A. I have two 4,7K pull-up resistors for SCL and SDA lines. Vdd is 5V. My compiler version is 4.110.
Master code:
Code: | #include <18f452.H>
#device *=16
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
#define SLAVE1_WRT_ADDR 0x12
#define SLAVE1_READ_ADDR 0x13
#define BUF_SIZE 8
//====================================
void main()
{
int16 i;
int16 data[BUF_SIZE];
data[0]=0;
while(1)
{
output_high(PIN_D0); //works
if(data[0]!=0x12) output_high(PIN_D1); //works
else output_low(PIN_D1); //doesn't work
delay_ms(1000);
i2c_start();
i2c_write(SLAVE1_READ_ADDR);
for(i = 0; i < 7; i++)
{
data[i] = i2c_read();
delay_ms(50);
}
delay_ms(500);
data[7] = i2c_read(0);
i2c_stop();
output_low(PIN_D0); //doesn't work
}
} |
Slave code:
Code: | #include <16F877A.h>
#device *=16
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT, PUT, NOLVP
#use delay(clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x12)
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;
state = i2c_isr_state();
output_high(PIN_D0); //doesn't work
if(state < 0x80)
{
incoming = i2c_read();
}
if((state >= 0x80))
{
i2c_write(0x12);
}
}
//======================================
void main ()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1){
delay_ms(300); //works
output_high(PIN_D1); //works
delay_ms(300); //works
output_low(PIN_D1); //works
}
} |
At the first try the SDA line goes low and never comes high (i see it from the LEDs). I searched the forum but it didn't helped much.
Can anyone tell me where i am mistaking? May i be mistaking with the "#fuses..." part? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Dec 15, 2010 3:31 am |
|
|
Even if things were working perfectly, you would never see the output_low(PIN_D0).
Think about it, the computer is executing 5MIPS. The pin is sent low, then the code jumps back to the start of the loop, and the pin is driven high. Time between, about 4 instruction times (two times for a jump, and a couple to update the TRIS). So under 1uSec. This will be completely invisible.....
Now, do not understand what you mean by seeing SDA go high with the LED's?. Are you saying that you have put an LED on the SDA line?. If so, this will almost certainly stop it working. The pull up is only 4.7K, and the PIC pin needs to go up to 4v, to see a logic 1 (assuming you are running on 5v). If it doesn't, the code cannot work. The interrupt will never trigger.....
Best Wishes |
|
|
Mortal
Joined: 15 Dec 2010 Posts: 2
|
|
Posted: Wed Dec 15, 2010 4:47 am |
|
|
Thank you Ttelmah...
I didn't think the LEDs on the SDA and SCL lines can cause problems. But they did. When i remove the LEDs the communication works perfectly.
Thank you... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Dec 15, 2010 6:11 am |
|
|
Worth just thinking about it. The 'high' level on a Schmitt input, is 0.8*Vcc (4v with a 5v supply). With 4K7R pull-ups, The I2C bus can source a maximum of 0.212mA, and take the line to this level. Any more, and the system won't work....
You can put LED's on the lines, by going the 'other way', and monitoring when the line is pulled _down_. In this direction, the drivers can typically deliver several mA, but really something involving less current (scope, logic analyser etc.,), and with faster responses (remember the pulses on the I2C but will only be 10uSec wide, even running at 100KHz), is the way to monitor the bus.
Best Wishes |
|
|
|
|
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
|