View Single Post
Old 22-06-2008, 02:11 AM   #1
manhha2799
Đệ tử 5 túi
 
Tham gia ngày: Apr 2007
Bài gửi: 121
:
Send a message via Yahoo to manhha2799
Giao tiếp I2C giữa các pic

- 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:
Code:
#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:
 
- 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 :

Code:
 
#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);
      }
   
}

thay đổi nội dung bởi: manhha2799, 22-06-2008 lúc 01:20 PM.
manhha2799 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn