Difference between revisions of "Matrix - I2C LCD1602 Keypad"

From FriendlyELEC WiKi
Jump to: navigation, search
(Resources)
 
(63 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Matrix - I2C LCD1602 Keypad|English]]
+
[[Matrix - I2C LCD1602 Keypad/zh|查看中文]]
  
==介绍==
+
==Introduction==
[[File:Matrix-I2C_LCD1602_Keypad_01.png|thumb]]
+
[[File:I2C LCD1602 Keypad.png|thumb|I2C LCD1602 Keypad]]
[[File:Matrix-I2C_LCD1602_Keypad_02.png|thumb]]
+
[[File:I2C LCD1602 Keypad01.png|thumb|I2C LCD1602 Keypad]]
* 模块Matrix-I2C_LCD1602由液晶显示屏模块LCD1602和GPIO扩展模块MCP23017两部分构成。<br>
+
* The Matrix-I2C_LCD1602_Keypad module is an easy-to-use display module based on the LCD1602.
* LCD1602是可以显示16x2个字符的液晶显示屏,其本身是并行接口,需要较多的IO资源才可以与之通讯。<br>
+
* 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.
* MCP23017
+
* 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.
  
==特性==
+
==Features==
* I2C通讯,显示和背光可控
+
* I2C interface, LCD display and backlight control
* 2.54mm排针接口,接线方便,通用性强
+
* 2.54mm spacing pin
* IIC模块PCB尺寸(mm):
+
* I2C PCB dimension (mm): 57 x 80
* LCD1602 PCB尺寸(mm):
+
* LCD1602 PCB dimension (mm): 36 x 80
[[File:lpdpcb.png|frameless|400px|IIC模块PCB]]
+
[[File:lpdpcb01.png|frameless|400px|IIC Module PCB]]
 
[[File:lcdpcb.png|frameless|400px|LCD1602 PCB]]
 
[[File:lcdpcb.png|frameless|400px|LCD1602 PCB]]
  
* 引脚说明:
+
* Pin Description:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|名称 || 描述
+
|Pin || Description
 +
|-
 +
|IRQ    || BUTTON IRQ
 
|-
 
|-
 
|SDA    || I2C SDA
 
|SDA    || I2C SDA
Line 25: Line 29:
 
|SCL    || I2C SCL
 
|SCL    || I2C SCL
 
|-  
 
|-  
|5V      || 电源5V
+
|5V      || Supply Voltage 5V
 
|-
 
|-
|GND    ||
+
|GND    || Ground
 +
|}
 +
 
 +
==Basic Device Operation==
 +
===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.
 +
 
 +
===LCD1602===
 +
* The connection diagram between the MCP23017 module's Pin0~Pin7 and the LCD module is shown below:
 +
[[File:图片不正确.png|frameless|400px|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.
 +
 
 +
==Applications==
 +
===Connect to NanoPi M1===
 +
Refer to the following connection diagram to connect the module to the NanoPi M1:<br>
 +
[[File:Matrix-I2C_LCD1602_Keypad_nanopi_m1.jpg|frameless|600px|Matrix-I2C_LCD1602_Keypad_nanopi_m1]]
 +
 
 +
===Connect to NanoPi 2===
 +
Refer to the following connection diagram to connect the module to the NanoPi 2:<br>
 +
[[File:matrix-i2c_lcd1602_keypad_nanopi 2.jpg|frameless|600px|matrix-i2c_lcd1602_keypad_nanopi 2]]
 +
 
 +
===Connect to NanoPi M2 / NanoPi 2 Fire===
 +
Refer to the following connection diagram to connect the module to the NanoPi M2/ NanoPi 2 Fire.<br>
 +
[[File:Matrix-I2C_LCD1602_Keypad_nanopi_m2.jpg|frameless|600px|Matrix-I2C_LCD1602_Keypad_nanopi_m2]]
 +
 
 +
===Connect to NanoPC-T2===
 +
Refer to the following connection diagram to connect the module to the NanoPC-T2:<br>
 +
[[File:Matrix-I2C_LCD1602_Keypad_NanoPC-T2.jpg|frameless|600px|Matrix-I2C_LCD1602_Keypad_NanoPC-T2]]
 +
 
 +
Connection Details:
 +
{| class="wikitable"
 
|-
 
|-
|IRQ     || 中断引脚
+
|Matrix-I2C_LCD1602_Keypad || NanoPC-T2
 +
|-
 +
|SDA    || Pin6
 +
|-
 +
|SCL    || Pin5
 +
|-
 +
|5V     || Pin29
 +
|-
 +
|GND    || Pin30
 
|}
 
|}
  
==工作原理==
+
==Compile & Run Test Program==
===MCP23017工作原理===
+
Boot your ARM board with Debian and copy the matrix code:
 +
<syntaxhighlight lang="bash">
 +
$ apt-get update && apt-get install git
 +
$ git clone https://github.com/friendlyarm/matrix.git
 +
</syntaxhighlight>
 +
If your cloning is done successfully a "matrix" directory will be generated.
  
===LCD1602工作原理===
+
Compile and install Matrix:
* 由模块的原理图可以看出PCF8574模块的输出引脚P0-P7与LCD模块的引脚连接如下图:
+
<syntaxhighlight lang="bash">
[[File:1602.png|frameless|400px|1602]]
+
$ cd matrix
* RS为指令/数据控制位,RW为读/写控制位,E为使能位(边沿触发),BL为背光灯控制位,D4-D7为数据位。
+
$ make && make install
* 由于LCD用到四个数据位,因此只能使用4线来驱动。通过指令表我们可以对LCD进行写指令设置LCD的工作转态,但这里的指令/数据(DB7-DB0)是八位的,而LCD却是4线驱动,因此每次写指令/数据时是先写高四位(DB7-DB4),再写低四位(DB3-DB0)。
+
</syntaxhighlight>
* 注意:LCD内置了192个常用字模,存放在CGROM,所以我们在显示字符A时可以直接写入“A”,此外LCD还有8个允许用户自定义的字符产生的RAM,称为CGRAM,这里由于没有涉及到所以不介绍,有兴趣的用户可以去了解一下。
+
 
 +
Run test program:
 +
<syntaxhighlight lang="bash">
 +
$ matrix-lcd1602_keypad
 +
</syntaxhighlight>
 +
Note: this module is not plug and play therefore before running the module please make sure it is connected to an ARM board.<br>
 +
Here is what you should observe:<br>
 +
[[File:matrix-lcd1602_keypad_result.png|frameless|600px|matrix-lcd1602_keypad_result]] <br>
 +
By default the LCD displays the current time. If you press F1 it will display the IP address. If you press F5 it will display the current time. Users can define the functions of F2, F3 and F4.<br>
 +
If the LCD doesn't show them clearly you can adjust the resistor to highlight the characters.
 +
 
 +
==Code Sample==
 +
This Matrix code sample can work with all the ARM boards mentioned in this module's wiki. The name of this code sample is "matrix-i2c_lcd1602_keypad". Here is its source code:
 +
<syntaxhighlight lang="c">
 +
int main(int argc, char ** argv)
 +
{
 +
    int devFD, board;
 +
    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];
 +
    int i2cDev = 0;
 +
   
 +
    if ((board = boardInit()) < 0) {
 +
        printf("Fail to init board\n");
 +
        return -1;
 +
    }
 +
   
 +
    if (argc == 2)
 +
        i2cDev = atoi(argv[1]);   
 +
    if ((devFD = LCD1602KeyInit(i2cDev)) == -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>
 +
For more details about this APIs called in this code sample refer to [[Matrix API reference manual]] <br>
  
==下载Matrix源码==
+
<!---
Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git <br>
+
==Download Matrix Source Code==
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。<br>
+
All the matrix modules' code samples are open source. They are maintained on GitHub - https://github.com/friendlyarm/matrix.git <br>
*matrix-nanopi分支包含了Matrix对NanoPi的支持;
+
Each branch in this hub contains the matrix modules' code samples for a board that the matrix modules can work with.<br>
*matrix-tiny4412分支包含了Matrix对Tiny4412的支持;
+
* The nanopi branch contains the matrix modules' code samples for the NanoPi
*matrix-raspberrypi分支包含了Matrix对RaspberryPi的支持;
+
* 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
  
在主机PC上安装git,以Ubuntu14.04为例
+
Please follow the steps below to get the source code:<br>
 +
Install the git utility on a PC running Ubuntu14.04
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
$ sudo apt-get install git
 
$ sudo apt-get install git
 
</syntaxhighlight>
 
</syntaxhighlight>
  
克隆Matrix配件代码仓库
+
Clone the matrix code from GitHub
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ git clone git://github.com/friendlyarm/matrix.git
+
$ git clone https://github.com/friendlyarm/matrix.git
 
</syntaxhighlight>
 
</syntaxhighlight>
克隆完成后会得到一个matrix目录,里面存放着所有Matrix配件的代码。
+
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples.
  
==与NanoPi连接使用==
+
==Connect to NanoPi 2==
===准备工作===
+
===Hardware Connection===
在NanoPi上运行Debian系统,然后在主机PC上安装并使用相应的编译器。
+
Please refer to the following connection diagram to connect the Matrix-I2C_LCD1602_Keypad to the NanoPi 2:<br>
参考wiki:[[http://wiki.friendlyarm.com/wiki/index.php/NanoPi]]
+
[[File:matrix-i2c_lcd1602_keypad_nanopi 2.jpg|frameless|600px|matrix-i2c_lcd1602_keypad_nanopi 2]]
  
===硬件连接===
+
Connection Details:
参考下图连接模块Matrix-I2C_LCD1602和NanoPi <br>
+
[[File:matrix-i2c_lcd1602_nanopi.jpg|frameless|600px|matrix-i2c_lcd1602_nanopi]]
+
 
+
连接说明:
+
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|Matrix-I2C_LCD1602 || NanoPi       
+
|Matrix-I2C_LCD1602_Keypad || NanoPi 2        
 
|-
 
|-
 
|SDA    || Pin3
 
|SDA    || Pin3
Line 78: Line 242:
 
|SCL    || Pin5
 
|SCL    || Pin5
 
|-  
 
|-  
|5V       || Pin4
+
|5V     || Pin4
 
|-
 
|-
|GND     || Pin6
+
|GND   || Pin6
 
|}
 
|}
  
===编译测试程序===
+
===Compile Test Program===
进入Matrix代码仓库,切换到matrix-nanopi分支
+
Please login the matrix hub and enter the nanopi2 branch
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
$ cd matrix
 
$ cd matrix
$ git checkout matrix-nanopi
+
$ git checkout nanopi2
 
</syntaxhighlight>
 
</syntaxhighlight>
  
编译Matrix配件代码
+
Compile the Matrix code
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
$ make CROSS_COMPILE=arm-linux- clean
 
$ make CROSS_COMPILE=arm-linux- clean
Line 96: Line 260:
 
$ make CROSS_COMPILE=arm-linux- install
 
$ make CROSS_COMPILE=arm-linux- install
 
</syntaxhighlight>
 
</syntaxhighlight>
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi-Debian配套的arm-linux-gcc.4.4.3。<br>
+
Note: please make sure to install the cross compiler "arm-linux-gcc-4.9.3" on your PC, which is used to compile files for the NanoPi 2.<br>
编译出来的库文件位于install/lib目录下,而测试程序则位于install/bin目录下,模块Matrix-I2C_LCD1602对应的测试程序为matrix-i2c_lcd1602。<br>
+
Generated library files are under the "install/lib" directory. The test program is under the "install/usr/bin" directory.<br>
 +
The modules are under the "modules" directory. The driver's source code is in github: https://github.com/friendlyarm/linux-3.4.y.git <br>
  
===运行测试程序===
+
===Run Test Program===
拷贝库文件和测试程序到NanoPi的文件系统上
+
Please insert a TF card which is flashed with Debian to a Linux host and mount its boot and rootfs sections.<br>
 +
We assume the rootfs is mounted to /media/rootfs then please run the following commands to copy the module, library and test program to the card.<br>
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ cp install/bin/* nanopi_rootfs/bin/
+
$ cp modules /media/rootfs/ -r
$ cp install/lib/* nanopi_rootfs/lib/ -d
+
$ cp install/lib/* /media/rootfs/lib/ -d
 +
$ cp install/usr/bin/* /media/rootfs/usr/bin/
 
</syntaxhighlight>
 
</syntaxhighlight>
  
然后启动NanoPi,在Debian的shell终端中执行如下命令运行模块Matrix-I2C_LCD1602的测试程序 <br>
+
Insert this TF card to your NanoPi 2, power on and run the following command to start the matrix-lcd1602_keypad program.<br>
注意:此模块并不支持热插拔,启动系统前需要确保硬件正常连接。
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ matrix-i2c_lcd1602
+
$ matrix-lcd1602_keypad
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Note: this module is not plug and play therefore before running the module please make sure it is connected to a NanoPi 2.<br>
 +
Here is what you should expect:<br>
 +
[[File:matrix-lcd1602_keypad_result.png|frameless|600px|matrix-lcd1602_keypad_result]] <br>
 +
By default the LCD displays the current time. If you press F1 it will display the IP address. If you press F5 it will display the current time. Users can define the functions of F2, F3 and F4.
  
===代码展示===
+
===Code Sample===
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int main(int argc, char ** argv)
 
int main(int argc, char ** argv)
 
{
 
{
 
     int devFD;
 
     int devFD;
     if ((devFD = LCD1602Init()) == -1) {
+
    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");
 
         printf("Fail to init LCD1602\n");
 
         return -1;
 
         return -1;
 
     }
 
     }
 +
    LCD1602KeyClear(devFD);
 +
    printf("waiting key press...\n");
  
     if (LCD1602Clear(devFD) == -1) {
+
     while (1) {
         printf("Fail to Clear\n");
+
        keyValue = LCD1602GetKey(devFD);
         return -1;
+
        if (keyValue != lastKeyValue) {
    }
+
            lastKeyValue = keyValue;
    printf("clearing LCD1602\n");
+
        } else if (showDefault != 1){
    sleep(1);
+
            usleep(1000);
   
+
            continue;
    if (LCD1602DispLines(devFD, " B&G Char LCD", "--by FriendlyARM") == -1) {
+
         }
        printf("Fail to Display String\n");
+
        switch (keyValue) {
         return -1;
+
        // F1
     }
+
        case 0x1e:
     printf("displaying LCD1602\n");
+
            showDefault = 0;
     LCD1602DeInit(devFD);
+
            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;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==与Tiny4412连接使用==
+
==Connect to NanoPi==
===准备工作===
+
===Preparations===
参考Tiny4412光盘里的《友善之臂Ubuntu使用手册》,在Tiny4412上运行UbuntuCore系统,然后在主机PC上安装并使用相应的编译器。<br>
+
Please install a Debian on a NanoPi and an appropriate cross compiler on a PC. Please refer to wiki: [[NanoPi|NanoPi]] <br>
注意:只能使用Tiny4412SDK-1506的底板。
+
Compile a NanoPi kernel. Note: please use the kernel's source code from the nanopi-v4.1.y-matrix branch
 +
<syntaxhighlight lang="bash">
 +
$ 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
 +
</syntaxhighlight>
  
===硬件连接===
+
===Hardware Connection===
参考下图连接模块Matrix-I2C_LCD1602和Tiny4412 <br>
+
Please refer to the following connection diagram to connect the Matrix-I2C_LCD1602_Keypad to the NanoPi <br>
[[File:matrix-i2c_lcd1602_tiny4412.jpg|frameless|600px|matrix-i2c_lcd1602_tiny4412]]
+
[[File:matrix-i2c_lcd1602_keypad_nanopi.jpg|frameless|600px|matrix-i2c_lcd1602_keypad_nanopi]]
  
连接说明:
+
Connection Details:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|Matrix-I2C_LCD1602 || Tiny4412        
+
|Matrix-I2C_LCD1602_Keypad || NanoPi        
 
|-
 
|-
|SDA    || CON18 SDA
+
|SDA    || Pin3
 
|-
 
|-
|SCL    || CON18 SCL
+
|SCL    || Pin5
 
|-  
 
|-  
|5V      || CON18 5V
+
|5V      || Pin4
 
|-
 
|-
|GND    || CON18 GND
+
|GND    || Pin6
 
|}
 
|}
  
===编译测试程序===
+
===Compile Test Program===
进入Matrix代码仓库,切换到matrix-tiny4412分支
+
Please login the matrix hub and enter the nanopi branch
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
$ cd matrix
 
$ cd matrix
$ git checkout matrix-tiny4412
+
$ git checkout nanopi
 
</syntaxhighlight>
 
</syntaxhighlight>
  
编译matrix配件代码
+
Compile the matrix code
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ make CROSS_COMPILE=arm-linux-gnueabihf- clean
+
$ make CROSS_COMPILE=arm-linux- clean
$ make CROSS_COMPILE=arm-linux-gnueabihf-
+
$ make CROSS_COMPILE=arm-linux-
$ make CROSS_COMPILE=arm-linux-gnueabihf- install
+
$ make CROSS_COMPILE=arm-linux- install
 
</syntaxhighlight>
 
</syntaxhighlight>
注意:请确保你的主机PC当前使用的交叉编译器为Tiny4412-UbuntuCore配套的arm-linux-gnueabihf-gcc-4.7.3。<br>
+
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.<br>
编译出来的库文件位于install/lib目录下,而测试程序则位于install/bin目录下,模块Matrix-I2C_LCD1602对应的测试程序为matrix-i2c_lcd1602。
+
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".<br>
  
===运行测试程序===
+
===Run Test Program===
拷贝库文件和测试程序到Tiny4412的UbuntuCore的文件系统上
+
Please copy the library files and test program to the NanoPi
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ cp install/bin/* tiny4412_rootfs/bin/
+
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/
$ cp install/lib/* tiny4412_rootfs/lib/ -d
+
$ cp install/lib/* nanopi_rootfs/lib/ -d
 
</syntaxhighlight>
 
</syntaxhighlight>
  
然后启动Tiny4412,在UbuntuCore的shell终端中执行如下命令运行模块Matrix-I2C_LCD1602的测试程序<br>
+
Power on the NanoPi and run the following command in Debian's terminal<br>
注意:此模块并不支持热插拔,启动系统前需要确保硬件正常连接。
+
Note: this module is not plug and play therefore before running the module please make sure it is connected to a NanoPi.
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ matrix-i2c_lcd1602
+
$ matrix-i2c_lcd1602_keypad
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===代码展示===
+
===Code Sample===
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int main(int argc, char ** argv)
 
int main(int argc, char ** argv)
 
{
 
{
 
     int devFD;
 
     int devFD;
     if ((devFD = LCD1602Init()) == -1) {
+
    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");
 
         printf("Fail to init LCD1602\n");
 
         return -1;
 
         return -1;
 
     }
 
     }
 +
    LCD1602KeyClear(devFD);
 +
    printf("waiting key press...\n");
  
     if (LCD1602Clear(devFD) == -1) {
+
     while (1) {
         printf("Fail to Clear\n");
+
        keyValue = LCD1602GetKey(devFD);
         return -1;
+
        switch (keyValue) {
    }
+
         // F1
    printf("clearing LCD1602\n");
+
        case 0x1e:
    sleep(1);
+
            showDefault = 0;
   
+
            LCD1602KeyClear(devFD);
    if (LCD1602DispLines(devFD, " B&G Char LCD", "--by FriendlyARM") == -1) {
+
            LCD1602KeyDispStr(devFD, 0, 0, "#F1-IP address");
        printf("Fail to Display String\n");
+
            showIP(devFD, "usb0");
         return -1;
+
            break;
     }
+
            // F2   
     printf("displaying LCD1602\n");
+
         case 0x1d:
     LCD1602DeInit(devFD);
+
            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;
 
     return 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==与RaspberryPi连接使用==
+
==Connect to Tiny4412==
 +
===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.<br>
 +
Note: only the Tiny4412SDK-1506 carrier board can work with this module.
  
==与Arduino连接使用==
+
===Hardware Connection===
===准备工作===
+
Please refer to the following diagram to connect the Matrix-I2C_LCD1602_Keypad to the Tiny4412<br>
确保你的Arduino板能正常下载运行程序,参考:https://www.arduino.cc/en/Guide/HomePage <br>
+
[[File:matrix-i2c_lcd1602_keypad_tiny4412.jpg|frameless|400px|matrix-i2c_lcd1602_keypad_tiny4412]]
这里我们使用的是Arduino uno板。
+
<br>
 +
Connection Details:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-I2C_LCD1602_Keypad || Tiny4412
 +
|}
  
===硬件连接===
+
===Compile Test Program===
参考下图连接模块Matrix-I2C_LCD1602和Arduino Uno <br>
+
Please login the Matrix hub and enter the matrix-tiny4412 branch
[[File:matrix-i2c_lcd1602_arduino.jpg|frameless|600px|matrix-i2c_lcd1602_arduino]]
+
<syntaxhighlight lang="bash">
 +
$ cd matrix
 +
$ git checkout tiny4412
 +
</syntaxhighlight>
  
===编译运行测试程序===
+
Compile the Matrix code
由于arduino开发板并不运行Linux系统,所以matrix for arduino的代码由独立的仓库进行管理:git://github.com/friendlyarm/matrix-arduino.git <br>
+
克隆matrix-arduino代码仓库
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ git clone git://github.com/friendlyarm/matrix-arduino.git
+
$ make CROSS_COMPILE=arm-linux-gnueabihf- clean
 +
$ make CROSS_COMPILE=arm-linux-gnueabihf-
 +
$ make CROSS_COMPILE=arm-linux-gnueabihf- install
 
</syntaxhighlight>
 
</syntaxhighlight>
克隆完成后会得到一个matrix-arduino目录,里面存放着所有Matrix 配件用于支持arduino的代码。
+
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.<br>
 +
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".
  
拷贝matrix-i2c_lcd1602目录到Arduino IDE的libraries目录下
+
===Run Test Program===
 +
Please copy the library files and test program to the Tiny4412
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ cd matrix-arduino
+
$ cp install/usr/bin/* tiny4412_rootfs/usr/bin/
$ cp matrix-i2c_lcd1602 Arduino_IDE/libraies -r
+
$ cp install/lib/* tiny4412_rootfs/lib/ -d
 
</syntaxhighlight>
 
</syntaxhighlight>
  
启动Arduino IDE <br>
+
Power on the Tiny4412 and run the following command in UbuntuCore's terminal <br>
点击File->Examples->matrix-i2c_lcd1602->displaychar,打开测试程序。 <br>
+
Note: this module is not plug and play therefore before running the module please make sure it is connected to a Tiny4412.
点击upload,运行测试程序。 <br>
+
<syntaxhighlight lang="bash">
 +
$ matrix-i2c_lcd1602_keypad
 +
</syntaxhighlight>
  
===代码展示===
+
===Code Sample===
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
#include <Wire.h>
+
int main(int argc, char ** argv)
#include <LiquidCrystal_I2C.h>
+
 
+
// set the LCD address to 0x27 for a 16 chars and 2 line display
+
LiquidCrystal_I2C lcd(0x27,16,2); 
+
 
+
void setup()
+
 
{
 
{
  lcd.init();                      
+
    int devFD;
  lcd.backlight();
+
    int keyValue = 0;
  lcd.setCursor(2, 0);
+
    int showDefault = 1;
  lcd.print("B&G Char LCD");  
+
    int needClear = 1;
  lcd.setCursor(0, 1);
+
    time_t lt;
  lcd.println("--by FriendlyARM");
+
    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");
  
void loop()
+
    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;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==相关资料==
+
==Connect to RaspberryPi==
 +
 
 +
==Connect to Arduino==
 +
--->
 +
==Resources==
 +
[http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf MCP23017.pdf]  <br>
 +
[https://www.openhacks.com/uploadsproductos/eone-1602a1.pdf LCD1602.pdf]
 +
 
 +
==Update Log==
 +
===Feb-24-2016===
 +
* Added the driver's source code location in Section 5.2
 +
===June-19-2016===
 +
* Re-organized and simplified wiki

Latest revision as of 14:59, 19 June 2016

查看中文

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 Module 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 MCP23017 module's Pin0~Pin7 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 Applications

4.1 Connect to NanoPi M1

Refer to the following connection diagram to connect the module to the NanoPi M1:
Matrix-I2C_LCD1602_Keypad_nanopi_m1

4.2 Connect to NanoPi 2

Refer to the following connection diagram to connect the module to the NanoPi 2:
matrix-i2c_lcd1602_keypad_nanopi 2

4.3 Connect to NanoPi M2 / NanoPi 2 Fire

Refer to the following connection diagram to connect the module to the NanoPi M2/ NanoPi 2 Fire.
Matrix-I2C_LCD1602_Keypad_nanopi_m2

4.4 Connect to NanoPC-T2

Refer to the following connection diagram to connect the module to the NanoPC-T2:
Matrix-I2C_LCD1602_Keypad_NanoPC-T2

Connection Details:

Matrix-I2C_LCD1602_Keypad NanoPC-T2
SDA Pin6
SCL Pin5
5V Pin29
GND Pin30

5 Compile & Run Test Program

Boot your ARM board with Debian and copy the matrix code:

$ apt-get update && apt-get install git
$ git clone https://github.com/friendlyarm/matrix.git

If your cloning is done successfully a "matrix" directory will be generated.

Compile and install Matrix:

$ cd matrix
$ make && make install

Run test program:

$ matrix-lcd1602_keypad

Note: this module is not plug and play therefore before running the module please make sure it is connected to an ARM board.
Here is what you should observe:
matrix-lcd1602_keypad_result
By default the LCD displays the current time. If you press F1 it will display the IP address. If you press F5 it will display the current time. Users can define the functions of F2, F3 and F4.
If the LCD doesn't show them clearly you can adjust the resistor to highlight the characters.

6 Code Sample

This Matrix code sample can work with all the ARM boards mentioned in this module's wiki. The name of this code sample is "matrix-i2c_lcd1602_keypad". Here is its source code:

int main(int argc, char ** argv)
{
    int devFD, board;
    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];
    int i2cDev = 0;
 
    if ((board = boardInit()) < 0) {
        printf("Fail to init board\n");
        return -1;
    }
 
    if (argc == 2)
        i2cDev = atoi(argv[1]);    
    if ((devFD = LCD1602KeyInit(i2cDev)) == -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;
}

For more details about this APIs called in this code sample refer to Matrix API reference manual

7 Resources

MCP23017.pdf
LCD1602.pdf

8 Update Log

8.1 Feb-24-2016

  • Added the driver's source code location in Section 5.2

8.2 June-19-2016

  • Re-organized and simplified wiki