MCU 4*4 matrix keyboard

Publisher:SparklingRiverLatest update time:2017-02-06 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 Use the microcontroller P1 port to expand into a 4*4 keyboard. Press the key to light up the LED of the P0 port. 

 

The expansion of the matrix keyboard is not complicated, but the detection code part is complicated. 

 

IO expansion principle

 

Matrix keyboard.png

    A matrix keyboard, also known as a row-column keyboard, is a keyboard that uses four I/O lines as row lines and four I/O lines as column lines.

A key is set at each intersection of a row line and a column line, so that the number of keys on the keyboard is 4*4.

This matrix keyboard structure can effectively improve the utilization rate of I/O ports in the single-chip computer system.

 

Detection principle

 

    When no key is closed, the row line and column line are open. When a key is closed, the two I/O port lines connected to the closed key are short-circuited.

 

    How to determine whether a key is pressed:

  1. Coarse scan. The row line outputs low level, and the column line outputs high level. Read the Px port, if the data is not equal to 0x0F, a key is pressed.

  2. Fine scan. The row lines output low level row by row, and other IO ports output high level. Read the Px port, if the data is not equal to the output value, a key is pressed. And the number of rows can be determined. Similarly, the column lines output low level row by row, and the number of columns can be detected.

  3. Convert key codes according to row and column numbers 

 


code show as below:

#include 
#define uchar unsigned char  

#define LED_ON(X) P0 = ~(1<<(X)) //LED turns on at low level

uchar KeyScan(void);

void main(void)
{
	uchar key;

	while(1){
		key = KeyScan();

		if(key > 0)
		{
			key = key -1;
			LED_ON(key); 
		} 
	}
}

uchar KeyScan(void)						
{

	uchar i,val,row,col,key;

	P1 = 0xF0; //Row line set to 0, column line set to 1

	if( P1 != 0x0F0 ) //button pressed
	{
		for(i=0;i<4;i++) //row scan	
		{
			val = 0xFF&(~(1<


Keywords:MCU Reference address:MCU 4*4 matrix keyboard

Previous article:C51 MCU LED Example Program Set
Next article:LCD1602 liquid crystal display Chinese characters

Recommended ReadingLatest update time:2024-11-23 19:03

The driving principle and design of buzzer in single chip microcomputer
A buzzer is an electronic sounder with an integrated structure. This article introduces how to use a single-chip microcomputer to drive a buzzer. It is widely used as a sound-generating device in electronic products such as computers, printers, copiers, alarms, and telephones.   Buzzers are mainly divided into two typ
[Microcontroller]
Interrupt mechanism of STM32 microcontroller
This picture is a schematic diagram of an external interrupt line or external event line. There is a slash across the signal line and a note with the word 19 next to it, indicating that there are 19 sets of such lines. The blue dotted arrow in the figure marks the transmission path of the external interrupt signa
[Microcontroller]
Interrupt mechanism of STM32 microcontroller
Research on LCD Display Technology of Low-cost Single-Chip Microcomputer System
  1 Introduction   With the rapid development of electronic technology, the design of electronic circuits has become increasingly complex. Microcontrollers have been increasingly widely used in electronic design because of their high integration, high stability and low cost.   First, although the use of digital di
[Microcontroller]
Research on LCD Display Technology of Low-cost Single-Chip Microcomputer System
AVR MCU related
Model identification analysis of AVR microcontrollers   1. The letter following the model indicates the voltage operating range. With "V": 1.8-5.5V; if it is not included, without "V": 2.7-5.5V.   Example: ATmega48-20AU, without "V" means the operating voltage is 2.7-5.5V.   2. The digital part of the suffix indicat
[Microcontroller]
Global semiconductor sales in 2019 fell 12% to $412 billion, with the Americas suffering the most
On February 3rd local time, the Semiconductor Industry Association (SIA) announced that global semiconductor industry sales in 2019 were US$412.1 billion, a decrease of 12.1% from the total in 2018.     SIA said global sales in December 2019 were $36.1 billion, down 5.5% from the same period last year and down 1.7%
[Semiconductor design/manufacturing]
Global semiconductor sales in 2019 fell 12% to $412 billion, with the Americas suffering the most
Multifunctional dual-interface memory design based on MSP430FR series MCU
This article introduces a method to implement multifunctional dual-interface memory based on TI's new generation MSP430FR series MCU. Compared with traditional memory (such as FLASH, SRAM, EEPROM), FRAM has more advantages and more powerful functions. Using the flexibility of MCU, users can design a powerful and flexi
[Microcontroller]
Multifunctional dual-interface memory design based on MSP430FR series MCU
Delixi inverter em60g7r5t4b parameter setting
Delixi inverter EM60G7R5T4B is a high-performance inverter, widely used in industrial automation, electric power, metallurgy, chemical industry, building materials and other industries. This article will introduce the parameter settings of Delixi inverter EM60G7R5T4B in detail, including basic parameters, control mo
[Embedded]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号