gặp vấn đề gửi dữ liệu từ slave đến master, mình dung pic16f877a

Collapse
X
 
  • Giờ
  • Show
Clear All
new posts
  • travp
    Thành viên mới
    • Sep 2012
    • 5

    #1

    gặp vấn đề gửi dữ liệu từ slave đến master, mình dung pic16f877a

    ví dụ mình muốn truyền số 5 từ master đến slave , rồi con slave lại gửi số 5 đó cho con master và con chủ phải hiện giá trị đó ra cổng B,mình viết code rùi mô phổng trên protues nó toàn bị lỗi.
    code của mình đây sửa hộ mình với:
    con master:
    #include <16F877A.H>
    #fuses HS,NOWDT,NOPROTECT,NOLVP
    #use Delay(Clock=20000000)
    #use i2c(master, sda=PIN_C4, scl=PIN_C3)
    int8 value_re1,n;
    void write_I2C(int8 value1,int8 slave_addr)
    {
    i2c_start();
    i2c_write(slave_addr);
    i2c_write(value1);
    i2c_stop();
    }

    void read_I2C()
    {

    i2c_start();
    n=i2c_write(0x11);
    while(n==1)
    {
    i2c_start();
    n=i2c_write(0x11);

    }
    value_re1 = i2c_read();
    i2c_read(1,0);
    i2c_stop();
    }

    void main()
    {
    const int8 slave_addr = 0x10;
    set_tris_b(0x00);
    while(1)
    {
    write_I2C(5,slave_addr);
    delay_ms(10);
    read_I2C();
    output_b(value_re1);
    }
    }
    còn đây là slave:
    #include <16F877A.H>
    #fuses HS,NOWDT,NOPROTECT,NOLVP

    #use delay(Clock=20000000)
    #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10)
    BYTE addr;
    #INT_SSP
    void i2c_isr()
    {
    BYTE incoming, state;
    state = i2c_isr_state();
    if(state < 0x80)
    {
    incoming = i2c_read();
    if(state == 1)
    addr = incoming;

    }
    if(state ==0x80)
    {
    i2c_write(addr);

    }
    }

    void main()
    {
    enable_interrupts(INT_SSP);
    enable_interrupts(GLOBAL);
    set_tris_b(0x00);
    while(1)
    {
    output_b(addr);

    }
    }
  • KhoaiLuoc
    Thành viên tích cực
    • Dec 2010
    • 137

    #2
    Chổ này phải là

    #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10, CW_HARDWARE)
    Mình nhớ là như vậy ... bạn xem thử file help phần I2C xem sao

    Comment

    Về tác giả

    Collapse

    travp Tìm hiểu thêm về travp

    Bài viết mới nhất

    Collapse

    Đang tải...