📄 key.c
字号:
/******************************************************************************
* PC/AT keyboard driver.
* Based in part on Atmel's application note AVR313. www.atmel.com
******************************************************************************/
#include <mega16.h>
#include <key.h>
#include <scancodes.h>
#include <delay.h>
/******************************************************************************
* InitializeKeyboard()
* Initializes the ports and control registers needed for running the keyboard.
******************************************************************************/
unsigned char edge; /* 0 = neg. 1 = pos. */
unsigned char bitcount;
void InitializeKeyboard( void )
{
GICR =0x40; /* enable INT0 interrupt */
#asm("sei"); /* enable global intterupts */
MCUCR =0x02; /* trigger INT0 on falling edge */
DDRD&=~0x0c;
PORTD|=0x0c;
/* PD2, PD3 are inputs */
edge = 0;
bitcount = 11;
return;
}
/******************************************************************************
* interrupt [EXT_INT0] void ext_int0_isr (void)
* When the keyboard triggers the external interrupt this function is called
* to retrieve the data.
******************************************************************************/
interrupt [EXT_INT0] void ext_int0_isr (void)
{
static unsigned char scancode = 0;
if(!edge) // Routine entered at falling edge
{
if( ( bitcount < 11 ) && ( bitcount > 2 ) )
{
// Bit 3 to 10 is data. Parity bit, start and stop bits are ignored.
scancode = ( scancode >> 1 );
if( PIND&0x08 ) //如果数据是1 PD3是键盘数据输入端,PO2是中断口
{
scancode = scancode | 0x80; // Store a '1' for this bit
}
}
MCUCR=0x03; // Set interrupt on rising edge
edge = 1;
}
else // Routine entered at rising edge
{
MCUCR =0x02; // Set interrupt on falling edge
edge = 0;
if(--bitcount == 0) // All bits received
{
bitcount = 11;
key= Decode( scancode );
}
}
}
unsigned char Decode( unsigned char scancode )
{
int i = 0;
unsigned char temp = 0;
static unsigned char shift = 0;
unsigned char key_value=0;
if( ( scancode == 0x12 ) || ( scancode == 0x59 ) )
{
// left or right shift按住SHIFT键
shift = 1;
}
else
{
// a key other than shift was pressed, do a table look-up
for( i = 0; i < 50; i++ )//查表,寻找对应键值
{
temp = char_table[i*3];//PRG_RGB可能因为要用FLASH中数据要用此函数
//i*3应该是表中各键值对应初始地址,因为一块就要三个数据
if ( temp == scancode )
{
if( shift ) //如果按了SHIFT就取后面那个大些的字母
{
key_value = char_table[i*3+2];
}
else
{
key_value = char_table[i*3+1];
}
break;
}
}
shift = 0;
}
delay_ms(20);
return key_value;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -