当前位置: 首页 > article >正文

stm32移植LCD2002驱动

介绍

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引脚,每个引脚功能如下

管脚号符号功能
1Vss电源地( GND)
2Vdd电源电压(+5V)
3V0LCD 驱动电压(可调)
4RS寄存器选择输入端,输入 MPU 选择模块内部寄存器类型信号:
RS=0,当 MPU 进行写模块操作,指向指令寄存器;
当 MPU 进行读模块操作,指向地址计数器;
RS=1,无论 MPU 读操作还是写操作,均指向数据寄存器
5R/W读写控制输入端,输入 MPU 选择读/写模块操作信号:
R/W=0 读操作; R/W=1 写操作
6E使能信号输入端,输入 MPU 读/写模块操作使能信号:
读操作时,高电平有效;写操作时,下降沿有效
7DB0数据输入/输出口, MPU 与模块之间的数据传送通道
8DB1数据输入/输出口, MPU 与模块之间的数据传送通道
9DB2数据输入/输出口, MPU 与模块之间的数据传送通道
10DB3数据输入/输出口, MPU 与模块之间的数据传送通道
11DB4数据输入/输出口, MPU 与模块之间的数据传送通道
12DB5数据输入/输出口, MPU 与模块之间的数据传送通道
13DB6数据输入/输出口, MPU 与模块之间的数据传送通道
14DB7数据输入/输出口, MPU 与模块之间的数据传送通道
15A背光的正端+5V
16K背光的负端 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()
{
	// lcd2002
    rt_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成本,减少电位器的使用
在这里插入图片描述


http://www.kler.cn/a/572192.html

相关文章:

  • 数据结构理论
  • 解码未来!安徽艾德未来智能科技有限公司荣获“GAS消费电子科创奖-产品创新奖”!
  • 爬虫基础:一文掌握网页基础和爬虫原理
  • Javaweb中的过滤器
  • Compose Multiplatform开发记录之文本输入框
  • Svelte vs Vue:前端框架的深度对比与应用场景分析
  • RabbitMQ — 数据持久化实现MQ可靠性
  • 机场网络安全安全运营体系
  • 数据开发方向经过15年的发展再一次走到了十字路口
  • 【JQuery—前端快速入门】JQuery 操作元素
  • 火语言RPA--PDF提取文本
  • windows 上删除 node_modules
  • 紧跟 Web3 热潮,RuleOS 如何成为行业新宠?
  • Go红队开发—编解码工具
  • ML.NET库学习020:基于 ML.NET + Tiny Yolo的实时视频流物体检测应用
  • Azure AI Document Intelligence与Microsoft Entra ID集成完全指南
  • 使用WebSocket进行通信的图形用户界面应用程序
  • 为什么要提倡尽早返回(Early Return)
  • 【现代深度学习技术】卷积神经网络04:多输入多输出通道
  • week 3 - More on Collections - Lecture 3