查看: 1624|回复: 0
打印 上一主题 下一主题

飞思卡尔MC9S12XS128调试PS2程序

[复制链接]
跳转到指定楼层
沙发
发表于 2016-4-5 19:43:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//======头文件=====//
#ifndef _PS2_H
#define _PS2_H


#define PS2_CLK PTJ_PTJ1
#define PS2_RW PORTA_PA1

typedef struct PS2_ValueType {

  char PS2_ValueData; /*  码表
                                   Num :77  /:E04A  *:7C   -:7B
                                   7:   6C  8:75    9:7D   +:79
                                   4:   6B  5:73    6:74   +:79
                                   1:   69  2:72    3:7A   Enter:E05A
                                   0:   70  .:71
                                   
                               */
                              
                               /*  对应返回值
                                   Num :-5  /:-4    *:7C   -:-2
                                   7:   7   8:8     9:9   +:-1
                                   4:   4   5:5     6:6   +:-1
                                   1:   1   2:2     3:3   Enter:10
                                   0:   0   .:-3    空格 -6
                               */
  unsigned char PS2_ValueKind; //0 按下,1松手
}PS2_ValueType;

#endif _PS2_H
//==============================C文件===================//
#include "S2.h"
#include "MC9S12XS128.h"


//extern unsigned char PS2_Buffer[3];
//extern unsigned char PS2_StopCodeFlag=0;

//从PS/2中获取一个按键
unsigned char PS2_GetData(void)
{
  unsigned char temp,i,res;
  for(i=0;i<11;i++) {
   
    while(PS2_CLK);
    if(i>0 && i<9) {
      res=res>>1;
      if(PS2_RW) {
       res=res|0x80;
      }
      
    }
    while (!PS2_CLK);
   
  }
  
  return res;
  
}

void Key_Interrupt(void) {

    //DDRJ=0X00;
    PIEJ_PIEJ1=1;
    PPSJ_PPSJ1=0;
}


void PS2_InsertBuffer(unsigned char *buffer,unsigned char bufferdata)
{
  unsigned char *tempdata=buffer+1;
  (*buffer++)=(*tempdata++);
  (*buffer++)=(*buffer++);
  (*buffer)=bufferdata;
}

void PS2_GetChar(PS2_ValueType *PS2_Value)
{

  unsigned char temp;
  temp=PS2_GetData();
  
  (*PS2_Value).PS2_ValueKind=0;
  
  if(temp!=0xe0 && temp!=0xf0)
  {
      switch (temp)
      {
        /*  对应返回值
          Num :-6  /:-4      *:-5   -:-2
          7:   7   8:8     9:9   +:-1
          4:   4   5:5     6:6   +:-1
          1:   1   2:2     3:3   Enter:10
          0:   0   .:-3
        */
        case 0x70 : (*PS2_Value).PS2_ValueData=0; return;
        case 0x69 : (*PS2_Value).PS2_ValueData=1; return;
        case 0x72 : (*PS2_Value).PS2_ValueData=2; return;
        case 0x7A : (*PS2_Value).PS2_ValueData=3; return;
        case 0x6B : (*PS2_Value).PS2_ValueData=4; return;
        case 0x73 : (*PS2_Value).PS2_ValueData=5; return;
        case 0x74 : (*PS2_Value).PS2_ValueData=6; return;
        case 0x6C : (*PS2_Value).PS2_ValueData=7; return;
        case 0x75 : (*PS2_Value).PS2_ValueData=8; return;
        case 0x7D : (*PS2_Value).PS2_ValueData=9; return;
        case 0x71 : (*PS2_Value).PS2_ValueData=-3; return;
        case 0x79 : (*PS2_Value).PS2_ValueData=-1; return;
        case 0x7B : (*PS2_Value).PS2_ValueData=-2; return;
        case 0x7C : (*PS2_Value).PS2_ValueData=-5; return;
        case 0x77 : (*PS2_Value).PS2_ValueData=-6; return;
        case 0x66 : (*PS2_Value).PS2_ValueData=-7; return;
   
      }

   
  }
  else if(temp==0xe0)
  {
     temp=PS2_GetData();
     if(temp==0xf0)
     {
      
      temp=PS2_GetData();
      (*PS2_Value).PS2_ValueKind=1;
      if(temp==0x5A)
      {
         (*PS2_Value).PS2_ValueData=10; return;
      }
      else if(temp==0x4A)
      {
         (*PS2_Value).PS2_ValueData=-4; return;  
      }

    }
    else
    {
   
      (*PS2_Value).PS2_ValueKind=0;
      if(temp==0x5A)
      {
         (*PS2_Value).PS2_ValueData=10; return;
      }
      else if(temp==0x4A)
      {
         (*PS2_Value).PS2_ValueData=-4; return;  
      }
      
     }

  }
  else if (temp==0xf0)
  {
     
      (*PS2_Value).PS2_ValueKind=1;
      temp=PS2_GetData();
      switch (temp)
      {
        /*  对应返回值
          Num :-6  /:-4      *:-5   -:-2
          7:   7   8:8     9:9   +:-1
          4:   4   5:5     6:6   +:-1
          1:   1   2:2     3:3   Enter:10
          0:   0   .:-3     空格 -7
        */
        case 0x70 : (*PS2_Value).PS2_ValueData=0; return;
        case 0x69 : (*PS2_Value).PS2_ValueData=1; return;
        case 0x72 : (*PS2_Value).PS2_ValueData=2; return;
        case 0x7A : (*PS2_Value).PS2_ValueData=3; return;
        case 0x6B : (*PS2_Value).PS2_ValueData=4; return;
        case 0x73 : (*PS2_Value).PS2_ValueData=5; return;
        case 0x74 : (*PS2_Value).PS2_ValueData=6; return;
        case 0x6C : (*PS2_Value).PS2_ValueData=7; return;
        case 0x75 : (*PS2_Value).PS2_ValueData=8; return;
        case 0x7D : (*PS2_Value).PS2_ValueData=9; return;
        case 0x71 : (*PS2_Value).PS2_ValueData=-3; return;
        case 0x79 : (*PS2_Value).PS2_ValueData=-1; return;
        case 0x7B : (*PS2_Value).PS2_ValueData=-2; return;
        case 0x7C : (*PS2_Value).PS2_ValueData=-5; return;
        case 0x77 : (*PS2_Value).PS2_ValueData=-6; return;
        case 0x66 : (*PS2_Value).PS2_ValueData=-7; return;
   
      }
  }
  
}



点击此处下载 ourdev_716935F63Z4Y.rar(文件大小:1K) (原文件名S2.rar)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入因仑

本版积分规则

快速回复 返回顶部 返回列表