PDA

View Full Version : Giao tiếp I2C giữa các pic


manhha2799
22-06-2008, 02:11 AM
- Em có viết chương trình cho 1 pic master 16f877a và 2 slave 16f877a. Với 1 slave thì em truyền dữ liệu bằng giao thức I2C được rồi( tham khảo trong mục giao tiếp I2C). Nhưng khi truyền dữ liệu từ pic master qua cho 2 slave thi không được nữa, nó không chạy gì hết. Tại sao vậy? . Các bác chỉ giúp em. Bác nào có code của 1 master và 2 salve đã chạy thì post lên cho mọi người xem nhé. Em thấy vấn đề này có nhiều người hỏi rồi mà không ai trả lời hết.
- Em chưa làm mạch thực, chỉ mới mô phỏng protues 7.2 sp2.

Em post code của master và 2 slave.
- Code cho master:

#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=20000000)

#use i2c(master, sda=PIN_C4, scl=PIN_C3, force_hw)

void write_I2C(int8 value1,int8 slave_addr1,int8 value2,int8 slave_addr2)
{
i2c_start();
i2c_write(slave_addr1);
i2c_write(value1);
i2c_stop();

i2c_start();
i2c_write(slave_addr2);
i2c_write(value2);
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,value_re1;
int8 i,j;
const int8 N = 8;
const int8 DIGITS[N] ={ 0b11111111,
0b11111110,
0b11111100,
0b11111000,
0b11110000,
0b11100000,
0b11000000,
0b10000000,
};
const int8 slave_addr3 = 0x10;
const int8 slave_addr4 = 0x20;
set_tris_b(0x00);
set_tris_d(0x00);
while(1)
{
for (i=0;i<=8;i++)
{
write_I2C(DIGITS[i],slave_addr3,DIGITS[i],slave_addr4);
delay_ms(100);}
}
}



- Code cho slave 1 :

#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP

#use delay(Clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10, force_hw)

int8 value = 0x01;

#INT_SSP
void i2c_isr1()
{
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);
}
}
}


- Code cho slave 2 :


#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(Clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x20, force_hw)

int8 value = 0x01;

#INT_SSP
void i2c_isr2()
{
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);
}

}

namqn
22-06-2008, 05:27 AM
Tôi không dùng CCS C, nhưng code mà bạn đã post có phần directive cho master có vẻ không hợp lý:
#use i2c(multi_master, sda=PIN_C4, scl=PIN_C3, force_hw)
Bạn chỉ có 1 master, tại sao lại thiết lập là multi_master?

Thân,

manhha2799
22-06-2008, 01:27 PM
Em đã sửa lại la master. Nhưng vẫn không hiển thị được. Có ai đã làm bằng ccs c thì chỉ em với.

namqn
22-06-2008, 05:32 PM
Em đã sửa lại la master. Nhưng vẫn không hiển thị được. Có ai đã làm bằng ccs c thì chỉ em với.
Bạn nên làm mạch thực. Proteus không thể mô phỏng mọi thứ, và có nhiều trường hợp mô phỏng tốt những mạch thực không chạy.

Thân,