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 27-11-2012, 03:42 AM   #1
khienpzo
Đệ tử 5 túi
 
Tham gia ngày: Sep 2010
Bài gửi: 107
:
Question 1 Master 2 Slave chuẩn I2C

Em đang làm đề tài 1 Master thu thập dữ liệu ADC từ 2 Slave rồi gửi lên máy tính. Khi làm 1 Master - 1 Slave thì chạy ngon lành khi làm 2 Slave thì ko chạy nữa và nếu chỉ hoạt động 1 trong 2 thì chạy bình thường ... Em post bài để xin ý kiến các anh chị .. Thân

Code Master:
[
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#use i2c(MASTER,SDA=PIN_C4,SCL=PIN_C3,fast)
#use RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)
#define slave_address1 0x10
#define slave_address2 0x20
//========================
char data1,data2 =0;
unsigned int16 control;

//=========================

#INT_RDA
void ngatrs232()
{
char c;
c = getc();
if (c=='a')
{
putc(255);
putc(255);
delay_ms (10);
}
if (c== 'b' )
{
control = 1;
}
if (c== 'f' )
{
control = 2;
}
}
void main()
{
control=0;
data1 =0;
data2 =0;
enable_interrupts(INT_RDA);
ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
delay_ms(50);
set_tris_D(0x00);
set_tris_C(0x80);
while(true)
{
i2c_start();
i2c_write(slave_address1);
data1 = i2c_read(0);
i2c_stop();
delay_ms(10);

i2c_start();
i2c_write(slave_address2);
data2 = i2c_read(0);
i2c_stop();
delay_ms(10);
if (control == 1)
{
putc(data1);
putc(data2);
delay_ms(10);
}
}
}

]


code Slave: 2 slave giống hệt nhau chỉ khác địa chỉ


[
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#use i2c (SLAVE,SDA=PIN_C4,SCL=PIN_C3,address=0x20,force_hw ) // khai bao chuan truyen thong su dung//
//================================================== ===
int8 adc_result;
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;

state = i2c_isr_state();

if(state < 0x80) // Master truyen du lieu
{
incoming = i2c_read();
}

if(state >= 0x80) // Master nhan du lieu tu Slave
{
i2c_write(adc_result);
}
}

//======================================
void main ()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
adc_result = read_adc();

enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);

while(true)
{
adc_result = read_adc();
delay_ms(500);
}
}
]
khienpzo vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Tags
1990


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à 06:44 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