/*
Program effect: The 51 single-chip computer key scans the digital tube display input, and the digital tube is displayed in increments, just like
inputting a number when making a phone call. The program is simple and compact
and relatively difficult to understand, but as long as you understand the previous program, this program
can be understood.
Programmer: http://www.51hei.com Please keep the copyright when reprinting.
Note: If you can't compile in Keil, please delete all leading blanks in each line,
*/
#include //Header file
#include
#define uchar unsigned char //Macro definition
#define uint unsigned int
sbit jdq=P3^5; //Bit declaration, drive relay pin
sbit fmq=P3^4; //Bit declaration, drive buzzer pin
code uchar table[]={0x3f,0x06,0x5b,//Number displayed by digital tube
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
code uchar key_tab[17]={ //This array is the keyboard code
0xed,0x7e,0x7d,0x7b, // 0,1,2,3,
0xbe,0xbd,0xbb,0xde, // 4,5,6,7,
0xdd,0xdb,0x77,0xb7, // 8,9,a, b,
0xee,0xeb,0xd7,0xe7,0xff}; // c,d,e,f,
uchar l_tmpdata[8]={0,0,0,0,0,0,0,0,0};
uchar l_key=0xff; //Define variables to store key values
uchar l_keyold=0xff;//As a credential for whether the key is released
void readkey(); //Sub-function to get the key value
void display(uchar *lp,uchar lc); //Display sub-function
void delay(); //Delay sub-function
void init(); //Initialization sub-function
void main() //Main function
{
init(); //Call the initialization sub-function
}
void init()
{
uchar i,j; //Define local variableswhile
(1)
{
readkey(); //Call the key value acquisition sub-function, that is, scan the keyboard and obtain the key
valueif((l_keyold!=0xff)&&(l_key<10)) //Only the keys 0-9 are processed
{
if(j<8) //Use j to represent the number of keys pressed, no more than 8j
++;
for(i=j-1;i>0;i--) //Circular shift
datal_tmpdata[i]=l_tmpdata[i-1];
l_tmpdata[0]=l_key; //After shifting the data, add the newly input data to
the taill_key=0xff;
}
display(l_tmpdata,j); //Output the obtained key code value
}
}
void readkey() //Sub-function to obtain the key value
{
uchar i,j,key;
j=0xfe; //Set the initial
valuekey=0xff;
for(i=0;i<4;i++) //The lower 4 bits of port P0 output 0 in a loop and scan the keyboard
{
P0=j;
if((P0&0xf0)!=0xf0) //Judge whether a key is pressed, if so: the upper 4 bits cannot all be 1
{
key=P0; //Read the value of P0 and exit the loop, otherwise continue the loop
break;
}
j=_crol_(j,1); //The function of this function is: left shift and circular shift
}
if(key==0xff) //If the value of port P0 cannot be read, such as interference, no processing is done
{
l_keyold=0xff;
fmq=1; //The button is released and the buzzer stops
ringingreturn;
}
if(l_keyold==key) //Check whether the button is released, if they are equal, it is not releasedreturn
;
fmq=0; //The button is pressed and the buzzer
soundsl_keyold=key; //Get the key code as a certificate
for releasefor(i=0;i<17;i++) //Look up the table to get the corresponding hexadecimal value and store it in l_key
{
if (key==key_tab[i])
{
l_key=i;
break;
}
}
}
void display(uchar *lp,uchar lc)//display sub-function
{
uchar i;
P2=0;
P1=0x07; //light up the eighth digital tubefor
(i=0;i
{
P2=table[lp[i]];
delay();
P2=0;
if(i==7)
break;
P1--;
}
}
void delay()//delay sub-function, that is, five empty instructions
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
Note: If it cannot be compiled, please delete all leading spaces in each line.
Reference address:Keyboard scanning-digital tube incremental display program
Program effect: The 51 single-chip computer key scans the digital tube display input, and the digital tube is displayed in increments, just like
inputting a number when making a phone call. The program is simple and compact
and relatively difficult to understand, but as long as you understand the previous program, this program
can be understood.
Programmer: http://www.51hei.com Please keep the copyright when reprinting.
Note: If you can't compile in Keil, please delete all leading blanks in each line,
*/
#include
#include
#define uchar unsigned char //Macro definition
#define uint unsigned int
sbit jdq=P3^5; //Bit declaration, drive relay pin
sbit fmq=P3^4; //Bit declaration, drive buzzer pin
code uchar table[]={0x3f,0x06,0x5b,//Number displayed by digital tube
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
code uchar key_tab[17]={ //This array is the keyboard code
0xed,0x7e,0x7d,0x7b, // 0,1,2,3,
0xbe,0xbd,0xbb,0xde, // 4,5,6,7,
0xdd,0xdb,0x77,0xb7, // 8,9,a, b,
0xee,0xeb,0xd7,0xe7,0xff}; // c,d,e,f,
uchar l_tmpdata[8]={0,0,0,0,0,0,0,0,0};
uchar l_key=0xff; //Define variables to store key values
uchar l_keyold=0xff;//As a credential for whether the key is released
void readkey(); //Sub-function to get the key value
void display(uchar *lp,uchar lc); //Display sub-function
void delay(); //Delay sub-function
void init(); //Initialization sub-function
void main() //Main function
{
init(); //Call the initialization sub-function
}
void init()
{
uchar i,j; //Define local variableswhile
(1)
{
readkey(); //Call the key value acquisition sub-function, that is, scan the keyboard and obtain the key
valueif((l_keyold!=0xff)&&(l_key<10)) //Only the keys 0-9 are processed
{
if(j<8) //Use j to represent the number of keys pressed, no more than 8j
++;
for(i=j-1;i>0;i--) //Circular shift
datal_tmpdata[i]=l_tmpdata[i-1];
l_tmpdata[0]=l_key; //After shifting the data, add the newly input data to
the taill_key=0xff;
}
display(l_tmpdata,j); //Output the obtained key code value
}
}
void readkey() //Sub-function to obtain the key value
{
uchar i,j,key;
j=0xfe; //Set the initial
valuekey=0xff;
for(i=0;i<4;i++) //The lower 4 bits of port P0 output 0 in a loop and scan the keyboard
{
P0=j;
if((P0&0xf0)!=0xf0) //Judge whether a key is pressed, if so: the upper 4 bits cannot all be 1
{
key=P0; //Read the value of P0 and exit the loop, otherwise continue the loop
break;
}
j=_crol_(j,1); //The function of this function is: left shift and circular shift
}
if(key==0xff) //If the value of port P0 cannot be read, such as interference, no processing is done
{
l_keyold=0xff;
fmq=1; //The button is released and the buzzer stops
ringingreturn;
}
if(l_keyold==key) //Check whether the button is released, if they are equal, it is not releasedreturn
;
fmq=0; //The button is pressed and the buzzer
soundsl_keyold=key; //Get the key code as a certificate
for releasefor(i=0;i<17;i++) //Look up the table to get the corresponding hexadecimal value and store it in l_key
{
if (key==key_tab[i])
{
l_key=i;
break;
}
}
}
void display(uchar *lp,uchar lc)//display sub-function
{
uchar i;
P2=0;
P1=0x07; //light up the eighth digital tubefor
(i=0;i
P2=table[lp[i]];
delay();
P2=0;
if(i==7)
break;
P1--;
}
}
void delay()//delay sub-function, that is, five empty instructions
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
Note: If it cannot be compiled, please delete all leading spaces in each line.
Previous article:ADC0831 analog-to-digital conversion program
Next article:Keyboard interface programming: addition and subtraction operations
Recommended Content
Latest Microcontroller Articles
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
- 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
MoreDaily News
- 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
Guess you like
- Leisure
- Recommended voltage regulator chip and MCU
- Design of an extremely low power digital thermometer using MSP430
- Hey guys, how did you fix this error?
- AD17 PCB space is invalid! What to do?
- Overview of TI's 28335 chip
- Discussion on reverse connection of MOS tube in protection circuit
- Bor application of bluenrg-1
- Download and debug interface issues on product circuit boards
- Multisim op amp output is greater than the power supply problem