?? kbc.c
字號:
#include <io8515.h>
#include <macros.h>
#include "pindefs.h"
#include "scancodes.h"
#define BUFF_SIZE 64
//const ascii[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
// 0x39,0x41,0x42,0x43,0x44,0x45,0x46};
unsigned char bitcount;//PC鍵盤數據長度計數
unsigned char kb_buffer[BUFF_SIZE];//鍵盤緩沖區
unsigned char input=0;//緩沖區讀指針
unsigned char output=0;//緩沖區寫指針
//送鍵盤按鍵ASCII碼到鍵盤緩沖區
void put_kbbuff(unsigned char c)
{
kb_buffer[input]=c;
if (input<(BUFF_SIZE-1))
input++;
else
input=0;
}
//從鍵盤緩沖區讀取按鍵的ASCII碼
unsigned char get_char(void)
{
unsigned char temp;
if(output==input)
return 0;
else
{
temp=kb_buffer[output];
if(output<(BUFF_SIZE-1))
{
output++;
}
else
output=0;
return temp;
}
}
//為運行讀取PC鍵盤程序進行初始化
void init_kb(void)
{
MCUCR=0x02;//設置8515的INT0為下降沿觸發中斷
GIMSK|=(1<<INT0);//使能INT0中斷
SEI(); //開中斷
bitcount = 11;
}
//對PC鍵盤的掃描碼進行解碼
void decode(unsigned char sc)
{
static unsigned char shift,up,shiftup;
unsigned char i;
if (sc==0xf0)//按鍵釋放
{
up=1;
return;
}
if (up==1)//SHIF鍵開關
{
up=0;
if ((sc==0x12)|(sc==0x59)) shift=0;
return;
}
switch (sc)
{
case 0x12:{//檢測左SHIF鍵
shift=1;
shiftup=1;
}
case 0x59:{//檢測右SHIF鍵
shift=1;
shiftup=1;
}
default:{
if (shift==0)
{
for(i = 0;unshifted[i][0]!=sc && unshifted[i][0]; i++);
if (unshifted[i][0] == sc)
put_kbbuff(unshifted[i][1]);
}
else
{
for(i = 0;shifted[i][0]!=sc && shifted[i][0]; i++);
if (shifted[i][0] == sc)
{
put_kbbuff(shifted[i][1]);
}
}
}
}
}
//鍵盤數據讀取程序
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
static unsigned char data;
switch (bitcount)
{
case 11:{
if ((PIN_KB&(1<<DATAPIN))!=0)
return;
else
bitcount--;
break;}
case 2:{
bitcount--;
break;}
case 1:{
bitcount--;
if ((PIN_KB&(1<<DATAPIN))==0)
{
bitcount=11;
return;
}
else
{
bitcount=11;
decode(data);
}
break;}
default:{
data = (data >> 1);
if((PIN_KB&(1<<DATAPIN))!=0)
data|=0x80;
bitcount--;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -