1 | $regfile = "m128def.DAT"
|
2 | $crystal = 4000000
|
3 | $baud1 = 9600
|
4 | $baud = 9600
|
5 |
|
6 | Config Porta = Output
|
7 | 'Define the pins used for network card.
|
8 | 'Define the pins used for network card.
|
9 |
|
10 | Ne2000_a0_pin Alias Pinc.0
|
11 | Ne2000_a1_pin Alias Pinc.1
|
12 | Ne2000_a2_pin Alias Pinc.2
|
13 | Ne2000_a3_pin Alias Pinc.3
|
14 | Ne2000_a4_pin Alias Pinc.4
|
15 | Ne2000_ior_pin Alias Pinc.5
|
16 | Ne2000_iow_pin Alias Pinc.6
|
17 | Ne2000_reset_pin Alias Pinc.7
|
18 |
|
19 | Ne2000_a0 Alias Portc.0
|
20 | Ne2000_a1 Alias Portc.1
|
21 | Ne2000_a2 Alias Portc.2
|
22 | Ne2000_a3 Alias Portc.3
|
23 | Ne2000_a4 Alias Portc.4
|
24 | Ne2000_ior Alias Portc.5
|
25 | Ne2000_iow Alias Portc.6
|
26 | Ne2000_reset Alias Portc.7
|
27 |
|
28 |
|
29 | Ne2000_d0_pin Alias Pina.0
|
30 | Ne2000_d1_pin Alias Pina.1
|
31 | Ne2000_d2_pin Alias Pina.2
|
32 | Ne2000_d3_pin Alias Pina.3
|
33 | Ne2000_d4_pin Alias Pina.4
|
34 | Ne2000_d5_pin Alias Pina.5
|
35 | Ne2000_d6_pin Alias Pina.6
|
36 | Ne2000_d7_pin Alias Pina.7
|
37 |
|
38 | Ne2000_d0 Alias Porta.0
|
39 | Ne2000_d1 Alias Porta.1
|
40 | Ne2000_d2 Alias Porta.2
|
41 | Ne2000_d3 Alias Porta.3
|
42 | Ne2000_d4 Alias Porta.4
|
43 | Ne2000_d5 Alias Porta.5
|
44 | Ne2000_d6 Alias Porta.6
|
45 | Ne2000_d7 Alias Porta.7
|
46 |
|
47 | 'Load the library.
|
48 |
|
49 | $lib "ne2000.lbx"
|
50 | $external Ne2000_init , Ne2000_poll , Ne2000_udp_tx
|
51 |
|
52 | 'Dimention some variables for the libary.
|
53 | Dim Host_mac(6) As Byte
|
54 | Dim Host_ip(4) As Byte
|
55 | Dim Host_port As Word
|
56 | Dim My_mac(6) As Byte
|
57 | Dim My_port As Word
|
58 | Dim My_ip(4) As Byte
|
59 | Dim Udp_data(128) As Byte At &H270
|
60 | 'overlay the string s over the array Udp_data() just to make things easy later.
|
61 | Dim S As String * 128 At &H270 Overlay
|
62 |
|
63 | 'helper varible
|
64 | Dim X As Byte
|
65 |
|
66 | Init:
|
67 | ' include the waitms 10 even if you don't have a LCD as the cls will load the required
|
68 | ' _waitms routine from the MCS.LBX
|
69 |
|
70 | Waitms 10
|
71 |
|
72 | ' Setup Our Ip Address.
|
73 | My_ip(1) = 192
|
74 | My_ip(2) = 168
|
75 | My_ip(3) = 1
|
76 | My_ip(4) = 102
|
77 |
|
78 | 'call the initilisation routine for the ne2000 card.
|
79 | Gosub Ne2000_init
|
80 |
|
81 | Main:
|
82 | ' Poll the card to see if there is data for us.
|
83 | ' The Poll routine takes car of decoding to packets.
|
84 | ' It will automaticly reply to ARP requests. and Decode UDP data packets.
|
85 | Gosub Ne2000_poll
|
86 |
|
87 |
|
88 | S = "Hello World"
|
89 | ' and make the Network card send it.
|
90 | Gosub Ne2000_udp_tx
|
91 | ' the subroutine Ne2000_udp_rx must be ended with a return.
|
92 | Return
|
93 |
|
94 |
|
95 | ' Go back and do it all again
|
96 | Goto Main
|
97 |
|
98 | 'This is a subroutine that the Ne2000_poll routine calls when there is
|
99 | 'UDP data for us.
|
100 | Ne2000_udp_rx:
|
101 | ' the array Host_mac() contains the PC's mac address this is needed if we
|
102 | ' want to send some data back.
|
103 | Print "Host ethernet address:";
|
104 | For X = 1 To 6
|
105 | Print Host_mac(x);
|
106 | Print " ";
|
107 | Next X
|
108 | Print
|
109 | ' the array Host_ip() contains the PC's IP address also needed if we
|
110 | ' want to send some data back.
|
111 | Print "Host IP address:";
|
112 | For X = 1 To 4
|
113 | Print Host_ip(x);
|
114 | Print " ";
|
115 | Next X
|
116 | Print
|
117 | ' the word Host_port is the IP port of the PC. Needed as well if we
|
118 | ' want to send some data back.
|
119 | Print "Host Port:";
|
120 | Print Host_port
|
121 | ' the array My_ip() contains the AVR's IP address also needed if we
|
122 | ' want to send some data back.
|
123 | Print "AVR's IP address:";
|
124 | For X = 1 To 4
|
125 | Print My_ip(x);
|
126 | Print " ";
|
127 | Next X
|
128 | Print
|
129 | ' the word My_port is the IP port of our AVR. Needed as well if we
|
130 | ' want to send some data back.
|
131 | Print "AVR Port:";
|
132 | Print My_port
|
133 | ' you can check My_port to see if it agrees with a predefined port if you like
|
134 | If My_port = 1234 Then
|
135 | ' so to get here the PC had to have the right IP address of our AVR and
|
136 | ' the right Port address too.
|
137 | ' the udp data is placed in the array Udp_data()
|
138 | ' we have overlayed the string s over the Udp_data() so that s will also contain
|
139 | ' the udp data but in a string format.
|
140 | Print S
|
141 | End If
|
142 | ' now lets send something back to the PC.
|
143 | ' fill the string s with some text.
|
144 | S = "Hello World"
|
145 | ' and make the Network card send it.
|
146 | Gosub Ne2000_udp_tx
|
147 | ' the subroutine Ne2000_udp_rx must be ended with a return.
|
148 | Return
|
149 | 'thats all
|
150 | End
|
151 |
|
152 | 'The rest of this example is for Visual Basic.
|
153 | 'It shows how to interface with the AVR using
|
154 | 'the Microsoft Winsock Control.
|
155 |
|
156 | 'In VB place a Microsoft Winsock Control on your form called Winsock1.
|
157 |
|
158 | 'You then setup the Winsock Control with the remote host IP address and port.
|
159 |
|
160 | ' Winsock1.Close
|
161 | ' Winsock1.Protocol = sckUDPProtocol
|
162 | ' Winsock1.RemoteHost = "192.168.254.102"
|
163 | ' Winsock1.RemotePort = "1234"
|
164 |
|
165 | 'To send a UDP packet to the host (AVR):
|
166 |
|
167 | ' Winsock1.SendData "What ever you want to send, up to 128 bytes"
|
168 |
|
169 | 'Data arrives back from the host (AVR) through an event:
|
170 |
|
171 | ' Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
|
172 | ' Dim strData As String
|
173 | ' Winsock1.GetData strData
|
174 |
|
175 | 'strData now contains the returned text / data
|
176 | 'bytesTotal is the length it should be 128
|
177 |
|
178 | ' End Sub
|