Nhập môn đệ tử
Tham gia ngày: Sep 2011
Bài gửi: 5
:
|
Code hoàn chỉnh đã test mạch thực tế giao tiếp giữa 1 master và 2 slaver, truyền và nhận 2 byte luôn, các bạn có thể tham khảo. Master sẽ gửi 1 số int16 (gừi từ 0, mỗi lần gửi xong tăng lên 1 đơn vị) xuống slave, slaver nhận được và gửi trở lại master. các bạn nhớ dùng Rs232 dọc về máy tính dể kiểm tra chính xác nhé
Code master:
#include <18f4431.h>
#include <def_4331.h>
#device 18f4431*=16 ADC=10
#include <stdlib.h> //tien xu li khi su dung ham ATOI()
#include <stdio.h> // for printf() call
#fuses nowdt,noprotect,nolvp,xt,put,hs
#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,b its=8)
#define slave_address_1 0x10
#define slave_address_2 0x20
#use i2c(master, sda=PIN_C4, scl=PIN_C5)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(a)
void write_slave_1(int16 a)
{ int8 hi,lo;
hi=make8(a,1);
lo=make8(a,0);
i2c_start();
i2c_write(slave_address_1);
delay_ms(1);
i2c_write(hi);
delay_ms(1);
i2c_write(lo);
i2c_stop();
}
void write_slave_2(int16 b)
{
int8 hi,lo;
hi=make8(b,1);
lo=make8(b,0);
i2c_start();
i2c_write(slave_address_2);
delay_ms(1);
i2c_write(hi);
delay_ms(1);
i2c_write(lo);
i2c_stop();
}
int16 read_slave_1()
{
int16 value_re_1;
int8 hi,lo;
i2c_start();
i2c_write(slave_address_1 + 1);
hi = i2c_read(1);
lo = i2c_read(0);
i2c_stop();
value_re_1 = make16(hi,lo);
return value_re_1;
}
int16 read_slave_2()
{
int16 value_re_2;
int8 hi,lo;
i2c_start();
i2c_write(slave_address_2 + 1);
hi = i2c_read(1);
lo = i2c_read(0);
i2c_stop();
value_re_2 = make16(hi,lo);
return value_re_2;
}
void main()
{
int16 value,value_re_1,value_re_2;
value = 0;
while(1)
{
write_slave_1(value);
write_slave_2(-value);
value++;
delay_ms(500);
value_re_1=read_slave_1();
value_re_2=read_slave_2();
printf("%ld",value_re_1);
printf(" ");
printf("%ld",value_re_2);
printf(" ");
delay_ms(100);
}
}
Code slaver 1:
#include <18f4431.h>
#include <def_4331.h>
#device 18f4431*=16 ADC=10
#include <stdlib.h> //tien xu li khi su dung ham ATOI()
#include <stdio.h> // for printf() call
#fuses nowdt,noprotect,nolvp,xt,put,hs
#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,b its=8)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0x10)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(a)
boolean a;
int16 value;
int8 hi,lo;
#INT_SSP
void i2c_isr()
{
int8 state;
state = i2c_isr_state();
if (state<= 0x80)
{
if(state == 1 )
{ hi = i2c_read(1);
}
if(state == 2)
{ lo = i2c_read(1);
a=true;
}
}
if (state >= 0x80)
{
if(state==0x80)
i2c_write(hi);
if(state==0x81)
i2c_write(lo);
}
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
a=false;
while(1)
{
if (a==true)
{value=make16(hi,lo);
printf("%ld",value);
printf(" ");
a=false;
}
}
}
Code slave 2:
#include <18f4431.h>
#include <def_4331.h>
#device 18f4431*=16 ADC=10
#include <stdlib.h> //tien xu li khi su dung ham ATOI()
#include <stdio.h> // for printf() call
#fuses nowdt,noprotect,nolvp,xt,put,hs
#use delay(clock=20000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,b its=8)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0x20)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(a)
boolean a;
int16 value;
int8 hi,lo;
#INT_SSP
void i2c_isr()
{
int8 state;
state = i2c_isr_state();
if (state<= 0x80)
{
if(state == 1 )
{ hi = i2c_read(1);
}
if(state == 2)
{ lo = i2c_read(1);
a=true;
}
}
if (state >= 0x80)
{
if(state==0x80)
i2c_write(hi);
if(state==0x81)
i2c_write(lo);
}
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
a=false;
while(1)
{
if (a==true)
{value=make16(hi,lo);
printf("%ld",value);
printf(" ");
a=false;
}
}
}
CHÚC CÁC BẠN MAY MẮN
|