#include#define uint unsigned int #define uchar unsigned char sbit DSCQ=P2^6; //bit defines the segment latch control terminal sbit WSCQ=P2^7; //bit definition bit latch control terminal uchar code table[]={0x3f,0x06,0x5b,0x4f, //0,1,2,3 0x66,0x6d,0x7d,0x07, //4,5,6,7 0x7f,0x6f,0x77,0x7c, //8,9,a,b 0x39,0x5e,0x79,0x71};//c,d,e,f void delay(uint t); //declare delay function void keyscan(); //Declare scanning function void main() { DSCQ=0; //Segment latch latch WSCQ=0; //bit latch latch P0=0x00; //P0 port outputs 0000 0000 WSCQ=1; //bit latch allows WSCQ=0; //Bit latch latch (the ground terminals of the digital tubes are all low level 0) P0=table[8]; //P0 port output 8 DSCQ=1; //Segment latch allows while(1) { keyscan(); //constant scan } } void delay(uint t) { uint x,y; for(x=t;x>0;x--) { for(y=120;y>0;y--) { } } } void keyscan() { uchar key1,key2; P3=0xfe; //1111 1110 (set P3.0 to low level 0. Write 1 to the high 4 bits to read the status of the 4 high I/O ports) key1=P3; //Read P3 port for the first time delay(15); //delay 15ms key2=P3; //Read P3 port for the second time key1=key1&0xf0;//1111 0000 (keep the high 4 bits of the first value and mask the low 4 bits) key2=key2&0xf0;//1111 0000 (keep the upper 4 bits of the second value and mask the lower 4 bits) if((key1==key2)&&key1!=0xf0)
//If the value of key1 is equal to the value of key2, and the value of key1 is not equal to 1111 0000, it means that a key is pressed. { switch(key1) { case 0xe0:P0=table[0];break;//1110 0000 case 0xd0:P0=table[1];break;//1101 0000 case 0xb0:P0=table[2];break;//1011 0000 case 0x70:P0=table[3];break;//0111 0000 } } P3=0xfd; //1111 1101 (set P3.1 to low level 0. Write 1 to the upper 4 bits to read the status of the 4 high-bit I/O ports) key1=P3; //Read P3 port for the first time delay(15); //delay 15ms key2=P3; //Read P3 port for the second time key1=key1&0xf0; //1111 0000 retain the upper 4 bits (retain the upper 4 bits of the first value and mask the lower 4 bits) key2=key2&0xf0; //1111 0000 retain the upper 4 bits (retain the upper 4 bits of the second value and mask the lower 4 bits) if((key1==key2)&&key1!=0xf0)
//If the value of key11 is equal to the value of key2, and the value of key1 is not equal to 1111 0000, it means that a key is pressed. { switch(key1) { case 0xe0:P0=table[4];break;//1110 0000 case 0xd0:P0=table[5];break;//1101 0000 case 0xb0:P0=table[6];break;//1011 0000 case 0x70:P0=table[7];break;//0111 0000 } } P3=0xfb; //1111 1011 (set P3.2 to low level 0. Write 1 to the high 4 bits to read the status of the 4 high I/O ports) key1=P3; //Read P3 port for the first time delay(15); //delay 15ms key2=P3; //Read P3 port for the second time key1=key1&0xf0;//1111 0000 (keep the high 4 bits of the first value and mask the low 4 bits) key2=key2&0xf0;//1111 0000 (keep the upper 4 bits of the second value and mask the lower 4 bits) if((key1==key2)&&key1!=0xf0)
//If the value of key11 is equal to the value of key2, and the value of K1 is not equal to 1111 0000, it indicates that a key is pressed { switch(key1) { case 0xe0:P0=table[8];break; //1110 0000 case 0xd0:P0=table[9];break; //1101 0000 case 0xb0:P0=table[10];break;//1011 0000 case 0x70:P0=table[11];break;//0111 0000 } } P3=0xf7; //1111 0111 (set P3.3 to low level 0. Write 1 to the upper 4 bits to read the status of the 4 high-bit I/O ports) key1=P3; //Read P3 port for the first time delay(15); //delay 15ms key2=P3; //Read P3 port for the second time key1=key1&0xf0;//1111 0000 (keep the high 4 bits of the first value and mask the low 4 bits) key2=key2&0xf0;//1111 0000 (keep the upper 4 bits of the second value and mask the lower 4 bits) if((key1==key2)&&key1!=0xf0)
//If the value of key1 is equal to the value of key2, and the value of key1 is not equal to 1111 0000, it means that a key is pressed. { switch(key1) { case 0xe0:P0=table[12];break;//1110 0000 case 0xd0:P0=table[13];break;//1101 0000 case 0xb0:P0=table[14];break;//1011 0000 case 0x70:P0=table[15];break;//0111 0000 } } }
Previous article:Make the buzzer sing "Beautiful Girl"
Next article:Summary of Single Chip Microcomputer Delay Program
Recommended ReadingLatest update time:2024-11-16 15:19
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
- How to use the crystal oscillator and capacitor in the TaunchPad G2 kit?
- How to set the PCB wiring to 100 Ω uniform routing?
- Hello everyone, I made a microphone, and I need to use a preamplifier, I use MC2830D, the effect is average
- Tomorrow's live broadcast: The development and latest applications of sensors in industrial motors. Make an appointment now to get 2 chances to win a prize draw.
- ADI New Chinese Information (July 2019)
- STM32L151RCT6 cannot enter low power after downloading the program
- The CC2541 is connected to the Bluetooth module, but the Bluetooth module cannot receive the data it sends.
- Selection of digital input chips such as liquid level switches
- 【NXP Rapid IoT Review】+ Unboxing Review
- micrpython