arduino教程系列26-ENC28J60以太网模块实验 以太网

这篇学习笔记写的东西版本有点老了,最新版可以参考这篇帖子:

http://www.geek-workshop.com/thread-2049-1-1.html



除了官方的W5100以太网模块,使用最广的就要数ENC28J60模块了。

此模块经过众多高手完善第三方库,已经和官方模块功能一样~

先看看本次使用的硬件,一块arduino uno,外加一个以太网扩展板。


IMGP5796_调整大小.jpg

2011-11-16 17:07 上传
(142.57 KB)




IMGP5804_调整大小.jpg

2011-11-16 17:07 上传
(147.45 KB)





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


1.jpg

2011-11-16 17:20 上传
(54.85 KB)





上面各个口定义如下


2.jpg

2011-11-16 17:20 上传
(63.23 KB)





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


3.jpg

2011-11-16 17:20 上传
(42.77 KB)





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


2011-11-16 17-14-07.jpg

2011-11-16 17:20 上传
(33.35 KB)





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


2011-11-16 16-38-56.jpg

2011-11-16 17:07 上传
(65.15 KB)





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


2011-11-16 16-10-18.jpg

2011-11-16 17:07 上传
(131.05 KB)





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


2011-11-16 16-11-42.jpg

2011-11-16 17:07 上传
(116.42 KB)





直接把代码从这里复制进去也可以。ARDUINO 代码复制打印

/*
 * 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, 1, 177 };
 
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
          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;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

要注意IP地址要使用和自己网络所对应的,且不能和其他IP冲突。我这次使用的是192.168.1.177,在我的局域网中,这个地址是没有使用的。


2011-11-16 16-45-40.jpg

2011-11-16 17:07 上传
(76.33 KB)





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


2011-11-16 17-04-20.jpg

2011-11-16 17:07 上传
(59.26 KB)





下面的附件就是最重要的,可以使用且功能齐全的ENC28J60的arduino库文件。
ENC28J60.rar (44.55 KB) 网盘下载http://pan.baidu.com/s/1dExdYV3


via - 极客工坊

标签: Arduino教程