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 Tìm Kiếm Bài Trong Ngày Ðánh Dấu Ðã Ðọc 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-06-2009, 11:43 PM   #1
vitechpham
Đệ tử 1 túi
 
Tham gia ngày: Oct 2007
Bài gửi: 15
:
loi giao tiep 3 pic slave ,1 master

minh dang lam luan van canh tay robot ,phan chuong trinh va mach da xong ,phan mo phong tren protues da chay ok nhung khi nap chip chay thu thi pic mater ko giao tiep duoc voi slave (sau khi nhan du lieu slave bat sang led) cham vao chan thach anh thi thinh thoang led slave sang ,day la chuong trinh cua minh viet bang ccs c cac bac cao thu xem gium

master
#include <16f877a.h>
#include <math.h>
#use delay(clock=12000000)
#FUSES hs,nowdt,noprotect,NOBROWNOUT,NOLVP,nodebug,put
#use i2c(sda=pin_e0,scl=pin_e1,fast=400000)
#use rs232(baud=9600,parity=e,xmit=pin_c6,rcv=pin_c7 )
#use fast_io(b)
#use fast_io(a)
#bit b1=0x06.1
#bit b3=0x06.3
#bit b5=0x06.5
#word b=0xa0a1
int8 g,s1,s0,n,h,p;
int16 z;
int16 k[4],i;
void ghirom(int16 v[],int16 h)
{
int8 f;
i2c_start();
i2c_write(0xa0);
i2c_write(make8(h,1));
i2c_write(make8(h,0));
for(f=0;f<=3;f++)
{
i2c_write(make8(v[f],1));
delay_ms(10);
i2c_write(make8(v[f],0));
delay_ms(10);
}
i2c_stop();
delay_ms(10);
z=z+8;
}
void docrom (int16 v[], int16 h)
{
int8 f,t[2],j=0;
for (f=0;f<=7;f++)
{
i2c_start();
i2c_write(0xa0);
i2c_write(make8(h+f,1));
i2c_write(make8(h+f,0));
i2c_start();
i2c_write(0xa1);
t[j]= i2c_read(0);
i2c_stop();
j=j+1;
if(j==2)
{
j=0;
v[f-1]=make16(t[0],t[1]);
}
delay_ms(1);
}
z=z+8;
}
void gap(int8 v[])
{
if (input(pin_d6)==1)
v[3]=10;
else
v[3]=0;
}
void gapvat(v[])
{
if (v[3]==10)
{
if (input(pin_d6)==1)
{
output_low(pin_d5);
output_high(pin_d4);
}
while (input(pin_d6)==1)
continue;
delay_ms(1000);
}
else
{
if (input(pin_d6)==0)
{
output_low(pin_d4);
output_high(pin_d5);
}
while (input(pin_d7)==1)
continue;
}
output_high(pin_d5);
output_high(pin_d4);
}
void kiemtra1()
{
int8 c=0;
while(c<3)
{
c=input(pin_b4)+input(pin_b2)+input(pin_b6);
continue;
}
}
void kiemtra0()
{
int8 c=113;
while(c>0)
{
c=input(pin_b4)+input(pin_b2)+input(pin_b6);
continue;
}
}
void doc()
{
int8 f;
for (f=0;f<=2;f++)
k[f]=0;
for(f=0;f<=15;f++)
{
kiemtra1();
output_high(pin_b7);
kiemtra0();
output_low(pin_b7);
k[0]=k[0]+b5;
k[0]<<=1;
k[1]=k[1]+b3;
k[1]<<=1;
k[2]=k[2]+b1;
k[2]<<=1;
}
}
void loi()
{
switch (s0)
{
case 1:
putc(n);
break;
case 2 :
putc(make8(k[n-160],1));
break;
case 0:
putc(make8(k[n-160],0));
break;
}
}
void demphat()
{
switch (s0)
{
case 0:
putc(n);
break;
case 1 :
putc(make8(k[n-160],1));
break;
case 2 :
putc(make8(k[n-160],0));
n=n+1;
break;
}
}
void main()
{
s1=0;
n=160;
s0=0;
i=0;
z=0;
set_tris_b(0x7e);
set_tris_a(0x30);
set_tris_c(0x00);
output_low(pin_a1);
output_high(pin_b7);
output_low(pin_c5);
output_low(pin_d5);
output_low(pin_d4);
kiemtra0();
output_low(pin_b7);
kiemtra1();
output_high(pin_b7);
while (input(pin_a5)==0)
{
if(input(pin_a4)==0&& s1==0)
{
s1=160;
output_high(pin_c5);
i=i+1;
doc();
gap(k);
ghirom(k,z);
}
if(input(pin_a4)==1)
{
s1=0;
output_low(pin_c5);
}
if (b>i || b<i)
b=i;
continue;
}
i=0;
z=0;
s1=160;
g=0;
while (true)
{
h=input(pin_b4)+input(pin_b2)+input(pin_b6);
p= g+h;
if (p==0)
{
output_low(pin_b7);
g=160;
s1=0;
if (i<=b)
{
docrom(k,z);
i=i+1;
}
else
{
z=0;
i=1;
docrom(k,z);
}
gapvat(k);
}
if(s1==0)
{
if(h>0)
{
output_high(pin_b7);
loi();
}
else
{
if(n>162)
{
g=0;
n=160;
s1=160;
output_high(pin_a1);
putc('x');
}
else
demphat();
if (s0<2)
s0=s0+1;
else
s0=0;
}
}
delay_ms(10);
output_low(pin_b7);
}
}

slave 1

#include <16f876a.h>
#include <float.h>
#include <math.h>
#use delay(clock=12000000)
#FUSES hs,NOPROTECT,nowdt,NOBROWNOUT,NOLVP,nodebug,put
#use rs232(baud=9600,parity=e,xmit=pin_c6,rcv=pin_c7)
#use fast_io(a)
#use fast_io(b)
#bit a4=0x05.4
#bit cl=0x18.0
#int_global
#priority int_rda,int_ext
int8 s[3],n,w,v,w1,v1,m,t;
int16 g,k,r,a;
float p,max=800,min=200;
void data()
{
int8 b;
v=0;
if (cl==1)
v=1;
s[n]=getc();
for (b=0;b<=7;b++)
v=v+bit_test(s[n],b);
v=v%2;
}
void xuat()
{
int8 q=0;
a=k;
loop :
while (input(pin_a2)==0)
continue;
a4=bit_test(a,15-q);
output_low(pin_a3);
while (input(pin_a2)==1)
continue;
output_high(pin_a3);
q=q+1;
if (q<=15)
goto loop;
}
void pid()
{
p=0.1*(g-k)+5*(g-k+r)+0*(g-k-r);
r=abs(g-k);
if(abs(p)>max)
p=max;
If(abs(p)<min)
p=min;
}
void xung()
{
if(g>k)
w=(int8)(255-abs(255*p/max));
if(k>g)
v=(int8)(255-abs(255*p/max));
}
#int_ext
void ngat()
{
if (input(pin_b2)==1)
k=k+1;
if(input(pin_b1)==1)
k=k-1;
if (g==k)
{
w=0;
v=0;
set_pwm1_duty(255);
set_pwm2_duty(255);
p=0;
r=0;
t=0;
m=0;
enable_interrupts(int_rda);
output_low(pin_a3);
}
}
void demthu()
{
int t1=0;
output_high(pin_a3);
data();
if (input(pin_a2)==0)
t=t+1;
if (t>3)
t1=1;
if (t==10)
t1=2;
if (v==0 && t<=3)
{
n=n+1;
if (s[0]==160 && n==3)
{
output_high(pin_c4);
g=make16(s[1],s[2]);
n=0;
}
output_low(pin_a3);
}
if (t1==1)
output_low(pin_a3);
if(t==10)
{
m=160;
t=0;
v=0;
output_high(pin_a3);
disable_interrupts(int_rda);
}
}
#int_rda
void nhap()
{
demthu();
}
void main()
{
int8 s1=0;
t=0;
n=0;
w=0;
v=0;
v1=0;
w1=0;
r=0;
k=0;
g=0;
p=0;
m=0;
set_tris_b(0x0f);
set_tris_a(0x07);
set_tris_c(0xc0);
output_low(pin_c4);
output_high(pin_a3);
output_high(pin_c2);
output_low(pin_c1);
enable_interrupts(int_rda);
enable_interrupts(global);
while(input(pin_b3)==1)
continue;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(255);
set_pwm2_duty(255);
output_low(pin_a3);
while (input(pin_a2)==1)
continue;
output_high(pin_a3);
ext_int_edge(0,h_to_l);
enable_interrupts(int_ext);
while(input(pin_a1)==0)
{
if (input(pin_a0)==0&& s1==0)
{
s1=160;
disable_interrupts(int_ext);
xuat();
}
if (input(pin_a0)==1)
s1=0;
enable_interrupts(int_ext);
continue;
}
output_low(pin_a3);
while (true)
{
delay_ms(1);
if( m ==160)
{
if (g>k || g<k)
pid();
xung();
if (w>w1 || w<w1)
set_pwm1_duty(w);
if (v>v1 || v<v1)
set_pwm2_duty(v);
w1=w;
v1=v;
}
}
}


slave2

#include <16f876a.h>
#include <float.h>
#include <math.h>
#use delay(clock=12000000)
#FUSES hs,NOPROTECT,nowdt,NOBROWNOUT,NOLVP,nodebug,put
#use rs232(baud=9600,parity=e,xmit=pin_c6,rcv=pin_c7)
#use fast_io(a)
#use fast_io(b)
#bit a4=0x05.4
#bit cl=0x18.0
#int_global
#priority int_rda,int_ext
int8 s[3],n,w,v,w1,v1,m,t;
int16 g,k,r,a;
float p,max=800,min=200;
void data()
{
int8 b;
v=0;
if (cl==1)
v=1;
s[n]=getc();
for (b=0;b<=7;b++)
v=v+bit_test(s[n],b);
v=v%2;
}
void xuat()
{
int8 q=0;
a=k;
loop :
while (input(pin_a2)==0)
continue;
a4=bit_test(a,15-q);
output_low(pin_a3);
while (input(pin_a2)==1)
continue;
output_high(pin_a3);
q=q+1;
if (q<=15)
goto loop;
}
void pid()
{
p=0.1*(g-k)+5*(g-k+r)+0*(g-k-r);
r=abs(g-k);
if(abs(p)>max)
p=max;
If(abs(p)<min)
p=min;
}
void xung()
{
if(g>k)
w=(int8)(255-abs(p*255/max));
if(k>g)
v=(int8)(255-abs(p*255/max));
}
#int_ext
void ngat()
{
if (input(pin_b2)==1)
k=k+1;
if(input(pin_b1)==1)
k=k-1;
if (g==k)
{
w=0;
v=0;
set_pwm1_duty(255);
set_pwm2_duty(255);
p=0;
r=0;
t=0;
m=0;
enable_interrupts(int_rda);
output_low(pin_a3);
}
}
void demthu()
{
int8 t1=0;
output_high(pin_a3);
data();
if (input(pin_a2)==0)
t=t+1;
if (t>=4)
t1=1;
if (t1<=6)
t1=2;
if ( v==0 && t1==2)
{
n=n+1;
if (s[0]==161 && n==3)
{
output_high(pin_c4);
g=make16(s[1],s[2]);
n=0;
}
output_low(pin_a3);
}
if (t1<2)
output_low(pin_a3);
if(t==10)
{
m=160;
t=0;
v=0;
output_high(pin_a3);
disable_interrupts(int_rda);
}
}
#int_rda
void nhap()
{
demthu();
}
void main()
{
int8 s1=0;
t=0;
n=0;
w=0;
v=0;
v1=0;
w1=0;
r=0;
k=0;
g=0;
p=0;
m=0;
set_tris_b(0x0f);
set_tris_a(0x07);
set_tris_c(0xc0);
output_low(pin_c4);
output_high(pin_a3);
output_high(pin_c2);
output_low(pin_c1);
enable_interrupts(int_rda);
enable_interrupts(global);
while(input(pin_b3)==1)
continue;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(255);
set_pwm2_duty(255);
output_low(pin_a3);
while (input(pin_a2)==1)
continue;
output_high(pin_a3);
ext_int_edge(0,h_to_l);
enable_interrupts(int_ext);
while(input(pin_a1)==0)
{
if (input(pin_a0)==0&& s1==0)
{
s1=160;
disable_interrupts(int_ext);
xuat();
}
if (input(pin_a0)==1)
s1=0;
enable_interrupts(int_ext);
continue;
}
output_low(pin_a3);
while (true)
{
delay_ms(1);
if( m==160)
{
if (g>k || g<k)
pid();
xung();
if (w>w1 || w<w1)
set_pwm1_duty(w);
if (v>v1 || v<v1)
set_pwm2_duty(v);
w1=w;
v1=v;
}
}
}


slave3


#include <16f876a.h>
#include <float.h>
#include <math.h>
#use delay(clock=12000000)
#FUSES hs,NOPROTECT,nowdt,NOBROWNOUT,NOLVP,nodebug,put
#use rs232(baud=9600,parity=e,xmit=pin_c6,rcv=pin_c7)
#use fast_io(a)
#use fast_io(b)
#bit a4=0x05.4
#bit cl=0x18.0
#int_global
#priority int_rda,int_ext
int8 s[3],n,w,v,w1,v1,m,t;
int16 g,k,r,a;
float p,max=800,min=200;
void data()
{
int8 b;
v=0;
if (cl==1)
v=1;
s[n]=getc();
for (b=0;b<=7;b++)
v=v+bit_test(s[n],b);
v=v%2;
}
void xuat()
{
int8 q=0;
a=k;
loop :
while (input(pin_a2)==0)
continue;
a4=bit_test(a,15-q);
output_low(pin_a3);
while (input(pin_a2)==1)
continue;
output_high(pin_a3);
q=q+1;
if (q<=15)
goto loop;
}
void pid()
{
p=0.1*(g-k)+5*(g-k+r)+0*(g-k-r);
r=abs(g-k);
if(abs(p)>max)
p=max;
If(abs(p)<min)
p=min;
}
void xung()
{
if(g>k)
w=(int8)(255-abs(p*255/max));
if(k>g)
v=(int8)(255-abs(p*255/max));
}
#int_ext
void ngat()
{
if (input(pin_b2)==1)
k=k+1;
if(input(pin_b1)==1)
k=k-1;
if (g==k)
{
w=0;
v=0;
set_pwm1_duty(255);
set_pwm2_duty(255);
p=0;
r=0;
t=0;
m=0;
enable_interrupts(int_rda);
output_low(pin_a3);
}
}
void demthu()
{
int8 t1=0;
output_high(pin_a3);
data();
if (input(pin_a2)==0)
t=t+1;
if (t>=7)
t1=1;
if (t<=9)
t1=2;
if (v==0 && t1==2)
{
n=n+1;
if (s[0]==162 && n==3)
{
output_high(pin_c4);
g=make16(s[1],s[2]);
n=0;
}
output_low(pin_a3);
}
if (t1<2)
output_low(pin_a3);
if(t==10)
{
m=160;
t=0;
v=0;
output_high(pin_a3);
disable_interrupts(int_rda);
}
}
#int_rda
void nhap()
{
demthu();
}
void main()
{
int8 s1=0;
t=0;
n=0;
w=0;
v=0;
v1=0;
w1=0;
r=0;
k=0;
g=0;
p=0;
m=0;
set_tris_b(0x0f);
set_tris_a(0x07);
set_tris_c(0xc0);
output_low(pin_c4);
output_high(pin_a3);
output_high(pin_c2);
output_low(pin_c1);
enable_interrupts(int_rda);
enable_interrupts(global);
while(input(pin_b3)==1)
continue;
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(255);
set_pwm2_duty(255);
output_low(pin_a3);
while (input(pin_a2)==1)
continue;
output_high(pin_a3);
ext_int_edge(0,h_to_l);
enable_interrupts(int_ext);
while(input(pin_a1)==0)
{
if (input(pin_a0)==0&& s1==0)
{
s1=160;
disable_interrupts(int_ext);
xuat();
}
if (input(pin_a0)==1)
s1=0;
enable_interrupts(int_ext);
continue;
}
output_low(pin_a3);
while (true)
{
delay_ms(1);
if( m ==160)
{
if (g>k || g<k)
pid();
xung();
if (w>w1 || w<w1)
set_pwm1_duty(w);
if (v>v1 || v<v1)
set_pwm2_duty(v);
w1=w;
v1=v;
}
}
}
Hình Kèm Theo
File Type: jpg untitled.JPG (238.9 KB, 83 lần tải)

thay đổi nội dung bởi: vitechpham, 08-06-2009 lúc 12:03 AM.
vitechpham vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Ðiều Chỉnh
Xếp Bài

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à 11:50 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