Chào các bạn!!
Cho mình post sơ đồ và code của minh lên đây cho các bạn coi và có gì góp ý cho mình nha.
Đây là đoạn code Master: Master truyền một mảng cho Slave và nhận về mảng mà đã chuyển cho Slave để hiển thị lên port_b(kiểm tra hihi...) giống echo.
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();
}
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;
const int8 N = 8;
const int8 DIGITS[N] ={ 0b11111111,
0b11111110,
0b11111100,
0b11111000,
0b11110000,
0b11100000,
0b11000000,
0b10000000,
};
const int8 slave_addr = 0x10;
set_tris_b(0x00);
while(1){
for(i = 0; i<8; i++){
write_I2C(DIGITS[i], slave_addr);
delay_ms(500);
value_re = read_I2C(slave_addr);
output_b(value_re);
}
}
}
Còn đây là đoạn code Slave:
Code:
#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);
}
}