Ðề tài: Giao tiếp I2C
View Single Post
Old 13-09-2006, 05:24 PM   #12
hoanf
Đệ tử 3 túi
 
Tham gia ngày: Jun 2006
Bài gửi: 51
:
:d

Chào các bạn và Hà.
Mình mới viết được chương trình giao tiếp I2C đơn giản dùng các hàm của CCS.
Mời các bạn xem và cho nhận xét

Master: truyền dữ liệu cho Slave. Mỗi lần truyền 1 byte.

Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=4000000)

#define SLAVE_ADDRESS 0x10
#use i2c(master, sda=PIN_C4, scl=PIN_C3)

void write_I2C(int8 a)
{
   i2c_start();
   i2c_write(SLAVE_ADDRESS);
   i2c_write(a);
   i2c_stop();
}

void main()
{
   int8 value;
   
   value = 0;
   while(1){
      write_I2C(value);
      value++;
      delay_ms(100);
   }
}

Slave thì chỉ tiến hành kiểm tra có phải Master truyền hay kô. Nếu truyền thì nhận byte dữ liệu và hiển thị lên port_B:
Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP

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

int8 value;

#INT_SSP
void i2c_isr()
{
   int8 state;
   int8 address;
   state = i2c_isr_state();
   if(state == 0)
      address = i2c_read();
   else if(state < 0x80)
         value = i2c_read();
}

void main()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);

   set_tris_b(0x00);
   while(1){
      output_b(value);
   }
}
Thân
hoanf

thay đổi nội dung bởi: hoanf, 15-09-2006 lúc 10:00 AM.
hoanf vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn