// Crystal: 8.0000Mhz
//key.h
#ifndef _4X4_H_
#define _4X4_H_
#define No_key 255
#define key_port PORTB
#define key_port_ddr DDRB
#define key_port_pin PINB
unsigned char keyboard_Scan(void);
#endif
//key.c
#include
#include
#include "key.h"
const unsigned char key_table[16] =
{
1, 2, 3,12,
4, 5, 6,13,
7, 8, 9,14,
10,0,11,15,
};
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i delay_1ms();
}
//4x4 matrix keyboard scanning function
unsigned char keyboard_Scan(void)
{
unsigned char temp,key,row,Column;
key_port_ddr = 0b00001111; // high four bits input row line / low four bits output column line
key_port = 0b11110000; // high four bits open pull-up resistor / low four bits output low level
delay_nus(5); // delay 5us
if((key_port_pin & 0xF0)!= 0xF0) // make an initial check to see if any key is pressed, if not, return
{ // if not all row lines are 1, a key may be pressed
delay_nms(5); // delay debounce
//Set the initial value of the column line 3~0=1110
for(Column=0,key_port=0b11111110;Column<4;Column++)
{
for(row=0,temp=0b11101111;row<4;row++)//Set the initial value of the row line 7~4=1110
{
while((key_port_pin & 0xF0)==(temp & 0xF0))//Enter the row line and check whether there is a key pressed in this row
{
key=4*row+Column;//Key code = 4*row input value + column scan valuekey
=key_table[key];//Keyboard code conversion key valuereturn
(key);
}
temp<<=1;//Shift the row line left by 1 bit
}
key_port=((key_port<<1)|0x01);//Shift the column line scan value left by 1 bit and scan the next row
}
}
return (No_key);
}
//main.c
#include
#include
#include "key.h"
void main(void)
{
unsigned char Key;
while(1)
{
Key = keyboard_Scan(); // 键盘扫描
switch(Key)
{
case 0:
break;
......
default:
break;
}
}
}
I personally think it is better to use a timer for the delay in the main function.
The key_table array can be modified if the circuit connection is different
Previous article:AVR six IO ports drive LCD1602
Next article:AVR Learning Experience (IV)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- The maximum compensation is more than 200,000 yuan, and Toshiba’s Dalian factory is closed!
- Could you please help me analyze what the Darlington tube does in this circuit? Thank you.
- 550 Cases of Electrical Engineering Design and Calculation
- About BMS secondary protection chip
- [Perf-V Evaluation] Clock generation based on Perf-V development board
- Correctly understand the phase difference caused by capacitance and inductance
- 【SoC】Realize Ethernet interaction function
- Can anyone provide a download link for the AD package library (with 3D effects)? It's quite complete. Thank you.
- [ESP32-Korvo Review] (1) Development Board Circuit Connection
- What to do if ESP32-WROOM-32 has high power consumption when connected to the Internet