想做一个东西 就是 通过 C51单片机 转接 PS2 接口的鼠标到电脑 把鼠标送给电脑的 数据修改下 ,效果就是 左移 鼠标 指针是右移 右移鼠标 指针左移 上下正常。网上找了很多代码 看不太懂。
有个代码 谁能帮我改动下吗?????
谢谢!!!!!!!!!!!!!!!!!!
这个是我们坛子里的高手写的 用液晶显示 鼠标的坐标
#include <reg52.H>
#include <intrins.H>
#include <head.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DATA P2 //数据口
sbit RS = P1^5; //并行的指令/数据选择信号, H数据, L命令
sbit RW = P1^7; //并行读写选择信号, H读, L写
sbit E = P1^6; //并行使能端, H有效, L无效
sbit PSB = P1^1; //并/串接口选择, H并,L串
sbit RET = P1^4; //复位, L有效
sbit DATA=P0^2;
sbit CLK=P3^2;
uchar count,ASCII=65,Data1,Data2,Data3;
uchar left=0,right=0,byte=0;
uchar l_press=0,r_press=0,l_press0=0,r_press0=0; //后两个变量时表示哦前一时刻的按键情况000000000
int X,Y,XJ,YJ;
uchar code datas1[] = {"PS2 鼠标检测实验"};
uchar code datas2[] = {"坐标情况:"};
uchar code datas3[] = {"x:"};
uchar code datas4[] = {"y:"};
uchar code datas5[] = {"L:"};
uchar code datas6[] = {"R:"};
uchar code datas7[] = {"按键:"};
void trains(char Code);
void send(uchar Data);
//延时约2us
//检测LCD是否处于忙状态, 若忙返回1, 空闲返回0
bit checkBusy()
{
bit busy;
RS = 0;
RW = 1;
E = 1;
delayUs();
busy = (bit)(LCD_DATA&0x80);
E = 0;
return busy;
}
//等待LCD到空闲
void wait()
{
while(checkBusy());
}
//写命令
void writeCmd(uchar cmd)
{
wait();
RS = 0;
RW = 0;
E = 0;
delayUs();
LCD_DATA = cmd;
delayUs();
E = 1;
delayUs();
E = 0;
}
//写数据
void writeData(uchar dat)
{
wait();
RS = 1;
RW = 0;
E = 0;
delayUs();
LCD_DATA = dat;
delayUs();
E = 1;
delayUs();
E = 0;
}
//初始化LCD
void init()
{
PSB = 1; //并口方式
writeCmd(0x30); //基本指令, 扩充指令为34H
delayMs(10);
writeCmd(0x0c); //显示开, 关光标
writeCmd(0x01); //清屏
delayMs(10);
}
void setPosition(uchar x, uchar y)
{
uchar p;
switch(x%4)
{
case 0: p = 0x80; break; //第一行开始地址
case 1: p = 0x90; break; //第二行
case 2: p = 0x88; break; //第三行
case 3: p = 0x98; break; //第四行
}
p += y;
writeCmd(p);
}
void writeString(uchar * str)
{
uchar i = 0;
while(str[i] != ' ')
{
writeData(str[i++]);
}
}
void DisplayGraphic(unsigned char code *adder)
{
int i,j;
//*******显示上半屏内容设置
for(i=0;i<32;i++) //
{
writeCmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
writeCmd(0x80); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
writeData(*adder);
adder++;
}
}
//*******显示下半屏内容设置
for(i=0;i<32;i++) //
{
writeCmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
writeCmd(0x88); //SET 水平地址 HORIZONTAL ADD
for(j=0;j<16;j++)
{
writeData(0xff);
adder++;
}
}
}
void initkb() //键盘初始化函数
{
EA=1;//?
EX0=1;
IT0=1;
count=0;
}
void exter0() interrupt 0
{
count++;
if(count>=2 && count<=9)
{
Data1=(Data1>>1);
if(DATA==1)
Data1|=0x80;
}
if(count>=13 && count<=20)
{
Data2=(Data2>>1);
if(DATA==1)
Data2|=0x80;
}
if(count>=24 && count<=31)
{
Data3=(Data3>>1);
if(DATA==1)
Data3|=0x80;
}
}
void trains1(char scancode)
{
l_press0=l_press;
r_press0=r_press;
if(scancode & 0x01)
l_press=1;
else l_press=0;
if(scancode & 0x02)
r_press=1;
else r_press=0;
if(l_press0==0 && l_press==1)
{left++;}
if(r_press0==0 && r_press==1)
{right++;}
}
void trains2(char scancode)
{
X=X+scancode;
}
void trains3(char scancode)
{
Y=Y+scancode;
}
void main()
{
init();
initkb();
delayMs(100);
//开始显示各种00000
setPosition(0, 0);
writeString(datas1);
setPosition(1, 0);
writeString(datas2);
setPosition(2, 0);
writeString(datas3);
setPosition(2, 4);
writeString(datas4);
setPosition(3, 0);
writeString(datas7);
setPosition(3, 4);
writeString(datas5);
setPosition(3, 6);
writeString(datas6);
send(0xF4);//使能命令的发送000000000
delayMs(200);
count=0;
while(1)
{
if(count==33)
{
count=0;
trains1(Data1);
trains2(Data2);
trains3(Data3);
}
if(count>33) //纠错机制00000000
{
EX0=0;
delayMs(2);
count=0;
EX0=1;
}
if(count==0)
{
setPosition(2, 1);
if(X>=0)
{
writeData('+');
XJ=X;
}
else
{writeData('-');XJ=-X;}
writeData(XJ/10000+48);
writeData((XJ%10000)/1000+48);
writeData((XJ%1000)/100+48);
writeData((XJ%100)/10+48);
writeData(XJ%10+48);
setPosition(2, 5);
if(Y>=0)
{
writeData('+');
YJ=Y;
}
else
{writeData('-');YJ=-Y;}
writeData(YJ/10000+48);
writeData((YJ%10000)/1000+48);
writeData((YJ%1000)/100+48);
writeData((YJ%100)/10+48);
writeData(YJ%10+48);
setPosition(3, 5);
writeData(left/10+48);
writeData(left%10+48);
setPosition(3, 7);
writeData(right/10+48);
writeData(right%10+48);
}
}
}
void send(uchar Data)
{
uchar i,high=0;
EX0=0;
CLK=0;
delay10us(12);
DATA=0;
delay10us(2);
CLK=1;
while(CLK==1); //开始发送八位数据
for(i=0;i<=7;i++)
{
if(Data & 0x01)
{
DATA=1;
high++;
}
else {DATA=0;}
Data=(Data>>1);
while(CLK==0);
while(CLK==1);
}
if((high%2)==0) DATA=1;
else DATA=0;
while(CLK==0);
while(CLK==1);
DATA=1;
while(DATA==1);
while(CLK==1);
while(CLK==0);
EX0=1;//写数据程序结束后开中断
}
=============================================================
这个是#include <head.h>头文件
#define uchar unsigned char
#define uint unsigned int
void delayUs()
{
_nop_();
_nop_();
}
//经示波器测试这是40us延时0000000000
void delay10us(uchar i) //10us延时程序
{ uchar a=1;
uchar b;
for(b=0;b<i;b++)
{ while(a--);a=1;}
}
//延时 a * 2ms0000000
void delayMs(uint a)
{
uint i, j;
for(i = a; i > 0; i--)
for(j = 100; j > 0; j--);
}
谢谢了 不要显示
鼠标 PS2 -----单片机-----PC电脑 就这样子接
还有鼠标电源是不是要单片机控制啊??????????
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
当时实现的情况是,单片机读取PS/2鼠标的按键和移动信息,再通过单片机的串口发送给做界面的ARM芯片串口;
附件为原理图,状态转换图,51原代码等,供单片机识别PS/2鼠标反面的参考;
一周热门 更多>