MCU keyboard C program

Publisher:psi33Latest update time:2012-12-11 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

void int1proc() interrupt IE1_VECTOR using 1
{
unsigned char i, key;
code unsigned char PS2TAB[] = {//20-key PS2 keypad key code table
0x70,//0
0x69,//1
0x72,//2
0x7a,//3
0x6b,//4
0x73,//5
0x74,//6
0x6c,//7
0x75,//8
0x7d,//9
0x05,//F1
0x06,//F2
0x04,//F3
0x0c,//F4
0x03,//F5
0x0b,//F6
0x5a,//Enter
0x76,//Esc
0x66,//Bksp
0x71//KP.
};
if (PS2Buffers.PS2KeyCount == 0){//PS2 start bit testif
(!PS2CLOCK && !PS2DATA){//Low level is the start bitif
(PS2Buffers.PS2KeyExtFlage != 0xf0){
PS2Buffers.PS2KeyTemp = 0;
PS2Buffers.PS2KeyExtFlage = 0;
PS2Buffers.PS2KeyPopError = 0;
}
PS2Buffers.PS2KeyCount ++;//Pulse count
}
else PS2Buffers.PS2KeyPopError = 0xeb;//Set key start bit error number 0xeb
}
else if (PS2Buffers.PS2KeyCount < 9){//PS2 data bit
key = PS2Buffers.PS2KeyTemp;//Get keyboard buffer shift data
key >>= 1;
if (PS2DATA) key = 0x80;
PS2Buffers.PS2KeyTemp = key;
PS2Buffers.PS2KeyCount++;//Pulse count
}
else if (PS2Buffers.PS2KeyCount == 9){//PS2 data odd parity
ACC = PS2Buffers.PS2KeyTemp;//Get keyboard buffer shift data (C51 takes even parity)
if (P != PS2DATA) PS2Buffers.PS2KeyCount ++;//Pulse count
else{
PS2Buffers.PS2KeyPopError = 0xec;////Set key odd parity error number 0xec
PS2Buffers.PS2KeyCount = 0;//Pulse count resets to zero
}
}
else if (PS2Buffers.PS2KeyCount == 10){//Stop bit
key = PS2Buffers.PS2KeyTemp;//Get keyboard buffer shift data
PS2Buffers.PS2KeyCount = 0;//Pulse count resets to zero
if (PS2DATA){//High level is stop bit
if (key == 0xe0){//This is an extended
keyPS2Buffers.PS2KeyExtFlage = 0xe0;//Set the extended key flag (the small keyboard only has the Enter key)
}
else if (key == 0xf0){//This is a key break code, key releasePS2Buffers.PS2KeyExtFlage
= 0xf0;//Set the key release flag
}
else{//This must be a key valueif
((key == 0xaa) (key == 0xfa)) PS2Buffers.PS2KeyPushCount = 0;//Long
key press counter clearselse
{
PS2CLOCK = 0;//Prevent the PS2 keyboard from sending back data immediatelyPS2Buffers.PS2KeyPopError
= 0xaa;//Error codefor
(i = 0; i < 20; i++){
if (key == (PS2TAB[i])){//Searchkey
= i + 1;
if (PS2Buffers.PS2KeyExtFlage == 0xf0){
key = 0x80; //Key release
PS2Buffers.PS2KeyPushCount = 0; //Long press key counter cleared
PS2Buffers.PS2KeyMessage = key; //Save current key value and execute command
}
else{
if (key != PS2Buffers.PS2KeyVal){//Changed a
keyPS2Buffers.PS2KeyPushCount = 0;//Reset the long-press key
counterPS2Buffers.PS2KeyMessage = key;//Save the current key value and execute the command
}
else{//The key has not been changedPS2Buffers.PS2KeyPushCount
++;//Long-press key counter countif
(PS2Buffers.PS2KeyPushCount > DEFPS2PUSHCOUNT){//Long-press time is
upPS2Buffers.PS2KeyMessage = key 0x40;//Save the current key value and execute the
commandPS2Buffers.PS2KeyPushCount
= 0;//Reset the long-press key counter
}
}
}
PS2Buffers.PS2KeyVal = key;//Save the current key value 1~20 or 0x80+(1~20)
PS2Buffers.PS2KeyTemp = 0;//Key code shift recorder
PS2Buffers.PS2KeyExtFlage = 0;//Extended key flag
PS2Buffers.PS2KeyPopError = 0;//Key release flag or error code
break;
}
}
if (PS2Buffers.PS2KeyPopError) PS2Buffers.PS2KeyPushCount = 0;//Long press key
Counter cleared
PS2CLOCK = 1;//Release PS2 clock bus
}
}
}
else PS2Buffers.PS2KeyPopError = 0xed;//Set stop bit error number 0xed
}
else PS2Buffers.PS2KeyCount = 0;//PS2 keyboard error
}[page]
/*------------------------------------------------------------------
Run PS2 keyboard scattered transfer command using function pointer array
-------------------------------------------------------------------*/
void PS2CommandExec(unsigned char key)
{
unsigned int i;
code void *funcpushbuffers[] = {//Command transfer table (16 DWs in assembly)
/*-------------------------------------------------------------------
Keyboard c program 15 key press, key release and long key press event processing function pointer address
-------------------------------------------------------------------*/
(void *)ClrWdt + 0x0000,
/*---------------------------------------------
10 function key press event processing (independent management)
----------------------------------------------*/
(void *)PS2F1KeyPush + 0x5b7d, //Function key F1 key press event processing
(void *)PS2F2KeyPush + 0xa6ea, //Function key F2 key press event processing
(void *)PS2F3KeyPush + 0xf157, //Function key F3 key press event processing
(void *)PS2F4KeyPush + 0x4cc4, //Function key F4 key press event processing
(void *)PS2F5KeyPush + 0x9731, //Function key F5 key press event processing
(void *)PS2F6KeyPush + 0xe2ae, //Function key F6 key press event processing
(void *)PS2EnterKeyPush + 0x3d1b, //Function key Enter key press event processing
(void *)PS2EscKeyPush + 0x8888, //Function key Esc key press event processing
(void *)PS2BkspKeyPush + 0xd3f5, //Function key Bksp key press event processing
(void *)PS2KpKeyPush + 0x2e62, //Function key Kp key press event processing
/*---------------------------------------------
2 key release event processing (centralized management)
----------------------------------------------*/
(void *)PS2NumberKeyPop + 0x79df, //Number key release event processing
(void *)PS2FuncKeyPop + 0xc44c, //Function key release event processing
/*---------------------------------------------
2 long key press event processing (centralized management)
------------------------------*/
(void *)PS2NumberKeyPushL + 0x1fb9, //Number key long press event processing
(void *)PS2FuncKeyPushL + 0x6a26, //Function key long press event processing
/*---------------------------------------------
1 number key press event processing (centralized management)
----------------------------------------------*/
(void *)PS2NumberKeyPush + 0xb593 //Number key 0~9 press event processing
};
/*--------------------------------------------*/
i = key;
key &= 0x3f;//Remove the key release and long press flags, and get the true key codekey
--;
if (key < 20){//Only 20 keysif
(key < 10){//Number keys 0~9
if (i <= 10) key = 15;//(scattered transfer number 0) press number keys 0~9
else{
if (i & 0x80) key = 11;//(scattered transfer number 11) release number keys 0~9
else key = 13;//(scattered transfer number 13) long press number keys 0~9
}
}
else{//Press function keys F1~F6, Enter..KP
if ((i & 0xc0) == 0) key -= 9;//(scattered transfer number 1~10) press function keys F1~F6, Enter..KP
else{
if (i & 0x80) key = 12;//(scattered transfer number 12) release function key
else key = 14;//(scattered transfer number 14) long press function key
}
}
ClrWdt(); //Feed the dog (who knows how long the keyboard program will run, just feed it first)
i = ((key * 53 & 0xf) * 0x1000)+ ((key * 43 & 0xf) * 0x100) + ((key * 23 &
0xf) * 0x10) + (key * 13 & 0xf);
_icall_((void *)funcpushbuffers[key] - i); //Get the keyboard transfer table and execute the keyboard command
}
}
This keyboard program is very different from the general ones on the Internet. It makes full use of external interrupts (0 words) hotpower[1 time]

****The following is a version of the single-chip keyboard C program. Welcome readers to experiment. http://www.51hei.com to compile **************************************

unsigned CHAR key,key_h,kpush;
unsigned int key_l;

//Buttons connected to p1.0, p1.1, p1.2

void int_t0(void) interrupt 1 {
unsigned CHAR dd,i;
TL0=TL0+30;TH0=0xfb; //800
/* key identification */
if ((P1&0x7)==0x7) {
if ((key_l>30)&&(key_l<800)&&(key_h>30)) { //Release the key. If the previous key press time is less than 1 second, read the key value
key=kpush;
}
if ((++key_h)>200) key_h=200;
key_l=0;
if (key>=0x80) key=0; //If the previous key was pressed for 1 second, clear the key value
} else {
kpush=P1&0x7;
key_l++;
if ((key_l>800)&&(key_h>30)) { //If the key is pressed for more than 1 second, add 0x80 to the key value to indicate a long key
key=kpush|0x80;
key_h=0;
key_l=0;
}
}
}
void main(void) {
TMOD=0x1;TR0=1;ET0=1;EA=1;
while (1) {
while (!key) {}
SWITCH (key) {
case 1:break;
case 2:break;
}
}
}

Keywords:MCU Reference address:MCU keyboard C program

Previous article:Notes on single chip microcomputer programming and stepper motor control
Next article:Design of air pressure altimeter based on MCU

Recommended ReadingLatest update time:2024-11-16 17:53

51 single chip microcomputer automatic watering system
The microcontroller source program is as follows: #include reg51.h #define uchar unsigned char #define uint unsigned int sbit ad_cs=P1^3; //ADC0832 control bit sbit ad_clk=P1^0; sbit ad_dat=P1^1; sbit lcd_rs=P2^7; //LCD1602 control bit sbit lcd_e=P2^6; sbit key1=P1^4; //Independent key sbit key2=P3^2; sbit key
[Microcontroller]
51 single chip microcomputer automatic watering system
MCU smoke alarm program + PCB + schematic diagram
  The microcontroller source program is as follows: #include reg52.h //Include header file, generally no need to change, the header file contains the definition of special function registers #include "intrins.h"                                                                                            #define
[Microcontroller]
MCU smoke alarm program + PCB + schematic diagram
NRF24L01 receiving program (MCU is STC89C52)
#include #include intrins.h typedef   unsigned int uint; typedef   unsigned char uchar; #define TX_ADDR_WITDH 5 //Set the transmit address width to 5 bytes #define RX_ADDR_WITDH 5 #define TX_DATA_WITDH 8 #define RX_DATA_WITDH 8 #define R_REGISTER       0x00   // Read register #define W_REGISTER       0x20 
[Microcontroller]
Study Notes: Learning MCU from Scratch (1) Establishing the Experimental Environment
I bought a learning board from the STC series on Taobao for more than 300 yuan. It has many functional interfaces and can perform most of the experiments in the textbook. I used it as our experimental platform. Let me first give a brief introduction to this board: * USB power supply * 8 LEDs * 8 full 8-digit dig
[Microcontroller]
Study Notes: Learning MCU from Scratch (1) Establishing the Experimental Environment
Experiment 2: Sorting and addition (80C51 single-chip computer assembly language programming)
There are two numbers with a length of 10H, which are placed in the memory with the first address of 30H and 40H respectively (low byte). Find their corresponding sum and place it in the memory with the first address of 50H (process the carry bit), then arrange them in ascending order and place them in the memory with
[Microcontroller]
GigaDevice sold 200 million MCUs in 2020. Why is the market demand so strong?
According to recent investor survey information released by GigaDevice, the company's MCU shipments in 2020 were close to 200 million. At the same time, the company also analyzed the reasons behind the surge in MCU market demand! On the same day, GigaDevice's stock price rose by 8.59%.   According to GigaDevice, th
[Microcontroller]
GigaDevice sold 200 million MCUs in 2020. Why is the market demand so strong?
A brief discussion on the application of C8051 single chip microcomputer in variable air volume air conditioning control system
1. Introduction: The variable air volume (VAV) air conditioning system is an air conditioning system that uses a variable air volume box to adjust the air volume and the fresh air return mixing ratio of the room, and adjusts the air volume or fresh air return mixing ratio of the air conditioning unit accord
[Microcontroller]
Development of heat meter communication module based on 89C2051 single chip microcomputer
1 Introduction The popularization of centralized heating and the implementation of metered heating are effective means of building energy conservation, but this needs to be achieved through the automation of heating system operation and management. In order to study the control and regulation methods of the
[Microcontroller]
Development of heat meter communication module based on 89C2051 single chip microcomputer
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号