4*4 keyboard function with buffered interrupt response

Publisher:WanderlustGlowLatest update time:2016-08-16 Source: eefocusKeywords:Cache Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include "key.h"

#include "scom.h"

unsigned char KeyNum[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //Key value buffer
unsigned char KeyCntWr; //Write key value counter
unsigned char KeyCntRd; //Read key value counter
//unsigned char KeyNum;

void KeyInit(void)
{
    P1DIR=0Xf0;     
 P1REN=0X0f; //Enable pull-up
    P1OUT=0X0f;
 
    P1IFG=0x00;
    P1IE=0X0f; //Interrupt enable   
    P1IES=0X0f; //Column line falling edge allows P1 interrupt
}

void key_delay(Word _us)

  while(_us--);
}
//line number decoding
unsigned char coding(unsigned char n) //decoding
{
    switch(n)
    {
        case 0x0e: return 3; //input is 11111110 which is line number 3        
        case 0x0d: return 2; //input is 11111101 which is line number 2 
        case 0x0b: return 1; //input is 11111011 which is line number 1 
        case 0x07: return 0; //input is 11110111 which is line number 0 
  default : return 0xf0;
    }
}

//获取健值
unsigned char key(void)
{
    char temp,shift,keyname,a;
    temp=0x10;
    keyname=0xff;

    for(shift=0;shift<4;shift++)    
    {
        P1OUT=temp^0xff; //high four-bit output 11101111, 11011111, 101111111, 011111111, 11111111
        temp<<=1;
  a=P1IN&0x0f;
        if(a!=0x0f) //low four-bit input, if the port value is not detected to be high level, it means that a key is pressed in this column
            keyname=coding(a)+shift*4; //key value = row number + example number*4
    }
    return keyname; 
}

//Detect whether a key is pressed   
unsigned char keyj(void)      
{
    unsigned char x;
    x=(P1IN&0X0f); // P1.0--P1.3 are column lines, input, P1.4-P1.7 are row lines, output
    return(x); // No key is pressed, return 0x0f; a key is pressed, return non-0x0f

//以查询方式获取键值,且译码。
unsigned char GetKey(void)
{   
    unsigned char key_value=0xff;
 
 if(KeyCntRd!=KeyCntWr)    //相等键盘缓存区无键值
 {
  switch(KeyNum[KeyCntRd])
  {
   case 0x00: key_value=0x0a;  break;  //dot
   case 0x01: key_value=0x06;  break;     //six
   case 0x02: key_value=0x07;  break;     //seven
   case 0x03: key_value=0x08;  break;     //eight
   case 0x04: key_value=0x09;  break;     //nine
   case 0x05: key_value=0x00;  break;     //zero
   case 0x06: key_value=0x0b;  break;     //Down
   case 0x07: key_value=0x0c;  break;     //Enter
   
   case 0x08: key_value=0x0d;  break;     //Esc
   case 0x09: key_value=0x01;  break;     //one
   case 0x0a: key_value=0x02;  break;     //two
   case 0x0b: key_value=0x03;  break;     //three
   case 0x0c: key_value=0x04;  break;     //four
   case 0x0d: key_value=0x05;  break;     //five
   case 0x0e: key_value=0x0e;  break;     //UP
   case 0x0f: key_value=0x0f;  break;     //Clear
   
   default  : key_value=0xff;  break;     
   
  }
  KeyCntRd++;
  if(KeyCntRd==8)
   KeyCntRd=0;
 }
 return(key_value);
}

#pragma vector=PORT1_VECTOR
__interrupt void Key_INT(void)
{
    if(keyj()!=0X0f)
    {
        key_delay(5000); //Debounceif
        (keyj()!=0X0f)
        {
     KeyNum[KeyCntWr++]=key();
   if(KeyCntWr==8) //If the keyboard buffer is full, start from the beginningKeyCntWr
      =0;
   
   if(KeyCntWr==KeyCntRd) //If the write counter is equal to the read counter, the read counter will increase by 1 accordingly 
   {
    KeyCntRd++;
    if(KeyCntRd==8)
     KeyCntRd=0;
   }
   BellOn(1);
  }
    }
    P1OUT=0X0f;
    P1IFG=0X00; //Clear interrupt flag
}

 

The cache key value is 7 or 8. That is, the last 7 or 8 key values ​​are valid.

Keywords:Cache Reference address:4*4 keyboard function with buffered interrupt response

Previous article:MSP430 P1.1 P1.2 P1.3 Second function
Next article:MSP430 Tutorial 16: MSP430 MCU Development Summary

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号