Wednesday, 02 July 2025
     
 
Main Menu
Home Home
Shop Shop
News News
BASCOM-AVR BASCOM-AVR
BASCOM-8051 BASCOM-8051
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-AVR Help BASCOM-AVR Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login
Username

Password

If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News
 
     
 

 
   
     
 
AN #126 - Network programming with the NE2000 Print
Network programming with the NE2000

This AN is written by David Manning

The library hides all the hard parts of the Ne2000 programming.

The NE2000 network card is hooked up like this :



A sample UDP application:

' Example program to show how to use the libary 'Ne2000.lbx'
' By David Manning. bascomavr@ihug.co.nz
' All comments wellcome.

' Copyright (c) 2003 CTR Electronics Ltd. All rights reserved.
' You are given the right to use this code and the ne2000.lbx for private use.
' If you want to use it for commercial applications then please contact
' CTR Electronics Ltd at.
' ctrelectronic@ihug.co.nz

' THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
' OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
' ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
' DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
' DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
' GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
' INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
' NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' SOFTWARE.

' This code and LBX is only tested with RTL8019AS chip sets. The LBX should
' work with any NE2000 8bit / 16bit card that can work in 8bit mode.
' please report to me any other chip sets that work.

' Thank you.

$regfile = "8535DEF.DAT"
$crystal = 4000000

'Define the pins used for network card.

Ne2000_reset_pin
Alias Pind.0
Ne2000_iow_pin
Alias Pind.1
Ne2000_ior_pin
Alias Pind.2
Ne2000_a0_pin
Alias Pind.7
Ne2000_a1_pin
Alias Pind.6
Ne2000_a2_pin
Alias Pind.5
Ne2000_a3_pin
Alias Pind.4
Ne2000_a4_pin
Alias Pind.3

Ne2000_reset
Alias Portd.0
Ne2000_iow
Alias Portd.1
Ne2000_ior
Alias Portd.2
Ne2000_a0
Alias Portd.7
Ne2000_a1
Alias Portd.6
Ne2000_a2
Alias Portd.5
Ne2000_a3
Alias Portd.4
Ne2000_a4
Alias Portd.3

Ne2000_d0_pin
Alias Pinb.7
Ne2000_d1_pin
Alias Pinb.6
Ne2000_d2_pin
Alias Pinb.5
Ne2000_d3_pin
Alias Pinb.4
Ne2000_d4_pin
Alias Pinb.3
Ne2000_d5_pin
Alias Pinb.2
Ne2000_d6_pin
Alias Pinb.1
Ne2000_d7_pin
Alias Pinb.0

Ne2000_d0
Alias Portb.7
Ne2000_d1
Alias Portb.6
Ne2000_d2
Alias Portb.5
Ne2000_d3
Alias Portb.4
Ne2000_d4
Alias Portb.3
Ne2000_d5
Alias Portb.2
Ne2000_d6
Alias Portb.1
Ne2000_d7
Alias Portb.0

'Load the library.

$lib "ne2000.lbx"
$external Ne2000_init , Ne2000_poll , Ne2000_udp_tx

'Dimention some variables for the libary.
Dim Host_mac(6) As Byte
Dim Host_ip(4) As Byte
Dim Host_port As Word
Dim My_mac(6) As Byte
Dim My_port As Word
Dim My_ip(4) As Byte
Dim Udp_data(128) As Byte At &H79
'overlay the string s over the array Udp_data() just to make things easy later.
Dim S As String * 128 At &H79 Overlay

'helper varible
Dim X As Byte

Init
:
' include the waitms 10 even if you don't have a LCD as the cls will load the required
' _waitms routine from the MCS.LBX

 
Waitms 10

' Setup Our Ip Address.
 My_ip
(1) = 192
 My_ip
(2) = 168
 My_ip
(3) = 254
 My_ip
(4) = 102
'call the initilisation routine for the ne2000 card.
 
Gosub Ne2000_init

Main
:
' Poll the card to see if there is data for us.
' The Poll routine takes car of decoding to packets.
' It will automaticly reply to ARP requests. and Decode UDP data packets.
 
Gosub Ne2000_poll
' Go back and do it all again
 
Goto Main

'This is a subroutine that the Ne2000_poll routine calls when there is
'UDP data for us.
Ne2000_udp_rx
:
' the array Host_mac() contains the PC's mac address this is needed if we
' want to send some data back.
 
Print "Host ethernet address:";
 
For X = 1 To 6
 
Print Host_mac(x);
 
Print " ";
 
Next X
 
Print
' the array Host_ip() contains the PC's IP address also needed if we
' want to send some data back.
 
Print "Host IP address:";
 
For X = 1 To 4
 
Print Host_ip(x);
 
Print " ";
 
Next X
 
Print
' the word Host_port is the IP port of the PC. Needed as well if we
' want to send some data back.
 
Print "Host Port:";
 
Print Host_port
' the array My_ip() contains the AVR's IP address also needed if we
' want to send some data back.
 
Print "AVR's IP address:";
 
For X = 1 To 4
 
Print My_ip(x);
 
Print " ";
 
Next X
 
Print
' the word My_port is the IP port of our AVR. Needed as well if we
' want to send some data back.
 
Print "AVR Port:";
 
Print My_port
' you can check My_port to see if it agrees with a predefined port if you like
 
If My_port = 1234 Then
' so to get here the PC had to have the right IP address of our AVR and
' the right Port address too.
' the udp data is placed in the array Udp_data()
' we have overlayed the string s over the Udp_data() so that s will also contain
' the udp data but in a string format.
 
Print S
 
End If
' now lets send something back to the PC.
' fill the string s with some text.
 S
= "Hello World"
' and make the Network card send it.
 
Gosub Ne2000_udp_tx
' the subroutine Ne2000_udp_rx must be ended with a return.
 
Return
'thats all
End

'The rest of this example is for Visual Basic.
'It shows how to interface with the AVR using
'the Microsoft Winsock Control.

'In VB place a Microsoft Winsock Control on your form called Winsock1.

'You then setup the Winsock Control with the remote host IP address and port.

'  Winsock1.Close
' Winsock1.Protocol = sckUDPProtocol
' Winsock1.RemoteHost = "192.168.254.102"
' Winsock1.RemotePort = "1234"

'To send a UDP packet to the host (AVR):

' Winsock1.SendData "What ever you want to send, up to 128 bytes"

'Data arrives back from the host (AVR) through an event:

' Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
' Dim strData As String
' Winsock1.GetData strData

'strData now contains the returned text / data
'bytesTotal is the length it should be 128

' End Sub

You can download the library and the code


TIP : you can use WAITMS in your code as well instead of the call to CLS