博客首页 | 排行榜 |

wwq100的博客

个人档案
博文分类
FM3135 Demo 板的键盘扫描程序  2009-10-01 20:40

 #include "include.h"

#include "key.h"
 
void keypad_init()
{
PERIPHEN2 |= 0x08; //BIT3 - IOPORTEN 0.disable  1.enable 
/*
BIT7 - PWC1EN 0.disable  1.enable 
BIT6 - PWC0EN 0.disable  1.enable 
BIT5 - AUEN 0.disable  1.enable 
BIT4 - XRAM2CODE 0.disable  1.enable 
BIT3 - IOPORTEN 0.disable  1.enable 
BIT2 - WDTEN 0.disable  1.enable 
BIT1 - PWMSFREN 0.disable  1.enable 
BIT0 - FPIEN 0.disable  1.enable 
*/
PORTINEN |=0x40; //Port 6 input logic is activated
/*
bit7. Reserved (0) Keep this bit at 0
bit6. Port 6 Input Enable Register
0 = Port 6 input logic is deactivated
1 = Port 6 input logic is activated
bit5~bit0 同bit6
*/
P6PINCFG = 0xf0; //When: 1:  input (reset value)   0: output
// columns are input   rows are output 
P6 = 0x00;
}
 
uchar keypad_scan(void)
{
unsigned char temp;
unsigned char keypressed = 0x00;//var holding ASCII value of the last key
//pressed (could be global)
unsigned char keylines = 0xf0; //variable to read the actual I/O port
unsigned char keyrow = 0x00; //Row position of the pressed key
unsigned char keycol = 0x00; //Column position of the pressed key
// rows and columns association table
const char code keyrowcolmap[]=
{0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x03,0x0F,0x0F,0x0F,0x02,0x0F,0x01,0x00,0x0F};
 
// Ascii code associated with pressed key
const char code keyascii[4][4] ={
{'C','4','8','0'},
{'1','5','9','L'},
{'2','6','U','D'},
{'3','7','E','R'}
};
//--Retrieve the line number
//重新得到的行号
KEYPADPORT = 0x00; //Send 0 on keylines
for(temp = 0; temp < 0x80; temp++);//延迟
keylines = KEYPADPORT; //Read Keypad Port
keylines &= 0xf0; //Isolate upper nibble
if(keylines != 0xf0)// valid keypad_scan
{
//temp = (keylines >> 3); //Position columns to lower portion
//keylines = (temp >> 1);
//keycol = keyrowcolmap[keylines];
switch (keylines)
{
case 0xe0: //1110
keycol = 0x00;
break;
case 0xd0://1101
keycol = 0x01;
break;
case 0xb0://1011
keycol = 0x02;
break;
case 0x70://0111
keycol = 0x03;
break;
//在其它情况下,keycol为0x00
}
//--Retrieve Column number
for(temp = 0; temp < 0x80; temp++);
KEYPADPORTDIR = 0x0f; //columns are output / rows are input
KEYPADPORT = 0x00; //Send 0 on each column
for(temp = 0; temp < 0x80; temp++);
keylines = KEYPADPORT; //Read Keypad Port
keylines &=0x0f; //Isolate lower 4 bit (rows)
//-retrieve the line value
keyrow = keyrowcolmap[keylines];
if((keyrow != 0x0F)&& (keycol != 0x0F))
{
//--Get the ascii value of the key
keypressed = keyascii[keyrow][keycol];
}//end of if key row / col
//--Set KEYPADPORT as before
KEYPADPORTDIR = 0xf0; //Columns are input / rows are output
KEYPADPORT = 0x00; //Clear the Columns driver inputs
}
return keypressed;
}
 
类别:vrs51L3074单片机 |
下一篇:FM3135笔记
以下网友评论只代表其个人观点,不代表本网站的观点或立场