新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR單片機(jī)程序——電腦鍵盤(P/S2口協(xié)議)——pckey

AVR單片機(jī)程序——電腦鍵盤(P/S2口協(xié)議)——pckey

作者: 時(shí)間:2016-11-23 來源:網(wǎng)絡(luò) 收藏

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

本文引用地址:http://butianyuan.cn/article/201611/320265.htm

#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; //標(biāo)識(shí)是否有字符被收到


SIGNAL(SIG_INTERRUPT0)
{
if ((IntNum > 0) && (IntNum < 9))
{
KeyV = KeyV >> 1; //因鍵盤數(shù)據(jù)是低>>高,結(jié)合上一句所以右移一位
if (GETPSDATA()) KeyV = KeyV | 0x80; //當(dāng)鍵盤數(shù)據(jù)線為1時(shí)為1到最高位
}
IntNum++;
while (!GETPSCLK()); //等待PS/2CLK拉高

if (IntNum > 10)
{
IntNum = 0; //當(dāng)中斷11次后表示一幀數(shù)據(jù)收完,清變量準(zhǔn)備下一次接收
BF = 1; //標(biāo)識(shí)有字符輸入完了
cli(); //關(guān)中斷等顯示完后再開中斷 (注:如這里不用BF和關(guān)中斷直接調(diào)Decode()則所Decode中所調(diào)用的所有函數(shù)要聲明為再入函數(shù))
}
}

void Decode(unsigned char ScanCode) //注意:如SHIFT+G為12H 34H F0H 34H F0H 12H,也就是說shift的通碼+G的通碼+shift的斷碼+G的斷碼
{
unsigned char TempCyc;
if (!Key_UP) //當(dāng)鍵盤松開時(shí)
{
switch (ScanCode) {
case 0xF0 :Key_UP = 1;break;// 當(dāng)收到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; //標(biāo)識(shí)字符處理完了
}


int main(void)
{
DDRD=0x70;
PORTD=0x00;
Initialize_LCD();
Write_Position(1,1);
Write_String("hello");
GICR=(1<MCUCR=(1<sei();//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,
};




評(píng)論


技術(shù)專區(qū)

關(guān)閉