用ARM編寫的4*4鍵盤
#include "config.h"
#define KEY_COL1 1<<12
#define KEY_COL2 1<<13
#define KEY_COL3 1<<14
#define KEY_COL4 1<<15
#define KEY_ROW1 1<<8
#define KEY_ROW2 1<<9
#define KEY_ROW3 1<<10
#define KEY_ROW4 1<<11
#define KEYO KEY_ROW1|KEY_ROW2|KEY_ROW3|KEY_ROW4 //行,設(shè)置為輸出口
#define KEYI KEY_COL1|KEY_COL2|KEY_COL3|KEY_COL4 //列,設(shè)置為輸入口
//按鍵抬起,則將相應(yīng)的鍵值返回
uint16 get_key(void)
{
uint16 scc,re;
IO0SET=KEYI;
IO0CLR=KEYO;
while((IO0PIN&0xf000)==0xf000);
delay(10);
if((IO0PIN&0xf000)!=0xf000)
{
scc=0xfe00;
while((scc&0x1000)!=0)
{
IO0SET=scc;
IO0CLR=((~scc)&0xff00);
if((IO0PIN&0xf000)!=0xf000)
{
re=((IO0PIN&0xf000)|0x0fff);
while((IO0PIN&0xf000)!=0xf000);//等待按鍵抬起,返回鍵值
return (~(scc|0x00ff)|(~re));
}
else scc=(scc<<1)|0x0100;
}
}
return 0;
}
//按鍵在LCD上顯示相應(yīng)的字符
#include "config.h"
#define KEY_COL1 1<<12
#define KEY_COL2 1<<13
#define KEY_COL3 1<<14
#define KEY_COL4 1<<15
#define KEY_ROW1 1<<8
#define KEY_ROW2 1<<9
#define KEY_ROW3 1<<10
#define KEY_ROW4 1<<11
#define KEYO KEY_ROW1|KEY_ROW2|KEY_ROW3|KEY_ROW4 //行,設(shè)置為輸出口
#define KEYI KEY_COL1|KEY_COL2|KEY_COL3|KEY_COL4
#define uchar unsigned char
#define UART0_BPS 115200
uchar keynum;
void uart0_int(void)
{
uint16 FDIV;
U0LCR=0x83;
FDIV=(Fpclk/16)/UART0_BPS;
U0DLM=FDIV/256;
U0DLL=FDIV%256;
U0LCR=0x03;
}
void uart0(uchar keynum)
{
PINSEL0=0x05;
uart0_int();
U0THR=keynum;
while(!(U0LSR&0x40));
}
int main()
{
extern uint16 get_key();
uint16 key;
IO0DIR|=KEYO;
int_lcd();
while(1)
{ PINSEL0=0;
PINSEL1=0;
key=get_key();
delay(10);
switch(key)
{
case 0x1100:clear();send_str("0");keynum=0; break;
case 0x2100:clear();send_str("1");keynum=1; break;
case 0x4100:clear();send_str("2");keynum=2; break;
case 0x8100:clear();send_str("3");keynum=3; break;
case 0x1200:clear();send_str("4");keynum=4; break;
case 0x2200:clear();send_str("5");keynum=5; break;
case 0x4200:clear();send_str("6");keynum=6; break;
case 0x8200:clear();send_str("7");keynum=7; break;
case 0x1400:clear();send_str("8");keynum=8; break;
case 0x2400:clear();send_str("9");keynum=9; break;
case 0x4400:clear();send_str("A");keynum=A; break;
case 0x8400:clear();send_str("B");keynum=B; break;
case 0x1800:clear();send_str("C");keynum=C; break;
case 0x2800:clear();send_str("D");keynum=D; break;
case 0x4800:clear();send_str("傳輸");uart0(keynum); break;
case 0x8800:clear();send_str("轉(zhuǎn)換"); break;
default:break;
}
}
}
評論