PS/2 Keyboard Interface C51 Driver

Publisher:blq0681Latest update time:2016-11-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/**************************
 Resources used by the file
1. External interrupt 0
2. Port: P3.3, P3.4
**************************/
sbit PS2_DAT=P3^7; //Data

uchar code unshifted[][2]= //shift key not pressed decoding table
{  
  0x0e,'`',
  0x15,'q',
  0x16,'1',
  0x1a,'z',
  0x1b,'s',
  0x1c,'a',
  0x1d,'w',
  0x1e,'2',
  0x21,'c',
  0x22,'x',
  0x23,'d',
  0x24,'e',
  0x25,'4',
  0x26,'3',
  0x29,' ',
  0x2a,'v',
  0x2b,'f',
  0x2c,'t',
  0x2d,'r',
  0x2e,'5',
  0x31,'n',
  0x32,'b',
  0x33,'h',
  0x34,'g',
  0x35,'y',
  0x36,'6',
  0x39,',',
  0x3a,'m',
  0x3b,'j',
  0x3c,'u',
  0x3d,'7',
  0x3e,'8',
  0x41,',',
  0x42,'k',
  0x 43,'i',
  0x44,'o',
  0x45,'0',
  0x46,'9',
  0x49,'.',
  0x4a,'/',
  0x4b,'l',
  0x4c,';',
  0x4d,'p',
  0x4e,'-',
  0x52,'\'',
  0x54,'[',
  ', 0x7a,'3 '
  ,
  0x7b
  ,'
  -',
  0x7c,'*',   0x7d,'9', 0,0 }; uchar code shifted[   ]
  [   2
  ] = //shift key pressed decoding table   { 0x0e   ,'~',   0x15   ,   'Q',   0x16 ,   ' !   '   ,   0x1a,'Z   '   ,   0x1b,'S',   0x1c,'A',   0x1d,'W',   0x1e,'@',   0x21,'C',   0x22,'X',   0x23,'D',   0x24,'E',   0x25,'$',   0x26,'#',   0x29,' ',   0x2a,'V',   0x2 b,'F',   0x2c,'T',   0x2d,'R',   0x2e,'%',   0x31,'N',   0x32,'B',   0x33,'H',   0x34,'G',   0x35,'Y',   0x36,'^',   0x39,'L',  0x3a,'M',   0x3b,'J',   0x3c,'U',   0x3d,'&',   0x3e,'*',   0x41,'<',   0x42,'K',   0x43,'I',   0x44, 'O',   0x45,')',   0x46,'(',   0x49,'>',   0x4a,'?',   0x4b,'L',   0x4c,':',   0x4d,'P',   0x4e,'_ ',   0x52,'"',   0x54,'{',   0x55,'+',   0x5b,'}',   0x5d,'|',   0x61,'>',   0x69,'1',   0x6b,'4',   0x6c,'7',



































































  0x70,'0',
  0x71,'.',
  0x72,'2',
  0x73,'5',
  0x74,'6',
  0x75,'8',
  0x79,'+',
  0x7a,'3',
  0x7b, '-',
  0x7c,'*',
  0x7d,'9',
  0,0
};


void Decode(uchar scancode);//declare function prototype
uchar bitcount,bytecount,byte2; //edge is 0 falling edge interrupt, 1 rising edge interrupt
uchar ascii2[35],ascii[99]; //bitcount is the bit count value
         //ascii is the translated ASCII code, the initial value is space
//******************************************************         
//External interrupt service subfunction  
//*******************************************************       
void StopInt() interrupt 0 
{   
 static uchar udata; //Declare local static variables to save scan codes
   if(bitcount < 11 && bitcount > 2) //3 to 10 bits are data, start bit, check bit and stop bit are ignored
   { 
    udata = (udata >> 1); //right shift to save data
  if(PS2_DAT)
  {
     udata|=0x80; //store a '1'
  }
   }
    if(--bitcount==0) //If all 11 bits are received
   {
     bitcount = 11; //Reset to 11-bit data
  ascii[bytecount]=udata;
  if(bytecount<6)bytecount++;
   }
}   
/*******************************************
Function name: DecodeFunction

Parameter: scancode--scan code to be translatedReturn
value: None
/********************************************/
void Decode(uchar scancode)
{
  static uchar up=0,shift=0; //up is the pass/break code flag, shift is the shift key pressed flaguchar
  i;
  if (!up) //The received 11-bit data is a pass code (up is 0)
  {
    switch (scancode)//Start translating the scan code
    {
     case 0xF0: //Keyboard release flag (the following byte is a break code)
      up=1; //Set up to the break code flagbreak
      ;
      case 0x12: //Left shift key pressedshift
      =1; //Set shift to the pressed flagbreak
      ;
      case 0x59: //Right shift key pressedshift
      =1; //Set shift to the pressed flagbreak
      ;   
      default:        
      if(!shift) //If the shift key is not pressed
   { //Look up the unshifted table, the left column in the table is the scan code, and the right column is the corresponding ASCII code
        for(i=0;unshifted[i][0]!=scancode&&unshifted[i][0];i++);
         if(unshifted[i][0]==scancode) 
         {
           ascii2[3]=unshifted[i][1];
         }
   } 
   else //If the shift key is pressed
   { //Look up the shifted table
      for(i=0;shifted[i][0]!=scancode&&shifted[i][0];i++);
         if(shifted[i][0]==scancode) 
         {
           ascii2[3]=shifted[i][1];
         }
   }   
      break;
    }
  } 
  else //The received 11-bit data is a broken code (up is 1)
  {
    up = 0; //Reset the broken code flag
    switch (scancode) //Detect shift key release
    {
      case 0x12 : //Left shift key
      shift = 0;
      break;
      case 0x59: //right shift key
      shift = 0;
      break;
      default:
      break;
    }
  }
}


Reference address:PS/2 Keyboard Interface C51 Driver

Previous article:128*64 LCD screen (ST7920) C51 driver
Next article:IO port simulation SPI communication C51 program

Recommended ReadingLatest update time:2024-11-16 14:29

Application of digital measurement chip PS081 in solar scale and high-precision digital sensor
Preface One application direction of digital measurement chip PS081 is solar scale. Compared with traditional electronic scale, solar scale solution using digital measurement chip PS081 of ACAM has many competitive advantages. Because the competition point of traditional electronic scale is only price, Chinese scale m
[Test Measurement]
Sony PS5 system update: fix game installation and controller charging issues
      A new firmware update for the PlayStation 5 has been released, fixing at least one major issue that has been plaguing gamers since launch.      In addition to rest mode crashes and game download issues, some PS5 players have been reporting an issue where the console uninstalls games installed using discs. This
[Mobile phone portable]
C51 MCU LCD1602 Driver
LCD1602 Introduction LCD1602 character liquid crystal (16 characters per line, two lines in total) - a dot matrix liquid crystal module specially used to display letters, numbers and symbols. It is composed of several 5x7 or 5x10 dot matrix characters. Each dot matrix character position can be used to display a chara
[Microcontroller]
C51 MCU LCD1602 Driver
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号