EasyPic4 with PIC16F887 + ENC28J60 web server

General discussion on mikroPascal.
Post Reply
Author
Message
etf
Posts: 3
Joined: 30 Mar 2011 20:22

EasyPic4 with PIC16F887 + ENC28J60 web server

#1 Post by etf » 01 Apr 2011 20:00

Hello.

I have been trying to make EasyPic4 with PIC16F887 and serial ethernet board ENC28J60 working.
I can not browse the web page on the pic.
I did what is said in the program comments.
my computer has an ip adress : 192.168.20.32
Subnet mask : 255.255.255.0
Default gateway : 192.168.20.60
I tried with different ip and gateway ,but the result was the same.
Serial Ethernet Board's leds : LED A is always on, POWER is always ON, LEB B in the begining blinks and than dies.
Here is my code:
/*
* Project Name:
httpserver_example (Ethernet Library http server demo for ENC28J60 mcu)
* Target Platform:
PIC
* Copyright:
(c) mikroElektronika, 2006.
*
* description :
* this code shows how to use the ENC28J60 mini library :
* the board will reply to ARP & ICMP echo requests
* the board will reply to HTTP requests on port 80, GET method with pathnames :
* / will return the HTML main page
* /s will return board status as text string
* /t0 ... /t7 will toggle RC0 to RC7 bit and return HTML main page
* all other requests return also HTML main page
*
* target devices :
* any PIC with integrated SPI and more than 4 Kb ROM memory
* 32 to 40 MHz clock is recommended to get from 8 to 10 Mhz SPI clock,
* otherwise PIC should be clocked by ENC clock output due to ENC silicon bug in SPI hardware
* if you try lower PIC clock speed, don't be surprised if the board hang or miss some requests !
* tested with PIC16F877A@10Mhz on EasyPIC4 board
*
* EP settings :
* RA2 & RA3 pots jumper : closed
* PORTB : pull-down
* PORTC : pull-down
* BUTTONS : pull-up
*
* RC0 : !RESET to ENC reset input pin
* RC1 : !CS to ENC chip select input pin
* the ENC28J60 SPI bus CLK, SO, SI must be connected to the corresponding SPI pins of the PIC
* the INT and WOL signals from the ENC are not used
*
* Test configuration:
MCU: PIC16F877A
Dev.Board: EasyPIC4
Oscillator: HS, 10.000MHz
Ext. Modules: mE Serial Ethernet board
SW: mikroC v6.2.0.0.
* NOTES:
None.
*/

#define SPI_Ethernet_HALFDUPLEX 0
#define SPI_Ethernet_FULLDUPLEX 1

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;




/************************************************************
* ROM constant strings
*/
const unsigned char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: " ; // HTTP header
const unsigned char httpMimeTypeHTML[] = "text/html\n\n" ; // HTML MIME type
const unsigned char httpMimeTypeScript[] = "text/plain\n\n" ; // TEXT MIME type
unsigned char httpMethod[] = "GET /"; // supported http method
/*
* 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 = "<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 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+\">Set/Reset Bit\"+i+\"</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} ; // my MAC address
unsigned char myIpAddr[4] = {192, 168, 20, 60} ; // my IP address
unsigned char getRequest[15] ; // HTTP request buffer
unsigned char dyna[31] ; // buffer for dynamic response
unsigned long httpCounter = 0 ; // counter of HTTP requests

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

/*
* put the constant string pointed to by s to the ENC transmit buffer
*/
unsigned int putConstString(const char *s)
{
unsigned int ctr = 0 ;

while(*s)
{
SPI_Ethernet_putByte(*s++) ;
ctr++ ;
}
return(ctr) ;
}

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

/*
* 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 localPort, unsigned int reqLength, char *canClose)
{
unsigned int len = 0 ; // my reply length
unsigned int i ; // general purpose integer

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 = SPI_Ethernet_getByte() ;
}
getRequest = 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) ; // convert read adc channel 2 value into string
len += putConstString("var AN2=") ;
len += putString(dyna) ;
len += putConstString(";\n") ;

// add AN3 value to reply
intToStr(ADC_Read(3), dyna) ; // convert read adc channel 2 value into string
len += putConstString("var AN3=") ;
len += putString(dyna) ;
len += putConstString(";\n") ;

// add PORTB value (buttons) to reply
len += putConstString("var PORTB=") ;
intToStr(PORTB, dyna) ; // convert read portb state into string
len += putString(dyna) ;
len += putConstString(";\n") ;

// add PORTD value (LEDs) to reply
len += putConstString("var PORTD=") ;
intToStr(PORTD, dyna) ; // convert read latd value (portd led's state) into string
len += putString(dyna) ;
len += putConstString(";\n") ;



// add HTTP requests counter to reply
intToStr(httpCounter, dyna) ; // convert httpCounter value into string
len += putConstString("var REQ=") ;
len += putString(dyna) ;
len += putConstString(";\n") ;

}
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
}
}
else if(getRequest[5] == 'r')
{
PORTD=0x00;
}
else if(getRequest[5] == 'a')
{
PORTD=0xFF;
}
else if(getRequest[5] == 'n')
{
PORTD=~PORTD;
}
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)
{
return 0 ; // back to the library with the length of the UDP reply
}

/*
* main entry
*/
void main()
{
ADCON1 = 0x00 ; // ADC convertors will be used

PORTA = 0 ;
TRISA = 0xff ; // set PORTA as input for ADC

PORTB = 0 ;
TRISB = 0xff ; // set PORTB as input for buttons

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

/*
* initialize ethernet board
* start 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) // endless loop
{
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
*/
}
}

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: EasyPic4 with PIC16F887 + ENC28J60 web server

#2 Post by slavisa.zlatanovic » 04 Apr 2011 13:23

Hi!

We strongly encourage users to switch to mikroPascal PRO version because non-PRO version is an ancestor of PRO compiler
and is no longer being developed.
Every new feature, improvement and bug fix will only affect new versions of PRO compilers.
If you're a registered user you'll receive PRO license key free of charge.
Also, see the Migration Document for more details:
Migration_document.PNG
Migration_document.PNG (15.87 KiB) Viewed 6390 times
Best regards
Slavisa

Post Reply

Return to “mikroPascal General”