xhhuang 发表于 2014-6-13 08:39:53

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

本帖最后由 xhhuang 于 2014-6-13 15:51 编辑

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

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

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

void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=command & 0xf0;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(command & 0x0f)<<4;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=dat & 0xf0;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(dat & 0x0f)<<4;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0)    address = 0x80 + x;
else          address = 0xC0 + x;
LCD_Command_Write(address);
}

void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y );
LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)
{
    LCD_SET_XY( X, Y );    //设置地址
    while (*s)             //写字符串
    {
      LCD_Data_Write(*s);   
      s ++;
    }
}

void setup (void)
{
int i = 0;
for (i=6; i <= 12; i++)
   {
   pinMode(i,OUTPUT);
   }
delay(100);
LCD_Command_Write(0x28);//4线 2行 5x7
delay(50);
LCD_Command_Write(0x06);
delay(50);
LCD_Command_Write(0x0c);
delay(50);
LCD_Command_Write(0x80);
delay(50);
LCD_Command_Write(0x01);
delay(50);

}

void loop (void)
{
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(3,0,str1);//第1行,第4个地址起
   delay(50);
   LCD_Write_String(1,1,str2);//第2行,第2个地址起
   delay(5000);
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(0,0,str3);
   delay(50);
   LCD_Write_String(0,1,str4);
   delay(5000);

}

xhhuang 发表于 2014-6-13 08:45:19

#include <SPI.h>
#include <Ethernet.h>

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



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

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




void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=command & 0xf0;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(command & 0x0f)<<4;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=dat & 0xf0;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(dat & 0x0f)<<4;
for (i=DB; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0)    address = 0x80 + x;
else          address = 0xC0 + x;
LCD_Command_Write(address);
}

void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y );
LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)
{
    LCD_SET_XY( X, Y );    //设置地址
    while (*s)             //写字符串
    {
      LCD_Data_Write(*s);   
      s ++;
    }
}

void setup (void)
{
    Serial.begin(9600);
    if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
}

   Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP(), DEC);
    Serial.print(".");
}
Serial.println();


int i = 0;
for (i=6; i <= 12; i++)
   {
   pinMode(i,OUTPUT);
   }
delay(100);
LCD_Command_Write(0x28);//4线 2行 5x7
delay(50);
LCD_Command_Write(0x06);
delay(50);
LCD_Command_Write(0x0c);
delay(50);
LCD_Command_Write(0x80);
delay(50);
LCD_Command_Write(0x01);
delay(50);

// this check is only needed on the Leonardo:


// start the Ethernet connection:

// print your local IP address:


}

void loop (void)
{
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(3,0,str1);//第1行,第4个地址起
   delay(50);
   LCD_Write_String(1,1,str2);//第2行,第2个地址起
   delay(5000);

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

Serial.print(Ethernet.localIP(), DEC);

转换显示到LCD上。呵呵。。有做过这个吗?

caiwenping 发表于 2014-6-13 08:59:21

为什么不用LiquidCrystal库呢.....

xhhuang 发表于 2014-6-13 09:40:56

http://forum.arduino.cc/index.php?topic=110672.0

其有看到这个贴子,也看过官方的接线的图,
http://arduino.cc/en/uploads/Tutorial/LCD_bb.png
左下角的那个可变电阻,因为那块的问题,因为我没有那个东西,就试着用固定的电阻,但LCD就是不亮。也不想用那个东西,太大不方便我整合,
哪个大神能帮我修改一下那个LiquidCrystal接线图, 我是新手多谢了。

弘毅 发表于 2014-6-13 11:38:31

D13-D10管脚都不要占用,就可以,你这个是1602和网络扩展板管脚冲突了。

xhhuang 发表于 2014-6-13 15:50:58

本帖最后由 xhhuang 于 2014-7-16 13:42 编辑

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

页: [1]
查看完整版本: 将DHCP获取的网络IP地址打印显示到LCD