Các anh cho em hỏi vấn đề về I2C cái: Em copy chương trình ở thread này và dùng giao tiếp 2 PIC thì có vấn đề như sau: Khi sử dụng để mô phỏng thì chạy bình thường như ý muốn nhưng khi chạy với mạch thật và thêm modul RS232 để đưa lên Terminal trên PC để check thì chỉ thấy có FF được gửi lên. Cao thủ nào từng gặp lỗi này thì gợi ý em cách debug cái

many thanks!
Code: Master
Code:
[#include <16F877A.H>
#include <DEFS_16F877A.h>
#include <def_877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=20000000)
#define use_portd_lcd TRUE
#include <lcd.c>
#include <STDLIBM.H>
//#include <toanlcd.c>
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7)
int8 value_re;
unsigned char* data_in;
const int8 N = 20;
//========================================================
void putch(unsigned char a)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG = a;
}
void write_I2C(unsigned char* digit,int8 slave_addr,int n)
{
int i;
i2c_start();
i2c_write(slave_addr);
for (i=0;i<N;i++)
i2c_write(digit[i]);
i2c_start();
i2c_write(slave_addr);
i2c_stop();
}
void read_I2C()
{
int i;
i2c_start();
i2c_write(0x11);
for (i=0;i<N;i++)
{
value_re = i2c_read();
data_in[i]=value_re;
}
i2c_start();
i2c_write(0x11);
i2c_stop();
}
void main()
{
int8 value_re=2;
int8 i=10;
char* DIGITS[N] ={ 0b11111111,
0b11111110,
0b11111100,
0b11111000,
0b11110000,
0b11100000,
0b10101010,
0b01010101,
};
const int8 slave_addr = 0x10;
set_tris_b(0x00);
putch('a');
data_in=malloc(50);
//write_I2C(DIGITS,0x10,8);
read_I2C();
for (i=0;i<N;i++)
{
output_b(data_in[i]);
putch(data_in[i]);
delay_ms(1000);
}
}]
Code slave:
Code:
:[#include <16F877A.H>
#include <STDLIBM.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(Clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10)
char* data;
int8 value = 0x01,valueh=0,valuel=0;
int8 state;
#INT_SSP
void i2c_isr()
{
state = i2c_isr_state();
if(state >=0x80)i2c_write(data[state-0x80]);
else if(state>0)data[state-1]=i2c_read();
}
void main()
{
const int N=20;
int i;
data=malloc(N);
for (i=0;i<N;i++)
data[i]=i;
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
set_tris_b(0x00);
while(1);
}]
thanks