#include "__EthEnc28j60.h"// duplex config flags#define Spi_Ethernet_HALFDUPLEX 0x00 // half duplex#define Spi_Ethernet_FULLDUPLEX 0x01 // full duplex// mE ehternet NIC pinoutsfr 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 headerconst unsigned char httpMimeTypeHTML[] = "text/htmlnn" ; // HTML MIME typeconst unsigned char httpMimeTypeScript[] = "text/plainnn" ; // TEXT MIME typeunsigned 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 addressunsigned char myIpAddr[4] = {192, 168, 20, 60} ; // my IP addressunsigned char getRequest[15] ; // HTTP request bufferunsigned char dyna[30] ; // buffer for dynamic responseunsigned 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 bufferunsigned 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 */ } }
SPI1_Init();SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;
unsigned char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ; // MAC addressunsigned char myIpAddr[4] = {192, 168, 20, 60} ; // IP address
UP....
const unsigned char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: " ; // HTTP header
unsigned char httpMethod[] = "GET /";
const unsigned char httpMimeTypeHTML[] = "text/html\n\n" ;
const unsigned char httpMimeTypeScript[] = "text/plain\n\n" ;
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;
Eto na eto na eto na.
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.