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 07-09-2008, 01:06 AM   #3
toanck86
Đệ tử 1 túi
 
Tham gia ngày: Apr 2007
Bài gửi: 20
:
Chào các bạn!
Mình muốn hỏi thêm các bạn một vấn đề nữa trong giao tiếp I2C. Bài toán của mình vẫn như trên (giao tiếp giữa 2 con PIC 16F877A). Cho hỏi hàm i2c_isr_state() khi nào trả giá trị về 1,2... 0x81,0x82...Mình mới chỉ thành công khi giao tiếp 1 lần truyền 1 byte, còn 2 byte thì không đúng, code truyền 2 byte:
code master:
Code:
#include <16F877A.H>
#include <def_877a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use Delay(Clock=20000000)
#define use_portd_lcd TRUE
#include <lcd.c>
//#include <toanlcd.c>
#use i2c(master, sda=PIN_C4, scl=PIN_C3)
int8 value_re,value_re1,value_re2;
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();
}

void read_I2C(int8 slave_addr)
{
   i2c_start();
   delay_ms(1);
   i2c_write(slave_addr + 1);
   delay_ms(1);
   value_re1 = i2c_read(0);
   delay_ms(1);
   value_re2 = i2c_read(0);
   delay_ms(1);
   i2c_stop();
    
}

void main()
{
   int8 value_re=2;
   int8 i=10;
   const int8 N = 8;
   const int8 slave_addr = 0x10;
   set_tris_b(0x00);
   trisd=0;
   rd3=0;
   LCD_init();
   delay_ms(200);
   lcd_send_byte(0,0x1);
   while(1){
         
         read_I2C(slave_addr);
         lcd_send_byte(0,0x1);
         delay_ms(10);
         lcd_gotoxy(1,1);
         Printf(LCD_putc,"so1:%u\nso2:%u",value_re1,value_re2);
         delay_ms(400);
         
     }
}
//************************************************

code slave:
#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP

#use delay(Clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10)
int8 bang[]={3,4,5,6};
int8 value = 0x01,valueh=0,valuel=0;

#INT_SSP
void i2c_isr()
{
   int8 state;
   state = i2c_isr_state();
   if(state >= 0x80)i2c_write(bang[state - 0x80]);
     
}

void main()
{  
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   set_tris_b(0x00);
   while(1);
}
/*kết quả nhận được là trên màn hình LCD: so1:3; so2:255.
Giúp mình nha!
// mình vẫn chưa cho code vào trong khung được

thay đổi nội dung bởi: namqn, 07-09-2008 lúc 02:35 AM.
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à 03:43 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