圣源电子制作

标题: arduino学习笔记 - ENC28J60 以太网模块实验 [打印本页]

作者: sydz    时间: 2012-4-28 23:21
标题: arduino学习笔记 - ENC28J60 以太网模块实验
转载自互联网!!!仅供学习
除了官方的W5100以太网模块,使用最广的就要数ENC28J60模块了。
此模块经过众多高手完善第三方库,已经和官方模块功能一样~
先看看本次使用的硬件,一块arduino uno,外加一个以太网扩展板。

(, 下载次数: 58)

(, 下载次数: 50)

如果大家不是使用的这种大板,而是使用的下图的小板,就需要自己接线。

(, 下载次数: 56)

上面各个口定义如下

(, 下载次数: 56)

如果是下图这种标了1,2,3的。1号口对应的是VCC。

(, 下载次数: 58)

与arduino控制板连接对应表如下。

(, 下载次数: 55)

首先需要进入IDE的libraries文件夹中,把官方的以太库文件夹Ethernet删掉,或者移出文件夹,因为ENC28J60的函数名称和官方的一模一样,如果同时放入,就会编译错误或者下载出错。无法正常使用。

(, 下载次数: 56)

然后把本次使用的ENC28J60库文件拷入libraries文件夹

(, 下载次数: 61)

打开IDE,在Examples中,打开ENC28J60的演示例子WebServer。

(, 下载次数: 57)

直接把代码从这里复制进去也可以。
  1. /*
  2. * Web Server
  3. *
  4. * A simple web server that shows the value of the analog input pins.
  5. */

  6. #include <Ethernet.h>

  7. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  8. byte ip[] = { 192, 168, 1, 177 };

  9. Server server(80);

  10. void setup()
  11. {
  12.   Ethernet.begin(mac, ip);
  13.   server.begin();
  14. }

  15. void loop()
  16. {
  17.   Client client = server.available();
  18.   if (client) {
  19.     // an http request ends with a blank line
  20.     boolean current_line_is_blank = true;
  21.     while (client.connected()) {
  22.       if (client.available()) {
  23.         char c = client.read();
  24.         // if we've gotten to the end of the line (received a newline
  25.         // character) and the line is blank, the http request has ended,
  26.         // so we can send a reply
  27.         if (c == '\n' && current_line_is_blank) {
  28.           // send a standard http response header
  29.           client.println("HTTP/1.1 200 OK");
  30.           client.println("Content-Type: text/html");
  31.           client.println();
  32.          
  33.           // output the value of each analog input pin
  34.           for (int i = 0; i < 6; i++) {
  35.             client.print("analog input ");
  36.             client.print(i);
  37.             client.print(" is ");
  38.             client.print(analogRead(i));
  39.             client.println("
  40. ");
  41.           }
  42.           break;
  43.         }
  44.         if (c == '\n') {
  45.           // we're starting a new line
  46.           current_line_is_blank = true;
  47.         } else if (c != '\r') {
  48.           // we've gotten a character on the current line
  49.           current_line_is_blank = false;
  50.         }
  51.       }
  52.     }
  53.     // give the web browser time to receive the data
  54.     delay(1);
  55.     client.stop();
  56.   }
  57. }
复制代码
要注意IP地址要使用和自己网络所对应的,且不能和其他IP冲突。我这次使用的是192.168.1.177,在我的局域网中,这个地址是没有使用的。

(, 下载次数: 57)

这样。。就可以在浏览器中浏览了,网页显示的是模拟口的读数。

(, 下载次数: 55)

下面的附件就是最重要的,可以使用且功能齐全的ENC28J60的arduino库文件。
ENC28J60.rar (44.55 KB, 下载次数: 245)






欢迎光临 圣源电子制作 (http://bbs.syyyd.com/) Powered by Discuz! X3.4