#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.
Previous article:MSP430 P1.1 P1.2 P1.3 Second function
Next article:MSP430 Tutorial 16: MSP430 MCU Development Summary
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- [RVB2601 Creative Application Development] 4. RVB2601 Key Test (with Anti-shake Processing)
- DSP system design issues
- Simulation of a Microstrip Patch Antenna for 2.4 GHz Applications with Radiat...
- Guess what this signal is?
- Zhongke Bluexun [RT-Thread RISC-V Evaluation Board] DS18B20 Temperature Measurement
- [2022 Digi-Key Innovation Design Competition] - Intelligent Electronic Weighing System
- Open source vacuum cleaner CircuitRoomba
- Are there any PCB manufacturers who can popularize PCB surface treatment technology?
- Altium designer 18 steps to design circuit boards
- [ST NUCLEO-G071RB Review]_08_PWM timer control three-color LED experiment