Specific function implementation
When we press different keys in the matrix keys, the corresponding content is displayed on the LCD1602. Pressing a key multiple times at the same time can switch to different letters or numbers, and the LED light will flash. Finally, when we press confirm, the buzzer will sound with different frequencies.
device
Buzzer, AT89C51, several buttons, LCD1602, two LED lights, resistor
Simulation display diagram
Before simulation
After simulation
Knowledge introduction
Matrix button control principle
If independent buttons are used to connect to the microcontroller, each button requires an I/O port of the microcontroller. If a microcontroller system requires more buttons, using independent buttons will occupy too many I/O port resources. I/O port resources are often precious in single-chip microcomputer systems. In order to reduce I/O port pins when multiple buttons are used, matrix buttons are introduced.
Matrix key detection principle
Key detection is generally implemented by scanning. First, a certain column becomes low level, and the remaining columns are high level, and then it is detected whether a low level appears in each row. If it does not appear, it means that a certain row of keys has not been pressed. If it appears, it means that the key has been pressed and the position can be locked.
Matrix button control method
①Determinant method:
In the above picture, all the first columns are set to 0, and the remaining positions are all 1.
Set all the first rows to 0, and set all other bits to 1.
Therefore, we can conclude that the button pressed is exactly the first row and first column.
② Line flipping method:
First, set all rows to 0, then detect whether the column contains a low level, and if so, record the position of the column; then flip it, set all columns to 0, detect whether the row has a low level, and if so, record The position of the row (this article uses the line flipping method)
Main function code (C language) KEIL5 implementation
#include
#include void delay(unsigned char ms); void delay2(int i); void lcd_wcmd(unsigned char cmd); void lcd_pos(unsigned char pos); void lcd_wdat(unsigned char dat); void lcd_init(); void xianshi(); void KeyDown(); void la_ba(); sbit rs= P2^6; sbit rw = P2^5; sbit ep = P2^7; sbit d=P3^0; sbit d1=P3^1; sbit lb=P3^2; int b,c,s=0,q=0,w=0; #define GPIO_KEY P1 unsigned char dis1[32]; unsigned char dis2[9]={',','A','D','G','J','M','P','T','W'}; unsigned char dis3[9]={'1','2','3','4','5','6','7','8','9'}; unsigned int code laba[36][5]={ 1,2,3,3,3,//A 2,1,1,1,3,//B 2,1,2,1,3,//C 2,1,1,3,3,//D 1,3,3,3,3,//E 1,1,2,1,3,//F 2,2,1,3,3,//G 1,1,1,1,3,//H 1,1,3,3,3,//I 1,2,2,2,3,//J 2,1,2,3,3,//K 1,2,1,1,3,//L 2,2,3,3,3,//M 2,1,3,3,3,//N 2,2,2,3,3,//0 1,2,2,1,3,//P 2,2,1,2,3,//Q 1,2,1,3,3,//R 1,1,1,3,3,//S 2,3,3,3,3,//T 1,1,2,3,3,//U 1,1,1,2,3,//V 1,2,2,3,3,//W 2,1,1,2,3,//X 2,1,2,2,3,//Y 2,2,1,1,3,//Z 2,2,2,2,2,//0 1,2,2,2,2,//1 1,1,2,2,2,//2 1,1,1,2,2,//3 1,1,1,1,2,//4 1,1,1,1,1,//5 2,1,1,1,1,//6 2,2,1,1,1,//7 2,2,2,1,1,//8 2,2,2,2,1,//9 }; //LCD1602 code starts bit lcd_bz() { bit result; rs = 0; rw = 1; ep = 1; _nop_(); _nop_(); _nop_(); _nop_(); result = (bit)(P0 & 0x80); ep = 0; return result; } void lcd_wcmd(unsigned char cmd) { while(lcd_bz());//Determine whether the LCD is busy rs = 0; rw = 0; ep = 0; _nop_(); _nop_(); P0 = cmd; _nop_(); _nop_(); _nop_(); _nop_(); ep = 1; _nop_(); _nop_(); _nop_(); _nop_(); ep = 0; } void lcd_pos(unsigned char pos) { lcd_wcmd(pos | 0x80); } void lcd_wdat(unsigned char dat) { while(lcd_bz());//Determine whether the LCD is busy rs = 1; rw = 0; ep = 0; P0 = that; _nop_(); _nop_(); _nop_(); _nop_(); ep = 1; _nop_(); _nop_(); _nop_(); _nop_(); ep = 0; } void lcd_init() { lcd_wcmd(0x38); delay(1); lcd_wcmd(0x0c); delay(1); lcd_wcmd(0x06); delay(1); lcd_wcmd(0x01); delay(1); } void xianshi() { int i=0,j=1,n=0; while(dis1[i] != '') { lcd_pos(0x00);//Set the display position while(j!=0&&dis1[i] != '') { lcd_wdat(dis1[i]);//Display characters i++; j=i%16; } lcd_pos(0x40);//Set the display position j=1; while(j!=0&&dis1[i] != '') { lcd_wdat(dis1[i]);//Display characters i++; j=i%16; } j=1; if(dis1[i] != '') i-=16; } i=0; } //LCD1602 code ends void delay(unsigned char ms) { unsigned char i; while(ms--) { for(i = 0; i< 250; i++) { _nop_(); _nop_(); _nop_(); _nop_(); } } } void delay2(int i) { while(i--); } //Press the button void KeyDown() { int KeyValue=0; GPIO_KEY=0x0f; if(GPIO_KEY!=0x0f) { delay(5); //Debounce if(GPIO_KEY!=0x0f) { GPIO_KEY=0X0F; switch(GPIO_KEY) { case(0X07): KeyValue=0;break; case(0X0b): KeyValue=1;break; case(0X0d): KeyValue=2;break; case(0X0e): KeyValue=12;break; } } GPIO_KEY=0XF0; switch(GPIO_KEY) { case(0X70): KeyValue=KeyValue;break; case(0Xb0): KeyValue=KeyValue+3;break; case(0Xd0): KeyValue=KeyValue+6;break; case(0Xe0): KeyValue=KeyValue+9;break; } while(GPIO_KEY!=0xf0); if(KeyValue!=c&&dis1[w]!='') {b=0;w++;} if(KeyValue<=8&&s==0) { if(c==12||c==15)w--; dis1[w]=dis2[KeyValue]+b; d=0;delay(25);d=1;delay(25); } else if(KeyValue<=8&&s==1) { if(c==12||c==15)w--; dis1[in]=dis3[KeyValue]; d=0;delay(25);d=1;delay(25); } else if(KeyValue==12) { //When the key is 12, delete one digit (backspace) if(w>0) { In--; dis1[w]=' '; } else { dis1[w]=' '; } d1=0;delay(25);d1=1;delay(25); } else if(KeyValue==15) { //When 15 is pressed, add a space while(w!=0) { In--; dis1[w]=' '; } } else if(KeyValue==9) { //display number 7 q++; s=q%2; if(c==12||c==15)w-=2; } else if(KeyValue==10) { //display number 8 if(s==0) dis1[w]=' '; else dis1[w]='0'; } else if(KeyValue==11) { //display number 9 dis1[w]=' '; } else if(KeyValue==18) { dis1[w]=' '; In--; } else if(KeyValue==21) { la_ba(); } b++; if(b==3)b=0; c=KeyValue; } } //buzzer void la_ba() { int i,j,t; for(i=0;dis1[i]!='';i++) { if(dis1[i]>=65&&dis1[i]<=90) { d1=0;delay(25);d1=1;delay(25); for(j=0;laba[dis1[i]-65][j]!=3;j++) { if(laba[dis1[i]-65][j]==1) { t=100; while(t--) { lb=~lb; delay2(70); } delay(50); } if(laba[dis1[i]-65][j]==2) { t=300; while(t--) { lb=~lb; delay2(70); } delay(50); } if(laba[dis1[i]-65][j+1]==3) { delay(100); } } } if(dis1[i]>=48&&dis1[i]<=57) { d=0;delay(25);d=1;delay(25); for(j=0;j<5;j++) { if(laba[dis1[i]-22][j]==1) { t=100; while(t--) { lb=~lb; delay2(70); } delay(50); } if(laba[dis1[i]-22][j]==2) { t=300; while(t--) { lb=~lb; delay2(70); } delay(50); } if(j==4) { delay(100); } } } } } void main() { lcd_init();//Initialize LCD delay(10); while(1) { KeyDown(); xianshi(); } }
Previous article:Alternative entry methods and programming ideas for 51 microcontrollers
Next article:Implementing "reversing radar" anti-collision system based on HC-SR04 ultrasonic module
Recommended ReadingLatest update time:2024-11-16 10:24
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- Hand-drawn illustrations revealing the secrets of communication circuits and sensor circuits (Mims III)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Commonly used PCB software on the market and corresponding common file suffixes
- Which NB-IOT module is better?
- Ready to use, STM32G474 project of HST221 temperature and humidity sensor
- [SGP40 Review] 1. Unboxing Review
- Do you use fast charging? How long does it take for you to fully charge your phone?
- Comparison of CC2540 and nRF51822 application development
- The main purpose of immersion gold circuit board and the difference from other processes
- TAxIV interrupts for msp430
- Please recommend: Python classic textbook!
- EEWORLD University Hall----Live Replay: 100G/400G Optical Communication Test Solutions