本文介紹關于ARV單片機通過一塊芯片W5100上網模塊與網絡進行連接,一直以來對于嵌入式arduino怎么樣和網絡進行連接一直是我很好奇的問題,難得這幾天公司給我一個關于利用嵌入式讓AVR連接的應用,雖然對于英語水平還是很低下的我,沒辦法,為了興趣和工作,只好硬著頭皮看著外國一些關于AVR與網絡連接的論壇。其中收獲還是蠻大的,讓自己知道了一些關于網絡的知識和電路,特別是自己把一塊ENC28J60上網模塊的PCB順利畫出來進行生產,在畫這個板的時候也是費了一番功夫,根據原理圖,我覺得第12和第13引腳不應該是短路的,可是當PCB板拿到手的時候進行硬件測試,居然發現這兩個引腳連在了一起。百思不得其解,把PCB板和畫的圖進行對比是沒有錯的,后來懷疑是IC的問題,但是經過看數據手冊和IC不接在電路中的測試,沒有問題。最近對HR911105A網口進行測試,興奮地發現了問題是第3和第6是連在一起的。找出問題根源。
一直對于arduino這種開發平臺和獨特的編程風格的感興趣。特別是他具有一個實時和電腦進行通訊的系統平臺,長話短說,還是把硬件進行連接,以及代碼的編寫和調試,因為ardunio官方指代了一個關于Ethernet的庫,所以把它下載下來,加以研究。終于寫出了下面的一段代碼:
#include <SPI.h>
//Test Code:
/*
* Web Server
*
* A simple web server that shows the value of the analog input pins.
*/
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 48 };
Server server(80);
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == 'n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// output the value of each analog input pin
client.print("welcome to B2CQSHOP");
client.println("<br />");
client.print("//*************************************");
client.println("<br />");
client.print("");
client.println("<br />");
client.print("//*************************************");
client.println("<br />");
for (int i = 0; i < 6; i++) {
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("<br />");
}
break;
}
if (c == 'n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != 'r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
client.stop();
}
}
其實,寫這番代碼的時候也是費了一番周折的,因為參考網上一些關于ethernet的寫法,剛開始編譯的時候系統老是報錯。錯誤的結果是:
經過查資料,終于知道了平時我們所說的SPI是什么東西,原來是“串行外圍設備接口”,也就是網上很多LCD顯示,為什么只有6根線就可以控制顯示內容的東東。在官方網上找了好久,終于把到庫,下載下來以后調用進程序,終于可以順利的編譯。心里那個激動,呵呵。希望可以出現預期結果。
但是問題不是我所想的那么簡單。把自己程序中的IP地址輸到網站地址欄,回車。暈。居然提示:
結果出來了:/勝利