PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 05-09-2008, 03:22 PM   #1
toanck86
Đệ tử 1 túi
 
Tham gia ngày: Apr 2007
Bài gửi: 20
:
hỏi về giao tiếp I2C

Chào các bạn!
Mình đang gặp khó khăn trong giao tiếp I2C. Bài toán của mình là giao tiếp giữa 2 con PIC 16F877A. Khi mình sử dụng thạch anh 4 Mhz thì nó chạy tốt. Nhưng khi thay con thạch anh 20 Mhz thì không còn chạy được nữa

code master:
Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=20000000)
#use i2c(master, sda=PIN_C4, scl=PIN_C3)

void write_I2C(int8 value, int8 slave_addr)
{
   i2c_start();
   delay_ms(1);
   i2c_write(slave_addr);
   delay_ms(1);
   i2c_write(value);
   delay_ms(1);
   i2c_stop();
}

int8 read_I2C(int8 slave_addr)
{
   int8 value_re;
   i2c_start();
   delay_ms(1);
   i2c_write(slave_addr + 1);
   delay_ms(1);
   value_re = i2c_read(0);
   delay_ms(1);
   i2c_stop();
   return value_re;
}

void main()
{
   int8 value_re=2;
   int8 i;
   const int8 N = 8;
   const int8 slave_addr = 0x10;
   set_tris_b(0x00);
   while(1){
      
         write_I2C(1, slave_addr);
         delay_ms(500);
         value_re = read_I2C(slave_addr);
         output_b(value_re);
         delay_ms(500);
          write_I2C(0, slave_addr);
         delay_ms(500);
         value_re = read_I2C(slave_addr);
         output_b(value_re);
         delay_ms(500);
     }
}
************************************************** ***********
code slave:
Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP

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

int8 value = 0x01;

#INT_SSP
void i2c_isr()
{
   int8 state;

   state = i2c_isr_state();
   if(state < 0x80 && state>0)
      value = i2c_read();
   if(state == 0x80){
      i2c_write(value);
   }
}

void main()
{
   
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   set_tris_b(0x00);
   while(1){
      output_b(value);
   }
}
//xin loi vi chua cho code vao khung duoc, mong cac ban thong cam. Giup minh nha!

thay đổi nội dung bởi: namqn, 05-09-2008 lúc 04:31 PM.
toanck86 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 12:33 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam