1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Ethernet for PICs using ENC28J60

Discussion in 'Microcontrollers' started by Yobortsa, Sep 4, 2009.

  1. Yobortsa

    Yobortsa Member

    Joined:
    Aug 19, 2009
    Messages:
    46
    Likes:
    0
    Location:
    Brisbane, Australia
    Hi all,

    I am dabbling with Microchip's ENC28J60 10baseT ethernet chip and a PIC12F675 at the moment, but thinking this is too much of a challenge for a newbie and perhaps I should use the Microchip TCP/IP stack first (which was designed for at least PIC18 MCUs) rather than writing my own SPI bit-bang routines for a 12F675 and working at packet level.

    Which of the PIC18 MCUs is a good starting point for the ENC28J60?

    Obviously it needs an SPI interface and I'm not concerned with any other specific I/O functionality at this point nor do I have any requirement for significant RAM or program memory.

    All I have used so far is a PIC12F675, so the simpler the better!

    Thanks,

    David
     
  2. Yobortsa

    Yobortsa Member

    Joined:
    Aug 19, 2009
    Messages:
    46
    Likes:
    0
    Location:
    Brisbane, Australia
    12F675 code for SPI if anybody is interested

    Here is the 12F675 SPI bit-bang code I've written so far if anybody is interested, but I haven't even been able to figure out if it's working or not. I'm not confident the timing is 100% but it looks ok on a scope (I think!).

    Code (text):

    #include <p12F675.inc>
    ;     __CONFIG (_INTRC_OSC_NOCLKOUT & _WDT_OFF &_BODEN_OFF &_CP_OFF &_CPD_OFF & _MCLRE_OFF)
         __CONFIG (_INTRC_OSC_NOCLKOUT & _WDT_OFF &_BODEN_OFF &_CP_OFF &_CPD_OFF)

    ;-----------------------------------------------------------------------------------------------------------------------------
    ; RAM allocation
    ;
    ; Register locations 20h-5Fh are General Purpose registers,
    ; implemented as static RAM and are mapped across both banks.
    ;-----------------------------------------------------------------------------------------------------------------------------

    COUNT1      equ 020h        ;First counter for our delay loops
    COUNT2      equ 021h        ;Second counter for our delay loops  

    spi_in1     equ 022h
    spi_in2     equ 023h
    spi_in3     equ 024h
    spi_out1    equ 025h
    spi_out2    equ 026h
    spi_out3    equ 027h

    spi_status  equ 028h
    spi_bit     equ 029h        ; Bit number counter
    spi_count   equ 02ah        ; Number of bytes to read/write
    spi_in      equ 02bh        ; Points to spi in buffer
    spi_out     equ 02ch        ; Points to spi out buffer

    ;-----------------------------------------------------------------------------------------------------------------------------
    ; ENC28J60 Instructions and Registers
    ;
    RCR     equ 000h
    RBM     equ 020h
    WCR     equ 040h
    WBM     equ 060h
    BFS     equ 080h
    BFC     equ 0a0h
    SRC     equ 0ffh

    ; Bank 0
    ERDPTL      EQU 00h
    ERDPTH      EQU 01h
    EWRPTL      EQU 02h
    EWRPTH      EQU 03h
    ETXSTL      EQU 04h
    ETXSTH      EQU 05h
    ETXNDL      EQU 06h
    ETXNDH      EQU 07h
    ERXSTL      EQU 08h
    ERXSTH      EQU 09h
    ERXNDL      EQU 0ah
    ERXNDH      EQU 0bh
    ERXRDPTL    EQU 0ch
    ERXRDPTH    EQU 0dh
    ERXWRPTL    EQU 0eh
    ERXWRPTH    EQU 0fh
    EDMASTL     EQU 10h
    EDMASTH     EQU 11h
    EDMANDL     EQU 12h
    EDMANDH     EQU 13h
    EDMADSTL    EQU 14h
    EDMADSTH    EQU 15h
    EDMACSL     EQU 16h
    EDMACSH     EQU 17h

    ; Bank1
    EHT0        EQU 00h
    EHT1        EQU 01h
    EHT2        EQU 02h
    EHT3        EQU 03h
    EHT4        EQU 04h
    EHT5        EQU 05h
    EHT6        EQU 06h
    EHT7        EQU 07h
    EPMM0       EQU 08h
    EPMM1       EQU 09h
    EPMM2       EQU 0ah
    EPMM3       EQU 0bh
    EPMM4       EQU 0ch
    EPMM5       EQU 0dh
    EPMM6       EQU 0eh
    EPMM7       EQU 0fh
    EPMCSL      EQU 10h
    EPMCSH      EQU 11h
    EPMOL       EQU 14h
    EPMOH       EQU 15h
    ERXFCON     EQU 18h
    EPKTCNT     EQU 19h

    ; Bank2
    MACON1      EQU 00h
    MACON3      EQU 02h
    MACON4      EQU 03h
    MABBIPG     EQU 04h
    MAIPGL      EQU 06h
    MAIPGH      EQU 07h
    MACLCON1    EQU 08h
    MACLCON2    EQU 09h
    MAMXFLL     EQU 0ah
    MAMXFLH     EQU 0bh
    MICMD       EQU 12h
    MIREGADR    EQU 14h
    MIWRL       EQU 16h
    MIWRH       EQU 17h
    MIRDL       EQU 18h
    MIRDH       EQU 19h

    ; Bank3
    MAADR5      EQU 00h
    MAADR6      EQU 01h
    MAADR3      EQU 02h
    MAADR4      EQU 03h
    MAADR1      EQU 04h
    MAADR2      EQU 05h
    EBSTSD      EQU 06h
    EBSTCON     EQU 07h
    EBSTCSL     EQU 08h
    EBSTCSH     EQU 09h
    MISTAT      EQU 0ah
    EREVID      EQU 12h
    ECOCON      EQU 15h
    EFLOCON     EQU 17h
    EPAUSL      EQU 18h
    EPAUSH      EQU 19h

    ; Registers common to all banks...
    EIE     EQU 1bh
    EIR     EQU 1ch
    ESTAT       EQU 1dh
    ECON2       EQU 1eh
    ECON1       EQU 1fh

    ; Physical registers
    PHCON1      EQU 00h
    PHSTAT1     EQU 01h
    PHID1       EQU 02h
    PHID2       EQU 03h
    PHCON2      EQU 10h
    PHSTAT2     EQU 11h
    PHIE        EQU 12h
    PHIR        EQU 13h
    PHLCON      EQU 14h
    ;-----------------------------------------------------------------------------------------------------------------------------

        org 0h
    ;   goto Init

    ;   org 04h
    ;   retfie

    ;-----------------------------------------------------------------------------------------------------------------------------
    ; Init inputs and outputs by setting TRISIO port
    ;
    ; TRISIO bit = 1 Input
    ; TRISIO bit = 0 Output
    ;
    ; Can either set my moving 8 bit value to W register then moving W to F or by using BSF (set bit 1) or BCF (set bit 0):
    ;
    ;   bsf     STATUS,5    ; Switch to Bank 1
    ;   movlw   b'00110101' ; Set the Port A pins
    ;   movwf   TRISIO      ;
    ;   bcf     STATUS,5    ; Switch back to Bank 0
    ;-----------------------------------------------------------------------------------------------------------------------------

    Init
    ;   org 10h

        bsf     STATUS,5        ; Switch to Bank 1
        movlw   b'00100000'     ; Set I/O 1 output (for SCK - clock)
                    ; Set I/O 2 output (for CS - Slave Select)
        movwf   TRISIO      ; Set I/O 4 output (for SO - to SI on ENC28J60)
                    ; Set I/O 5 input (for SI - to SO on ENC28J60)
        bcf     STATUS,5        ; Switch back to Bank 0

        movlw   b'00000100'     ; Set cs high, all other outputs low
        movwf   GPIO

    ;-----------------------------------------------------------------------------------------------------------------------------
    ; Setting an output high or low
    ;
    ; If it's only one output you want to update, use BSF (to set high) or BCF (to set low):
    ;
    ;   bsf     GPIO,0      ; Set GP IO 0 high
    ;   Call Delay
    ;   bcf     GPIO,2      ; Set GP IO 2 low
    ;-----------------------------------------------------------------------------------------------------------------------------

    ;   bsf GPIO,0
    ;   call DelayLong
    ;   bcf GPIO,0
        call DelayLong

    doagain
        movlw   2
        movwf   spi_count   ; Set the number of bytes to send/receive
        movlw   RCR | ERXFCON
        movwf   spi_out1
        call spi

        goto doagain




        movlw   2
        movwf   spi_count   ; Set the number of bytes to send/receive
        movlw   WCR | ECON1
        movwf   spi_out1    ; Write ECON1
        movlw   2
        movwf   spi_out2    ; Switch to bank 2
        call spi

        movlw   2
        movwf   spi_count   ; Set the number of bytes to send/receive
        movlw   WCR | MIREGADR
        movlw   PHLCON
        movwf   spi_out2
        call    spi

        movlw   2
        movwf   spi_count
        movlw   WCR | MIWRL
        movlw   b'10000010'
        call    spi

        movlw   2
        movwf   spi_count
        movlw   WCR | MIWRH
        movlw   b'00111000'
        call    spi

        goto doagain


    ;-----------------------------------------------------------------------------------------------------------------------------
    ; SPI subroutine
    ; ==============
    ;
    ; Call with:
    ;
    ;  spi_count set to the number of bytes to write/read
    ;  spi_in set to the first register address of the inbound buffer
    ;  spi_out set to the first register address of the outbound buffer
    ;-----------------------------------------------------------------------------------------------------------------------------

    spi
        movlw   spi_in1
        movwf   spi_in      ; Set the start of the inbound buffer
        movlw   spi_out1
        movwf   spi_out     ; Set the start of the outbound buffer

        movlw   8       ; Set bit counter
        movwf   spi_bit     ;   to 8 bits

        bcf GPIO,2      ; Set CS line low

    spi_looplong
        nop
        nop
        nop
        nop
        nop
        nop
        nop

    spi_loop
        bcf GPIO,1      ; **** Set clock line low ****

        movf    spi_out,W   ; Point to next spi_out byte
        movwf   FSR     ;   with File Select Register
        rlf INDF,F      ; Roll next data bit into Carry flag
        btfss   STATUS,C    ; Is it high? If yes, skip...
        goto BitLow

        bsf GPIO,4      ; Set SO line high
        goto spi_donebit    ; We have done the bit, continue on...

    BitLow
        bcf GPIO,4      ; Set SO line low
        nop         ; Wait an extra cycle if BitLow to match high alternative

    spi_donebit
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop

        bcf STATUS,C    ; Clear the Carry flag

        bsf GPIO,1      ; **** Set clock line high ****

        btfsc   GPIO,5      ; Read bit from SI line, skip if clear (0)
        bsf STATUS,C    ; Set the Carry flag if bit not clear (1)
        movf    spi_in,W    ; Point to next spi_in byte
        movwf   FSR     ;   with File Select Register
        rlf INDF,F      ; Roll the incoming bit into the buffer

        nop         ; Wait an extra cycle to match other side of clock

        decfsz  spi_bit,F   ; Decrement bit counter, skip if zero
        goto spi_looplong   ; Go do next bit...

        decfsz  spi_count,F ; Decrement our byte counter, skip if zero
        goto spi_nextbyte   ; Go do next byte, otherwise...
        nop         ; Wait 210ns (each instruction is 1us or 1000ns)
        bsf GPIO,2      ; Set CS high
        return          ; Return to program

    spi_nextbyte
        movlw   8       ; Reset bit counter
        movwf   spi_bit     ;   for next 8 bits
        incf    spi_out,F   ; Point to next byte in outbound buffer
        incf    spi_in,F    ; Point to next byte in inbound buffer
        goto spi_loop       ; Go do next byte...

    ;-----------------------------------------------------------------------------------------------------------------------------
    ; Delay subroutine

    DelayLong
        call Delay
        call Delay
        call Delay
        call Delay
        call Delay
        call Delay
        call Delay
        call Delay
        call Delay
        return

    Delay
    Loop1
        decfsz  COUNT1,1    ; This second loop keeps the LED
        goto Loop1          ; turned off long enough for us to
        decfsz  COUNT2,1    ; see it turned off
        goto Loop1          ;
        return

    ;-----------------------------------------------------------------------------------------------------------------------------
        end
    ;-----------------------------------------------------------------------------------------------------------------------------

     
     
  3. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Hey im very interested in this project. Can someone post all the parts needed to set up a ethernet board..

    I know i need the:
    ENC28J60

    and was looking at the schematics for the eval board. I need also:
    Code (text):

    25mhz crystal
    50 ohm resistors
    180 ohm resistors
    2.7k resistors

    10uf caps
    0.1uf caps
    33pf caps

    I see a L1 but i think its a ferret beed

    Ethernet Jack
    they have a 256K SPIā„¢ Bus Serial EEPROM but not sure if its needed 100%
     
    and a PIC of course but anything else? Or something i can change or something?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada

    You need the Ethernet transformer or a RJ45 jack with it built in. You don''t need the extra eeprom unless you want to store more web pages or data than the flash on the PIC will hold. It is not a hard project if you use MicroChip's stack. Here is a picture of a 18F4620 interfaced to the ENC28J60 (minus the RJ45 jack) which shows how simple the hardware is:
     

    Attached Files:

    Last edited: Sep 4, 2009
  6. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    I am thinking you could pull the transformer from an old internet card ?

    3v0
     
  7. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada
    That's where I got the Valor FL1012 from. :D
     
  8. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    heh if thats the case i have a bunch of old motherboards. im sure they have the transformer stuff built into the jack. It seems like a save :D

    Thanks for the tip. You think it would be hard to implement a SPI eeprom or infact a SD card using SPI just like a eeprom ? Does the stack have a fat32 type structure or is it custom?

    I watched so many videos its not funny. I love the web vending machine one. I seen all 3 and fell in love. i would love to make a ethernet board so i can monitor stuff and host or send the data to myself.

    1 question... does the ethernet stack have a server type code built in? So when i connect it, it will serve the info auto? Or does one need it to a PC ?
     
  9. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada
    The Microchip stack, that I used, had the code for I2C eeprom already. You'd have to write your own if you wanted SPI.
    The stack had it's own file format called MPFS. I seem to remember that Microchip also provided a utility to convert files to MPFS format.
    Yes. The one I played with was the HTTP server. It served up a webpage where you could turn on a LED with a mouse click and read values from the ADC with your web browser. You did need a terminal program to setup DCHP or static IP's via the serial port if you didn't want to modify the code and hard code those options in. Pretty neat little application. There were also FTP & SNMP modules available.
     
  10. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Thats awesome. I would make a little handheld device to program them This way no PC is needed and its not hard coded.

    This sounds awesome. Is it still like:

    microchip.com/tcpip ?
     
  11. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Last edited: Sep 4, 2009
  12. DirtyLude

    DirtyLude Well-Known Member

    Joined:
    Aug 5, 2003
    Messages:
    1,904
    Likes:
    56
    Location:
    Toronto, Canada
    What you are looking for is called a MagJack. Your DipMicro site has cheap ethernet connectors with built in magnetics. OEM ethernet cards and Motherboards and such almost always have the magnetics seperate from the jack.

    Check out the hackaday articles:
    How-To: Web server on a business card (Part 1) - Hack a Day
    How-To: Web server on a business card (Part 2) - Hack a Day

    Personally I use a Wiznet W5100 for an 8bit uC solution. It's real easy, with the TCP stack right on chip. You can have simple servers running with just a state machine on the uC. Unfortunately they seemed to have jacked up the price of the w5100 to $5 each. Also it is a hard chip to etch for/mount. It has 0.4mm lead spacing, which means board houses with 8mil spacing requirements wont work with it. 0.4mm is very odd spacing.
     

    Attached Files:

  13. DirtyLude

    DirtyLude Well-Known Member

    Joined:
    Aug 5, 2003
    Messages:
    1,904
    Likes:
    56
    Location:
    Toronto, Canada
    Also the higher series Stellaris Cortex-M3 uC's have a MAC/PHY built right into the chip and are ready to go. You already have a JTAG programmer and Crossworks. You may not want to try that out right at first, but keep it in mind. I'm definitely going to be trying them out sometime when I have the time.
     
  14. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    the motherboards i have dont have no seperate magnets. all is single. But i will def... get from DipMicro.

    After i learn this im moving over to GSM. Ill buy a cheap sim card (prepaid) and test it out. I guess i would need 2 sim cards and 2 GSM modules but heh it will be fun.

    Do you think i should contact GSM company like tmobile and ask if there is a plan i can put the chip on so i just have data? and thus save money
     
  15. gaspode42

    gaspode42 Member

    Joined:
    Feb 7, 2009
    Messages:
    218
    Likes:
    3
    Location:
    UK
    Jason

    Have a look at the attached.

    Rupert
     

    Attached Files:

  16. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Last edited: Sep 6, 2009
  17. DirtyLude

    DirtyLude Well-Known Member

    Joined:
    Aug 5, 2003
    Messages:
    1,904
    Likes:
    56
    Location:
    Toronto, Canada
    It works for me. It's just the datasheet for the Wiznet W5100 premade module.

    That's what I originally tested with before I made up a board.
     
  18. gaspode42

    gaspode42 Member

    Joined:
    Feb 7, 2009
    Messages:
    218
    Likes:
    3
    Location:
    UK
    @DirtyLude
    I am just playing with one at the moment - did you manage to get DHCP working on it?

    Rupert
     
  19. DirtyLude

    DirtyLude Well-Known Member

    Joined:
    Aug 5, 2003
    Messages:
    1,904
    Likes:
    56
    Location:
    Toronto, Canada
    I haven't implemented DHCP on it. I just assigned a static IP, since it's connecting to my home network. I guess I should put in DHCP sometime, but it's not a priority for me. I'm actually working with the W5300 now. Pretty much the same thing in terms of interface, though.

    I'm currently working on a simple RF mesh network that I'd like to integrate into the HTTP server and be able to control RF nodes and read sensor data over the web.
     
  20. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    its my pc :( but its wierd only that pdf... i will try to convert all to jpgs and read it. If not ill try my virtual box pc and see if i can peek in

    Edit:
    Finally got it to work ok. I had to split it with photoshop into 14 different pages then merge it back heh nice device
     
    Last edited: Sep 6, 2009
  21. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US

Share This Page