微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR单片机程序——电脑键盘(P/S2口协议)——pckey

AVR单片机程序——电脑键盘(P/S2口协议)——pckey

时间:11-23 来源:互联网 点击:

#include avr/io.h>
#include interrupt.h>
#include
#include
#include
#include "scancodes.h"
#include "74c922.h"

#define GETPSDATA()((PIND>>PIND7)&0x1) //connect to PD7
#define GETPSCLK() ((PIND>>PIND2)&0x1) //connetc to PD2

static unsigned char IntNum = 0; //count the interrupt
static unsigned char KeyV; //it is the key Value
static unsigned char DisNum = 1; //显示用指针
static unsigned char Key_UP=0;//Key_UP means release the key, Shift = 0
static unsigned char BF = 0; //标识是否有字符被收到

SIGNAL(SIG_INTERRUPT0)
{
if ((IntNum > 0) && (IntNum < 9))
{
KeyV = KeyV >> 1; //因键盘数据是低>>高,结合上一句所以右移一位
if (GETPSDATA()) KeyV = KeyV | 0x80; //当键盘数据线为1时为1到最高位
}
IntNum++;
while (!GETPSCLK()); //等待PS/2CLK拉高

if (IntNum > 10)
{
IntNum = 0; //当中断11次后表示一帧数据收完,清变量准备下一次接收
BF = 1; //标识有字符输入完了
cli(); //关中断等显示完后再开中断 (注:如这里不用BF和关中断直接调Decode()则所Decode中所调用的所有函数要声明为再入函数)
}
}

void Decode(unsigned char ScanCode) //注意:如SHIFT+G为12H 34H F0H 34H F0H 12H,也就是说shift的通码+G的通码+shift的断码+G的断码
{
unsigned char TempCyc;
if (!Key_UP) //当键盘松开时
{
switch (ScanCode) {
case 0xF0 :Key_UP = 1;break;// 当收到0xF0,Key_UP置1表示断码开始
default:
if (DisNum > 16)
{
DisNum = 1;
Clear_Screen();
}

for (TempCyc = 0;(UnShifted[TempCyc][0]!=ScanCode)&&(TempCyc<60); TempCyc++); //查表显示
if (UnShifted[TempCyc][0] == ScanCode)
{
Write_Position(2,DisNum);
Write_Data(UnShifted[TempCyc][1]);
}
DisNum++;

break;
}
}
else
{
Key_UP = 0;
}
BF = 0; //标识字符处理完了
}

int main(void)
{
DDRD=0x70;
PORTD=0x00;
Initialize_LCD();
Write_Position(1,1);
Write_String("hello");
GICR=(1MCUCR=(1sei();//start the interrupt
do
{
if (BF)
Decode(KeyV);
else
sei(); //开中断
}
while(1);
}

"scancodes.h"

unsigned char/* code*/ UnShifted[59][2] = {
0x1C, a,
0x32, b,
0x21, c,
0x23, d,
0x24, e,
0x2B, f,
0x34, g,
0x33, h,
0x43, i,
0x3B, j,
0x42, k,
0x4B, l,
0x3A, m,
0x31, n,
0x44, o,
0x4D, p,
0x15, q,
0x2D, r,
0x1B, s,
0x2C, t,
0x3C, u,
0x2A, v,
0x1D, w,
0x22, x,
0x35, y,
0x1A, z,
0x45, 0,
0x16, 1,
0x1E, 2,
0x26, 3,
0x25, 4,
0x2E, 5,
0x36, 6,
0x3D, 7,
0x3E, 8,
0x46, 9,
0x0E, `,
0x4E, -,
0x55, =,
0x5D, ,
0x29, ,
0x54, [,
0x5B, ],
0x4C, ;,
0x52, ,
0x41, ,,
0x49, .,
0x4A, /,
0x71, .,
0x70, 0,
0x69, 1,
0x72, 2,
0x7A, 3,
0x6B, 4,
0x73, 5,
0x74, 6,
0x6C, 7,
0x75, 8,
0x7D, 9,
};

unsigned char/* code*/ Shifted[59][2] = {
0x1C, A,
0x32, B,
0x21, C,
0x23, D,
0x24, E,
0x2B, F,
0x34, G,
0x33, H,
0x43, I,
0x3B, J,
0x42, K,
0x4B, L,
0x3A, M,
0x31, N,
0x44, O,
0x4D, P,
0x15, Q,
0x2D, R,
0x1B, S,
0x2C, T,
0x3C, U,
0x2A, V,
0x1D, W,
0x22, X,
0x35, Y,
0x1A, Z,
0x45, 0,
0x16, 1,
0x1E, 2,
0x26, 3,
0x25, 4,
0x2E, 5,
0x36, 6,
0x3D, 7,
0x3E, 8,
0x46, 9,
0x0E, ~,
0x4E, _,
0x55, +,
0x5D, |,
0x29, ,
0x54, {,
0x5B, },
0x4C, :,
0x52, ",
0x41, <,
0x49, >,
0x4A, ?,
0x71, .,
0x70, 0,
0x69, 1,
0x72, 2,
0x7A, 3,
0x6B, 4,
0x73, 5,
0x74, 6,
0x6C, 7,
0x75, 8,
0x7D, 9,
};


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top