cjusto
Joined: 26 Apr 2006 Posts: 38 Location: Portugal
|
i2c problem: slave freezes i2c bus |
Posted: Wed May 10, 2006 2:48 pm |
|
|
hi guys!
i am trying to make an i2c based protocol message.
i need to interact 2 pics (16F877).
i am sending 6 bytes from master to slave. till here no problem.
after recognizing the correct sequence i send data from slave to master.
i can send one byte with no problems.
when i send the second, the clock line remains low. when i disconnect the line in the slave's side, it becomes in a high level.
can someone check what i am doing wrong??
here is the code for master:
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay (clock=4000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c (MASTER, SDA=PIN_C4, SCL=PIN_C3)
#define SLAVE_ADDRESS 0xA0
void initI2C()
{
output_float(PIN_C3);
output_float(PIN_C4);
}
void main ()
{
int i;
BYTE in_buffer[11] = {0,0,0,0,0,0,0,0,0,0};
initI2C();
delay_ms(1000);
printf("MASTER \r\n");
while(TRUE){
i2c_start();
printf("i2c start\r\n");
i2c_write(SLAVE_ADDRESS); // Device Address
printf("address sent \r\n");
i2c_write(0x01);
printf("SOH sent\r\n");
i2c_write(0x30);
printf("0 sent\r\n");
i2c_write(0x30);
printf("0 sent\r\n");
i2c_write(0x31);
printf("1 sent \r\n");
i2c_write(0x32);
printf("2 sent\r\n");
i2c_write(0x17);
printf("etb sent \r\n");
//begining slave's data reception
//inicio de recepcao de dados do slave
delay_ms(1000);
i2c_start();
i2c_write(SLAVE_ADDRESS + 1);
printf("enviei endereco + 1 \r\n");
for(i=0;i<2;i++)
{
// delay_ms(100);
in_buffer[i] = i2c_read(0xff);
printf("in_buffer[%d] = %c \r\n",i, in_buffer[i]);
}
i2c_stop();
printf("i2c stop\r\n");
delay_ms(3000);
}
}
|
here is the code for the slave:
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay (clock=4000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define SLAVE_ADDRESS 0xa0
#use i2c (SLAVE, SDA=PIN_C4, SCL=PIN_C3, address= SLAVE_ADDRESS, FORCE_HW)
int x = 0;
int y = 0;
int code_full = 0;
int code_done = 0;
BYTE in_buffer[10] = {0,0,0,0,0,0,0,0,0,0};
//BYTE out_buffer[11] = {0,0,0,0,0,0,0,0,0,0,0};
BYTE out_buffer[2] = {0,0};
void initI2C()
{
output_float(PIN_C3);
output_float(PIN_C4);
}
#INT_SSP
void ssp_interupt ()
{
if(i2c_poll())
{
in_buffer[x] = i2c_read(0xff);
printf("\r\nrecebi %d",in_buffer[x]);
x++;
}
else //if(code_done)
{
i2c_write(out_buffer[y]);
printf("enviei %d\r\n",out_buffer[y]);
y++;
}
if(x==7)
{
x = 0;
code_full = 1;
}
if(y==11)
{
y = 0;
code_done = 0;
}
//clear_interrupt(INT_SSP);
}
void main ()
{
int incoming;
int i;
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
printf("\r\nSLAVE: main ");
while(TRUE)
{
if(code_full)
{
if(in_buffer [1] == 1) //soh
if(in_buffer [2] == 48) //0
if(in_buffer [3] == 48) //0
if(in_buffer [4] == 49) //1
if(in_buffer [5] == 50) //2
if(in_buffer [6] == 23) //etb
{
out_buffer[0] = 2; //stx
printf("\r\n buffer0 written");
out_buffer[1] = 48; //0
printf("\r\n buffer1 written");
code_done = 1;
printf("escrevi valores\r\n");
}
code_full = 0;
}
}
}
|
i hope someone can help me!!
thank you |
|