本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
ps2键盘,按键按下可以显示通码,按键弹起却不能显示断码,求教
程序如下:
//ICC-AVR application builder : 2008-6-19 9:34:52
// Target : M48
// Crystal: 3.2768Mhz
#include <iom48v.h>
#include <macros.h>
#define uchar unsigned char
#define unint unsigned int
#define SYSEN 0x01
#define LCDON 0x03
#define BIAS3 0x29
#define cs_set asm ("sbi 0x0b,2")
#define cs_clr asm ("cbi 0x0b,2")
#define clk_set asm ("sbi 0x0b,4")
#define clk_clr asm ("cbi 0x0b,4")
#define dat_set asm ("sbi 0x0b,3")
#define dat_clr asm ("cbi 0x0b,3")
//PD5--SCL
#define scl_set asm ("sbi 0x09,5")
#define scl_clr asm ("cbi 0x09,5")
//PD6--SDA
#define sda_set asm ("sbi 0x09,6")
#define sda_clr asm ("cbi 0x09,6")
//7位液晶反响显示
uchar lcdshow[11] = {0x77,0x06,0x6b,0x4f,0x1e,0x5d,0x7d,0x07,0x7f,0x5f,0x00};
uchar lcd[7] = {0,10,10,10,10,10,10};
//-----------LCD function-------------
void delay()
{
NOP();
NOP();
NOP();
NOP();
NOP();
}
void send(uchar sdata,uchar n)
{
uchar temp;
for(;n>0;n--)
{
temp = sdata & 0x80;
if(temp)
dat_set;
else
dat_clr;
clk_clr;
delay();
clk_set;
delay();
sdata = sdata << 1;
}
}
void lcd_init()
{
cs_clr; //片选信号为低,选通液晶
send(0x80,3);
send(SYSEN,9);
send(LCDON,9);
send(BIAS3,9);
cs_set; //片选信号为高
clk_set; //片选时钟为高
}
void lcd_display(uchar lcdshow[],uchar *p,uchar m)
{
uchar showdata;
cs_clr;
send(0xA0,9);
for(;m>0;m--)
{
showdata = lcdshow[*p];
send(showdata,8);
p++;
}
cs_set; //片选信号为高
clk_set; //片选时钟为高
}
//-----------LCD function-------------
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x1C;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//-------------PS2-----------------
//PD5=>=SCL
//PD6=>=SDA
void delaytime(uchar i)
{
for(;i>0;i--);
}
void port()
{
DDRD&=~BIT(6); //sda input
//PORTD |= BIT(6);
//PORTD&=~BIT(6);
//DDRD|=BIT(5); //scl output
//DDRD&=~BIT(5);
//PORTD|= BIT(5);
PORTD&=~BIT(5);
//lcd[1]=9;
}
unint receiveByte()
{
uchar i,j,temp,rec_byte;
//lcd[1]=0;
while(!(PIND&0X20));
delaytime(6);
//while((PIND&0X20));
for(i=0;i<9;i++)
{
delaytime(10);
while((PIND&0X20));
delaytime(10);
rec_byte=rec_byte >>1;
//rec_byte=rec_byte << 1;
if((PIND & 0X40))
{
lcd[1]=8;
rec_byte=rec_byte|0x80;
}
else
{
lcd[0]=9;
rec_byte=rec_byte&0x7F;
}
delaytime(10);
while(!(PIND&0X20));
delaytime(10);
}
/*
for(j=0;j<2;j++)
{
delaytime(6); //接受校验位和停止位
while(!(PIND&0X20));
delaytime(6);
while((PIND&0X20));
}*/
return(rec_byte);
}
void displaycount(uchar dis)
{
// lcd[5]=dis/16;
uchar temp;
lcd[6]=dis%10;
temp=dis/10;
lcd[5]=temp%10;
lcd[4]=temp/10;
}
//-------------PS2-----------------
void main(void)
{
uchar dis;
init_devices();
port();
lcd_init();
lcd_display(lcdshow,lcd,7);
while(1)
{
dis=receiveByte();
displaycount(dis);
lcd_display(lcdshow,lcd,7);
}
} |