Microcontroller: Matrix keyboard and LCD1602

1. Matrix keyboard

                            

1. The method of detecting the button:

(1) First, assign all 1-4 to a low level, and 5-8 to a high level. When a button is pressed, there will be a point in 5-8 that will be pulled low.

(2) Then set one position in 1-4 to 0 and other positions to 1 in turn, and observe the corresponding return value to determine which button is pressed.

2. Corresponding program

unsigned char bsp_mkeyscan()
{
    unsigned char keyvalue = 0x99;
	KEYPORT = 0xF0;
	if (KEYPORT != 0xF0)
	{
		KEYPORT = 0xFE;
		if (KEYPORT != 0xFE)
		{
			keyvalue = KEYPORT;
			while(keyvalue == KEYPORT)bsp_segb();;
		}
		KEYPORT = 0xFD;
		if (KEYPORT != 0xFD)
		{
			keyvalue = KEYPORT;
			while(keyvalue == KEYPORT)bsp_segb();;
		}
		KEYPORT = 0xFB;
		if (KEYPORT != 0xFB)
		{
			keyvalue = KEYPORT;
			while(keyvalue == KEYPORT)bsp_segb();;
		}
		KEYPORT = 0xF7;
		if (KEYPORT != 0xF7)
		{
			keyvalue = KEYPORT;
			while(keyvalue == KEYPORT)bsp_segb();;
		}
	}
	return keyvalue; //根据keyvalue的值即可以确定按下的键
}

2. LCD1602

1. LCD principle

LCD (Liquid Crystal Display): the abbreviation of liquid crystal display, a passive display, that is, the liquid crystal itself does not emit light, but after the liquid crystal is processed, it can change the characteristics of the light passing direction, and achieve black characters on a white background or white characters on a black background. displayed purpose.
  Liquid crystal displays have the advantages of low power consumption and strong anti-interference ability, and are widely used in instrumentation and control systems.

2. LCD classification

According to the arrangement shape, it is divided into: field type, dot matrix character type and dot matrix graphic.
 Field types
      are widely used in spreadsheets, digital meters, and calculators.
 The dot matrix character type
      displays letters, numbers and symbols. It is composed of 5 × 7 or 5 × 4 lattices
and is widely used in single-chip microcomputer application systems.
 In devices such as dot-matrix graphics
      notebook computers and color TVs.

 1602 LCD is also called 1602 character LCD. It is a dot matrix LCD module specially used to display letters, numbers, symbols, etc. It consists of several 5X7 or 5X11 dot matrix character bits, each dot matrix character bit can be displayed a character. There is a dot space between each bit, and there are also spaces between each line, which play the role of character spacing and line spacing.

 1602LCD means that the displayed content is 16X2, that is, it can display two lines, each line of 16 characters LCD module.

3. Pins of LCD1602

                

Ground pin 1

The second pin is connected to the 5V positive power supply

The 3rd pin VL is the contrast adjustment terminal of the liquid crystal display. The contrast is the weakest when connected to a positive power supply, and the highest when the contrast is grounded. When the contrast is too high, a "ghost image" will be produced. The contrast can be adjusted by a 10K potentiometer during use.

Pin 4: RS is the register selection, select the data register when the high level, select the instruction register when the low level.

第5脚:R/W为读写信号线,高电平时进行读操作,低电平时进行写操作。当RS和R/W共同为低电平时可以写入指令或者显示地址RS为低电平R/W为高电平时可以读忙信号RS为高电平R/W为低电平时可以写入数据。

第6脚:E端为使能端,当E端由高电平跳变成低电平时,液晶模块执行命令。

第7~14脚:D0~D7为8位双向数据线。

第15脚:背光源正极。
第16脚:背光源负极。

4、显示地址映射

第一行第一个的地址是:80H

第二行第一个的地址是:C0H

5、字符发生存储器

 在对液晶模块的初始化中要先设置其显示模式,在液晶模块显示字符时光标是自动右移的,无需人工干预。每次输入指令前都要判断液晶模块是否处于忙的状态。

 1602液晶模块内部的字符发生存储器(CGROM)已经存储了128个不同的点阵字符图形,这些字符有:阿拉伯数字、英文字母的大小写、常用的符号等,每一个字符都有一个固定的代码(ASCII字符集字库),比如大写的英文字母“A”的代码是01000001B(41H),显示时模块把地址41H中的点阵字符图形显示出来,我们就能看到字母“A”

6、LCD指令说明
 指令1:清显示,指令码01H,光标复位到地址00H位置。
 指令2:光标复位,光标返回到地址00H。
 指令3:光标和显示模式设置 I/D:光标移动方向,高电平右移,低电平左移S:屏幕上所有文字是否左移或者右移。高电平表示有效,低电平则无效
 指令4:显示开关控制。 D:控制整体显示的开与关,高电平表示开显示,低电平表示关显示; C:控制光标的开与关,高电平表示有光标,低电平表示无光标;B:控制光标是否闪烁,高电平闪烁,低电平不闪烁。
 指令5:光标或显示移位 S/C:高电平时移动显示的文字,低电平时移动光标。
 指令6:功能设置命令 DL:高电平时为4位总线,低电平时为8位总线   N:低电平时为单行显示,高电平时双行显示 F: 低电平时显示5x7的点阵字符,高电平时显示5x10的点阵字符。
 指令7:字符发生器RAM地址设置。
 指令8:DDRAM地址设置。
 指令9:读忙信号和光标地址 BF:为忙标志位,高电平表示忙,此时模块不能接收命令或者数据,如果为低电平表示不忙。
 指令10:写数据。
 指令11:读数据。
7、LCD1602的初始化过程。
延时15mS
 写指令38H(不检测忙信号)
 延时5mS
 写指令38H(不检测忙信号)
 延时5mS
 写指令38H(不检测忙信号)
 以后每次写指令、读/写数据操作均需要检测忙信号
 写指令38H:显示模式设置
 写指令08H:显示开
 写指令01H:清屏
 写指令06H:显示光标移动设置
 写指令0CH:显示开及光标设置
8、自定义字符显示
(1)1602LCD共有8个自定义字符。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324458178&siteId=291194637