极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9861|回复: 5

将DHCP获取的网络IP地址打印显示到LCD

[复制链接]
发表于 2014-6-13 08:39:53 | 显示全部楼层 |阅读模式
本帖最后由 xhhuang 于 2014-6-13 15:51 编辑

想将DHCP获取的地址打印到LCD,不知道怎么处理。

下面是参照 cd 1602 接线参照弘毅的教程 ,接线打印显示LCD,均没有问题。不过一旦上加DHCP获取IP地址,LCD就无法显示了,谁有这方面的经验,跪谢!!

  1. int LCD1602_RS=12;   
  2. int LCD1602_RW=11;   
  3. int LCD1602_EN=10;   
  4. int DB[] = { 6, 7, 8, 9};
  5. char str1[]="Welcome to";
  6. char str2[]="geek-workshop";
  7. char str3[]="this is the";
  8. char str4[]="4-bit interface";

  9. void LCD_Command_Write(int command)
  10. {
  11. int i,temp;
  12. digitalWrite( LCD1602_RS,LOW);
  13. digitalWrite( LCD1602_RW,LOW);
  14. digitalWrite( LCD1602_EN,LOW);

  15. temp=command & 0xf0;
  16. for (i=DB[0]; i <= 9; i++)
  17. {
  18.    digitalWrite(i,temp & 0x80);
  19.    temp <<= 1;
  20. }

  21. digitalWrite( LCD1602_EN,HIGH);
  22. delayMicroseconds(1);
  23. digitalWrite( LCD1602_EN,LOW);

  24. temp=(command & 0x0f)<<4;
  25. for (i=DB[0]; i <= 9; i++)
  26. {
  27.    digitalWrite(i,temp & 0x80);
  28.    temp <<= 1;
  29. }

  30. digitalWrite( LCD1602_EN,HIGH);
  31. delayMicroseconds(1);
  32. digitalWrite( LCD1602_EN,LOW);
  33. }

  34. void LCD_Data_Write(int dat)
  35. {
  36. int i=0,temp;
  37. digitalWrite( LCD1602_RS,HIGH);
  38. digitalWrite( LCD1602_RW,LOW);
  39. digitalWrite( LCD1602_EN,LOW);

  40. temp=dat & 0xf0;
  41. for (i=DB[0]; i <= 9; i++)
  42. {
  43.    digitalWrite(i,temp & 0x80);
  44.    temp <<= 1;
  45. }

  46. digitalWrite( LCD1602_EN,HIGH);
  47. delayMicroseconds(1);
  48. digitalWrite( LCD1602_EN,LOW);

  49. temp=(dat & 0x0f)<<4;
  50. for (i=DB[0]; i <= 9; i++)
  51. {
  52.    digitalWrite(i,temp & 0x80);
  53.    temp <<= 1;
  54. }

  55. digitalWrite( LCD1602_EN,HIGH);
  56. delayMicroseconds(1);
  57. digitalWrite( LCD1602_EN,LOW);
  58. }

  59. void LCD_SET_XY( int x, int y )
  60. {
  61.   int address;
  62.   if (y ==0)    address = 0x80 + x;
  63.   else          address = 0xC0 + x;
  64.   LCD_Command_Write(address);
  65. }

  66. void LCD_Write_Char( int x,int y,int dat)
  67. {
  68.   LCD_SET_XY( x, y );
  69.   LCD_Data_Write(dat);
  70. }

  71. void LCD_Write_String(int X,int Y,char *s)
  72. {
  73.     LCD_SET_XY( X, Y );    //设置地址
  74.     while (*s)             //写字符串
  75.     {
  76.       LCD_Data_Write(*s);   
  77.       s ++;
  78.     }
  79. }

  80. void setup (void)
  81. {
  82.   int i = 0;
  83.   for (i=6; i <= 12; i++)
  84.    {
  85.      pinMode(i,OUTPUT);
  86.    }
  87.   delay(100);
  88.   LCD_Command_Write(0x28);//4线 2行 5x7
  89.   delay(50);
  90.   LCD_Command_Write(0x06);
  91.   delay(50);
  92.   LCD_Command_Write(0x0c);
  93.   delay(50);
  94.   LCD_Command_Write(0x80);
  95.   delay(50);
  96.   LCD_Command_Write(0x01);
  97.   delay(50);

  98. }

  99. void loop (void)
  100. {
  101.    LCD_Command_Write(0x01);
  102.    delay(50);
  103.    LCD_Write_String(3,0,str1);//第1行,第4个地址起
  104.    delay(50);
  105.    LCD_Write_String(1,1,str2);//第2行,第2个地址起
  106.    delay(5000);
  107.    LCD_Command_Write(0x01);
  108.    delay(50);
  109.    LCD_Write_String(0,0,str3);
  110.    delay(50);
  111.    LCD_Write_String(0,1,str4);
  112.    delay(5000);

  113. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-6-13 08:45:19 | 显示全部楼层
  1. #include <SPI.h>
  2. #include <Ethernet.h>

  3. int LCD1602_RS=12;   
  4. int LCD1602_RW=11;   
  5. int LCD1602_EN=10;   
  6. int DB[] = { 6, 7, 8, 9};
  7. char str1[]="Welcome to";
  8. char str2[]="geek-workshop";



  9. byte mac[] = {  
  10.   0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

  11. // Initialize the Ethernet client library
  12. // with the IP address and port of the server
  13. // that you want to connect to (port 80 is default for HTTP):
  14. EthernetClient client;




  15. void LCD_Command_Write(int command)
  16. {
  17. int i,temp;
  18. digitalWrite( LCD1602_RS,LOW);
  19. digitalWrite( LCD1602_RW,LOW);
  20. digitalWrite( LCD1602_EN,LOW);

  21. temp=command & 0xf0;
  22. for (i=DB[0]; i <= 9; i++)
  23. {
  24.    digitalWrite(i,temp & 0x80);
  25.    temp <<= 1;
  26. }

  27. digitalWrite( LCD1602_EN,HIGH);
  28. delayMicroseconds(1);
  29. digitalWrite( LCD1602_EN,LOW);

  30. temp=(command & 0x0f)<<4;
  31. for (i=DB[0]; i <= 9; i++)
  32. {
  33.    digitalWrite(i,temp & 0x80);
  34.    temp <<= 1;
  35. }

  36. digitalWrite( LCD1602_EN,HIGH);
  37. delayMicroseconds(1);
  38. digitalWrite( LCD1602_EN,LOW);
  39. }

  40. void LCD_Data_Write(int dat)
  41. {
  42. int i=0,temp;
  43. digitalWrite( LCD1602_RS,HIGH);
  44. digitalWrite( LCD1602_RW,LOW);
  45. digitalWrite( LCD1602_EN,LOW);

  46. temp=dat & 0xf0;
  47. for (i=DB[0]; i <= 9; i++)
  48. {
  49.    digitalWrite(i,temp & 0x80);
  50.    temp <<= 1;
  51. }

  52. digitalWrite( LCD1602_EN,HIGH);
  53. delayMicroseconds(1);
  54. digitalWrite( LCD1602_EN,LOW);

  55. temp=(dat & 0x0f)<<4;
  56. for (i=DB[0]; i <= 9; i++)
  57. {
  58.    digitalWrite(i,temp & 0x80);
  59.    temp <<= 1;
  60. }

  61. digitalWrite( LCD1602_EN,HIGH);
  62. delayMicroseconds(1);
  63. digitalWrite( LCD1602_EN,LOW);
  64. }

  65. void LCD_SET_XY( int x, int y )
  66. {
  67.   int address;
  68.   if (y ==0)    address = 0x80 + x;
  69.   else          address = 0xC0 + x;
  70.   LCD_Command_Write(address);
  71. }

  72. void LCD_Write_Char( int x,int y,int dat)
  73. {
  74.   LCD_SET_XY( x, y );
  75.   LCD_Data_Write(dat);
  76. }

  77. void LCD_Write_String(int X,int Y,char *s)
  78. {
  79.     LCD_SET_XY( X, Y );    //设置地址
  80.     while (*s)             //写字符串
  81.     {
  82.       LCD_Data_Write(*s);   
  83.       s ++;
  84.     }
  85. }

  86. void setup (void)
  87. {
  88.     Serial.begin(9600);
  89.     if (Ethernet.begin(mac) == 0) {
  90.     Serial.println("Failed to configure Ethernet using DHCP");
  91.     // no point in carrying on, so do nothing forevermore:
  92.     for(;;)
  93.       ;
  94.   }
  95.   
  96.      Serial.print("My IP address: ");
  97.   for (byte thisByte = 0; thisByte < 4; thisByte++) {
  98.     // print the value of each byte of the IP address:
  99.     Serial.print(Ethernet.localIP()[thisByte], DEC);
  100.     Serial.print(".");
  101.   }
  102.   Serial.println();
  103.   
  104.   
  105.   int i = 0;
  106.   for (i=6; i <= 12; i++)
  107.    {
  108.      pinMode(i,OUTPUT);
  109.    }
  110.   delay(100);
  111.   LCD_Command_Write(0x28);//4线 2行 5x7
  112.   delay(50);
  113.   LCD_Command_Write(0x06);
  114.   delay(50);
  115.   LCD_Command_Write(0x0c);
  116.   delay(50);
  117.   LCD_Command_Write(0x80);
  118.   delay(50);
  119.   LCD_Command_Write(0x01);
  120.   delay(50);

  121.   // this check is only needed on the Leonardo:


  122.   // start the Ethernet connection:

  123.   // print your local IP address:


  124. }

  125. void loop (void)
  126. {
  127.    LCD_Command_Write(0x01);
  128.    delay(50);
  129.    LCD_Write_String(3,0,str1);//第1行,第4个地址起
  130.    delay(50);
  131.    LCD_Write_String(1,1,str2);//第2行,第2个地址起
  132.    delay(5000);

  133. }
复制代码
这是加了DHCP获取的代码。好像就不太行了,不过DHCP地址能获取到,我能从COM monitor中看到地址。不过老实讲,即使LCD,能显示,我也不知道如何将

Serial.print(Ethernet.localIP()[thisByte], DEC);

转换显示到LCD上。呵呵。。有做过这个吗?
回复 支持 反对

使用道具 举报

发表于 2014-6-13 08:59:21 | 显示全部楼层
为什么不用LiquidCrystal库呢.....
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-13 09:40:56 | 显示全部楼层
http://forum.arduino.cc/index.php?topic=110672.0

其有看到这个贴子,也看过官方的接线的图,

左下角的那个可变电阻,因为那块的问题,因为我没有那个东西,就试着用固定的电阻,但LCD就是不亮。也不想用那个东西,太大不方便我整合,
哪个大神能帮我修改一下那个LiquidCrystal接线图, 我是新手多谢了。
回复 支持 反对

使用道具 举报

发表于 2014-6-13 11:38:31 | 显示全部楼层
D13-D10管脚都不要占用,就可以,你这个是1602和网络扩展板管脚冲突了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-13 15:50:58 | 显示全部楼层
本帖最后由 xhhuang 于 2014-7-16 13:42 编辑

嗯,将11,12移动6,7问题就解决了,

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-23 07:49 , Processed in 0.048174 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表