It's not my first time working on microcontrollers and Ethernet interfaces, last time I've used an Arduino board with Ethernet shield for a voice control application. Now I have to build everything from scratch, and the target microcontroller is an atmega8, so the little program memory must be considered. Here is how to use Atmega8 and enc28j60 together for an Ethernet capable application.
July 30, 2012
My Atmega8 will use an external crystal, and run at 16MHz. It is connected to a 5V supply. The enc28J60 requires 3.3V. The two chips must communicate using the SPI interface.
I found the original project posted on tuxgraphics . The solution is a good starting point, but it has lots of issues like: no buffering between the microcontroller and the Ethernet chip, microcontroller takes CLK signals from the Ethernet chip, not being able to run at various values, not a chance at 16MHz as I need it.
There are numerous reports of issues caused by the voltage difference in the SPI link. I have decided to use a buffer, as indicated by Mirek, from Poland, here. He also sells ready made Ethernet boards that are extremely well built and will save you considerable time for such a project.
The buffer chip is SN74LS125A. For the Ethernet chip I purchased a simple board from Ebay, featuring the enc28J60 chip, the 25MHz crystal, the magnetic ethernet connector, and a few other components:
When the 3.3V are applied and the network cable is connected, the two leds green and yellow should turn on from time to time. I bought two, and both refused to show any led activity. The boards were dead. Looking carefully I've noticed the crystals are soldered improperly and were shorting a few paths of the circuit. Correcting this, solved the problem and the boards were ready for use.
Here is the circuit diagram for these boards:
The connector exposes CLKOUT, INT, WOL, SO, SI, SCK, CS, RESET, VCC and GND. I'm currently using only INT,SO,SI,SCK,CS, VCC and GND. The board takes 3.3V so make sure you use a regulator.
Now you are ready to connect it to the Atmega8. SO goes to MISO, SI to MOSI, SCK to SCK, I have connected CS to PB2(SS/OC1B) and INT to PD2 (INT0).
The software I've prepared for this article, comes with a minimalistic TCP/IP stack supporting IP, ARP, UDP and TCP, based on the original work of Guido Socher and released under GPL V2 license.
I made it as a simple webserver, to open TCP port 80, so you can connect with your browser but you need to provide a few parameters: http://microcontroller_ip/?cmd=1 . In my sample I've used http://192.168.2.24/?cmd=1 , but you'll need to change the code for a new IP that matches your network settings.
The firmware replies to PINGs which are counted and displayed on the LCD and on the Atmega webpage. The webpage shows the number of times it has been accessed.
Update: October 11, 2012
For a simpler variant, you can skip the buffer and just connect the ENC28J60 module directly to the Atmega8. It will work well, especially if you don't have other devices connected via SPI.
Here is the software to get this running. It comes as a small WEBSERVER, configured as 192.168.2.24 in code, and supports a DS18B20 temperature sensor connected to the PC4 pin, as well as a DHT-11 temperature and humidity sensor, to show the temperature via the web interface:
The problem is the ENC28J60's mini TCP/IP stack uses a lot of the Atmega8's available flash memory. Aprox.6-7K out of 8K available. This leaves very little room for additional logic.
More on temperature sensors here.
Update: November 03, 2012
Here is the very basic webserver implementation, featuring only the Ethernet interface, and the minimum software, that runs a local webserver. IP is configured as 192.168.2.24, so take that into account when building your own.
I tested this on my testboard, nothing fancy:
The code is available here:
Jan 05, 2015 update: Jarda sent me a variant for compiled for atmega128 designed in Atmel Studio 6.2 + ASF. Code attached: ATxMega128D3_ETH_example