介绍
LCD2002支持20X2个字符串显示,引脚功能和读写时序跟LCD1602都很像
-
LCD类型:字符点阵
-
点 阵 数:20×2
-
外形尺寸:116.0mm×37.0mm(长宽)
-
视域尺寸:83.0mm×18.6mm
-
点 距 离:0.05mm×0.05mm
-
点 大 小:0.65mm×0.6mm
-
控 制 器:SPLC780
-
玻璃类型:FSTN或STN
-
显示内容:20(例)×2(行)
-
背光类型:LED白光(白底黑字,蓝底白字)
-
数据传输:并口
-
工作电压:5V
LCD2002的应用场景有:
- 物联网数据采集端显示:展示传感器数值
- 工业自动化:电机控制展示信息
- 嵌入式场景:智能家居终端设备
引脚
LCD2002有16Pin引脚,每个引脚功能如下
管脚号 | 符号 | 功能 |
---|---|---|
1 | Vss | 电源地( GND) |
2 | Vdd | 电源电压(+5V) |
3 | V0 | LCD 驱动电压(可调) |
4 | RS | 寄存器选择输入端,输入 MPU 选择模块内部寄存器类型信号: RS=0,当 MPU 进行写模块操作,指向指令寄存器; 当 MPU 进行读模块操作,指向地址计数器; RS=1,无论 MPU 读操作还是写操作,均指向数据寄存器 |
5 | R/W | 读写控制输入端,输入 MPU 选择读/写模块操作信号: R/W=0 读操作; R/W=1 写操作 |
6 | E | 使能信号输入端,输入 MPU 读/写模块操作使能信号: 读操作时,高电平有效;写操作时,下降沿有效 |
7 | DB0 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
8 | DB1 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
9 | DB2 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
10 | DB3 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
11 | DB4 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
12 | DB5 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
13 | DB6 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
14 | DB7 | 数据输入/输出口, MPU 与模块之间的数据传送通道 |
15 | A | 背光的正端+5V |
16 | K | 背光的负端 0V |
原装的LCD并没有焊接排针,需要自己手动焊接
移植
引脚定义,一般只需要用到以下13个Pin
#define LCD_RS_PIN GET_PIN(A, 4)
#define LCD_RW_PIN GET_PIN(A, 5)
#define LCD_E_PIN GET_PIN(A, 6)
#define LCD_D0_PIN GET_PIN(A, 7)
#define LCD_D1_PIN GET_PIN(B, 0)
#define LCD_D2_PIN GET_PIN(B, 1)
#define LCD_D3_PIN GET_PIN(B, 2)
#define LCD_D4_PIN GET_PIN(B, 10)
#define LCD_D5_PIN GET_PIN(B, 11)
#define LCD_D6_PIN GET_PIN(B, 12)
#define LCD_D7_PIN GET_PIN(B, 13)
#define LCD_V0 GET_PIN(B, 4)
#define LCD_BL GET_PIN(B, 5)
初始化LCD2002引脚
void LCD_GPIO_Init()
{rt_pin_mode(LCD_V0, PIN_MODE_OUTPUT);rt_pin_mode(LCD_BL, PIN_MODE_OUTPUT);rt_pin_mode(LCD_RS_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_RW_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_E_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D0_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D1_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D2_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D3_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D4_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D5_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D6_PIN, PIN_MODE_OUTPUT);rt_pin_mode(LCD_D7_PIN, PIN_MODE_OUTPUT);
}
写数据
void LCD_WriteData(uint8_t data)
{rt_pin_write(LCD_RS_PIN, PIN_HIGH); rt_pin_write(LCD_RW_PIN, PIN_LOW); rt_pin_write(LCD_E_PIN, PIN_LOW); rt_pin_write(LCD_D0_PIN, (data & 0x01) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D1_PIN, (data & 0x02) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D2_PIN, (data & 0x04) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D3_PIN, (data & 0x08) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D4_PIN, (data & 0x10) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D5_PIN, (data & 0x20) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D6_PIN, (data & 0x40) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D7_PIN, (data & 0x80) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_E_PIN, PIN_HIGH); rt_thread_mdelay(5); rt_pin_write(LCD_E_PIN, PIN_LOW);
}
写命令
void LCD_WriteCommand(uint8_t command)
{rt_pin_write(LCD_RS_PIN, PIN_LOW); rt_pin_write(LCD_RW_PIN, PIN_LOW); rt_pin_write(LCD_E_PIN, PIN_LOW); rt_pin_write(LCD_D0_PIN, (command & 0x01) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D1_PIN, (command & 0x02) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D2_PIN, (command & 0x04) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D3_PIN, (command & 0x08) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D4_PIN, (command & 0x10) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D5_PIN, (command & 0x20) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D6_PIN, (command & 0x40) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_D7_PIN, (command & 0x80) ? PIN_HIGH : PIN_LOW);rt_pin_write(LCD_E_PIN, PIN_HIGH); rt_thread_mdelay(5); rt_pin_write(LCD_E_PIN, PIN_LOW);
}
设置光标位置
void LCD_SetCursor(uint8_t col, uint8_t row)
{uint8_t address = col;if (row == 1) {address += 0x40; }LCD_WriteCommand(0x80 | address);
}
绘制字符,需要配合LCD_SetCursor
接口使用
void LCD_Print(char* str)
{while (*str) {LCD_WriteData(*str++);}
}
LCD初始化,设置清屏、光标位置自动累加等
void LCD_Init() {rt_pin_write(LCD_V0, PIN_HIGH);rt_pin_write(LCD_BL, PIN_HIGH);rt_thread_mdelay(15); LCD_WriteCommand(0x38);rt_thread_mdelay(5);LCD_WriteCommand(0x0c);rt_thread_mdelay(5);LCD_WriteCommand(0x06);rt_thread_mdelay(5);LCD_WriteCommand(0x01);rt_thread_mdelay(5);
}
测试用例,在屏幕第一行显示Levitation
字样
static void lcd2002_entry(void* parameter)
{LCD_GPIO_Init();LCD_Init();LCD_WriteCommand( 0x80 ); LCD_SetCursor(5, 0);LCD_Print("Levitation");while(1) {rt_thread_mdelay(500);}
}int main()
{// lcd2002rt_thread_t lcd2002_thread = rt_thread_create("lcd2002", lcd2002_entry, RT_NULL, 512, 21, 20);if (lcd2002_thread != RT_NULL) {rt_thread_startup(lcd2002_thread);}while (1){rt_pin_write(LED0_PIN, PIN_HIGH);rt_thread_mdelay(10);rt_pin_write(LED0_PIN, PIN_LOW);rt_thread_mdelay(10);}return RT_EOK;
}
显示效果
总结
买的是5V供电的LCD2002,但它的数据引脚是可以兼容3.3V的GPIO的,详细可参考数据手册关于芯片引脚电气特性的说明
立创上面汉昇的LCD2002背光引脚BLK、BLA的说明是错误的
延申
BLA
背光和V0
对比度通过使用1KHz方波改变占空比可以实现背光和对比度的调节,可以节约硬件Bom成本,减少电位器的使用