📄 def_key.c
字号:
#include"config.h"
//ps2与单片机收发数据的常用函数,采用Atmega128 pd0时钟口 pd1数据口
//unsigned char InIndex=0,OutIndex=0;
//unsigned char KeyBuff[BUFF_SIZE]={0,0,0,0,0,0,0,0};
/*********************************************************/
//函数:Order()
//功能:单片机向键盘发送命令,关闭中断
//输入:命令
//输出:?
//描述:
/*********************************************************/
void Order(unsigned char orderByte)
{
unsigned int i, j;
EIMSK = 0x00;//关外部中断屏蔽寄存器
DDRD|=0x03;
PORTD &= 0xfe; //Bring the Clock line low for at least 100 microseconds.
delay_us(100);
PORTD &= 0xfd; //Bring the Data line low.
delay_us(15);
DDRD&=0xfe; //Release the Clock line.
while(PIND&0x01); //Wait for the device to bring the Clock line low.
for(i=0,j=0;i<8;i++)
{
if(orderByte&0x01) //Set/reset the Data line to send the first data bit.
{
PORTD |= (1<<1);
j++;
}
else
PORTD &= ~(1<<1);
orderByte >>=1;
while(!(PIND&0x01)); //Wait for the device to bring Clock high.
while(PIND&0x01); //Wait for the device to bring Clock low.
}
if(!(j&0x01)) PORTD |= (1<<1); //Send parity bit
else PORTD &= ~(1<<1);
while(!(PIND&0x01)); //Wait for the device to bring Clock high.
while(PIND&0x01); //Wait for the device to bring Clock low.
PORTD |= (1<<1); //Send stop bit
while(!(PIND&0x01)); //Wait for the device to bring Clock high.
while(PIND&0x01); //Wait for the device to bring Clock low.
DDRD &=0xfd ; //Release the Data line.
while(PIND&0x02); //Wait for the device to bring Data low.
while(PIND&0x01); //Wait for the device to bring Clock low.
while((PIND&0x03)!=0x03); //Wait for the device to release Data and Clock.
EIMSK = 0x01;//开外部中断屏蔽寄存器
}
/*************************
//函数:ps2_init()
//功能:ps2键盘上电初始化与单片机的交互识别
//输入:
//输出:
*************************/
void ps2_init(void)
{
unsigned int i;
EIMSK = 0x00;//关外部中断屏蔽寄存器
for(i=0;i<1000;i++)
;//延时稳定
/* 初始化变量 */
DDRC |= 0x80;
PORTC |= 0x80; //关键盘电源
for(i=0;i<1000;i++)
delay_us(10);
DDRD |= 0x07;
PORTC &= 0x7f;//开键盘
PORTD |= 0x03;
EIMSK = 0x01;//开外部中断屏蔽寄存器
delay_us(50);
state_flag=1;
while(!KeyBoardConnectFlag);//等待键盘自检成功
Order(COMMAND_RESET); //复位键盘
delay_us(5);
Order(COMMAND_SETSTATUS);//设置状态灯CapsLock,NumLock,ScrollLock
Order(DISALL); //关闭所有灯
Order(COMMAND_SETSTATUS);
Order(NUMLOCK_EN); //开NumLock灯
Order(COMMAND_SETRATE); //设置速率延时
Order(0x20); //500ms/30
Order(COMMAND_ENABLE); //使能键盘
Order(COMMAND_SETRATE); //设置速率延时
Order(0x20); //
delay_us(200); //延时
state_flag=0;
}
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
//external interupt on INT0
static unsigned char BitCount=11,KeyData=0;
if(BitCount<11 && BitCount>2)
{
DDRD&=0xfd;
KeyData>>=1;
if(KeyBoardSda)
KeyData|=0x80;
}
if(--BitCount==0)
{
BitCount=11;
//KeyBuff[InIndex]=KeyData;
//if(KeyData == CODE_POST)
// KeyBoardConnectFlag = 1;//键盘上电初始化成功的标志
KeyBoardDecode(KeyData);
//if(++InIndex==BUFF_SIZE)
//InIndex=0;
KeyData=0;
}
}
void delay_1us(void)
{
asm("nop");
}
void delay_us(unsigned int n)
{
unsigned int i;
for(i=n;i>0;i--)
delay_1us();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -