You are on page 1of 7

mikroElektronika • View topic - ENC28J60 and PIC18F4550 ethernet co...

1 trong 7

Login

http://www.mikroe.com/forum/viewtopic.php?f=147&t=59839

Register

FAQ

Search

It is currently 28 Nov 2014 03:09

View unanswered posts | View active topics
Board index » Website & Forums » User Projects

All times are UTC + 1 hour

ENC28J60 and PIC18F4550 ethernet communication.
Page 1 of 1 [ 5 posts ]
Print view

Previous topic | Next topic

Author
sameersaraf

Joined: 26 Feb 2014
06:26
Posts: 3

Message
Post subject: ENC28J60 and PIC18F4550 ethernet communication.

Posted: 05 May 2014 07:48

Hi everyone,
I seem to have problems working with enc28j60 and pic18f4550.
I am using an external 8MHz oscillator, I have selected 'divide by 2' option in the PLL Prescalar selection and
have enabled HSPLL to achieve 48MHz clock output.
I am connecting the ethernet board directly to the PC. I am using a cross ethernet cable.
Following are the pins used for the communication:
RC0(pin 15): Reset
RC1(pin 16): CS
RB1(pin 34): SCK
RB0(pin 33): SDI
RC7(pin 26): SDO.
I am unable to ping my ethernet board. I've been trying for a couple of days now, and its getting really
frustrating since i am not able to debug what and where the problem is exactly.
I am attaching my code below. Kindly help me in this matter.

Code: Select all
#include

"__EthEnc28j60.h"

#define Spi_Ethernet_HALFDUPLEX

0x00

// half duplex

#define Spi_Ethernet_FULLDUPLEX

0x01

// full duplex

// mE ehternet NIC pinout
sfr sbit SPI_Ethernet_Rst at LATC0_bit;

// for writing to output pin always use latch (PIC18 family)

sfr sbit SPI_Ethernet_CS

// for writing to output pin always use latch (PIC18 family)

at LATC1_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 code unsigned char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: " ;

// HTTP header

const code unsigned char httpMimeTypeHTML[] = "text/html\n\n" ;

// HTML MIME type

const code unsigned char httpMimeTypeScript[] = "text/plain\n\n" ;

// TEXT MIME type

unsigned char httpMethod[] = "GET /";
/*
* web page, splited into 2 parts :

11/28/2014 9:07 AM

fragmented data is handled more efficiently by linker * * this HTML page calls the boards to get its status. unsigned char dnsIpAddr[4] = {158.\ for(i=0. unsigned char gwIpAddr[4] = {158. 28. 57. 173 } .mikroElektronika • View topic .i++)\ {str+=\"<tr><td bgcolor=yellow>LED #\"+i+\"</td>\". // buffer for dynamic response unsigned long httpCounter = 0 . 0x3f} .255. 1 } .\ </script>\ </table></td></tr></table>\ This is HTTP request #<script>document.173\">\ <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 . 0x19.\ if(PORTB&(1<<i)){str+=\"<td bgcolor=red>ON\".i<8.\">\ <tr><th colspan=2>ADC</th></tr>\ <tr><td>AN2</td><td><script>document. 0xA5.write(REQ)</script></BODY></HTML>\ " .write(str) .i.}\ else {str+=\"<td bgcolor=#cccccc>OFF\".i<8.}\ str+=\"</td></tr>\".}\ document.mikroe.font-family: terminal . 1 } . and builds itself with javascript */ const code char *indexPage = // Change the IP address of the page to be refreshed "<meta http-equiv=\"refresh\" content=\"3. 57.\">\ <tr><th colspan=3>PORTD</th></tr>\ <script>\ var str. unsigned char ipMask[4] = {255.php?f=147&t=59839 * when coming short of ROM.}\ document. 0x14.font-family: terminal .}\ else {str+=\"<td bgcolor=#cccccc>OFF\". // HTTP request buffer unsigned char dyna[29] .\ if(PORTD&(1<<i)){str+=\"<td bgcolor=red>ON\".144.255..\ </script>\ " .\ for(i=0. 0x76.write(str) .\ str=\"\". 255.0) // DNS server IP address unsigned char getRequest[15] . unsigned char myIpAddr[4] = {158.i++)\ {str+=\"<tr><td bgcolor=pink>BUTTON #\"+i+\"</td>\".com/forum/viewtopic.i.\">\ <tr><th colspan=2>PORTB</th></tr>\ <script>\ var str.ENC28J60 and PIC18F4550 ethernet co. // counter of HTTP requests /******************************************* * functions */ 11/28/2014 9:07 AM .url=http://158.font-family: terminal .write(AN2)</script></td></tr>\ <tr><td>AN3</td><td><script>document. 144. const code char *indexPage2 = "</table></td><td>\ <table border=1 style=\"font-size:20px ..write(AN3)</script></td></tr>\ </table></td><td><table border=1 style=\"font-size:20px .}\ str+=\"</td><td><a href=/t\"+i+\">Toggle</a></td></tr>\".\ str=\"\".57. /*********************************** * RAM variables */ unsigned char myMacAddr[6] = {0x00. 2 trong 7 http://www. 144. // my MAC address // my ip addr // gateway (router) IP address // network mask (for example : 255. 0 } . 255. 144.

while(*s) { Spi_Ethernet_putByte(*s++) .. unsigned int 11/28/2014 9:07 AM . }*/ /* * it will be much faster to use library Spi_Ethernet_putString routine * instead of putString routine above. * */ #define putConstString SPI_Ethernet_putConstString /* * put the string pointed to by s to the ENC transmit buffer */ /*unsigned int putString(char *s) { unsigned int ctr = 0 . ctr++ .ENC28J60 and PIC18F4550 ethernet co. */ /*unsigned int putConstString(const code char *s) j { unsigned int ctr = 0 .. }*/ /* * it will be much faster to use library Spi_Ethernet_putConstString routine * instead of putConstString routine above. However. unsigned int remotePort. the code will be a little * bit bigger. User should choose between size and speed and pick the implementation that * suites him best. User should choose between size and speed and pick the implementation that * suites him best. or 0 if nothing to transmit * * if you don't need to reply to HTTP requests. ctr++ . while(*s) { Spi_Ethernet_putByte(*s++) . However. If you choose to go with the putConstString definition above * the #define line below should be commented out. } return(ctr) . * */ #define putString SPI_Ethernet_putString /* * this function is called by the library * 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.php?f=147&t=59839 /* * put the constant string pointed to by s to the ENC transmit buffer. If you choose to go with the putString definition above * the #define line below should be commented out.com/forum/viewtopic.mikroe. the code will be a little * bit bigger.mikroElektronika • View topic . 3 trong 7 http://www. * just define this function with a return(0) as single statement * */ unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost. } return(ctr) .

dyna) .mikroe. TEthPktFlags *flags) { unsigned int len. dyna) . len += putString(dyna) . // 0 . // one more request done if(getRequest[5] == 's') // if request path name starts with s. len += putConstString(". } httpCounter++ . dyna) . len < 10 . // add PORTB value (buttons) to reply len += putConstString("var PORTB=") .ENC28J60 and PIC18F4550 ethernet co. unsigned int reqLength. len += putString(dyna) . httpMethod.") .php?f=147&t=59839 localPort. WordToStr(PORTB. // my reply length // should we close tcp socket after response is sent? // library closes tcp socket by default if canClose flag is not reset here // *canClose = 0.. len += putConstString(". 4 trong 7 http://www. len += putConstString(". store dynamic datp6w transmit buffer { // the text string replied by this request can be interpreted as javascript statements // by browsers len = putConstString(httpHeader) . len = 0. len += putString(dyna) .com/forum/viewtopic. WordToStr(PORTD. 11/28/2014 9:07 AM .do not close socket // otherwise .close socket if(localPort != 80) // I listen only to web request on port 80 { return(0) .") . } getRequest[len] = 0 . len++) { getRequest[len] = SPI_Ethernet_getByte() . // add PORTD value (LEDs) to reply len += putConstString("var PORTD=") . the rest does not matter here for(len = 0 . len += putString(dyna) . // with text MIME type // add AN2 value to reply WordToStr(ADC_Read(2).") .mikroElektronika • View topic .. len += putConstString("var AN2=") . if(memcmp(getRequest. // HTTP header len += putConstString(httpMimeTypeScript) . } // get 10 first bytes only of the request. len += putConstString(". 5)) // only GET method is supported here { return(0) . len += putConstString("var AN3=") . // add AN3 value to reply WordToStr(ADC_Read(3). dyna) .") .

mikroElektronika • View topic . // HTTP header len += putConstString(httpMimeTypeHTML) . // set PORTA as input for ADC 11/28/2014 9:07 AM . len += putString(dyna) . unsigned int remotePort. or 0 if nothing to transmit * * if you don't need to reply to UDP requests.'0' . unsigned int reqLength. * just define this function with a return(0) as single statement * */ unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost. // convert ASCII to integer // create bit mask // toggle PORTD with xor operator } } if(len == 0) // what do to by default { len = putConstString(httpHeader) . len += putConstString("var REQ=") .com/forum/viewtopic.ENC28J60 and PIC18F4550 ethernet co. unsigned int destPort. bits 8 & 9 does not exist but does not matter { bitMask = getRequest[6] . bitMask = 1 << bitMask . } else if(getRequest[5] == 't') // if request path name starts with t.") . toggle PORTD (LED) bit number that comes after { unsigned char bitMask = 0 .php?f=147&t=59839 // add HTTP requests counter to reply WordToStr(httpCounter. len += putConstString(". TEthPktFlags *flags) { return(0). if(isdigit(getRequest[6])) // for bit mask // if 0 <= bit number <= 9. CMCON = 7. PORTA = 0 . // HTML page second part } return(len) . // with HTML MIME type len += putConstString(indexPage) . dyna) .. TRISA = 0xff .. 5 trong 7 http://www.mikroe. } /* * main entry */ void main() { ADCON1 = 0x0B. // HTML page first part len += putConstString(indexPage2) . LATD ^= bitMask . // return to the library with the number of bytes to transmit } /* * this function is called by the library * the user accesses to the UDP 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 UDP reply.

php?f=147&t=59839 PORTB = 0 .. while(1) // do forever { /* * if necessary. mikroElektronika team Joined: 30 Apr 2012 14:20 Posts: 2611 Your network IP address configuration is not valid.10.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_FULLDUPLEX) .mikroe.255 > class C network Best regards. Private network IP addresses should be used.255.255.255. Your router configuration should use one of those three IP pools: 10. // dhcp will not be used here.172.ENC28J60 and PIC18F4550 ethernet co. so use preconfigured addresses SPI_Ethernet_confNetwork(ipMask. Posted: 05 May 2014 09:20 Hello. // 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 */ } } Thanks in advance.0. LATD // set PORTB as input for buttons = 0 .0 . myIpAddr.255 > class A network 172.255.31.com/forum/viewtopic. gwIpAddr. SPI_Ethernet_Init(myMacAddr. dnsIpAddr) ..255 > class B networks 192. Top dejan.odabasic Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication. TRISD = 0 . Posted: 06 May 2014 06:30 11/28/2014 9:07 AM . Top sameersaraf Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication.mikroElektronika • View topic .16. //SPI_Rd_Ptr = SPI1_Read. TRISB = 0xff .0 .0.168. test the return value to get error code */ SPI_Ethernet_doPacket() .0.0.168.192. 6 trong 7 http://www.

odabasic Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication.1.. I have a question.20.168. Joined: 26 Feb 2014 06:26 Posts: 3 I made the following changes according to your suggestion: ip adress: 192. Posted: 07 May 2014 06:42 hello again. Thanks dejan. I am directly connecting my ethernet board to the pc (I'm using a cross cable for this purpose). but I'm new to all this stuff. I went through the topic you suggested..mikroElektronika • View topic .odabasic for your quick response. mikroElektronika team Please take a look at this topic: viewtopic.com/forum/viewtopic. Thanks in advance Top dejan..ENC28J60 and PIC18F4550 ethernet co. Found it informative..mikroe. Joined: 30 Apr 2012 14:20 Posts: 2611 Top sameersaraf Joined: 26 Feb 2014 06:26 Posts: 3 Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication.60 gw ip address: 192.20.255. B or C? I know this is too basic.php?f=164&t=49793 There you can find detail explanation how network parameters should be configured.168.0 dns ip adress: 192. Posted: 06 May 2014 08:44 Hello. so can i not just set any ip address from any of the three classes A. 7 trong 7 http://www.1 Still I am not able to ping my ethernet board Is there any other bug in the code that needs to be fixed? Kindly help..php?f=147&t=59839 Hi..168.255.6 subnet mask: 255. Top Display posts from previous: All posts Sort by Post time Ascending Go Page 1 of 1 [ 5 posts ] Board index » Website & Forums » User Projects All times are UTC + 1 hour Who is online Users browsing this forum: No registered users and 2 guests You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum Search for: Go Jump to: User Projects Go Powered by phpBB® Forum Software © phpBB Group 11/28/2014 9:07 AM . Best regards.