KHẢO SÁT, THIẾT KẾ BỘ TRUYỀN NHẬN TÍN HIỆU ĐIỀU KHIỂN QUA MẠNG ETHERNET BẰNG VI ĐIỀU KHIỂN

98 220 1
KHẢO SÁT, THIẾT KẾ BỘ TRUYỀN NHẬN TÍN HIỆU ĐIỀU KHIỂN QUA MẠNG ETHERNET BẰNG   VI ĐIỀU KHIỂN

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC NÔNG LÂM TP HỒ CHÍ MINH KHOA CƠ KHÍ & CƠNG NGHỆ ĐOÀN NGỌC PHƯỚC KHẢO SÁT, THIẾT KẾ BỘ TRUYỀN NHẬN TÍN HIỆU ĐIỀU KHIỂN QUA MẠNG ETHERNET BẰNG VI ĐIỀU KHIỂN Tp Hồ Chí Minh Tháng 06 năm 2008 BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC NÔNG LÂM TP HỒ CHÍ MINH KHOA CƠ KHÍ CƠNG NGHỆ KHẢO SÁT, THIẾT KẾ BỘ TRUYỀN NHẬN TÍN HIỆU ĐIỀU KHIỂN QUA MẠNG ETHERNET BẰNG VI ĐIỀU KHIỂN Chuyên ngành: Điều Khiển Tự Động Giáo viên hướng dẫn: Sinh viên thực hiện: Th.S Nguyễn Bá Vương Đoàn Ngọc Phước MSSV: 04138032 Tp Hồ Chí Minh Tháng 06 năm 2008 MINISTRY OF EDUCATION AND TRAINING NONG LAM UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY INVESTIGATE, DESIGN FOR TRANSMISSION AND RECEIVER CONTROL SIGNAL THROUGH NETWORK ETHERNET BY MICROCONTROLLER Speciality: Automatic Control ADVISOR: STUDENT: MSc Nguyen Ba Vuong Doan Ngoc Phuoc Student ID: 04138032 Ho Chi Minh City June, 2008 LỜI CẢM ƠN Trước tiên xin chân thành cảm ơn công lao sinh thành, dạy bảo tất tốt đẹp mà ba mẹ dành cho con, cho có ngày hơm Và với tất lòng thành, em xin chân thành cảm ơn: Ban giám hiệu trường Đại Học Nơng Lâm Tp Hồ Chí Minh, Q thầy Khoa Cơ Khí Cơng Nghệ tận tình truyền đạt cho tơi kiến thức q báu suốt thời gian học tập trường Trân trọng gửi đến Thạc sỹ Nguyễn Bá Vương lời cảm ơn chân thành Thầy cung cấp cho em nhiều kiến thức học tập, tận tình hướng dẫn giúp đỡ em nhiều để hoàn thành tốt đề tài Xin bày tỏ lòng cảm ơn đến người bạn gắn bó, động viên chia sẻ ngày tháng khó khăn năm học vừa qua Sinh viên Đồn Ngọc Phước i TĨM TẮT Đề tài tốt nghiệp: “Khảo sát, thiết kế truyền nhận tín hiệu điều khiển qua mạng Ethernet vi điều khiển.” Trong thực tế, để đơn giản việc giám sát điều khiển thiết bị máy tính, thường sử dụng cổng như: com, song song usb qua khe cắm mở rộng Tuy nhiên, việc sử dụng loại cổng vào việc giao tiếp, điều khiển bị số hạn chế định khoảng cách truyền không xa, tốc độ truyền chậm, chia sẻ cho nhiều máy tính thời gian gần đây, hãng sản xuất mainboard máy tính loại bỏ cổng com cổng song song board mạch Ngày nay, với phát triển giới, mạng máy tính áp dụng hầu hết nơi giới, giúp người trao đổi thông tin với cách dễ dàng, nhanh chóng Xuất phát từ lợi ích đó, việc sử dụng cổng RJ45 để điều khiển, giám sát qua mạng vi điều khiển giúp tiết kiệm chi phí lắp đặt, giúp việc trao đổi liệu trở nên dễ dàng truyền xa Trong khuôn khổ luận văn này, đề tài thực công việc thiết kế mạch cho vi điều khiển để giao tiếp qua cổng mạng RJ45 tìm hiểu vấn đề truyền liệu qua mạng Những vấn đề cần giải đề tài bao gồm: - Tìm hiểu giao thức mạng: UDP, TCP/IP, ICMP, ARP… - Thiết kế mạch vi điều khiển để giao tiếp qua mạng - Tìm hiểu chương trình phần mềm cho vi điều khiển - Kết nối vi điều khiển với máy tính thơng qua thiết bị mạng - Chạy thử nghiệm thiết bị mạng nội mạng Internet ii Kết đạt được: - Phần mạch vi điều khiển chạy ổn định - Có thể điều khiển từ máy tính đến vi điều khiển qua mạng nội mạng Internet Do thời gian thực đề tài ngắn nên thực cho trường hợp điều khiển qua mạng GVHD SVTH Th.S Nguyễn Bá Vương Đoàn Ngọc Phước iii SUMMARY Theme: INVESTIGATE, DESIGN FOR TRANSMISSION AND RECEIVER CONTROL SIGNAL THROUGH NETWORK ETHERNET BY MICROCONTROLLER In reality, to simplify the investigation and control the equipments by computer, we often use the computer ports such as: serial port, parallel port, usb ports or through expansion slot However, the usage of these ports on communication, control are facing some limitation such as transmitted distance is not far, transmission speed is slow, can not share for a lot of computers and especially in recent time the manufacturers of mainboard has eliminated com and parallel ports on board At present, with the development in the world, computer network is applied for computers, this can help with easy and fast information exchange From these advantages, the usage of RJ45 port for control, investigate network by microcontroller will save installment fee, easier in exchange data and farther transmission Thesis designs microcontroller circuit which can communnicate through RJ45 port and information by network The thesis required to solve: - Carrying out a fact-finding protocol investigation network - Designing microcontroller circuit to communicate through network - Carrying out a fact finding software investigation for microcontroller, connecting microcontroller with computers through network equipments - Trying test of equipment on local area network and Internet iv Results obtainned - Microcontroller and electrical board have been runing stably - Control between computers and microcontroller as well as Internet Advisor MSc Nguyen Ba Vuong Student Doan Ngoc Phuoc v MỤC LỤC Trang LỜI CẢM ƠN i TÓM TẮT ii MỤC LỤC vi DANH SÁCH CÁC HÌNH ix Mở đầu 1.1 Đặt vấn đề 1.2 Mục đích Tra cứu tài liệu 2.1 Tổng quan mạng máy tính 2.1.1 Tìm hiểu mạng máy tính a/ Khái niệm b/ Phân loại mạng máy tính c/ Kiến trúc vật lý mạng máy tính 2.1.2 Giới thiệu Internet a/ Khái niệm Internet b/ Lịch sử Internet 2.1.3 Mơ hình tham khảo OSI 2.1.4 Tìm hiểu giao thức mạng a/ Giao thức TCP/IP b/ Giao thức UDP c/ Giao thức HTTP 10 2.2 Tìm hiểu vi điều khiển 10 2.2.1 Định nghĩa 10 2.2.2 Vi điều khiển AVR 10 2.2.3 Vi điều khiển Atmega32 11 2.2.4 Đặc tính Atmega32 11 2.3 Tìm hiểu IC ENC28J60 giao tiếp qua mạng 12 vi 2.3.1 Tổng quan 12 2.3.2 Những đặc tính điều khiển Ethernet 14 Phương pháp phương tiện 15 3.1 Phương pháp thực đề tài 15 3.1.1 Chọn linh kiện để thực việc giao tiếp qua mạng 15 3.1.2 Thực phần điện tử 15 3.1.3 Thực phần mạng 15 3.2 Phương tiện thực đề tài 16 Thực đề tài 17 4.1 Thực phần điện tử 17 4.1.1 Thiết kế phần mạch cho vi điều khiển 18 a/ Phần mạch nguồn 18 b/ Phần mạch cho vi điều khiển 19 4.1.2 Tìm hiểu mạch ethernet nối tiếp 20 a/ Nguồn cung cấp cho chip ENC28J60 21 b/ Cổng kết nối RJ45 21 c/ IC đệm 74HCT254 22 d/ Bộ điều khiển ethernet độc lập ENC28J60 22 e/ Ngõ kết nối với vi điều khiển 23 4.2 Thực phần mạng 23 4.2.1 Switch 24 4.2.2 Cáp mạng UTP 24 4.2.3 Router ADSL 26 4.2.4 Định địa IP cho vi điều khiển máy tính 27 4.2.5 Định địa MAC cho vi điều khiển 27 4.2.6 Khai báo IP tĩnh cho máy tính 28 4.2.7 Cấu hình port forwarding cho router 29 4.3 Kết nối mạch vi điều khiển với máy tính qua cổng RJ45 31 4.4 Chạy khảo nghiệm 32 4.4.1 Khảo nghiệm mạch ENC28J60 kết nối với switch 32 4.4.2 Khảo nghiệm nhận tín hiệu từ vi điều khiển máy tính 32 A Khảo nghiệm lần 32 B Khảo nghiệm lần 34 C Khảo nghiệm lần 35 vii 'MAC-header 'Destination hardware address ' You have to put your router-mac- address here T_enetpacketdest0 = &H00 ' 00-0c-41-ae-7a-dc MAC-address of my router T_enetpacketdest1 = &H0C T_enetpacketdest2 = &H41 T_enetpacketdest3 = &HAE T_enetpacketdest4 = &H7A T_enetpacketdest5 = &HDC ' source (own source) Call Src2mymac T_enetpackettype = &H0008 ' = &h0800 ' fill IP-header T_ip_vers_len = &H45 T_tos = &H00 T_ip_pktlen0 = &H00 T_ip_pktlen1 = &H30 T_id0 = &H4A T_id1 = &HA5 T_flags = &H00 T_offset = &H00 T_ttl = &H80 'protocol (ICMP=1, TCP=6, UDP=11) T_ip_proto = &H11 'IP address of source T_ip_srcaddr0 = Myip(1) T_ip_srcaddr1 = Myip(2) T_ip_srcaddr2 = Myip(3) T_ip_srcaddr3 = Myip(4) 'IP address of destination 'you have to put the IP-number of T_ip_destaddr0 = 193 ' the NTP-server here T_ip_destaddr1 = 67 T_ip_destaddr2 = 79 T_ip_destaddr3 = 202 'UDP-header T_udp_srcport0 = &H13 T_udp_srcport1 = &H88 T_udp_destport0 = &H00 ' port 0025 = 37 NTP T_udp_destport1 = &H25 T_udp_len0 = &H00 T_udp_len1 = &H1C T_udp_data = Asc( "X") T_udp_data1 = &H0A ' lf T_udp_data2 = &H0D ' cr Call Ip_header_checksum Call Udp_checksum Call Echopacket End Sub ' Routine to convert the LONG from the NTP-server in to date and time ' Sub Ntp S(1) = T_udp_data S(2) = T_udp_data1 S(3) = T_udp_data2 S(4) = T_udp_data3 Swap S(1) , S(4) : Swap S(2) , S(3) L2 = L1 + 1139293696 L2 = L2 + 3600 Print "Date : " ; Date(l2) Print "Time : " ; Time(l2) End Sub ' offset UTC + hour 'Tcp-checksum Sub Tcp_checksum Local Whulp1 As Word Local Tempword2 As Word Tcp_cksum = Call Srcdestchksum 'resultaat in I_chksum32 Tempwordh = T_ip_pktlen0 Tempwordl = T_ip_pktlen1 I_chksum32 = I_chksum32 + Tempword Tempword2 = T_ip_vers_len And &H0F Tempword2 = Tempword2 * I_chksum32 = I_chksum32 - Tempword2 Whulp1 = Tempword - 20 Val2 = Highw(i_chksum32) Val1 = I_chksum32 I_checksum16 = Tcpchecksum(buffer(&H23) , Whulp1 , Val2 , Val1) Tcp_cksuml = High(i_checksum16) Tcp_cksumh = Low(i_checksum16) End Sub ' Routine to calculate the IP-checkum ' Sub Udp_checksum T_udp_chksum = &H00 Call Srcdestchksum 'resultaat in I_chksum32 'packet length I_value16h = T_udp_len0 I_value16l = T_udp_len1 I_chksum32 = I_chksum32 + I_value16 Result16h = T_udp_len0 Result16l = T_udp_len1 Val2 = Highw(i_chksum32) Val1 = I_chksum32 I_checksum16 = Tcpchecksum(buffer(&H23) , Result16 , Val1 , Val2) T_udp_chksum1 = High(i_checksum16) T_udp_chksum0 = Low(i_checksum16) End Sub Sub Srcdestchksum 'checksum TCP header I_chksum32 = I_value16h = T_ip_srcaddr0 I_value16l = T_ip_srcaddr1 I_chksum32 = I_chksum32 + I_value16 I_value16h = T_ip_srcaddr2 I_value16l = T_ip_srcaddr3 I_chksum32 = I_chksum32 + I_value16 I_value16h = T_ip_destaddr0 I_value16l = T_ip_destaddr1 I_chksum32 = I_chksum32 + I_value16 I_value16h = T_ip_destaddr2 I_value16l = T_ip_destaddr3 I_chksum32 = I_chksum32 + I_value16 'proto I_chksum32 = I_chksum32 + T_ip_proto End Sub 'HTTP Sub Http Local Msg_temp2 As String * 10 Local Tempstring1 As String * Local Ackpsh As Byte Local Pkthulp As Word Local Tempword1 As Word Local Tempword3 As Word Local Z As Byte Local Ztemp As Byte Local Tempstring2 As String * 13 Local Nhietdo As Word Work = Tcp_flags Tcp_fin = Work.0 Tcp_syn = Work.1 Tcp_rst = Work.2 Tcp_psh = Work.3 Tcp_ack = Work.4 Tcp_urg = Work.5 ' This code segment processes the incoming SYN from the client ' and sends back the initial sequence number (ISN) and acknowledges ' the incoming SYN packet If Tcp_syn = Then 'Move IP source address to destination address T_ip_destaddr = T_ip_srcaddr 'Make ethernet module IP address source address T_ip_srcaddr = My_ip Swap Tcp_srcportl , Tcp_destportl Swap Tcp_srcporth , Tcp_destporth Tcpdatalen_in = Client_seqnum0 = Tcp_seqnum0 Client_seqnum1 = Tcp_seqnum1 Client_seqnum2 = Tcp_seqnum2 Client_seqnum3 = Tcp_seqnum3 Client_seqnum = Client_seqnum + Tcpdatalen_in Tcp_acknum0 = Client_seqnum0 Tcp_acknum1 = Client_seqnum1 Tcp_acknum2 = Client_seqnum2 Tcp_acknum3 = Client_seqnum3 Tcp_seqnum0 = &HFF 'Initial sequencenumber Tcp_seqnum1 = &HFF ' Tcp_seqnum2 = &H10 ' Tcp_seqnum3 = &H11 ' Call Src2dest Set Flags.synflag Tcp_flags = Set Tcp_flags.1 Set Tcp_flags.4 T_ip_pktlen0 = &H00 T_ip_pktlen1 = 48 Call Ip_header_checksum Call Tcp_checksum Call Enc28j60packetsend(62) End If ' If an ACK and PSH is received and the destination port address is valid If Tcp_ack = Then If Tcp_psh = Then 'search for GET / and check for a parameter ' walk through the packet until GET / is found For Z = 40 To 200 Ztemp = Z Tempstring2 = Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) If Left(tempstring2 , 5) = "GET /" Then Exit For End If Next Z End If If Mid(tempstring2 , , 6) = "LEDOFF" Then Set Led End If If Mid(tempstring2 , , 6) = "LED1ON" Then Reset Led1 End If If Mid(tempstring2 , , 6) = "LED1OF" Then Set Led1 End If If Mid(tempstring2 , , 6) = "LED2ON" Then Reset Led2 End If If Mid(tempstring2 , , 6) = "LED2OF" Then Set Led2 End If If Mid(tempstring2 , , 6) = "LED3ON" Then Reset Led3 End If If Mid(tempstring2 , , 6) = "LED3OF" Then Set Led3 End If 'set flags Tcp_flags = Set Tcp_flags.4 'ack 'Move IP source address to destination address T_ip_destaddr = T_ip_srcaddr 'Make ethernet module IP address source address T_ip_srcaddr = My_ip Swap Tcp_srcportl , Tcp_destportl Swap Tcp_srcporth , Tcp_destporth 'swap mac Call Src2dest Call Inc2seq T_ip_pktlen = T_ip_pktlen0 * 256 T_ip_pktlen = T_ip_pktlen + T_ip_pktlen1 Incoming_ack = Incoming_ack + T_ip_pktlen Call Seq2ack Call Ack2inc Buffer(17) = Buffer(18) = 40 '5 x = 20 bytes Buffer(47) = &H50 'padding Buffer(55) = Buffer(56) = Buffer(57) = Buffer(58) = Buffer(59) = Buffer(60) = Call Ip_header_checksum Call Tcp_checksum Call Enc28j60packetsend(60) Ackpsh = End If End If If Tcp_ack = Then If Buffer(17) = Then If Buffer(18) = 40 Then If Ackpsh = Then 'ACK after a ACK-PSH 'set flags Tcp_flags = Set Tcp_flags.4 'ack Set Tcp_flags.3 'psh Set Tcp_flags.0 'fin 'Move IP source address to destination address T_ip_destaddr = T_ip_srcaddr 'Make ethernet module IP address source address T_ip_srcaddr = My_ip Swap Tcp_srcportl , Tcp_destportl Swap Tcp_srcporth , Tcp_destporth 'swap mac Call Src2dest Call Inc2seq Call Seq2ack Call Ack2inc Tempword3 = &H37 Restore Htmlcode Do Read Msg_temp Msg_temp2 = Right(msg_temp , 8) If Msg_temp2 = "endblock" Then Exit Do End If If Msg_temp2 = "sizeused" Then Tempword1 = Tempword3 + 24 Msg_temp = "Used: " + Str(tempword1) + " bytes" End If If Msg_temp2 = "date " Then Msg_temp = Date(l2) End If If Msg_temp2 = "time " Then Msg_temp = Time(l2) End If If Msg_temp2 = "copyleft" Then Msg_temp = "www.members.home.nl/bzijlstra" End If For Y = To Len(msg_temp) Tempstring1 = Mid(msg_temp , Y , 1) Buffer(tempword3) = Asc(tempstring1) Incr Tempword3 Next Y Loop Buffer(tempword3) = &H0D Incr Tempword3 Buffer(tempword3) = &H0A Tempword3 = Tempword3 - 14 Pkthulp = Tempword3 / 256 T_ip_pktlen0 = Pkthulp Pkthulp = T_ip_pktlen0 * 256 T_ip_pktlen1 = Tempword3 - Pkthulp Tempword3 = Tempword3 + 14 Call Ip_header_checksum Call Tcp_checksum Call Enc28j60packetsend(tempword3) End If End If End If End If End Sub ' TCP Sub Tcp If Tcp_destporth = Then If Tcp_destportl = 80 Then 'Call Lm35_test Call Http End If End If End Sub Sub Inc2seq Incoming_ack0 = Tcp_seqnum0 Incoming_ack1 = Tcp_seqnum1 Incoming_ack2 = Tcp_seqnum2 Incoming_ack3 = Tcp_seqnum3 End Sub Sub Seq2ack Tcp_seqnum0 = Tcp_acknum0 Tcp_seqnum1 = Tcp_acknum1 Tcp_seqnum2 = Tcp_acknum2 Tcp_seqnum3 = Tcp_acknum3 End Sub Sub Ack2inc Tcp_acknum0 = Incoming_ack0 Tcp_acknum1 = Incoming_ack1 Tcp_acknum2 = Incoming_ack2 Tcp_acknum3 = Incoming_ack3 End Sub Sub Src2mymac 'Make ethernet module mac address the source address T_enetpacketsrc0 = Mymac(1) T_enetpacketsrc1 = Mymac(2) T_enetpacketsrc2 = Mymac(3) T_enetpacketsrc3 = Mymac(4) T_enetpacketsrc4 = Mymac(5) T_enetpacketsrc5 = Mymac(6) End Sub Sub Src2dest 'move hardware source address to destination address T_enetpacketdest0 = T_enetpacketsrc0 T_enetpacketdest1 = T_enetpacketsrc1 T_enetpacketdest2 = T_enetpacketsrc2 T_enetpacketdest3 = T_enetpacketsrc3 T_enetpacketdest4 = T_enetpacketsrc4 T_enetpacketdest5 = T_enetpacketsrc5 Call Src2mymac End Sub ' htmlcode Htmlcode: Data "HTTP/1.0 200" , &H0D , &H0A , &H0D , &H0A Data "" Data "Doan Ngoc Phuoc" Data "" Data " _THIET KE BO DIEU KHIEN QUA MANG ETHERNET _" Data "GVHD: Nguyen Ba Vuong" Data "SVTH:Doan Ngoc Phuoc" Data " HE THONG DIEU KHIEN _" Data "

Ngày đăng: 15/06/2018, 12:32

Tài liệu cùng người dùng

Tài liệu liên quan