Ðề tài: Giao tiếp I2C
View Single Post
Old 25-10-2010, 10:02 AM   #167
liemcena
Nhập môn đệ tử
 
Tham gia ngày: Sep 2010
Bài gửi: 1
:
Giúp em i2c

giúp em i2c với em mới học về pic và i2c : dipswitch truyền dữ liệu cho master , master truyền cho slave ,slave hiển thị lên led(giống việc điều khiển đèn bằng dipswitch qua chuẩn i2c)
code Master:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=4000000)
#byte portb=0x06
#define SLAVE_ADDRESS 0x10
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
#use Fast_IO(b)
void write_I2C(int8 data)
{
i2c_start();
i2c_write(SLAVE_ADDRESS);
i2c_write(data);
i2c_stop();
}
void main()
{
int8 data;
set_tris_b(255);
data=input_b();
while(1)
{
write_I2C(data);
delay_ms(100);
}
}
code slave:

#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(Clock=4000000)
#use i2c(SLAVE,SDA=PIN_C4,SCL=PIN_C3)
#define SLAVE_ADDRESS 0x10
#use Fast_IO(b)

void read_I2C()
{
int8 data;
i2c_start();
i2c_read(SLAVE_ADDRESS);
data=i2c_read();
delay_ms(100);
i2c_stop();
}


void main()
{
int8 data;
set_tris_b(0x00);

while(1)
{
read_I2C();
output_b(data);
}
}
không biết sai ở đâu nữa mô phỏng bằng proteus không chạy gì cả
Hình Kèm Theo
File Type: jpg untitled.JPG (223.4 KB, 22 lần tải)

thay đổi nội dung bởi: liemcena, 26-10-2010 lúc 10:55 PM.
liemcena vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn