我想从DHCP服务器获取IP地址到PIC 18F4520设备,我使用mikroc SPI以太网库对PIC进行编程。我制作了一个代码,但它没有用。我想获取IP地址并将其显示在LCD上。任何人都可以帮我怎么做?
#include "__EthEnc28j60.h"
#include "__EthEnc28j60Private.h"
// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
/*// SD module connections
sbit Mmc_Chip_Select at LATC0_bit; // for writing to output pin always use latch
(PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISC0_bit;
// End SD module connections*/
//ENC28j60 connection
sbit SPI_Ethernet_CS at LATC1_bit;
sbit SPI_Ethernet_Rst at LATC0_bit;
sbit SPI_Ethernet_CS_Direction at TRISC1_bit;
sbit SPI_Ethernet_Rst_Direction at TRISC0_bit;
//End ENC28j60 connection
const char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: "; // HTTP header
const char httpMimeTypeHTML[] = "text/html\n\n"; // HTML MIME type
const char httpMimeTypeScript[] = "text/plain\n\n"; // TEXT MIME type
// default html page
char indexPage[] =
"<html><head><title>mikroElektronika</title></head><body>\
<h3 align=center>MikroElektronika Home Automatization System</h3>\
<form name=\"input\" action=\"/\" method=\"get\">\
<table align=center width=200 bgcolor=#4974E2 border=2><tr>\
<td align=center colspan=2><font size=4 color=white><b>Heat Control</b></font>\
</td></tr><tr><td align=center bgcolor=#4974E2><input name=\"tst1\" width=60 \
type=\"submit\" value=\"ON\"></td><td align=center bgcolor=#FFFF00>\
<input name=\"tst2\" type=\"submit\" value=\"OFF\"></td></tr></table>\
</form></body></html>";
// network parameters
char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f}; // my MAC address
char myIpAddr[4] = {0, 0, 0, 0}; // my IP address
unsigned char getRequest[20]; // HTTP request buffer
unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort,
unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags)
{
unsigned int len; // my reply length
if(localPort != 80) return(0); // I listen only to web request on port 80
// get 10 first bytes only of the request, the rest does not matter here
for(len = 0 ; len < 15 ; len++) getRequest[len] = SPI_Ethernet_getByte();
getRequest[len] = 0;
if(memcmp(getRequest, "GET /", 5)) return(0); // only GET method
if(!memcmp(getRequest+11, "ON", 2)) // do we have ON command
PORTB.F0 = 1; // set PORTB bit0
else
if(!memcmp(getRequest+11, "OFF", 3)) // do we have OFF command
PORTB.F0 = 0; // clear PORTB bit0
if (PORTB.F0)
{
memcpy(indexPage+340, "#FFFF00", 6); // highlight (yellow) ON
memcpy(indexPage+431, "#4974E2", 6); // clear OFF
}
else
{
memcpy(indexPage+340, "#4974E2", 6); // clear ON
memcpy(indexPage+431, "#FFFF00", 6); // highlight (yellow) OFF
}
len = SPI_Ethernet_putConstString(httpHeader); // HTTP header
len += SPI_Ethernet_putConstString(httpMimeTypeHTML); // with HTML MIME type
len += SPI_Ethernet_putString(indexPage); // HTML page first part
return len; // return to the library with the number of bytes to transmit
}
unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort,
unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags)
{
return 0; // back to the library with the length of the UDP reply
}
unsigned long i, size, j, k;
char filename[14] = "ASHAN.TXT"; // File names
int txt2,*pi;
unsigned short character[20];
unsigned short *contentBuffer;
void main(){
ADCON1 |= 0x0D; // Configure AN0 and AN1 pins as analog
CMCON |= 7; // coparators off
TRISA = 0xff ;
PORTA = 0 ;
PORTB = 0 ; //PORTB output
TRISB = 0 ;
PORTC = 0 ;
TRISC = 0b11011100 ; // set PORTC as input except for bits 0 (RESET) and 1 (CS)
Delay_ms(100);
//Initialize LCD
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF );
Lcd_Out(1, 1, "Status:");
Lcd_Cmd(_LCD_SECOND_ROW);
SPI1_Init();
SPI_Ethernet_Init(myMacAddr, myIpAddr,1);
SPI_Ethernet_initDHCP(5); // get network configuration from DHCP server, wait 5 sec for the response
memcpy(myIpAddr, Spi_Ethernet_getIpAddress(),4) ; // get assigned IP address
Lcd_Out_Cp(myIpAddr);
}
答案 0 :(得分:0)
首先想想我要做的就是运行Wireshark,看看你的DHCP发现是否有线,从那时起你就决定了解决问题的重点......