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 11-10-2010, 04:39 PM   #10
chungcb86
Đệ tử 1 túi
 
chungcb86's Avatar
 
Tham gia ngày: Dec 2009
Bài gửi: 24
:
I2C không chịu chay.

Các bác giúp e vụ i2c này với. code lấy của bác Hoàn. mô phỏng thì chạy cứ ầm ầm. nhưng vô mạch thật thì ... híc híc e nó nằm im.
Đây là code của Master
Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=4000000)

#use i2c(master, sda=PIN_C4, scl=PIN_C3, force_hw)

void write_I2C(int8 value, int8 slave_addr)
{
   i2c_start();
   i2c_write(slave_addr);
   i2c_write(value);
   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;
   int8 i;
   const int8 N = 8;
   const int8 DIGITS[N] ={ 0b11111111,
                           0b11111110,
                           0b11111100,
                           0b11111000,
                           0b11110000,
                           0b11100000,
                           0b11000000,
                           0b10000000,
                          };
   const int8 slave_addr = 0x10;

   set_tris_b(0x00);
   while(1){
      for(i = 0; i<8; i++){
         write_I2C(DIGITS[i], slave_addr);
         delay_ms(500);
         value_re = read_I2C(slave_addr);
         output_b(value_re);
      }
   }
}

Còn đây là code của Slave:
Code:
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,NOLVP

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

int8 value = 0x01;

#INT_SSP
void i2c_isr()
{
   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_d(0x00);
   while(1){
      output_d(value);
   }
}

thay đổi nội dung bởi: chungcb86, 11-10-2010 lúc 04:50 PM.
chungcb86 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à 08:11 PM.


Đượ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