// duplex config flags
#define Spi_Ethernet_HALFDUPLEX 0x00 // half duplex
#define Spi_Ethernet_FULLDUPLEX 0x01 // full duplex
// mE ehternet NIC pinout
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;
// end ethernet NIC definitions
// Lcd pinout settings
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
// Pin direction
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
const unsigned char httpHeader[] = "HTTP/1.1 200 OK Content-type: " ; // HTTP header
//const unsigned char httpMimeTypeHTML[] = "text/html" ; // HTML MIME type
const unsigned char httpMimeTypeScript[] = "text/plain" ; // TEXT MIME type
unsigned char httpMethod[] = "GET /";
unsigned char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f} ; // my MAC address
unsigned char myIpAddr[4] = {192, 168, 30, 50} ; // my IP address 192.168.30.50
unsigned char getRequest[15] ; // HTTP request buffer
unsigned char dyna[30] ; // buffer for dynamic response
unsigned long httpCounter = 0 ; // counter of HTTP requests
unsigned int putConstString(const char *s)
{
unsigned int ctr = 0 ;
while(*s)
{
Spi_Ethernet_putByte(*s++) ;
ctr++ ;
}
return(ctr) ;
}
//#define putConstString SPI_Ethernet_putConstString
unsigned int putString(char *s)
{
unsigned int ctr = 0 ;
while(*s)
{
Spi_Ethernet_putByte(*s++) ;
ctr++ ;
}
return(ctr) ;
}
//#define putString SPI_Ethernet_putString
unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, char *canClose)
{
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
httpCounter++ ; // one more request done
len=0;
len = putConstString(httpHeader) ; // HTTP header
len += putConstString(httpMimeTypeScript) ; // with text MIME type
// add Hello World to reply
len += putConstString("Hello World") ;
// return to the library with the number of bytes to transmit
return(len);
}
unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength)
{
unsigned int len ; // my reply length
// reply is made of the remote host IP address in human readable format
ByteToStr(remoteHost[0], dyna) ; // first IP address byte
dyna[3] = '.' ;
ByteToStr(remoteHost[1], dyna + 4) ; // second
dyna[7] = '.' ;
ByteToStr(remoteHost[2], dyna + 8) ; // third
dyna[11] = '.' ;
ByteToStr(remoteHost[3], dyna + 12) ; // fourth
dyna[15] = ':' ; // add separator
// then remote host port number
WordToStr(remotePort, dyna + 16) ;
dyna[21] = '[' ;
WordToStr(destPort, dyna + 22) ;
dyna[27] = ']' ;
dyna[28] = 0 ;
// the total length of the request is the length
// of the dynamic string plus the text of the request
len = 28 + reqLength;
// puts the dynamic string into the transmit buffer
SPI_Ethernet_putBytes(dyna, 28) ;
// then puts the request string converted into upper char into the transmit buffer
while(reqLength--)
{
SPI_Ethernet_putByte(toupper(SPI_Ethernet_getByte())) ;
}
return(len) ; // back to the library with the length of the UDP reply
}
void main(){
TRISD=0x00;
PORTD=0xAA;
SPI1_Init();
SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;
//Initialize LCD
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF );
Lcd_Out(1, 1, "IP");
Lcd_Cmd(_LCD_SECOND_ROW);
while(1){
SPI_Ethernet_doPacket();
}
}