Tải bản đầy đủ (.docx) (79 trang)

điều khiển thiết bị từ xa qua mạng ethernet PIC18f4550

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.97 MB, 79 trang )

MỤC LỤC
HÌNH ẢNH:
1
BẢNG:
2
Lời Cảm ơn

!"#!"$%&'&!"()*
+,-.,/01"23
 -4  5
!6'"7889:(;<=->81
?>:+@;*,A."?%7B
CB1"-D.5
;03',:E* F$CEB7
.&3DGE-H 5
I6+J*C.D9-K11"$+/
;L'M.,03>1"
.B3NA8 E,$8COP%?
81CE$Q+@4-3',8-05

3
Lời mở đầu
,"E3",3" 5("E3
-, 3'<CL.B4  
-8R% -%AS&8DT5
(>U0"",3E3B$*
<&23 điều khiển thiết bị qua mạng Ethernet.V+13
H CB.B3"
,77$3 'T  H%$B83"&EMW
B<,3,&X 3'<0YZ[$O5[JH \'%
/C,77.CB.B3'<*D


-:+7.B7775
V+13-3E+U&*>8R%AS
8DT5]HE%:E3B5
Giáo Viên Hướng DẫnNgười Thực Hiện
(;<=^)(L(_`abc`a`def
4
Mục tiêu đề tài
 1" E,A E/,"-:3S
,775
 !.B,&X 3'<g.f.Y[
f'<h$+:81.B-383X 5
 ]383"& '"YIfa5
Nhiệm vụ của đề tài
!BCB,U0. $1".* 
A"-UE1"i
 ^0 / - .B  jkZclmbaa`  -  >  '0  +<  ZZn  gjkZ  Z
ZE7h5
 Y,77-3/.?+A"
 ^0/o Enjk-/+U10+777
^Zdlpe`
 >BA-Znn?E> +"q7'
 S3$-E/,"(mô phỏng)
 r+1?3--+7/+U+1 7U0*. 5
Chương 1: Tổng quan về Ethernet
ETHERNETB,U'&gYs^h8+U&*"
 5="778+U.%3E9
c`I'E85
77E3 ,0/r7j s
-ALt`X 8uP'7I5I7 m75C'\U
5

SEU-U0/v"D8\ w$' 2
,30:$,r7s5,770
,-:D&%EQfI'E85Zo77c`I'E80%
'-Lcxl`-:81EDEE3BX f*[Z$k7$r75
ZoC0[77g%07fA30X 3*h
y' l`d5fX k*%[77.BE3B$
Lcxlaol`d5f0 -:0kl`d5fZ 7n787
IE7 788qZ [77gZnsIzZ[h5I6+J8
+U77{.BCEoX 
"  775^     o  k  l`d5f    o  S  /  X
775k*E3Bo770.".+@
3 ->C>,,773 5!6'"-:E0
'c``I'z8gm 877$kl`d5fh$77C- M
"D"E$'0,-"8+U3E2U$+
9-3E $77+g|788Ys^$kl`d5cch
 H81 %:5
1.1. Cấu trúc khung tin Ethernet
Z3o77.,&\[  >t:E
}nk->-<+A"3, F-: 3
gm 7h5
Cấu trúc khung Ethernet như sau:
Bảng 1. 1: Cấu trúc khung MAC theo IEEE 802.3/ Ethernet
6
j7 '7g\hit'78gS-S:m 7X
77h3+%81%"X '$C 3
<c`c`c`c`54C'$ES ?CB, 22c`I~5
n•[gn • 7[77hic'78gS-S:
m 7X 77hE:1813<'9X &
5^C 3<c`c`c`cc5
[78 s++788g< QShie'78

• !< QIsZX 77 +gm 7
h5
• €• X '%E%3<e'78i
o `i< Q 8
 f'780+JBQ8%
1. ``_``_`ZiZknZ}
2. ``_``_f[is‚
 f'78E7+8%<
o ci< Q 8
 `c_l`_Zd_``_``_``i+J'\3'+78
?&g8E 0 h
 ••_••_••_••_••_••i< Q'3g' + 8h
• ƒ&,&'>$77QE?Am 7
C< QJ-:< Qg+%hX C$6< Q
B"&"E'35-?$3
77 +.CB6\&„E88„5
&$C8R?%3m 7%",Ys^5
7
n7s++788g< Q2hie'785!< QIsZX
77 +2gm 7h5
Y^z…Yg&+zBChid'7853<X C0&:
X EA" 75
• ZCd,%H77m 7
o kl`d5fIsZ
 [JBQ&+X 77m 7
o [kr77i'D'\[Z$k7-r7-L
cxl`gEF'h
 [JBQ /X :EES 0
 `l``ikj
 `le`isPj

ji4be:ca``'78i
• [S:m 7DBeb'78$S:DBX
Eeb_cl†be'785
• VS:ED ca``'785[C$S:
Ckj77D ca``'78$;&'
S:Ckj+U%gb`$ate$ca``h5
•Zn ZPZg7++ 78hiES 8RS3
:.5jS ?S3,ZPZ73
15^ J $7?H$
 '<K-'<,'N5
1.1. Cấu trúc địa chỉ Ethernet
IK E,77<+,+%'\bl'< Q
ge7h5!< Q%<8%'<${< Q
IsZgI7+ s788Zs++788h5!< QIsZ'B+)'\3
A8D7 g"8Dceh$-S+Ui``ie`ixtil•ib•ile6``_e`_xt_l•_b•_
xe5V+,< QIsZ dEi
_ f73<*8%$<81F/k5
_ f78 +8%%<5
_ VE Cc< QIsZ+%& E,775!<
QIsZ8+U< Q2-< QS
775
1.2. Đặc tính điện
8
S"77*C 7*I 7875I*C I 787
8+U3K' X CB2'&-
0'5
_ ‡ˆ`‰^ ŠX '"3E‹(- ŠM,
"3E#(
_ ‡ˆc‰^ ŠX '"3E_(- ŠM,
"3E‹(

Hình 1. 1: Mã hóa Manchester
1.3. Các loại khung Ethernet
1.3.1. Các loại khung uniscat
]8,c.:,d5V77+,c
. C< Qi
_ IsZ2i``_e`_`l_xf_[‡_Zc5
_ IsZSi``_e`_`l_xf_s‡_cd
Hình 1. 2: Mô hình truyền thông unicast
9
! 85V.:c,3<5%
3,E,,08R.?i
_ ZQC,d%< QIsZSX J-:< QIsZ
X  E,X >0EU33
5
_ Z3,38 883< Q8R'N EU
A 5
1.3.2. Các khung broadcast
Z3    ' + 8 C  <  Q IsZ  S  ••_••_••_••_••_••
gbl'ch5?3$6+JJ-:< Q
IsZX  E,X >3,.E?-
EU5
] /sPj8+U' + 8B>< QIsZ
/-:c< Qkj:5&8D /<;8+U3
' + 8B37 F'<5
1.3.3. Các khung Multicast
,2:&8D,%</E%
5!< QIsZSX < Q6'"Q3,
J&C:%E?3:< Q5
(Chú ý: Địa chỉ MAC nguồn của khung luôn là địa chỉ MAC của giao
tiếp mạng tạo ra khung. Trong khi đó địa chỉ MAC đích của khung thì phụ

thuộc vào một trong ba loại khung nếu trên.)
1.4. Truy cập bus
I&8D-%.:,8+U77\%E
EE3E?E'8@0ZnIszZ[gZ 7n787IE7
788qZ8s-+ 7h-81\:"8%;S
L 1X "D5ƒ$&8DAD<:
"8%X "D?3S ?E,3,
E &5
10
Hình 1. 3: Minh họa phương pháp CSMA/CD
Nguyên tắc làm việc của phương pháp CSMA/CD:
7EE3EZnIszZ[$K,.C.?E'8
&81B835jE3E8 i
_ IK  ,  . E  1  7   +@  g 7  8787h      +@
KgCS"h>:E35
_ [-" .S"C& C$0-@CL
 ,JE3S"0+@5ZS->-?$E3K
,-@E7@B883S"E3-:S"?
7C & 5
_ E &$K,.EX'N'/"X
>$& @0-,5
I&>D &0'B39EU
{ 0>f5,s-,ZJ7+@5!+@K0
sCB:5S"4,s <E:0,
Z '-;$0 &,&BZ5s-
Z?S"E2$883-:S"-E3
"&5Z ,8RJEX'N'/"'T3
E3E$3,D?8R?"H
'/"-'/"E"5s-Z;CBc
S"vŒ w6'"B'33,?'58 CK,8R

11
& @0$:E3,5 @
0\ES7&?3C8  
9&3E-D A 3,J5
 '&8D   .S"Ts5
•BX ZnIszZ[S%$ 5V3-:
EE3E.<$-"ŽE0 'N&,\
>:,&X "D5ZS->-?$EE3E E+U
&*"D775
^BX ZnIszZ[S%'%<X  E
/5Z3?.'>• 03>\&?CB
6E6E,$3<DS3 5="8%8
+U.->;%E5P•E0-:3
u?3>EE3E8RSE-:3%E%E$M
N F+A"<Š$ 15
1.5. Các loại Ethernet
k*E3Bo770.".+@3
 ->C.,775IK,,+1 7fDiD
&$E/S"8+U-6S.-?5
1.5.1. Các hệ thống Ethernet 10Mb/s
_ c`' 87a5!0o770$+1 03E2U,+5
D&,c`I'z8$8+U'L8\$.+3ED &
E,,a``5
_ c`' 87d5ZC0v77w$+1 03"D3E2UN
-:D&c`I'z8$.+3ED X E,clagk
Md``h5
_ c`' 875ZA-9X vq87+w3E9,E5c`‡ 87,&
-:D{c`I'z8+1 0"D9,EZ f\05
_ c`‡ 87•5•-9X •'7}Eg8 h5!o8
 ,&-:D&c`I'z8$ Lcxxf5

12
1.5.2. Các hệ thống Ethernet tốc độ 100Mb/s- Ethernet cao tốc(fast
Ethernet)
_ c``‡ 875Zo77 &-:D&c``I'z803E9
,E@3E8 5
_ c``‡ 87r5ZArC0S*C ."Dg8
+U E  E3E *  C b‡za‡ X  o •[[kh '   2 d o
c``‡ 87•r(‘c``‡ 87r5
• c``‡ 87•r5D&c``I'z8$++U3E8  +75
• c``‡ 87r5D&c``I'z8$8+U3E9,E5
• c``‡ 87d-c``‡ 87b5Z3o8+Ud6E-b6E
3E96EZ f\00"  oS
8+U5
1.5.3. Các hệ thống Giga Ethernet
_ c```‡ 87riZArC0LS*C .go+1 0
B*C l‡zc`‡+J"DDD& •'7 7
E3B'\s^nkhoc```‡ 87r2f,i
• c```‡ 87_nri D & c```I'z8$ 8 +U 8   -:8C
95
• c```‡ 87_YriD&c```I'z8$8+U8 -:8C+5
• c```‡ 87_riD&c```I'z8$8+U3E25
_ c```‡ 87i=,&\D&] '$'L8\03E96E
Z a\05n+UB*C .0B,D& 
0,3E5
1.5.4. Chuẩn IEEE 802
kl`d3ok83,Ys^-,Is^
gmetropolitan area networkh5ZUB$3okl`d:,
3, 3CCS: +,5V3-:3,
$+A"3,7_' 87+.7-<NCJ
S:{75Z3,k88$+A".

7&+M7U37$6C37$,3 
.L$;TE,-X o5
13
Z3+<-U- /6kl`d3,: 
%Eg0+A"--?X >t}nkh51$
kl`d 0+A"}nk YYZg.B
0h-IsZg.B?E.h$+C3
CB"08 i
_ 0+A"
_ YYZ
_ IsZ
_ -?
={okl`d'>'\‡ 0ZoYs^zIs^k
l`dgIEEE 802 LAN/MAN standards Committee (LMSC)h5Z3o
+J&*%+{77$7P$,Ys^
+$3,Ys^+J'+7-'+7gBridging and Virtual Bridged
LANsh5Zo+{77okl`d5f5
Chương 2: Họ giao thức TCP/IP
2.1. Họ giao thức TCP/IP
Zjzkj'& /EŽED3"D,2
%-: 5Zjzkj0-9X Transmission Control Protocol
g  /. B . h z Internet Protocol  g]  /
k77h$ Zjzkj8+U&*3,U'&
;0,k775
ZjzkjQ2  /1CFEX .
 /5ZH {Cc" / '& /gn7}m
j8h5
Zjzkj &'& /B, U0
Si
14

1. ZEŽE. 3+X ,&
g|+7s7 ^7q#|s^h5
2. ZEŽE.A 3 +,5
Zjzkj8+U>.b {.>[[
g>X '&DEMIuh5Zjzkj7X 
> }nk$3>g7/140
Dhi
_ /+UgsEE Y 7h5
_  -?g 8EY 7h5
_ ,gk77Y 7h5
_ 0,g^7qk7m 7Y 7h5
Hình 2. 1: Kiến trúc TCP/IP
Z;1>}nk$.+A"$3>
40D+:$ K+A"0-&
.B{E7 +75V?+A">3>
 ,$ K>E7 +7/%-
0J>+A"ME7 +7A 5
15
Hình 2. 2: Quá trình đóng/mở gói dữ liệu trong TCP/IP
Hình 2. 3: Cấu trúc dữ liệu trong TCP/IP
=>-Rd5f %2+A" 35>-R
 %,33 $+A" A?A3 i
_ /+U+A"32{87 5
16
_  -?$-<+A"ZjD+:{
_ Zj8775
_ ,$+A"kj:+:{kj
+   5
_ 0$+A".{m 75
2.1.1. Tầng ứng dụng ( Application layer)

/+U0JX >Zjzkj' 23
>-3/+U%E8+UB?E,5!
+JB<+,- F+J5I&8D /
+Ui
_ [=Zjg[ =8Zm jhi /%>,&5
_ [^ng[ ^ 7n87hi"D0.5
_ n^IjgnE7^7qI  77jhi /,
5
_ •jg•7 8m7jhi /.?E5
_ •jg- •7 8m7jhi .?E'>5
_ nIjgnE7I  8m7jhi /5
_ Y^i>EN'<DEŽE+J
'<-&3X4&3SC0,5
_ /+U F+A"-::E+:g:E-?Bh F5-"
+JF'T8DEŽE /X :E-?B',&+
/ '0C+A"5^AF3'T8D-A/
+Uo4+JJF5(S+Ui /•j+JFd`
+A"-Fdc.B$ /nIj+JFdaO
2.1.2. Tầng giao vận(Transport layer)
ZC3"?EE0.A 33S-
<3.+A"$  /S2i
_ ’[jg’87[   jhiM{ /C+J5’[j
%E&+<-U/+U5^C%E0
.ED$Q3C+A"4,:, 
'3C:S5Z3/+U+J’[j
Q.ACCS:N$&?+A"EU&
17
-4/+U5Z3'&?1"'\
05
_ Zjg 888Zjhi^,-:’[j$Zj%E3

0.:D-'.+A"&3?5
^C%E&2+A"?A  ,$8+U3
 N3CX 03CCS:SE
,'0+:$'3?C$6, 7_B
''0?'3C*5Zj.3C
CS::-0ES ?3? 3C*?5
['S?$08R A 5
2.1.3. Tầng mạng( internet layer)
^T'000,5C/L3< Q$C
C-<gP7h+A"5b / {%
2i
_ kjg77jhiZC/L3< Q+A":.
-<H:S5
_ sPjgs++788P78jhiC/LE0+<< QkjX
3S< QIsZ5
_ kZIjgk77ZI788 7jhiC/L'3K
.+A"'<N5
_ k]Ijgk77]EI  7 jhiC/L.B
. :g 8h5
2.1.4. Lớp liên mạng (Network Interface Layer)
 E,0 -" F+A"A  ,
'<J&,5Z3/L' 2-"B83
?E.+@$B83K-+A"5[   
,'\4:E,gk77h8RD::E0,
g^7qk7m 7Y 7h.+A"$60,g^7q
k7m 7Y 7h8R%+A"4,-C::E,gk77h
H ?+A"5
18
' 23'< E,gZ +I,-Z3EI,h
->%E3BCB,&$

?E.-? '< E,C5
^*.?E\E0$77 /%E+:C' :EYYZ
gYYZh$IsZgI7+ s788Zh$-:E-?E8 5
2.2. Cấu trúc gói tin IP, TCP,ARP,UDP
2.2.1. Cấu trúc địa chỉ IP
I,k77+J"D< Qkjgfd'hBv<-<w33
S0-:CC 33< QEU&-30X
43SUB5
^33SD1E-:,k77>^kZ
g^7qkm Z77h8R%E3SC&< Qkjgkj
s+788h5
^33SD1E-:,k77
 &,U'&><,8R%E33SC&
< Qkjg0;+:81EŽEX ^kZh5
="D< Q.+Ž &8E:E$Ca
:E< Qkjis$‡$Z$[$5n13 'A 3:E< Q
\LF/3%HX C5
Hình 2. 4: tổ chức địa chỉ IP
_ !< Q:Esi:Es8+U'70X b'7B3< Q,5
^>0$C? '0'70X < QC
3<'T`5f'78M,8+UB3< Q3,5
ZCcde< Q:Esg3< Q'7/%h-:8D3S
19
,daef_d†ce5ttt5dcb3K&< Q:Esg8+U
f'78B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[JB'37 +7H\
&Dfd'S5
[  .+ F:E0$.+ FebV'5
2.2.3. Cấu trúc gói tin TCP
!-<+A"Zj{n77-:%H8 i
_ Z7j#d'7i8D"FZjX ,25
_ [78 j#d'7i8D"FZjX ,S5
21

_ n777'7i8D"'70X n77$n…^'>C
8D/1C' -'70C8D/1&0
c$Cn…^>8D/1X '705
_ sq7+7'7#d'7isZV'?>3<X 
S8D/1X CE7'0?5‡3?D3
877,S*,25
_ [  }mm78_b'i&+X E7 +7S7-<4fd'S5 8D
Q -<S'9X 2+A"5
_ P787-7+_e'S5
_ • 8i3'S.B
• ’P]i(JNoC"5
• sZVi(J'3?sZV'7C"15
• jn=iZ/Lj’n=5
• PniV\&,05
• n…^i!2'&C 8D"15
• •k^[iMX+A"\,25
_ |+q_d'7i8D'7+A"'94'7Q sZV
'7,2*8”8 B?5
_ Z78i78Eo7 +7@+A"5
_ ’7j7#d'7i’P]'?>3<S8Dce
'S8D/1C+<35
_ }E_d'7i-JJ{$ '33EX ZjCC&
+D X -JZj+  c8775
_ j ++_iEo•0-7 +7B'E7 +7H\
&Dfd'S5
_ Zj+  i/ +A"$X 0C&+D <afe'75
]S <CB '30Eo}E5
2.2.4. Cấu trúc gói tin ARP (Adrees Resolution Protocol)
I{3SJT0&,CJ&7k[-33
SJ0,-?CBm 7-?1E 0-"

.A  3SJ&,-?8
+U 7q 5("+@1EQ8+UE< Q38k[
< Qkj5,Q-"D+A"-m 7$B< Q
kjX ,S< Q-?-1Em 7:3?5
22
I&8+UBB< Qkj< Q-?sPj
gs++788P78jh5V 3SJD-&,-?
$H'< QkjX  B.A  3$
HE'< Q-?X  5sPj-%.B
4< Qkjfd'88 < Q77bl'85^ 8+U 
CsPj778-sPj7 5
Hình 2. 5: Mô tả đường truyền dữ liệu ARP
2.2.4.1. Mô tả hoạt động của khung ARP
Chuyển giao trực tiếp:!< Q-?&X < Qkj-S+U8
+U0,7E^_c`,EŽE6< Qkj-< 
Q-?35^ CB6< Qkjcxd5a5bl5f-< Q-?
f$C Cjs†mgksh5(:js< Q-?$ks< Qk77
-mBF5
Chuyển giao gián tiếp:ZB < Q&1"'T
33SD&'3:'&33S
0,$'3CC/ < QkjX 3SC0,$
{38R?'3-3%< QkjX >>
,&'3/ < Q-?X S>5VC 3SC
23
BvC"w-: 5
^ $ M8+U'QBA,3
< Q8+U:%gs++788P78Z 7hBLD&X -"
B < Q5
I&CsPj&77• 7.1E4
3:335(>E8+Ukj$CC< Q

kj;+@$CE' + 8:%
330,77g-:< Q••••–••••–••••h5
Bảng 2. 1: Mô tả gói thông tin ARP
VDE:3 /3$+A"sPjC
&<+,o7 +75!BsPjCB-"-:.
"3 $ +J&B/ &+X A
8 C5
E3S,C'<:EU$-->-?C
'< QkjX S>\&$ 8+U
24
B< QgP7-787s++788P78j#PsPjh,
{1sPjB-%.57C$C&3
X/ '< QkjX 33,$3,\&$C
&778:%33-3X,&C/ < Q
kjX 3,055
2.2.4.2. Gói tin của ARP
jE7i``l``kjg````5c```5````5````h5
_ = +q 77†e'787775
_ j7†b'78kj5
_ sPjE7 i
• c†0
• d†3E,
• fzb†PsPj0z3E,
Bảng 2. 2: Khung dữ liệu ARP
[78 
s++788
n7s++788
• 7
E7
gsPjh[  ZPZ

e'78 e'78 d'78 dl'78 b78
2.2.4.3. Trường của khung ARP
•!< QSg777+78  ++788hi
•••i••i••i••i••i••< Q' + 8sPj7785
•!< Q2g77787 ++788h< QX sPj05
•• 7E7i
o sPj063E,gsPj778z7hi``l`e
o PsPj063E,gPsPj778z7hi``l`fa
o kji``l``
2.2.5. Cấu trúc gói tin UDP
(J7 +7X ’[jCeb'S-:bi
25

×