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 08-09-2008, 10:32 AM   #5
toanck86
Đệ tử 1 túi
 
Tham gia ngày: Apr 2007
Bài gửi: 20
:
Chào các bạn!
Mình đã giải quyết được vấn đề trên. Mình gửi 4 số từ Master đến Slave và nhận lại về 4 số đó, cho hiển thị lên LCD. Ai có cách khác thì post lên cho mọi người cùng tham khảo nhé
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=0,value_re2=0,value_re3=0,value_re4=0;
void write_I2C(int8 value1,int8 value2,int8 value3,int8 value4,int8 slave_addr)
{
   i2c_start();
   i2c_write(slave_addr);
   i2c_write(value1);
   i2c_write(value2);
   i2c_write(value3);
   i2c_write(value4);
   i2c_start();
   i2c_write(slave_addr);
   i2c_stop();
}

void read_I2C()
{
   i2c_start();
   i2c_write(0x11);
   value_re1 = i2c_read();
   value_re2 = i2c_read();
   value_re3 = i2c_read();
   value_re4 = i2c_read();
  i2c_start();
  i2c_write(0x11);
  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){
         write_I2C(1,2,3,4,0x10);
         delay_ms(10);
         read_I2C();
        lcd_send_byte(0,0x1);
         delay_ms(1);
         lcd_gotoxy(1,1);
         Printf(LCD_putc,"so1:%u so2:%u\nso3:%u so4:%u",value_re1,value_re2,value_re3,value_re4);
         delay_ms(500);
         
         write_I2C(5,6,7,8,0x10);
         delay_ms(10);
         read_I2C();
        lcd_send_byte(0,0x1);
         delay_ms(1);
         lcd_gotoxy(1,1);
         Printf(LCD_putc,"so1:%u so2:%u\nso3:%u so4:%u",value_re1,value_re2,value_re3,value_re4);
         delay_ms(500);
     }
}
code slave:
Code:
#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[4];
int8 value = 0x01,valueh=0,valuel=0;
int8 state;
#INT_SSP
void i2c_isr()
{
   state = i2c_isr_state();
   if(state >=0x80)i2c_write(bang[state-0x80]);
   else if(state>0)bang[state-1]=i2c_read();
}

void main()
{  
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   set_tris_b(0x00);
   while(1);
}
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à 04:40 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