các anh giúp em với. em copy bài các bài trên xuống dùng và chạy bình thường. nhưng khi dùng cho 2 con slever thì gặp vấn đề. nếu con 1 chạy thì con 2 không chạy. nếu bỏ đoạn con 1 trong master thì con 2 chạy. nếu dùng cả 2 con thì chỉ có con 1 chạy xong rồi đứng yên. không rỏ nguyên nhân vì sao. mong chỉ giáo
Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=4000000)
#use i2c(master, sda=PIN_C4, scl=PIN_C3, force_hw)
void write_I2C(int8 value, int8 slave_addr)
{
i2c_start();
i2c_write(slave_addr);
i2c_write(value);
i2c_stop();
}
//==============================================
/*void write_I2C2(int8 value, int8 slave_addr1)
{
i2c_start();
i2c_write(slave_addr1);
i2c_write(value);
i2c_stop();
}*/
int8 read_I2C(int8 slave_addr)
{
int8 value_re;
i2c_start();
i2c_write(slave_addr+1);
value_re = i2c_read(0);
i2c_stop();
return value_re;
}
void main()
{
int8 value_re;
int8 i,a;
const int8 N = 8;
const int8 DIGITS[N] ={ 0b11111111,
0b11111110,
0b11111100,
0b11111000,
0b11110000,
0b11100000,
0b11000000,
0b10000000,
};
set_tris_b(0x00);
while(1)
{
// con 1
for(i = 0; i<8; i++)
{
write_I2C(DIGITS[i], 0x10);
delay_ms(500);
value_re = read_I2C(0x10);
output_b(value_re);
}
// con 2
for(a = 0; a<8; a++)
{
write_I2C(DIGITS[a],0x20);
delay_ms(500);
value_re = read_I2C(0x20);
output_b(value_re);
}
}
}
con slever1
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock=4000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10, force_hw)
int8 value = 0x01;
#INT_SSP
void i2c_isr()
{
int8 state;
state = i2c_isr_state();
if(state < 0x80)
value = i2c_read();
if(state == 0x80)
{
i2c_write(value);
}
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
set_tris_b(0x00);
while(1)
{
output_b(value);
}
}
con slever 2
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock=4000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x20, force_hw)
int8 value1 = 0x01;
#INT_SSP
void i2c_isr()
{
int8 state;
state = i2c_isr_state();
if(state < 0x80)
value1 = i2c_read();
if(state == 0x80)
{
i2c_write(value1);
}
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
set_tris_b(0x00);
while(1)
{
output_b(value1);
}
}