⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard.c

📁 基于MSP430F149单片机的PS/2键盘程序
💻 C
字号:
#include <msp430x14x.h>
typedef unsigned char uchar;
typedef unsigned int  uint;
/////////////////////////////////////////////////
#define BufferSize  32      //显示缓存大小
unsigned char bitcount=11;          //位计数变量
unsigned char kb_buffer[BufferSize];    //显示缓存
unsigned char input=0;       //数据压入缓存位置指针
unsigned char output=0;      //数据弹出缓存位置指针   
unsigned char pebit=0xff;    //奇偶校验标志位
unsigned char recdata=0;     //接收到的数据
////////////////////////////////////////////////

////////////////////////////////////////////////
uchar flag;
////////////////////////////////////////////
void PutChar(unsigned char c);
unsigned char GetChar(void);
void Init_KB(void);
unsigned char Decode(unsigned char sc);
////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
//不按Shift的字符对应的编码
const unsigned char unshifted[][2] = 
{
    0x0d,9,     //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',
    0x43,'i',
    0x44,'o',
    0x45,'0',
    0x46,'9',
    0x49,'.',
    0x4a,'/',
    0x4b,'l',
    0x4c,';',
    0x4d,'p',
    0x4e,'-',
    0x52,0x27,
    0x54,'[',
    0x55,'=',
    0x5a,13,     //Enter
    0x5b,']',
    0x5d,0x5c,
    0x61,'<',
    0x66,8,  //Back Space
    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
};

//按住Shift后字符对应的编码
const unsigned char shifted[][2] = 
{
    0x0d,9,     //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',
    0x2b,'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,'+',
    0x5a,13,    //Enter
    0x5b,'}',
    0x5d,'|',
    0x61,'>',
    0x66,8,     //Back Space
    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
};

///////////////////////////////////////////////////////////
/*******************************************
函数名称:PushBuff
功    能:将一个字符压入显示缓存,如果缓存以
          满则覆盖前面的数据
参    数:c--要显示的字符
返回值  :无
********************************************/
void PutChar(uchar c)
{
    kb_buffer[input] = c;
    if (input < (BufferSize-1))
        input++; 
    else
        input = 0;	 
}
/*******************************************
函数名称:PopChar
功    能:从显示缓存中取出一个字符
参    数:无
返回值  :取出的字符
********************************************/
uchar GetChar(void)
{
    uchar temp;
    
    if(output == input)
        return 0xff;
    else
    {
	    temp = kb_buffer[output];
	    if(output < (BufferSize-1))
	    {
	        output++;
        }
	    else
        {
	        output = 0;
        }
	    return temp;	  
    }	     
}
/*******************************************
函数名称:Init_KB
功    能:初始化与键盘相关的IO
参    数:无
返回值  :无
********************************************/
void Init_KB(void)
{
    P1DIR &=~ BIT4;     //Clock接P1.7,设置为输入
    P1DIR &=~ BIT5;     //SID接P5.6,设置为输入
    P1IES |= BIT4;      //下降沿中断
    P1IFG = 0x00;       //中断标志清零
    P1IE  |= BIT4;      //使能时钟端口中断
    P1SEL = 0x00;       //P1口作为IO使用
}
/*******************************************
函数名称:Decode
功    能:对来自键盘的信息进行解码,转换成对
          应的ASCII编码并压入缓存
参    数:sc--键盘发送过来的信息
返回值  :是否收到有效数据:0--否,1--是
说明    :本程序只能对基本按键(即键被按下时产
          生三个字节的扫描码的按键)做出解码,
          包括所有的可显示字符键和Table,
          Back Space和Enter三个特殊功能键。
基本按键的扫描码由三个字节组成,第1个字节为接通
码,第2、3字节为断开码;其中第1字节和第3字节相
同,中间字节为断开标志0xf0。
********************************************/
uchar Decode(uchar sc)
{
      static uchar shift = 0; //Shift键是否按下标志:1--按下,0--未按
      static uchar up = 0;    //键已放开标志:       1--放开,0--按下
      uchar i,flag = 0;
      
      if(sc == 0xf0)    //如果收到的是扫描码的第2个字节---0xf0:按键断开标志
      {
          up = 1;        
          return 0;
      }
      else if(up == 1)  //如果收到的是扫描码的第3个字节
      {
	      up = 0;         
          if((sc == 0x12) || ( sc==0x59))   shift = 0;
	      return 0;
      }	
      
      //如果收到的是扫描码的第1个字节
      if((sc == 0x12) || (sc == 0x59)) //如果是左右shift键
      {      
	      shift = 1;	        //设置Shift按下标志
          flag = 0;
      }		           	           
      else
      {
	      if(shift) //对按下Shift的键进行解码
		  {
		       for(i = 0;(shifted[i][0] != sc) && shifted[i][0];i++);
               if (shifted[i][0] == sc) 
               {
                    PutChar(shifted[i][1]);
                    flag = 1;
               }
		  }
		  else  //直接对按键进行解码
		  {
		       for(i = 0;(unshifted[i][0] != sc) && unshifted[i][0];i++);
               if(unshifted[i][0] == sc)  
               {
                    PutChar(unshifted[i][1]);
                    flag = 1;
               }
	      } 
      }
      if(flag)  return 1;
      else      return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -