PS/2 Processing C51 Header Files

Publisher:meilidaowlLatest update time:2016-09-06 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/**********************Chinese version*******************************/

/*****Function description: PS/2 keyboard application header file (interrupt processing) *****/
/*****Debugging platform: 51 single-chip microcomputer 11.0592M crystal *****/
/*****Author: Zheng Wen (ClimberWin) *****/
/*****Written date: October 5, 2009 *****/
/*****Version information: V1.0 *****/
/*****Modified date: *****/
/*****************************************************************/
#ifndef __C51_PS_2_H__
#define __C51_PS_2_H__

#include
#define uchar unsigned char
#define uint unsigned int
////////////////Data definition///////////////////////////////////////
sbit PS2_key_Da ta= P2^0; //define PS2 Da ta pin
sbit PS2_key_CLK =P3^3; //define PS2 Clk pin
bit PS2_flag=0; //Whether there is PS/2 key data receiving flag
bit PS2_key_UP=0, Shift=0;//Whether the PS2_key_UP is released, whether the Shift key is pressed
uchar clk_num = 0; //Interrupt count, used to determine the number of bits of received data
uchar PS2_da ta; //Key value storage address
uchar LCD1602_cursor = 0; //Display cursor position
///////////////Use function definition///////////////////////////////////
void PS2_decode(unsigned char PS2_Co de);
/////////////////Data receiving subroutine (interrupt processing)///////////////////////////////
//Keyboard protocol One frame contains 11 bits of serial protocol
//bit1 :START=0
//bit2-9: data bit DA TA0-DA TA7
//bit10 : parity bit
//bit11 :STOP=1
void PS2_data_receive (void) interrupt 2
{
    clk_num++; //Increase the number of interrupts by 1
    if ((clk_num>1) && (clk_num<10))
  {    
      if(PS2_key_Da ta==0) //Bit 0 receives and saves 
   {PS2_da ta=PS2_da ta>>1 | 0x00;}
   else //Bit 1 receives and saves 
   {PS2_da ta=PS2_da ta>>1 | 0x80;}
     
  } 
 while (!PS2_key_CLK); //Wait for PS/2CLK to be pulled high
 if (clk_num > 10) //When the interrupt occurs 11 times, it means that one frame of data has been received
  {
   clk_num = 0; //Clear count value
   PS2_flag = 1; //Turn on key value flag
   EA = 0; //Turn off interrupt
  }
}
///////////////////////Key value processing subroutine ////////////////////////////////////
void PS2_decode(unsigned char PS2_Co de) //Note: if SHIFT+G is 12H 34H F0H 34H F0H 12H, that is, shift pass code + G pass code + shift break code + G break code
{
 uchar tab_num; 

 if (PS2_key_UP==0) //In the initial state
  {
   switch (PS2_Co de)
    {
     case 0xF0 : //When 0xF0 is received, PS2_key_UP is set to 1 to indicate the start of the break code
      PS2_key_UP = 1;
     break;

     case 0x12: // Left SHIFT
      Shift = 1;
     break;

     case 0x59: //Right SHIFT
      Shift = 1;
     break;

     default:      
      
      if(!Shift) //If SHIFT is not pressed
       {
        for (tab_num = 0;(UnShifted[tab_num][0]!=PS2_Co de)&&(tab_num<59); tab_num++); //Table displayif
        (UnShifted[tab_num][0] == PS2_Co de) //Key handler
       } 
      else //Press SHIFT
       {
        for(tab_num = 0; (Shifted[tab_num][0]!=PS2_Co de)&&(tab_num<59); tab_num++); //Table displayif
        (Shifted[tab_num][0] == PS2_Co de) //Key handler

       }

     break;
    }
  }
 else //When PS2_key_UP==1, that is, the key is popped up.
  { 
   PS2_key_UP = 0; //Clear flag
   if (PS2_Code ==0x12 || PS2_Code ==0x59) Shift = 0; //When the SHIFT key is pressed, it needs to be cleared
  }
 PS2_flag = 0; //The identification character is processed
}

#endif

Reference address:PS/2 Processing C51 Header Files

Previous article:TFT LCD screen C51 control
Next article:2.4G wireless transmitter module nRF2402 application

Latest Microcontroller Articles
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号