0% found this document useful (0 votes)
66 views5 pages

Codigo Esperanza 2

The document describes an example code for using an ENC28J60 Ethernet controller chip with a PIC microcontroller to create an HTTP web server. The code initializes the Ethernet controller, handles ARP, ICMP, and HTTP requests, and serves a basic dynamic web page that displays sensor values read from the microcontroller's ports and ADC. When requests are received, it either returns the web page or toggles an output if a specific URL is requested.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views5 pages

Codigo Esperanza 2

The document describes an example code for using an ENC28J60 Ethernet controller chip with a PIC microcontroller to create an HTTP web server. The code initializes the Ethernet controller, handles ARP, ICMP, and HTTP requests, and serves a basic dynamic web page that displays sensor values read from the microcontroller's ports and ADC. When requests are received, it either returns the web page or toggles an output if a specific URL is requested.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

F:\Mis Documentos\Downloads\serial_ethernet_board_examples\Serial Ethernet Board Example

/*
* 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=/https/www.scribd.com/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

F:\Mis Documentos\Downloads\serial_ethernet_board_examples\Serial Ethernet Board Example


<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=/https/www.scribd.com/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] = {192, 168, 20, 60} ;
unsigned char
getRequest[15] ;
unsigned char
dyna[31] ;
unsigned long
httpCounter = 0 ;
/*******************************************
* 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)
2

//
//
//
//
//

my MAC addres
my IP address
HTTP request
buffer for dy
counter of HT

F:\Mis Documentos\Downloads\serial_ethernet_board_examples\Serial Ethernet Board Example


{
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 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) ;
}

// I listen only to web

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

// only GET method is su

httpCounter++ ;

// one more request done

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 read adc chan

// convert read adc chan

// convert read portb st

F:\Mis Documentos\Downloads\serial_ethernet_board_examples\Serial Ethernet Board Example


len += putConstString("var PORTD=") ;
intToStr(PORTD, dyna) ;
// convert read latd val
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')
{
unsigned char
bitMask = 0 ;
if(isdigit(getRequest[6]))
{
bitMask = getRequest[6] - '0' ;
bitMask = 1 << bitMask ;
PORTD ^= bitMask ;
}
}
if(len == 0)
{
len =
len +=
len +=
len +=
}

// convert httpCounter v

// if request path name


// for bit mask

// if 0 <= bit number <=

// convert ASCII to inte


// create bit mask
// toggle PORTD with xor

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

// return to the library

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

// 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 :
4

F:\Mis Documentos\Downloads\serial_ethernet_board_examples\Serial Ethernet Board Example


* reset bit on RC0
* CS bit on RC1
* my MAC & IP address
* full duplex
*/
SPI_init();
SPI_Ethernet_Init(&PORTC, 0, &PORTC, 1, myMacAddr, myIpAddr, SPI_Ethernet_FULLDU
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
*/
}

You might also like