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

📄 f_10_33_keytst.c

📁 * Use 10 MHz crystal frequency. * Use Timer0 for ten millisecond looptime. * Blink "Alive" LED e
💻 C
字号:


#include "config.h"
#include "serial.c"
#include "serio.c"
#include "delay.h"

const unsigned char
    key_table[] = "123456789*0#";

//RB3,RB2,R1 are rows
//RB7,RB6,RB4,RB3 are columns
 unsigned char keyscan(void) {
   char row,col,key;
   // all rows as inputs, latches already have zeros
   TRISB3=1; TRISB2=1; TRISB1=1;
   key = 0;
   for (col=7; col >3;col--) {
     for (row=3; row >0; row--) {
       bitclr(TRISB,row); // row as output
       if (!bittst(PORTB,col)) {
	 // found key press, return all rows as outputs
	 TRISB3=0; TRISB2=0; TRISB1=0;
	 return(key_table[key]);
       }
       bitset(TRISB,row);
       key++;
     }
   }
   TRISB3=0; TRISB2=0; TRISB1=0;
   return('E');
 }

volatile unsigned char keyflag, dly_cnt;

#if defined(HI_TECH_C)
void interrupt pic_isr(void)
#endif
#if defined(__18CXX)
#pragma interrupt pic_isr
void pic_isr(void)
#endif
{
  if (RBIF && RBIE) {  // check RBIE because RBIF is unstable
    RBIE = 0;  // disable interrupt
    dly_cnt=0;
    // start debounce timer
    TMR2=0; TMR2IF=0;  TMR2IE=1;
    TMR2ON=1; 
  }
  if (TMR2IF) {
    TMR2IF=0;
    dly_cnt++;
    if (dly_cnt == 2){
      //debounced, read key
      keyflag = keyscan();
      TMR2IE = 0; TMR2ON=0;
    }
  }
}



// wait until keypad idle for 100 ms
// when exit RB[7:4] = 1111
void keypad_idle(void) {
  char cnt,c;
  cnt = 0;
  while(cnt < 5) {
    c = PORTB & 0xF0;
    if (c == 0xF0) cnt++ ;
    else cnt=0;
    DelayMs(20);
  }
  // clear interrupt flag now that port is stable
  RBIF = 0;  
}

void main(void){

  serial_init(95,1); // 19200 in HSPLL mode, crystal = 7.3728 MHz
  TRISB=0xF1; // RB3,RB2,R1 outputs
  RB3=0; RB2=0; RB1=0;  // pull these low
  // enable the weak pullup on port B
  RBPU = 0;
  // configure timer 2
  // post scale of 15
  TOUTPS3 = 1; TOUTPS2 = 1; TOUTPS1 = 1; TOUTPS0 = 1;
  // pre scale of 16 */
  T2CKPS1 = 1;
  PR2 = 229;  // interrupt interrupt interval of 8 ms
  // now enable PORTB interrupt on change
  IPEN = 0;  PEIE = 1;  GIE = 1;  
  keyflag = 0;
  pcrlf();printf("Keypad test"); pcrlf();
  while(1) {
    // wait for key
    keypad_idle();
    keyflag = 0;
    RBIE = 1;
    while(!keyflag);
    printf("%c",keyflag);
    if (keyflag == '#') pcrlf();
  }
}

//for MCC18, place the interrupt vector goto
#if defined(__18CXX)
#if defined(HIGH_INTERRUPT)
#pragma code HighVector=HIGH_INTERRUPT
#else
#pragma code HighVector=0x0008  
#endif
void HighVector (void)
{
    _asm goto pic_isr _endasm
}
#pragma code
#endif





⌨️ 快捷键说明

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