caramoan tour package

caramoan tour package

Author Topic: MikroC's SPI Ethernet ENC28J60 Library  (Read 9680 times)

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
MikroC's SPI Ethernet ENC28J60 Library
« on: May 05, 2012, 04:51:13 PM »
The library info can be found in mikroC's help file.

Can also be found here,

SPI Ethernet ENC28J60 Library


Here is a basic hardware connection (found in the link above) using a PIC16F887,





A ready-to-connect ENC28J60 ethernet module can be found here:
http://www.electronicslab.ph/forum/index.php?topic=25117.0

Example projects can be found at the mikroC example folder.
On my laptop it's found here:
C:\Users\_LURKER_\Documents\Mikroelektronika\mikroC PRO for PIC\Examples\Extra Boards\SPI Ethernet


Very simple projects I made just by editing the mikroC's ethernet examples,













The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Philippine Electronics Forum

MikroC's SPI Ethernet ENC28J60 Library
« on: May 05, 2012, 04:51:13 PM »

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #1 on: May 05, 2012, 04:59:43 PM »
Let us start with the HTTP mini web server example.
For discussion's sake, I will use the example intended for PIC16F887.
I also modified the code for simplicity.

Code: [Select]
#include  "__EthEnc28j60.h"

// duplex config flags
#define Spi_Ethernet_HALFDUPLEX     0x00  // half duplex
#define Spi_Ethernet_FULLDUPLEX     0x01  // full duplex
// mE ehternet NIC pinout
sfr sbit SPI_Ethernet_Rst at RC0_bit;       
sfr sbit SPI_Ethernet_CS  at RC1_bit;
sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISC1_bit;
// end ethernet NIC definitions

/************************************************************
 * ROM constant strings
 */
const unsigned char httpHeader[] = "HTTP/1.1 200 OKnContent-type: " ;  // HTTP header
const unsigned char httpMimeTypeHTML[] = "text/htmlnn" ;              // HTML MIME type
const unsigned char httpMimeTypeScript[] = "text/plainnn" ;           // TEXT MIME type
unsigned char httpMethod[] = "GET /";
/*
 * web page, splited into 2 parts :
 * when coming short of ROM, fragmented data is handled more efficiently by linker
 *
 * this HTML page calls the boards to get its status, and builds itself with javascript
 */
const   char    *indexPage =                   // Change the IP address of the page to be refreshed
"<meta http-equiv="refresh" content="3;url=http://192.168.20.60">
<HTML><HEAD></HEAD><BODY>
<h1>PIC   ENC28J60 Mini Web Server</h1>
<a href=/>Reload</a>
<script src=/s></script>
<table><tr><td valign=top><table border=1 style="font-size:20px ;font-family: terminal ;">
<tr><th colspan=2>ADC</th></tr>
<tr><td>AN2</td><td><script>document.write(AN2)</script></td></tr>
<tr><td>AN3</td><td><script>document.write(AN3)</script></td></tr>
</table></td><td><table border=1 style="font-size:20px ;font-family: terminal ;">
<tr><th colspan=2>PORTB</th></tr>
</table></td><td>
</BODY></HTML>
" ;

/***********************************
 * RAM variables
 */
unsigned char   myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ;   // my MAC address
unsigned char   myIpAddr[4]  = {192, 168, 20, 60} ;                     // my IP address
unsigned char   getRequest[15] ;                                        // HTTP request buffer
unsigned char   dyna[30] ;                                              // buffer for dynamic response
unsigned long   httpCounter = 0 ;                                       // counter of HTTP requests

/*******************************************
 * functions
 */

#define putConstString  SPI_Ethernet_putConstString       //put the constant string pointed to by s to the ENC transmit buffer.

#define putString  SPI_Ethernet_putString                 //put the string pointed to by s to the ENC transmit buffer


unsigned int  SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags)
{
        /*
        * this function is automatically called by the library everytime it receive a request from the browser
        * the user accesses to the HTTP request by successive calls to Spi_Ethernet_getByte()
        * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
        * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit
        *
        * if you don't need to reply to HTTP requests,
        * just define this function with a return(0) as single statement
        *
        */
        unsigned int    len = 0 ;                   // my reply length
        unsigned int    i ;                         // general purpose integer

        // should we close tcp socket after response is sent?
        // library closes tcp socket by default if canClose flag is not reset here
        // flags->canClose = 0; // 0 - do not close socket
        // otherwise - close socket

        if(localPort != 80)                         // I listen only to web request on port 80
                {
                return(0) ;
                }

        // get 10 first bytes only of the request, the rest does not matter here
        for(i = 0 ; i < 10 ; i  )
                {
                getRequest[i] = SPI_Ethernet_getByte() ;
                }
               
        getRequest[i] = 0 ;

        if(memcmp(getRequest, httpMethod, 5))       // only GET method is supported here
                {
                return(0) ;
                }

        httpCounter   ;                             // one more request done

        if(getRequest[5] == 's')                    // if request path name starts with s, store dynamic data in transmit buffer
                {
                // the text string replied by this request can be interpreted as javascript statements
                // by browsers

                len = putConstString(httpHeader) ;              // HTTP header
                len  = putConstString(httpMimeTypeScript) ;     // with text MIME type

                // add AN2 value to reply
                IntToStr(ADC_Read(2), dyna) ;
                len  = putConstString("var AN2=") ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;

                // add AN3 value to reply
                IntToStr(ADC_Read(3), dyna) ;
                len  = putConstString("var AN3=") ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;

                // add HTTP requests counter to reply
                IntToStr(httpCounter, dyna) ;
                len  = putConstString("var REQ=") ;
                len  = putString(dyna) ;
                len  = putConstString(";") ;
                }
        else if(getRequest[5] == 't')                           // if request path name starts with t, toggle PORTD (LED) bit number that comes after
                {
                unsigned char   bitMask = 0 ;                   // for bit mask

                if(isdigit(getRequest[6]))                      // if 0 <= bit number <= 9, bits 8 & 9 does not exist but does not matter
                        {
                        bitMask = getRequest[6] - '0' ;         // convert ASCII to integer
                        bitMask = 1 << bitMask ;                // create bit mask
                        PORTD ^= bitMask ;                      // toggle PORTD with xor operator
                        }
                }

        if(len == 0)                                            // what do to by default
                {
                len =  putConstString(httpHeader) ;             // HTTP header
                len  = putConstString(httpMimeTypeHTML) ;       // with HTML MIME type
                len  = putConstString(indexPage) ;              // HTML page first part
                len  = putConstString(indexPage2) ;             // HTML page second part
                }

        return(len) ;                                           // return to the library with the number of bytes to transmit
        }

/*
 * main entry
 */
void    main()
        {
        ANSEL = 0x0C ;          // AN2 and AN3 convertors will be used
        C1ON_bit = 0;           // Disable comparators
        C2ON_bit = 0;
        PORTA = 0 ;
        TRISA = 0xff ;          // set PORTA as input for ADC

        ANSELH = 0;             // Configure other AN pins as digital I/O
        PORTB = 0 ;
        TRISB = 0xff ;          // set PORTB as input for buttons

        PORTD = 0 ;
        TRISD = 0 ;             // set PORTD as output

        /*
         * starts ENC28J60 with :
         * reset bit on RC0
         * CS bit on RC1
         * my MAC & IP address
         * full duplex
         */
        SPI1_Init();
        SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;

        while(1)                            // do forever
                {
                /*
                 * if necessary, test the return value to get error code
                 */
                SPI_Ethernet_doPacket() ;   // process incoming Ethernet packets

                /*
                 * add your stuff here if needed
                 * Spi_Ethernet_doPacket() must be called as often as possible
                 * otherwise packets could be lost
                 */
                }
        }

The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Philippine Electronics Forum

Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #1 on: May 05, 2012, 04:59:43 PM »

Offline tiktak

  • Gas Turbine
  • **
  • Posts: 2859
  • Pogi/Ganda Points: 203
  • Gender: Male
    • Tiktakx's Blog
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #2 on: May 05, 2012, 05:07:45 PM »
subscribing! :D aabangan ko ito :D
8051 stuff

Philippine Electronics Forum

Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #2 on: May 05, 2012, 05:07:45 PM »

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #3 on: May 05, 2012, 05:16:35 PM »
^^^ Salamat master TIKTAK. 
Please share naman po dito yung ginawa mo. :D

On topic,
Initialization of SPI and the ethernet library,

Code: [Select]
SPI1_Init();
SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;

Using the saved mac and IP address.
Code: [Select]
unsigned char   myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ;   // MAC address
unsigned char   myIpAddr[4]  = {192, 168, 20, 60} ;                         // IP address

In full-duplex mode, both devices can transmit and receive to and from each other at the same time.
The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Philippine Electronics Forum

Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #3 on: May 05, 2012, 05:16:35 PM »

Offline PlCUSER

  • Diesel Generator
  • *
  • Posts: 1909
  • Pogi/Ganda Points: 152
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #4 on: May 05, 2012, 05:21:37 PM »
UP....

subscribing. maganda tong ituturo sa atin ni master lurker. salamat master. tagal kong hinintay to. ;D ;D ;D

Philippine Electronics Forum

Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #4 on: May 05, 2012, 05:21:37 PM »

Offline RiDdLeR???

  • Hydroelectric
  • ***
  • Posts: 3032
  • Pogi/Ganda Points: 208
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #5 on: May 05, 2012, 05:47:15 PM »
uy! sabskraybing din ako dito  ....  si sis redhorse has now become a master! tamang tama di ko pa alam yung paggagamitan ko ng enc28j60 module ko!  :D ;D ;D

Offline tiktak

  • Gas Turbine
  • **
  • Posts: 2859
  • Pogi/Ganda Points: 203
  • Gender: Male
    • Tiktakx's Blog
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #6 on: May 05, 2012, 06:37:11 PM »
master simple lang po nagawa ko based sa example, abang mode ako dito sa tutorial mo :D
8051 stuff

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #7 on: May 05, 2012, 06:41:42 PM »
Hindi ito tutorial master, introduction lang po ito.
Si master prof na ang bahala sa tutorial.
Medyo nasanayan lang ng kunti ang mikroC master alyas.

UP....

^^^ Ito si master PICUSER, nagtuturo sa akin ng lahat na natutunan ko tungkol dito.
Isa ako sa mga recipient ni master santa noong last xmas, bigay niya ang ENC module as a free toy para mapaglaruan ko.
Maraming maraming salamat ulit master.


On topic,

Mga masters please do correct me if magkamali ako dito:

Code: [Select]
const unsigned char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: " ;  // HTTP header^^^
nContent-type >>> The mime type of the body of the request
1.1 200 OK >>> The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:

Code: [Select]
unsigned char httpMethod[] = "GET /";^^^ "GET", an entity corresponding to the requested resource is sent in the response. The "Get" is one the simplest Http method. Its main job is to ask the server for the resource. If the resource is  available then then it will given back to the user on your browser.

Code: [Select]
const unsigned char httpMimeTypeHTML[] = "text/html\n\n" ;  
^^^ it will return a HTML type response
          
Code: [Select]
const unsigned char httpMimeTypeScript[] = "text/plain\n\n" ;
^^^ it will return a TEXT type response

Na research ko lang lahat yan sa google. ;D ;D

"/n" >>> means next line


The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Offline tiktak

  • Gas Turbine
  • **
  • Posts: 2859
  • Pogi/Ganda Points: 203
  • Gender: Male
    • Tiktakx's Blog
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #8 on: May 05, 2012, 06:47:59 PM »
ang problema ko ngayon eh halos wala akong alam sa HTML
8051 stuff

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #9 on: May 05, 2012, 06:52:33 PM »
Dito lang po ako nagbabasa tungkol sa HTML as suggested by master PICUSER,
http://www.w3schools.com/html/default.asp
The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Offline carlsberg11

  • Lead Acid Battery
  • *******
  • Posts: 526
  • Pogi/Ganda Points: 30
  • Gender: Male
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #10 on: May 05, 2012, 07:20:55 PM »
ayun! up for this!! pa abang master lurker at sir picuser ha... pa enroll ako sa class nato :D :D :D
Anyone who has never made a mistakes has never tried anything new.

-Albert Einstein

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #11 on: May 07, 2012, 10:52:29 AM »
To configure the CS and RESET pin of the ENC module,

Code: [Select]
sfr sbit SPI_Ethernet_Rst at RC0_bit;
sfr sbit SPI_Ethernet_CS  at RC1_bit;
sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISC1_bit;

It tells where the RESET and CHIP SELECT pin is connected.

By the way, the SPI CS, SCK and SI inputs, as well as the RESET pin, are all 5V tolerant.
But their outputs are 3.3volt since the ENC is a 3.3 volt device.
If the host controller (mcu) is operated at 5V, it's quite likely it will not be within specifications when its SPI and interrupt inputs are driven by the 3.3V CMOS outputs on the ENC28J60. A unidirectional level translator would be necessary like the mikroc's hardware example above.

Another way is to lower the PIC mcu supply.
The SPI pins of the mcu (for example the PIC887, I'm not sure with other device) is schmitt triggered.
Looking at the datasheet, the high input should be 0.8VDD for schmitt triggered inputs.
So at least a 4.125volts supply for the PIC16F887.




Looking again at the datasheet, we could safely use a 10Mhz clock on PIC16F887 using a 4.125volts supply.



These is no problem with PIC16LF or PIC18LF.
As well as to the cheap mcu that I am using, PIC18F46K20.
The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Offline tiktak

  • Gas Turbine
  • **
  • Posts: 2859
  • Pogi/Ganda Points: 203
  • Gender: Male
    • Tiktakx's Blog
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #12 on: May 07, 2012, 11:05:11 AM »
74LS08 AND gate lang ginamit ko na buffer
8051 stuff

Offline dummy_c

  • Diesel Generator
  • *
  • Posts: 1588
  • Pogi/Ganda Points: 162
  • Gender: Male
  • If I die 2nyt, 8's bcoz maimai is not at my side!
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #13 on: May 07, 2012, 11:15:24 AM »
Tumpak!
Maraming salamat master.
Yan din ang suggested sa ENC datasheet, 74HCT08, since high speed ang SPI.
The world's technology is always changing, no doubt about it, and I hope it’s a while yet before it impacts my ability to tinker.

Offline PlCUSER

  • Diesel Generator
  • *
  • Posts: 1909
  • Pogi/Ganda Points: 152
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #14 on: May 07, 2012, 11:19:28 AM »
may mga non-LF series PIC mcu that works fine even with 3.3V supply. i tried it with PIC18F2550, PIC18F2620. plus ung binanggit ni master Lurker na cheap PIC18F4620. these three are guaranteed working with 3.3V supply.

powering your mcu with a 3.3V will give you a simpler hardware set up. saves money, space, and time sa pcb layout.

Offline Interlock()

  • Size C Battery
  • *****
  • Posts: 214
  • Pogi/Ganda Points: 16
  • Gender: Male
  • Puppet.Xander
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #15 on: May 07, 2012, 12:28:13 PM »
subs...
Everything Under CONTROL.
Just Master the Basics.
In every action there's always an equal reaction.
- Newton's 3rd Law of Motion

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #16 on: May 07, 2012, 12:38:14 PM »
Eto na eto na eto na. ;D ;D
Computer Engineering National Organization registration thread


Offline carlsberg11

  • Lead Acid Battery
  • *******
  • Posts: 526
  • Pogi/Ganda Points: 30
  • Gender: Male
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #17 on: May 07, 2012, 12:49:10 PM »
Eto na eto na eto na. ;D ;D

excited si prof! talagang ito na ito na! :D :D :D
Anyone who has never made a mistakes has never tried anything new.

-Albert Einstein

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #18 on: May 07, 2012, 12:50:26 PM »
To configure the CS and RESET pin of the ENC module,

Code: [Select]
sfr sbit SPI_Ethernet_Rst at RC0_bit;
sfr sbit SPI_Ethernet_CS  at RC1_bit;
sfr sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISC1_bit;

It tells where the RESET and CHIP SELECT pin is connected.

By the way, the SPI CS, SCK and SI inputs, as well as the RESET pin, are all 5V tolerant.
But their outputs are 3.3volt since the ENC is a 3.3 volt device.
If the host controller (mcu) is operated at 5V, it's quite likely it will not be within specifications when its SPI and interrupt inputs are driven by the 3.3V CMOS outputs on the ENC28J60. A unidirectional level translator would be necessary like the mikroc's hardware example above.

Another way is to lower the PIC mcu supply.
The SPI pins of the mcu (for example the PIC887, I'm not sure with other device) is schmitt triggered.
Looking at the datasheet, the high input should be 0.8VDD for schmitt triggered inputs.
So at least a 4.125volts supply for the PIC16F887.




Looking again at the datasheet, we could safely use a 10Mhz clock on PIC16F887 using a 4.125volts supply.



These is no problem with PIC16LF or PIC18LF.
As well as to the cheap mcu that I am using, PIC18F46K20.

Sa pagkaka-alam ko, ready na yung ENC namin dito. :D Tama po ba sir PICUSER?
Computer Engineering National Organization registration thread


Offline PlCUSER

  • Diesel Generator
  • *
  • Posts: 1909
  • Pogi/Ganda Points: 152
Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #19 on: May 07, 2012, 12:58:58 PM »
Ready na. Kasi hindi naman 5V powered mcu nun. ;D ;D ;D

Philippine Electronics Forum

Re: MikroC's SPI Ethernet ENC28J60 Library
« Reply #19 on: May 07, 2012, 12:58:58 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com