Difference between revisions of "Matrix - I2C LCD1602 Keypad"

From FriendlyELEC WiKi
Jump to: navigation, search
(Download Matrix Source Code)
Line 68: Line 68:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples.
 
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples.
 +
 +
==与NanoPi 2连接使用==
 +
===硬件连接===
 +
参考下图连接模块Matrix-I2C_LCD1602_Keypad和NanoPi 2:<br>
 +
[[File:matrix-i2c_lcd1602_keypad_nanopi 2.jpg|frameless|600px|matrix-i2c_lcd1602_keypad_nanopi 2]]
 +
 +
连接说明:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-I2C_LCD1602_Keypad || NanoPi 2     
 +
|-
 +
|SDA    || Pin3
 +
|-
 +
|SCL    || Pin5
 +
|-
 +
|5V    || Pin4
 +
|-
 +
|GND    || Pin6
 +
|}
 +
 +
===编译测试程序===
 +
进入Matrix代码仓库,切换到nanopi2分支
 +
<syntaxhighlight lang="bash">
 +
$ cd matrix
 +
$ git checkout nanopi2
 +
</syntaxhighlight>
 +
 +
编译Matrix配件代码
 +
<syntaxhighlight lang="bash">
 +
$ make CROSS_COMPILE=arm-linux- clean
 +
$ make CROSS_COMPILE=arm-linux-
 +
$ make CROSS_COMPILE=arm-linux- install
 +
</syntaxhighlight>
 +
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi 2配套的arm-linux-gcc-4.9.3。<br>
 +
编译成功后库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-I2C_LCD1602_Keypad对应的测试程序为matrix-lcd1602_keypad。<br>
 +
 +
===运行测试程序===
 +
将带有Debian系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。<br>
 +
假设rootfs分区的挂载路径为/media/rootfs,执行以下命令将Matrix的硬件驱动、库文件和测试程序拷贝到NanoPi 2的文件系统上。<br>
 +
<syntaxhighlight lang="bash">
 +
$ cp modules /media/rootfs/ -r
 +
$ cp install/lib/* /media/rootfs/lib/ -d
 +
$ cp install/usr/bin/* /media/rootfs/usr/bin/
 +
</syntaxhighlight>
 +
 +
将SD卡重新插入NanoPi 2,上电启动,在Debian的shell终端中执行以下命令运行模块Matrix-I2C_LCD1602_Keypad的测试程序。<br>
 +
<syntaxhighlight lang="bash">
 +
$ matrix-lcd1602_keypad
 +
</syntaxhighlight>
 +
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。<br>
 +
运行效果如下:<br>
 +
[[File:matrix-lcd1602_keypad_result.png|frameless|600px|matrix-lcd1602_keypad_result]] <br>
 +
在LCD上默认会显示当前时间,按下F1键会显示IP地址,F2、F3、F4键的功能则由用户自行定义,按下F5键重新显示时间。
 +
 +
===代码展示===
 +
<syntaxhighlight lang="c">
 +
int main(int argc, char ** argv)
 +
{
 +
    int devFD;
 +
    int keyValue = 0;
 +
    int lastKeyValue = -1;
 +
    int showDefault = 1;
 +
    int needClear = 1;
 +
    time_t lt;
 +
    char curTime[TIME_STR_BUFSIZE];
 +
    char preTime[TIME_STR_BUFSIZE];
 +
 +
    if ((devFD = LCD1602KeyInit()) == -1) {
 +
        printf("Fail to init LCD1602\n");
 +
        return -1;
 +
    }
 +
    LCD1602KeyClear(devFD);
 +
    printf("waiting key press...\n");
 +
 +
    while (1) {
 +
        keyValue = LCD1602GetKey(devFD);
 +
        if (keyValue != lastKeyValue) {
 +
            lastKeyValue = keyValue;
 +
        } else if (showDefault != 1){
 +
            usleep(1000);
 +
            continue;
 +
        }
 +
        switch (keyValue) {
 +
        // F1
 +
        case 0x1e:
 +
            showDefault = 0;
 +
            LCD1602KeyClear(devFD);
 +
            LCD1602KeyDispStr(devFD, 0, 0, "#F1-IP address");
 +
            if (showIP(devFD, "eth0")) {
 +
                if (showIP(devFD, "wlan0")) {
 +
                    if (showIP(devFD, "usb0")) {
 +
                        showIP(devFD, "lo");
 +
                    }
 +
                }
 +
            }
 +
            break;
 +
            // F2   
 +
        case 0x1d:
 +
            showDefault = 0;
 +
            LCD1602KeyClear(devFD);
 +
            LCD1602KeyDispStr(devFD, 0, 0, "#F2-Your favor");
 +
            LCD1602KeyDispStr(devFD, 0, 1, "Come add it");
 +
            break;
 +
            // F3   
 +
        case 0x1b:
 +
            showDefault = 0;
 +
            LCD1602KeyClear(devFD);
 +
            LCD1602KeyDispStr(devFD, 0, 0, "#F3-Your idea");
 +
            LCD1602KeyDispStr(devFD, 0, 1, "Come show it");
 +
            break;
 +
            // F4
 +
        case 0x17:
 +
            showDefault = 0;
 +
            LCD1602KeyClear(devFD);
 +
            LCD1602KeyDispStr(devFD, 0, 0, "#F4-About");
 +
            LCD1602KeyDispStr(devFD, 0, 1, "by FriendlyARM");
 +
            break;
 +
            // F5
 +
        case 0xf:
 +
            showDefault = 1;
 +
            break;
 +
        }
 +
        if (showDefault == 1) {
 +
            if (needClear) {
 +
                LCD1602KeyClear(devFD);
 +
                LCD1602KeyDispStr(devFD, 0, 0, "#Default");
 +
                needClear = 0;
 +
            }
 +
            memset(curTime, 0, TIME_STR_BUFSIZE);
 +
            lt = time(NULL);
 +
            strncpy(curTime, ctime(&lt) + 11, 8);
 +
            if(strcmp(curTime, preTime)) {
 +
                printf("time:%s\n", curTime);
 +
                LCD1602KeyDispStr(devFD, 0, 1, curTime);
 +
            }
 +
            memset(preTime, 0, TIME_STR_BUFSIZE);
 +
            strcpy(preTime, curTime);
 +
        } else {
 +
            needClear = 1;
 +
            usleep(1000);
 +
        }
 +
    }   
 +
    printf("quit reading key press\n");
 +
    LCD1602KeyDeInit(devFD);
 +
    return 0;
 +
}
 +
</syntaxhighlight>
 +
  
 
==Connect to NanoPi==
 
==Connect to NanoPi==

Revision as of 09:39, 31 December 2015

查看中文

1 Introduction

I2C LCD1602 Keypad
I2C LCD1602 Keypad
  • The Matrix-I2C_LCD1602_Keypad module is an easy-to-use display module based on the LCD1602.
  • This module integrates the LCD1602 and the MCP23017 module. It has five programmable keys which allow users to control the LCD1602's display and external devices connected to the module.
  • It has a potentiometer to adjust the LCD's back light.
  • The LCD1602 can display up to 16x2 characters. It has a parallel interface. You can control it via two I2C channels.
  • The MCP23017 has a 16-bit remote bidirectional I/O port, high speed I2C interface, three hardware address pins which make it able to connect up to eight devices. It communicates through I2C interface and converts input to parallel signals to the LCD1602.

2 Features

  • I2C interface, LCD display and backlight control
  • 2.54mm spacing pin
  • I2C PCB dimension (mm): 57 x 80
  • LCD1602 PCB dimension (mm): 36 x 80

IIC扩展模块PCB LCD1602 PCB

  • Pin Description:
Pin Description
IRQ BUTTON IRQ
SDA I2C SDA
SCL I2C SCL
5V Supply Voltage 5V
GND Ground

3 Basic Device Operation

3.1 MCP23017

  • The MCP23017 has a 16-bit remote bidirectional I/O port, high speed I2C interface. These I/O can be configured to inputs and outputs. Each input/output data is kept in its register. It has three hardware address pins which make it able to connect up to eight devices. In addition it has two configurable interrupt pins INTA and INTB.
  • The I2C write operation includes the control byte and register address sequence. This sequence is followed by eight bits of data from the master and an Acknowledge (ACK) from the MCP23017. The operation is ended with a Stop (P) or Restart (SR) condition being generated by the master.
  • The I2C Read operation includes the control byte sequence. This sequence is followed by another control byte (including the Start condition and ACK) with the R/W bit set (R/W = 1). The MCP23017 then transmits the data contained in the addressed register. The sequence is ended with the master generating a Stop or Restart condition.
  • The I2C sequential operation (Write or Read) works this way: instead of transmitting a Stop or Restart condition after a data transfer, the master clocks the next byte pointed to by the address pointer after each data transfer. The sequence is ended with the master sending a Stop or Restart condition.

3.2 LCD1602

  • The connection diagram between the PCF8574 module and the LCD module is shown below:

1602

  • RS is instruction/data register selection. RW is read/write selection. E is enable signal(edge triggering). BL is back light control. D4-D7 are data bits.
  • The LCD module is controlled by four data bits through which we can send instructions to control its state. Because it has eight instruction/data bits (DB7 - DB0) when writing each instruction/data we need to write the most significant four bits DB7 - DB4 first and then the least significant four bits DB3 - DB0.
  • Note: the LCD module has 192 most commonly used characters stored in CGROM. When we write a common character e.g "A" it will directly display "A". In addition it can store up to eight user defined characters in RAM called CGRAM.

4 Download Matrix Source Code

All the matrix modules' code samples are open source. They are maintained on GitHub - https://github.com/friendlyarm/matrix.git
Each branch in this hub contains the matrix modules' code samples for a board that the matrix modules can work with.

  • The nanopi branch contains the matrix modules' code samples for the NanoPi
  • The nanopi2 branch contains the matrix modules' code samples for the NanoPi 2
  • the tiny4412 branch contains the matrix modules' code samples for the Tiny4412
  • the raspberrypi branch contains the matrix modules' code samples for the RaspberryPi

Please follow the steps below to get the source code:
Install the git utility on a PC running Ubuntu14.04

$ sudo apt-get install git

Clone the matrix code from GitHub

$ git clone https://github.com/friendlyarm/matrix.git

If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples.

5 与NanoPi 2连接使用

5.1 硬件连接

参考下图连接模块Matrix-I2C_LCD1602_Keypad和NanoPi 2:
matrix-i2c_lcd1602_keypad_nanopi 2

连接说明:

Matrix-I2C_LCD1602_Keypad NanoPi 2
SDA Pin3
SCL Pin5
5V Pin4
GND Pin6

5.2 编译测试程序

进入Matrix代码仓库,切换到nanopi2分支

$ cd matrix
$ git checkout nanopi2

编译Matrix配件代码

$ make CROSS_COMPILE=arm-linux- clean
$ make CROSS_COMPILE=arm-linux-
$ make CROSS_COMPILE=arm-linux- install

注意:请确保你的主机PC当前使用的交叉编译器为NanoPi 2配套的arm-linux-gcc-4.9.3。
编译成功后库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-I2C_LCD1602_Keypad对应的测试程序为matrix-lcd1602_keypad。

5.3 运行测试程序

将带有Debian系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。
假设rootfs分区的挂载路径为/media/rootfs,执行以下命令将Matrix的硬件驱动、库文件和测试程序拷贝到NanoPi 2的文件系统上。

$ cp modules /media/rootfs/ -r
$ cp install/lib/* /media/rootfs/lib/ -d
$ cp install/usr/bin/* /media/rootfs/usr/bin/

将SD卡重新插入NanoPi 2,上电启动,在Debian的shell终端中执行以下命令运行模块Matrix-I2C_LCD1602_Keypad的测试程序。

$ matrix-lcd1602_keypad

注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
运行效果如下:
matrix-lcd1602_keypad_result
在LCD上默认会显示当前时间,按下F1键会显示IP地址,F2、F3、F4键的功能则由用户自行定义,按下F5键重新显示时间。

5.4 代码展示

int main(int argc, char ** argv)
{
    int devFD;
    int keyValue = 0;
    int lastKeyValue = -1;
    int showDefault = 1;
    int needClear = 1;
    time_t lt;
    char curTime[TIME_STR_BUFSIZE];
    char preTime[TIME_STR_BUFSIZE];
 
    if ((devFD = LCD1602KeyInit()) == -1) {
        printf("Fail to init LCD1602\n");
        return -1;
    }
    LCD1602KeyClear(devFD);
    printf("waiting key press...\n");
 
    while (1) {
        keyValue = LCD1602GetKey(devFD);
        if (keyValue != lastKeyValue) {
            lastKeyValue = keyValue;
        } else if (showDefault != 1){
            usleep(1000);
            continue;
        }
        switch (keyValue) {
        // F1
        case 0x1e:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F1-IP address");
            if (showIP(devFD, "eth0")) {
                if (showIP(devFD, "wlan0")) {
                    if (showIP(devFD, "usb0")) {
                        showIP(devFD, "lo");
                    }
                }
            }
            break;
            // F2    
        case 0x1d:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F2-Your favor");
            LCD1602KeyDispStr(devFD, 0, 1, "Come add it");
            break;
            // F3    
        case 0x1b:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F3-Your idea");
            LCD1602KeyDispStr(devFD, 0, 1, "Come show it");
            break;
            // F4
        case 0x17:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F4-About");
            LCD1602KeyDispStr(devFD, 0, 1, "by FriendlyARM");
            break;
            // F5
        case 0xf:
            showDefault = 1;
            break;
        }
        if (showDefault == 1) {
            if (needClear) {
                LCD1602KeyClear(devFD);
                LCD1602KeyDispStr(devFD, 0, 0, "#Default");
                needClear = 0;
            }
            memset(curTime, 0, TIME_STR_BUFSIZE);
            lt = time(NULL);
            strncpy(curTime, ctime(&lt) + 11, 8);
            if(strcmp(curTime, preTime)) {
                printf("time:%s\n", curTime);
                LCD1602KeyDispStr(devFD, 0, 1, curTime);
            }
            memset(preTime, 0, TIME_STR_BUFSIZE);
            strcpy(preTime, curTime);
        } else {
            needClear = 1;
            usleep(1000);
        }
    }    
    printf("quit reading key press\n");
    LCD1602KeyDeInit(devFD);
    return 0;
}


6 Connect to NanoPi

6.1 Preparations

Please install a Debian on a NanoPi and an appropriate cross compiler on a PC. Please refer to wiki: NanoPi
Compile a NanoPi kernel. Note: please use the kernel's source code from the nanopi-v4.1.y-matrix branch

$ git clone https://github.com/friendlyarm/linux-4.x.y.git
$ cd linux-4.x.y
$ git checkout nanopi-v4.1.y-matrix
$ make nanopi_defconfig
$ touch .scmversion
$ make

6.2 Hardware Connection

Please refer to the following connection diagram to connect the Matrix-I2C_LCD1602_Keypad to the NanoPi
matrix-i2c_lcd1602_keypad_nanopi

Connection Details:

Matrix-I2C_LCD1602_Keypad NanoPi
SDA Pin3
SCL Pin5
5V Pin4
GND Pin6

6.3 Compile Test Program

Please login the matrix hub and enter the nanopi branch

$ cd matrix
$ git checkout nanopi

Compile the matrix code

$ make CROSS_COMPILE=arm-linux- clean
$ make CROSS_COMPILE=arm-linux-
$ make CROSS_COMPILE=arm-linux- install

Note: please make sure to install the cross compiler "arm-linux-gcc-4.4.3" on your PC, which is used to compile files for the NanoPi-Debian.
Generated library files are under the "install/lib" directory. Applications are under the "install/usr/bin" directory. The test program for the "Matrix-I2C_LCD1602_Keypad" module is "matrix-i2c_lcd1602_keypad".

6.4 Run Test Program

Please copy the library files and test program to the NanoPi

$ cp install/usr/bin/* nanopi_rootfs/usr/bin/
$ cp install/lib/* nanopi_rootfs/lib/ -d

Power on the NanoPi and run the following command in Debian's terminal
Note: this module is not plug and play therefore before running the module please make sure it is connected to a NanoPi.

$ matrix-i2c_lcd1602_keypad

6.5 Code Sample

int main(int argc, char ** argv)
{
    int devFD;
    int keyValue = 0;
    int showDefault = 1;
    int needClear = 1;
    time_t lt;
    char curTime[TIME_STR_BUFSIZE];
    char preTime[TIME_STR_BUFSIZE];
    int hostNameLen = 0;
 
    if ((devFD = LCD1602KeyInit()) == -1) {
        printf("Fail to init LCD1602\n");
        return -1;
    }
    LCD1602KeyClear(devFD);
    printf("waiting key press...\n");
 
    while (1) {
        keyValue = LCD1602GetKey(devFD);
        switch (keyValue) {
        // F1
        case 0x1e:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F1-IP address");
            showIP(devFD, "usb0");
            break;
            // F2    
        case 0x1d:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F2-Your favor");
            LCD1602KeyDispStr(devFD, 0, 1, "Come add it");
            break;
            // F3    
        case 0x1b:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F3-Your idea");
            LCD1602KeyDispStr(devFD, 0, 1, "Come show it");
            break;
            // F4
        case 0x17:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F4-About");
            LCD1602KeyDispStr(devFD, 0, 1, "by FriendlyARM");
            break;
            // F5
        case 0xf:
            showDefault = 1;
            break;
        }
        if (showDefault == 1) {
            if (needClear) {
                LCD1602KeyClear(devFD);
                LCD1602KeyDispStr(devFD, 0, 0, "#Default");
                needClear = 0;
            }
            memset(curTime, 0, TIME_STR_BUFSIZE);
            lt = time(NULL);
            strncpy(curTime, ctime(&lt) + 11, 8);
            if(strcmp(curTime, preTime)) {
                printf("time:%s\n", curTime);
                LCD1602KeyDispStr(devFD, 0, 1, curTime);
            }
            memset(preTime, 0, TIME_STR_BUFSIZE);
            strcpy(preTime, curTime);
        } else {
            needClear = 1;
            usleep(1000);
        }
    }    
    printf("quit reading key press\n");
    LCD1602KeyDeInit(devFD);
    return 0;
}

7 Connect to Tiny4412

7.1 Preparations

Please refer to the Tiny4412's user's manual to install a UbuntuCore on the Tiny4412 and install an appropriate cross compiler on a PC.
Note: only the Tiny4412SDK-1506 carrier board can work with this module.

7.2 Hardware Connection

Please refer to the following diagram to connect the Matrix-I2C_LCD1602_Keypad to the Tiny4412
matrix-i2c_lcd1602_keypad_tiny4412
Connection Details:

Matrix-I2C_LCD1602_Keypad Tiny4412

7.3 Compile Test Program

Please login the Matrix hub and enter the matrix-tiny4412 branch

$ cd matrix
$ git checkout tiny4412

Compile the Matrix code

$ make CROSS_COMPILE=arm-linux-gnueabihf- clean
$ make CROSS_COMPILE=arm-linux-gnueabihf-
$ make CROSS_COMPILE=arm-linux-gnueabihf- install

Note: please make sure to install the cross compiler "arm-linux-gnueabihf-gcc-4.7.3" on your PC, which is used to compile files for the Tiny4412-UbuntuCore.
Generated library files are under the "install/lib" directory. Applications are under the "install/usr/bin" directory. The test program for the "Matrix-I2C_LCD1602_Keypad" module is "matrix-i2c_lcd1602_keypad".

7.4 Run Test Program

Please copy the library files and test program to the Tiny4412

$ cp install/usr/bin/* tiny4412_rootfs/usr/bin/
$ cp install/lib/* tiny4412_rootfs/lib/ -d

Power on the Tiny4412 and run the following command in UbuntuCore's terminal
Note: this module is not plug and play therefore before running the module please make sure it is connected to a Tiny4412.

$ matrix-i2c_lcd1602_keypad

7.5 Code Sample

int main(int argc, char ** argv)
{
    int devFD;
    int keyValue = 0;
    int showDefault = 1;
    int needClear = 1;
    time_t lt;
    char curTime[TIME_STR_BUFSIZE];
    char preTime[TIME_STR_BUFSIZE];
    int hostNameLen = 0;
 
    if ((devFD = LCD1602KeyInit()) == -1) {
        printf("Fail to init LCD1602\n");
        return -1;
    }
    LCD1602KeyClear(devFD);
    printf("waiting key press...\n");
 
    while (1) {
        keyValue = LCD1602GetKey(devFD);
        switch (keyValue) {
        // F1
        case 0x1e:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F1-IP address");
            showIP(devFD, "usb0");
            break;
            // F2    
        case 0x1d:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F2-Your favor");
            LCD1602KeyDispStr(devFD, 0, 1, "Come add it");
            break;
            // F3    
        case 0x1b:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F3-Your idea");
            LCD1602KeyDispStr(devFD, 0, 1, "Come show it");
            break;
            // F4
        case 0x17:
            showDefault = 0;
            LCD1602KeyClear(devFD);
            LCD1602KeyDispStr(devFD, 0, 0, "#F4-About");
            LCD1602KeyDispStr(devFD, 0, 1, "by FriendlyARM");
            break;
            // F5
        case 0xf:
            showDefault = 1;
            break;
        }
        if (showDefault == 1) {
            if (needClear) {
                LCD1602KeyClear(devFD);
                LCD1602KeyDispStr(devFD, 0, 0, "#Default");
                needClear = 0;
            }
            memset(curTime, 0, TIME_STR_BUFSIZE);
            lt = time(NULL);
            strncpy(curTime, ctime(&lt) + 11, 8);
            if(strcmp(curTime, preTime)) {
                printf("time:%s\n", curTime);
                LCD1602KeyDispStr(devFD, 0, 1, curTime);
            }
            memset(preTime, 0, TIME_STR_BUFSIZE);
            strcpy(preTime, curTime);
        } else {
            needClear = 1;
            usleep(1000);
        }
    }    
    printf("quit reading key press\n");
    LCD1602KeyDeInit(devFD);
    return 0;
}

8 Connect to RaspberryPi

9 Connect to Arduino

10 Resources