Matrix - I2C LCD1602 Keypad

From FriendlyELEC WiKi
Revision as of 09:22, 17 September 2015 by Yftan (Talk | contribs) (MCP23017)

Jump to: navigation, search

查看中文

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 下载Matrix源码

Matrix配件相关的代码是完全开源的,统一由一个仓库进行管理:git://github.com/friendlyarm/matrix.git
该仓库里不同的分支代表着Matrix配件所支持的不同开发板。

  • nanopi分支包含了Matrix对NanoPi的支持;
  • tiny4412分支包含了Matrix对Tiny4412的支持;
  • raspberrypi分支包含了Matrix对RaspberryPi的支持;

在主机PC上安装git,以Ubuntu14.04为例

$ sudo apt-get install git

克隆Matrix配件代码仓库

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

克隆完成后会得到一个matrix目录,里面存放着所有Matrix配件的代码。

5 与NanoPi连接使用

5.1 准备工作

在NanoPi上运行Debian系统,然后在主机PC上安装并使用相应的编译器。参考wiki:NanoPi
注意:必须使用nanopi-v4.1.y-matrix分支编译出来的内核。
下载NanoPi内核源代码并编译

$ 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 硬件连接

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

连接说明:

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

5.3 编译测试程序

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

$ cd matrix
$ git checkout nanopi

编译Matrix配件代码

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

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

5.4 运行测试程序

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

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

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

$ matrix-i2c_lcd1602_keypad

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

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 相关资料