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

📄 key.c

📁 使用MC9S12DG128芯片
💻 C
字号:
#include "includes.h"

//                  0    1    2    3    4    5    6    7
INT8U kcodtab[16]={0xE7,0xEB,0xED,0xEE,0xD7,0xDB,0xDD,0xDE,
//                  8    9                            enter
                   0xB7,0xBB,0xBD,0xBE,0x77,0x7B,0x7D,0x7E
};//the tab of key

 INT8U  ValueKey;

 INT8U  KeyFlag;
 
/********************************************************************************
定义指针函数
********************************************************************************/

void NoFunc(void) {
  _NOP_;
}


void(*Key_Func[16])(void) = {

  NoFunc,
  NoFunc,
  NoFunc,
  NoFunc,
  Car_Func,
  Car_Func2,
  dist_Func,
  NoFunc,
  Motor_Func,
  SCA_Func,
  Servo_Func,
  ACD_Func,
  ECT_Func,
  PWM_Func,
  NoFunc,
  NoFunc
  
}; 



/*************************************************************************

 中断服务程序 
  定时器1的时钟中断服务程序  
************************************************************************/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
__interrupt void RTI_ISR(void) {
  INT8U TempPortb,i;
  
  INT8U KeyValue;
  
  static INT8U Count=0,LastKey=0;
  
  TempPortb = PORTB; //save the the value of PORTB
  
  DDRB = 0x0F;   //high 4 bits input,low 4 bits output
  
  PUCR_PUPBE = 1; //DDRB high 4 bits pull-up
  
  PORTB = 0xF0;   //output 0xF0
  
  _NOP_        //must delay when bus clock is 24MHZ.disable delay when bus clock is 8MHZ
  _NOP_
  
  KeyValue = PORTB;
  
  if (KeyValue != 0xF0) {
  
      DDRB = (~DDRB);
      
      PORTB = (KeyValue | 0x0F);     //线反转法
  
      _NOP_ 
      _NOP_
      
      KeyValue = PORTB;
      
      for (i=0;i<16;i++) {
  
          if(KeyValue == kcodtab[i]){KeyValue = i;break;}
  
      }
    
    if (ValueKey == KeyValue) {
      
      Count += 1;
           
      if (Count == 40) { KeyFlag = ON; Count = 0; LastKey = ON;}
      
      else if (Count == 2 ) {
        
         if (LastKey == OFF) {KeyFlag = ON;}    //如不是持续按键,则允许
      }
    
    }
    else {
    
      ValueKey = KeyValue;    //如果前后的按键值不一样就重新计数,并赋予新的按键值
      
      Count = 1 ;
      
      KeyFlag = OFF;
    }
    
    
  } 
  else {
    
    LastKey = OFF;    //清楚持续按键标志
  
    Count = 0;
    
  }
  
  DDRB = 0xFF;
  
  PORTB = TempPortb; // resume the value of PORTB 
  
  CRGFLG = 0x80;  //clear the flag of RTI  
  
      
} 

/*************************************************************************
   时钟中断初始化 
**************************************************************************/
#pragma CODE_SEG DEFAULT

void RTIInit(void) {
  //RTICTL = 0x1B ->> 约为1ms
  
  RTICTL = 0x5F; /* set RTI prescaler is 32.768ms */ 
  
  //RTICTL = 0x7F;  
  
  CRGINT = 0x80; /* enable RTI interrupts */ 
  
  EnableInterrupts;
}

⌨️ 快捷键说明

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