Академический Документы
Профессиональный Документы
Культура Документы
/*
* 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 pathna
*
/
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
*
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 SP
*
if you try lower PIC clock speed, don't be surprised if the board hang or miss s
*
tested with PIC16F877A@10Mhz on EasyPIC3 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
*
the INT and WOL signals from the ENC are not used
*
* Test configuration:
MCU:
PIC16F877A
Dev.Board:
EasyPIC3
Oscillator:
HS, 10.000MHz
Ext. Modules:
mE Serial Ethernet board
SW:
mikroC v6.2.0.0.
* NOTES:
None.
*/
#define SPI_Ethernet_HALFDUPLEX
#define SPI_Ethernet_FULLDUPLEX
0
1
/************************************************************
* 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 typ
const unsigned char httpMimeTypeScript[] = "text/plain\n\n" ;
// TEXT MIME typ
unsigned char httpMethod[] = "GET /";
// supported htt
/*
* 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>\
1
//
//
//
//
//
my MAC addres
my IP address
HTTP request
buffer for dy
counter of HT
/*
* 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 tr
*
* 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
len = 0 ;
// my reply length
unsigned int
i ;
// general purpose integ
if(localPort != 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))
{
return(0) ;
}
httpCounter++ ;
if(getRequest[5] == 's')
// if request path name
{
// the text string replied by this request can be interpreted as javascr
// by browsers
len = putConstString(httpHeader) ;
len += putConstString(httpMimeTypeScript) ;
// 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 PORTB value (buttons) to reply
len += putConstString("var PORTB=") ;
intToStr(PORTB, dyna) ;
len += putString(dyna) ;
len += putConstString(";") ;
// add PORTD value (LEDs) to reply
3
// HTTP header
// with text MIME type
// convert httpCounter v
// what do to by default
putConstString(httpHeader) ;
putConstString(httpMimeTypeHTML) ;
putConstString(indexPage) ;
putConstString(indexPage2) ;
return(len) ;
}
//
//
//
//
HTTP
with
HTML
HTML
header
HTML MIME type
page first part
page second part
/*
* 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 tra
*
* 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,
{
return 0 ;
// back to the library with the length of the UD
}
/*
* main entry
*/
void
main()
{
ADCON1 = 0x00 ;
PORTA = 0 ;
TRISA = 0xff ;
PORTB = 0 ;
TRISB = 0xff ;
PORTD = 0 ;
TRISD = 0 ;
/*
* initialize ethernet board
* start ENC28J60 with :
4
// 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
*/
}