On the 51 microcontroller development board, operate the button settings, including pressing the button, displaying the corresponding position, and button password lock
1. Press the button to display the corresponding position
main.c
#include #include "Delay.h" #include "LCD1602.h" #include "MatrixKey.h" unsigned char KeyNum; void main() { LCD_Init(); LCD_ShowString(1,1,"helloworld"); while(1) { KeyNum=MatrixKey(); if(KeyNum) { LCD_ShowNum(2,1,KeyNum,2); } } } Delay.c void Delay(unsigned int xms) { unsigned char i, j; while(xms--) { i = 2; j = 239; do { while (--j); } while (--i); } } Delay.h #ifndef __DELAY_H__ #define __DELAY_H__ void Delay(unsigned int xms); #endif LCD1640.c #include //Pin configuration: sbit LCD_RS=P2^6; sbit LCD_RW=P2^5; sbit LCD_EN=P2^7; #define LCD_DataPort P0 //Function definition: /** * @brief LCD1602 delay function, 12MHz call can delay 1ms * @param None * @retval None */ void LCD_Delay() { unsigned char i, j; i = 2; j = 239; do { while (--j); } while (--i); } /** * @brief LCD1602 write command * @param Command The command to be written * @retval None */ void LCD_WriteCommand(unsigned char Command) { LCD_RS=0; LCD_RW=0; LCD_DataPort=Command; LCD_EN=1; LCD_Delay(); LCD_EN=0; LCD_Delay(); } /** * @brief LCD1602 write data * @param Data The data to be written * @retval None */ void LCD_WriteData(unsigned char Data) { LCD_RS=1; LCD_RW=0; LCD_DataPort=Data; LCD_EN=1; LCD_Delay(); LCD_EN=0; LCD_Delay(); } /** * @brief LCD1602 sets the cursor position * @param Line line position, range: 1~2 * @param Column Column position, range: 1~16 * @retval None */ void LCD_SetCursor(unsigned char Line,unsigned char Column) { if(Line==1) { LCD_WriteCommand(0x80|(Column-1)); } else if(Line==2) { LCD_WriteCommand(0x80|(Column-1+0x40)); } } /** * @brief LCD1602 initialization function * @param None * @retval None */ void LCD_Init() { LCD_WriteCommand(0x38);//Eight-bit data interface, two lines of display, 5*7 dot matrix LCD_WriteCommand(0x0c);//Display on, cursor off, flashing off LCD_WriteCommand(0x06);//After data reading and writing operations, the cursor automatically increases by one and the screen does not move. LCD_WriteCommand(0x01);//Reset the cursor and clear the screen } /** * @brief Display a character at the specified position on LCD1602 * @param Line line position, range: 1~2 * @param Column Column position, range: 1~16 * @param Char The character to be displayed * @retval None */ void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char) { LCD_SetCursor(Line,Column); LCD_WriteData(Char); } /** * @brief Start displaying the given string at the specified position on LCD1602 * @param Line starting line position, range: 1~2 * @param Column Starting column position, range: 1~16 * @param String The string to be displayed * @retval None */ void LCD_ShowString(unsigned char Line,unsigned char Column,char *String) { unsigned char i; LCD_SetCursor(Line,Column); for(i=0;String[i]!='';i++) { LCD_WriteData(String[i]); } } /** * @brief return value = X raised to the Y power */ int LCD_Pow(int X,int Y) { unsigned char i; int Result=1; for(i=0;i Result*=X; } return Result; } /** * @brief Start displaying the given number at the specified position on LCD1602 * @param Line starting line position, range: 1~2 * @param Column Starting column position, range: 1~16 * @param Number The number to be displayed, range: 0~65535 * @param Length The length of the number to be displayed, range: 1~5 * @retval None */ void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0'); } } /** * @brief Display the given number in signed decimal starting from the specified position on LCD1602 * @param Line starting line position, range: 1~2 * @param Column Starting column position, range: 1~16 * @param Number The number to be displayed, range: -32768~32767 * @param Length The length of the number to be displayed, range: 1~5 * @retval None */ void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length) { unsigned char i; unsigned int Number1; LCD_SetCursor(Line,Column); if(Number>=0) { LCD_WriteData('+'); Number1=Number; } else { LCD_WriteData('-'); Number1=-Number; } for(i=Length;i>0;i--) { LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0'); } } /** * @brief Display the given number in hexadecimal starting from the specified position on LCD1602 * @param Line starting line position, range: 1~2 * @param Column Starting column position, range: 1~16 * @param Number The number to be displayed, range: 0~0xFFFF * @param Length The length of the number to be displayed, range: 1~4 * @retval None */ void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i,SingleNumber; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { SingleNumber=Number/LCD_Pow(16,i-1)%16; if(SingleNumber<10) { LCD_WriteData(SingleNumber+'0'); } else { LCD_WriteData(SingleNumber-10+'A'); } } } /** * @brief Display the given number in binary starting from the specified position on LCD1602 * @param Line starting line position, range: 1~2 * @param Column Starting column position, range: 1~16 * @param Number Number to be displayed, range: 0~1111 1111 1111 1111 * @param Length The length of the number to be displayed, range: 1~16 * @retval None */ void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0'); } } LCD1640.h #ifndef __LCD1602_H__ #define __LCD1602_H__ //User calls function: void LCD_Init(); void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length); void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); #endif MatrixKey.c #include #include "Delay.h" unsigned char MatrixKey() { unsigned char KeyNumber = 0; P1=0xFF; P1_3=0; if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;} if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;} if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;} if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;} P1=0xFF; P1_2=0; if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;} if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;} if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;} if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;} P1=0xFF; P1_1=0; if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;} if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;} if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;} if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;} P1=0xFF; P1_0=0; if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;} if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;} if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;} if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;} return KeyNumber; } MatrixKey.h #ifndef __MATRIXKEY_H__ #define __MATRIXKEY_H__ unsigned char MatrixKey(); #endif 2.Key code lock Except for the main function, the functions in the password lock are different, and other library functions are the same. This is the benefit of modular programming. The user only needs to edit the logic part, and can call functions for programming other rule classes. main.c #include #include "Delay.h" #include "LCD1602.h" #include "MatrixKey.h" unsigned char KeyNum; unsigned int Password; unsigned int Count; void main() { LCD_Init(); LCD_ShowString(1,1,"password:"); while(1) { KeyNum=MatrixKey(); if(KeyNum) { if(KeyNum<=10) { if(Count<4) { Password*=10; //ÃÜÂë×óÒÆһλ Password+=KeyNum%10;//»ñȡһλÃÜÂë Count++; } LCD_ShowNum(2,1,Password,4); } if(KeyNum==11) { if(Password==2345) { LCD_ShowString(1,15,"OK"); Password=0; Count=0; LCD_ShowNum(2,1,Password,4); } else { LCD_ShowString(1,14,"ERR"); Password=0; Count=0; LCD_ShowNum(2,1,Password,4); } } if(KeyNum==12) { Password=0; Count=0; LCD_ShowNum(2,1,Password,4); } } } }
Previous article:7 common 51 microcontroller clock circuit diagrams
Next article:51 single chip microcomputer - digital tube display
- Popular Resources
- Popular amplifiers
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
- 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
- Gesture sensor
- The mastery of analog circuits is divided into three levels
- TI Automotive Headlight LED Lighting System (Single Stage) Solution
- Application of INA series current detection devices in TWS battery box
- KL25 uses low power timer to wake up MCU with low power consumption
- [RVB2601 Creative Application Development] Unboxing
- Would you consider domestic DDR4 memory?
- Analysis of the advantages and disadvantages of UWB high-precision positioning technology
- GD32E231 DIY Contest (4) - How to get long and short keys?
- How to Choose the Correct Thermistor for Your Temperature Sensor