Difference between revisions of "Matrix - I2C LCD1602 Keypad"

From FriendlyELEC WiKi
Jump to: navigation, search
(运行测试程序)
(代码展示)
Line 128: Line 128:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===代码展示===
+
===Code Sample===
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
int main(int argc, char ** argv)
 
int main(int argc, char ** argv)

Revision as of 02:16, 18 September 2015

查看中文

1 Introduction

I2C LCD1602
I2C LCD1602
  • 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 Spec:
Pin Comment
IRQ BUTTON IRQ
SDA I2C SDA
SCL I2C SCL
5V Power 5V
GND Ground

3 General Description

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. We put them in a git hub - git://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 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 hub

$ git clone git://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 Connect to NanoPi

5.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

5.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

5.3 Compile Test Program

Please login the matrix hub and enter the nanopi branch

$ cd matrix
$ git checkout nanopi

Compile 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".

5.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

5.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;
}

6 与Tiny4412连接使用

6.1 准备工作

参考Tiny4412光盘里的《友善之臂Ubuntu使用手册》,在Tiny4412上运行UbuntuCore系统,然后在主机PC上安装并使用相应的编译器。
注意:只能使用Tiny4412SDK-1506的底板。

6.2 硬件连接

参考下图连接模块Matrix-I2C_LCD1602_Keypad和Tiny4412
matrix-i2c_lcd1602_keypad_tiny4412
连接说明:

Matrix-I2C_LCD1602_Keypad Tiny4412

6.3 编译测试程序

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

$ cd matrix
$ git checkout tiny4412

编译Matrix配件代码

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

注意:请确保你的主机PC当前使用的交叉编译器为Tiny4412-UbuntuCore配套的arm-linux-gnueabihf-gcc-4.7.3。
编译出来的库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-I2C_LCD1602_Keypad对应的测试程序为matrix-i2c_lcd1602_keypad。

6.4 运行测试程序

拷贝库文件和测试程序到Tiny4412的UbuntuCore的文件系统上

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

然后启动Tiny4412,在UbuntuCore的shell终端中执行如下命令运行模块Matrix-I2C_LCD1602_Keypad的测试程序
注意:此模块并不支持热插拔,启动系统前需要确保硬件正常连接。

$ matrix-i2c_lcd1602_keypad

6.5 代码展示

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 与RaspberryPi连接使用

8 与Arduino连接使用

9 相关资料