//P1 port is connected to an external 3*4 matrix keyboard, where p1^0 does not need to be set to 1. Common methods for reading key values are:
Reference address:Lookup table matrix keyboard
//"Table Lookup Method" and "Reverse Method". The table lookup method is the shortest program, but it takes up a little more time. // A little storage space; the inversion method has the fastest execution speed, but requires the interface to be //Bidirectional. #include#include #define uint unsigned int #define uchar unsigned char uchar code table[]={ ~0x3F,~0x06,~0x5B,~0x4F,~0x66, ~0x6D,~0x7D,~0x07,~0x7F,~0x6F, ~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71}; //The common anode LED code is exactly opposite to the common cathode, so the cathode code is inverted uchar num; uchar keyscan(); /*void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); }*/ void main() { P0=table[0]; while(1) { num=keyscan(); if(num!=16) P0=table[num]; } } uchar keyscan() { uchar code table1[3][4]= {//0xee,0xde,0xbe,0x7e, 0xed,0xdd,0xbd,0x7d, 0xeb,0xdb,0xbb,0x7b, 0xe7,0xd7,0xb7,0x77}; uchar temp1=0xfd,temp2,i,j; //If it is 3*4, just change the initial value of temp1 to p1^0 without setting 1 and changing the table value for(i=0;i<4;i++)//Scan the lower four bits//If 3*4 keyboard i<3 is also possible { P1=temp1; //Output a line of 0 temp2=P1; //Read the status of P1 port immediately if((temp2&0xf0)!=0xf0)//If a key is pressed { for(j=0;j<4;j++)//Scan the upper four bits j=0 j=1 j=2 j=3 { if(temp2==table1[i][j])//look up table i=0 0 1 2 3 return i*4+j; //If found, return the key value i=1 4 5 6 7 } // i=2 8 9 10 11 } // i=3 12 13 14 15 else temp1=_crol_(temp1,1); // // i*4+j } return 16; //No code found, the return key is released }
Previous article:Play with LED dot matrix - scrolling display
Next article:STC MCU power-off free download
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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~~
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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
Guess you like
- 【RT-Thread Reading Notes】Reflections on RT-Thread Learning Chapter 6
- LTspice(2) .TEXT uses Xnn
- 【ST NUCLEO-H743ZI Review】——by supermiao123
- How to Reduce the Temperature in Your Car's Fuse Box
- 【What should I do】Bullshit
- STM32 supports VCP+MSC+HID mode
- How does AD20 perform multi-person collaboration? (Multiple picture warning
- 13. [Learning LPC1768 library functions] ADC experiment
- How often should flying probe test probes be replaced?
- Learn about SPICE for circuit simulation