Você está na página 1de 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

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

2 trong 7

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

* 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 code

char

*indexPage =

// Change the IP address of the page to be refreshed

"<meta http-equiv=\"refresh\" content=\"3;url=http://158.144.57.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 ;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>\
<script>\
var str,i;\
str=\"\";\
for(i=0;i<8;i++)\
{str+=\"<tr><td bgcolor=pink>BUTTON #\"+i+\"</td>\";\
if(PORTB&(1<<i)){str+=\"<td bgcolor=red>ON\";}\
else {str+=\"<td bgcolor=#cccccc>OFF\";}\
str+=\"</td></tr>\";}\
document.write(str) ;\
</script>\
" ;

const code

char

*indexPage2 =

"</table></td><td>\

<table border=1 style=\"font-size:20px ;font-family: terminal ;\">\


<tr><th colspan=3>PORTD</th></tr>\
<script>\
var str,i;\
str=\"\";\
for(i=0;i<8;i++)\
{str+=\"<tr><td bgcolor=yellow>LED #\"+i+\"</td>\";\
if(PORTD&(1<<i)){str+=\"<td bgcolor=red>ON\";}\
else {str+=\"<td bgcolor=#cccccc>OFF\";}\
str+=\"</td><td><a href=/t\"+i+\">Toggle</a></td></tr>\";}\
document.write(str) ;\
</script>\
</table></td></tr></table>\
This is HTTP request #<script>document.write(REQ)</script></BODY></HTML>\
" ;

/***********************************
* RAM variables
*/
unsigned char

myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ;

unsigned char myIpAddr[4]

= {158, 144,

57, 173 } ;

unsigned char gwIpAddr[4]

= {158, 144,

unsigned char ipMask[4]

= {255, 255, 255,

57, 1 } ;

unsigned char dnsIpAddr[4] = {158, 144, 28,

0 } ;
1 } ;

// my MAC address

// my ip addr

// gateway (router) IP address


// network mask (for example : 255.255.255.0)
// DNS server IP address

unsigned char

getRequest[15] ;

// HTTP request buffer

unsigned char

dyna[29] ;

// buffer for dynamic response

unsigned long

httpCounter = 0 ;

// counter of HTTP requests

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

11/28/2014 9:07 AM

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

3 trong 7

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

/*
* put the constant string pointed to by s to the ENC transmit buffer.
*/
/*unsigned int

putConstString(const code char *s)


j

{
unsigned int ctr = 0 ;

while(*s)
{
Spi_Ethernet_putByte(*s++) ;
ctr++ ;
}
return(ctr) ;
}*/
/*
* it will be much faster to use library Spi_Ethernet_putConstString routine
* instead of putConstString routine above. However, the code will be a little
* bit bigger. User should choose between size and speed and pick the implementation that
* suites him best. If you choose to go with the putConstString definition above
* the #define line below should be commented out.
*
*/
#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 ;

while(*s)
{
Spi_Ethernet_putByte(*s++) ;

ctr++ ;
}
return(ctr) ;
}*/
/*
* it will be much faster to use library Spi_Ethernet_putString routine
* instead of putString routine above. However, the code will be a little
* bit bigger. User should choose between size and speed and pick the implementation that
* suites him best. If you choose to go with the putString definition above
* the #define line below should be commented out.
*
*/
#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, 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

SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int

11/28/2014 9:07 AM

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

4 trong 7

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

localPort, unsigned int reqLength, TEthPktFlags *flags)


{
unsigned int

len;

// 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; // 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(len = 0 ; len < 10 ; len++)
{
getRequest[len] = SPI_Ethernet_getByte() ;
}
getRequest[len] = 0 ;
len = 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

datp6w 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


WordToStr(ADC_Read(2), dyna) ;
len += putConstString("var AN2=") ;
len += putString(dyna) ;
len += putConstString(";") ;

// add AN3 value to reply


WordToStr(ADC_Read(3), dyna) ;
len += putConstString("var AN3=") ;
len += putString(dyna) ;
len += putConstString(";") ;

// add PORTB value (buttons) to reply


len += putConstString("var PORTB=") ;
WordToStr(PORTB, dyna) ;
len += putString(dyna) ;
len += putConstString(";") ;

// add PORTD value (LEDs) to reply


len += putConstString("var PORTD=") ;
WordToStr(PORTD, dyna) ;
len += putString(dyna) ;
len += putConstString(";") ;

11/28/2014 9:07 AM

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

5 trong 7

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

// add HTTP requests counter to reply


WordToStr(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 ;

if(isdigit(getRequest[6]))

// for bit mask

// if 0 <= bit number <= 9, bits 8 & 9

does not exist but does not matter


{
bitMask = getRequest[6] - '0' ;
bitMask = 1 << bitMask ;
LATD ^= bitMask ;

// convert ASCII to integer


// create bit mask
// 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
}

/*
* 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, or 0 if nothing to transmit
*
* if you don't need to reply to UDP requests,
* just define this function with a return(0) as single statement
*
*/
unsigned int

SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int

destPort, unsigned int reqLength, TEthPktFlags *flags)


{

return(0);
}

/*
* main entry
*/
void

main()
{
ADCON1 = 0x0B;
CMCON = 7;

PORTA = 0 ;
TRISA = 0xff ;

// set PORTA as input for ADC

11/28/2014 9:07 AM

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

6 trong 7

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

PORTB = 0 ;
TRISB = 0xff ;

LATD

// set PORTB as input for buttons

= 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_Rd_Ptr = SPI1_Read;
SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;

// dhcp will not be used here, so use preconfigured addresses


SPI_Ethernet_confNetwork(ipMask, gwIpAddr, dnsIpAddr) ;

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
*/
}
}

Thanks in advance.

Top
dejan.odabasic

Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication.

Posted: 05 May 2014 09:20

Hello,
mikroElektronika team

Joined: 30 Apr 2012


14:20
Posts: 2611

Your network IP address configuration is not valid.


Private network IP addresses should be used.
Your router configuration should use one of those three IP pools:
10.0.0.0 - 10.255.255.255 > class A network
172.16.0.0 - 172.31.255.255 > class B networks
192.168.0.0 - 192.168.255.255 > class C network
Best regards.

Top
sameersaraf

Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication.

Posted: 06 May 2014 06:30

11/28/2014 9:07 AM

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

7 trong 7

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

Hi,
Thanks dejan.odabasic for your quick response.
Joined: 26 Feb 2014
06:26
Posts: 3

I made the following changes according to your suggestion:


ip adress: 192.168.20.60
gw ip address: 192.168.20.6
subnet mask: 255.255.255.0
dns ip adress: 192.168.1.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.
Thanks in advance

Top
dejan.odabasic

Post subject: Re: ENC28J60 and PIC18F4550 ethernet communication.

Posted: 06 May 2014 08:44

Hello,
mikroElektronika team

Please take a look at this topic: viewtopic.php?f=164&t=49793


There you can find detail explanation how network parameters should be configured.
Best regards.
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.

Posted: 07 May 2014 06:42

hello again..
I went through the topic you suggested.. Found it informative..
I have a question..
I am directly connecting my ethernet board to the pc (I'm using a cross cable for this purpose), so can i not
just set any ip address from any of the three classes A, B or C?
I know this is too basic, but I'm new to all this stuff.

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

Você também pode gostar