Keyboard scanning-digital tube incremental display program

Publisher:zeta16Latest update time:2012-11-10 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*
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

Previous article:ADC0831 analog-to-digital conversion program
Next article:Keyboard interface programming: addition and subtraction operations

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号