You are on page 1of 5

Ben's HobbyCorner - Software - Bascom-examples

http://members.home.nl/bzijlstra/software/exampl...

Here you can nd a step-by-step tutorial about the Bascom-AVR code to drive an AVR and an ENC28J60 ethernetcontroller.

There has been een problem with the newer Bascom-AVR-version, from 1.11.90 on. But it has been solved. This message was posted in the www.mcselec.com Bascom-AVR forum:

I have left the tutorial untouched, so you will have to make to two corrections in the code on your own. This tutorial is based on the hardware from www.tuxgraphics.org Step 1. The rst steps..... You can cut and paste all green parts of the next text and place it into the Bascom-AVR IDE.

1 of 5

08/10/2011 08:21 AM

Ben's HobbyCorner - Software - Bascom-examples

http://members.home.nl/bzijlstra/software/exampl...

The board from www.tuxgraphics.org

The Atmega168 microcontroller that is used on the above board has some fusebits. Care should be taken while programming these fusebits. First, start with a small description what you are planning to do with date and/or version-number. '----------------------------------------------------------' Atmega168 and ENC28J60 '----------------------------------------------------------' Version 1.0 - june 2007 Make a small note what you did with the fusebits of the microcontroller For this program the following fusebits should be set:

We are going to use the CLKOUT of the ENC28J60. On power-up the clockfrequency is 6.25 Mhz (1/4 of the ENC28J60 main clock), but by changing the settings of the ENC28J60 we will be using 12.5 Mhz.

2 of 5

08/10/2011 08:21 AM

Ben's HobbyCorner - Software - Bascom-examples

http://members.home.nl/bzijlstra/software/exampl...

At the Bascom-AVR options put Hardware stack, Soft stack and Framesize all at 64 Bytes.

$crystal = 6250000 $regle = "M168def.dat" You can also use these commands to override the Bascom-AVR-options $hwstack = 64 $swstack = 64 $framesize = 64

$crystal is used to give the Mhz or MegaCycles you are using. In this case a external clock of 6.25 Mhz is used. $regle is used to have Bascom-AVR use the right registers for the right microcontroller. In this case we are using a Atmega168. Step 2: Preparing the SPI-port and Chip-select-pin The next piece of code is to congure two output pins. PORTB.2 is the chipselect of the ENC28J60, PORTB.1 is the LED. Cong Portb.1 = Output Cong Portb.2 = Output Enc28j60_cs Alias Portb.2 To make your code a bit more readable, you can use aliases. Here I give Portb.2 the name ENC28J60_cs and throughout the program I can refer to this alias. The ENC28J60 is a stand-alone Ethernet controller with an industry standard Serial Peripheral Interface (SPI). It is designed to serve as an Ethernet network interface for any controller equipped with SPI. With a single Cong SPI command Bascom-AVR congures the complete SPI-bus. See the Help for Bascom-AVR for every single parameter of the Cong SPI command. 'Conguration of the SPI-bus Cong Spi = Hard , Interrupt = O , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0 'init the spi pins Spiinit

This is all you need to communicate with the ENC28J60 ethernet chip. Step 3: Dierent kinds of registers....

3 of 5

08/10/2011 08:21 AM

Ben's HobbyCorner - Software - Bascom-examples

http://members.home.nl/bzijlstra/software/exampl...

From the datasheet: Memory organization. The ENC28J60 consists of Control Registers which are used to control and monitor the ENC28J60. The Control registers memory contains the registers that are used for conguration, control and status retrieval of the ENC28J60. The Control registers are directly read and written to by the SPI interface. In this tutorial we will make two subroutines to read and write the Control registers: Declare Sub Enc28j60readcontrolregbyte(byval Register As Byte) Declare Sub Enc28j60writecontrolregbyte(byval Register As Byte , Byval Value As Byte) The Ethernet buer contains transmit and receive memory used by the Ethernet controller in a single memory space. The sizes of the memory areas are programmable by the host controller using the SPI interface. The Ethernet buer memory can only be accessed via the read buer memory and write buer memory SPI commands. We will make two subroutines to read and write the buer memory: Declare Sub Enc28j60readbuer Declare Sub Enc28j60writebuer The PHY registers are used for conguration, control and status retrieval of the PHY module. The registers are not directly accessible trhough the SPI interface; they can only be accessed through Media Independent Interface Management (MIM) implemented in the MAC. We will make two subroutines to read and write the PHY registers: Declare Sub Enc28j60readphyword(byval Phyregister As Byte) Declare Sub Enc28j60writephyword(byval Phyregister As Byte , Byval Wdata As Word)

4 of 5

08/10/2011 08:21 AM

Ben's HobbyCorner - Software - Bascom-examples

http://members.home.nl/bzijlstra/software/exampl...

We are nearly there.... The Control register memory is partitioned into four banks, selectable by the bank select bits BSEL1:BSEL0 in the ECON1 register (don't worry, later more about this). Each bank is 32 bytes long and addressed by a 5-bit address value....

Hmmmm. Only 5 bits to address a Control register but they are split into four banks. Perhaps the remaining three bits of the byte can be used to select the right bank. What about this: 'for bank selection Const Bank0 = &B00_000000 Const Bank1 = &B01_000000 Const Bank2 = &B10_000000 Const Bank3 = &B11_000000

Assigned constants consume no program memory because they only serve as a reference to the compiler. The compiler will replace all occurrences of the symbol with the assigned value. Const Epausl = &H18 Or Bank3 Const Epaush = &H19 Or Bank3 From a byte we will be using two bits for the bankselection, and ve bits for the register. Leaving a single bit we will be needing later on (dierence between Ethernet-, MAC- and Phy-registers) If we check register epausl we will have: 00011000 OR 11000000 = 11011000 All these registers, banks etc. are saved in a seperate include le which can be found here: $include "enc28j60.inc" You will have to download this le and save it in the same directory where your mainprogram is.

5 of 5

08/10/2011 08:21 AM

You might also like