学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx

上传人:b****6 文档编号:8265883 上传时间:2023-01-30 格式:DOCX 页数:14 大小:242.07KB
下载 相关 举报
学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx_第1页
第1页 / 共14页
学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx_第2页
第2页 / 共14页
学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx_第3页
第3页 / 共14页
学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx_第4页
第4页 / 共14页
学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx

《学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx》由会员分享,可在线阅读,更多相关《学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx(14页珍藏版)》请在冰豆网上搜索。

学习51单片机与PS2鼠标程序+LCD1602显示坐标.docx

学习51单片机与PS2鼠标程序+LCD1602显示坐标

学习51单片机与PS2鼠标程序

简介:

PS2鼠标,LCD1602显示坐标,有LED指示灯,等等~~~~~~其实发现学起来也很简单。

第一步:

PS/2接口和协议简介

1PS/2接口和协议 

1.1接口的物理特性 

   PS/2接口用于许多现代的鼠标和键盘,由IBM最初开发和使用。物理上的PS/2接口有两种类型的连接器:

5脚的DIN和6脚的mini-DIN。图1就是两种连接器的引脚定义。使用中,主机提供+5V电源给鼠标,鼠标的地连接到主机电源地上。

 

1.2接口协议原理 

   PS/2鼠标接口采用一种双向同步串行协议。即每在时钟线上发一个脉冲,就在数据线上发送一位数据。在相互传输中,主机拥有总线控制权,即它可以在任何时候抑制鼠标的发送。方法是把时钟线一直拉低,鼠标就不能产生时钟信号和发送数据。在两个方向的传输中,时钟信号都是由鼠标产生,即主机不产生通信时钟信号。

   如果主机要发送数据,它必须控制鼠标产生时钟信号。方法如下:

主机首先下拉时钟线至少100μs抑制通信,然后再下拉数据线,最后释放时钟线。通过这一时序控制鼠标产生时钟信号。当鼠标检测到这个时序状态,会在10ms内产生时钟信号。如图3中A时序段。主机和鼠标之间,传输数据帧的时序如图2、图3所示。2.2数据包结构在主机程序中,利用每个数据位的时钟脉冲触发中断,在中断例程中实现数据位的判断和接收。在实验过程中,通过合适的编程,能够正确控制并接收鼠标数据。但该方案有一点不足,由于每个CLOCK都要产生一次中断,中断频繁,需要耗用大量的主机资源。

  

 

2PS/2鼠标的工作模式和协议数据包格式

2.1PS/2鼠标的四种工作模式 

   PS/2鼠标的四种工作模式是:

Reset模式,当鼠标上电或主机发复位命令0xFF给它时进入这种模式;Stream模式鼠标的默认模式,当鼠标上电或复位完成后,自动进入此模式,鼠标基本上以此模式工作;Remote模式,只有在主机发送了模式设置命令0xF0后,鼠标才进入这种模式;Wrap模式,这种模式只用于测试鼠标与主机连接是否正确。 

   PS/2鼠标在工作过程中,会及时把它的状态数据发送给主机。发送的数据包格式如表1所示。

 

Byte1中的Bit0、Bit1、Bit2分别表示左、右、中键的状态,状态值0表示释放,1表示按下。Byte2和Byte3分别表示X轴和Y轴方向的移动计量值,是二进制补码值。Byte4的低四位表示滚轮的移动计量值,也是二进制补码值,高四位作为扩展符号位。这种数据包由带滚轮的三键三维鼠标产生。若是不带滚轮的三键鼠标,产生的数据包没有Byte4其余的相同。

第二步:

/**********************XXXX.C部分*********************/

#include

#include"mouse.h"

#include"LCD1602_4.h"

#include"DELAY52.h"

sbitbeep=P3^7;

voidmain()

{

LCD1602_Init();//初始化液晶1602

Init_mouse();

mouse_send_data(0xf4);//向鼠标发送0xF4命令发其开始工作

//mouse_send_data(0xc8);

//mouse_send_data(0x64);

//mouse_send_data(0x50);

EX1=0;//关掉外部中断以避开鼠标发回的应答

delayms(100);

EX1=1;//重开外部中断

while

(1)

{led=1;

CLEARSCREEN;//清屏

LCD1602_write_string(0,0,"x:

");

num(0,2,move_x);//x坐标值

LCD1602_write_string(0,8,"y:

");

num(0,10,move_y);//y坐标值

//LCD1602_write_string(1,8,"z:

");

//num(1,10,move_z);//y坐标值

if(mouse_data[0]&0x01)//如果点下左键

{

beep=0;

LCD1602_write_string(1,0,"left");

}

elseif(mouse_data[0]&0x02)//如果点下右键

{

beep=0;

LCD1602_write_string(1,0,"right");

}

elseif(mouse_data[0]&0x04)//如果点下中键

{

beep=0;

LCD1602_write_string(1,0,"middle");

}

else

{

beep=1;

LCD1602_write_string(1,0,"nothing");

}

delayms(50);

}

}

 

/********************XXX.H文件部分**********************/

#ifndefMOUSE_H

#defineMOUSE_H

#include"DELAY52.h"

//Setsamplerate200;

//Setsamplerate200;

//Setsamplerate80;

sbitmouse_SDA=P3^4;//数据线P3_5

sbitmouse_CLK=P3^3;//时钟线P3_3

sbitled=P3^6;

//sbitled1=P1^3;

ucharbdatamouse_byte;//接收字节bdata-->可寻址的片内RAM

sbitmouse_byte_bit0=mouse_byte^0;//mouse_byte第0位

sbitmouse_byte_bit1=mouse_byte^1;//mouse_byte第1位

sbitmouse_byte_bit2=mouse_byte^2;//mouse_byte第2位

sbitmouse_byte_bit3=mouse_byte^3;//mouse_byte第3位

sbitmouse_byte_bit4=mouse_byte^4;//mouse_byte第4位

sbitmouse_byte_bit5=mouse_byte^5;//mouse_byte第5位

sbitmouse_byte_bit6=mouse_byte^6;//mouse_byte第6位

sbitmouse_byte_bit7=mouse_byte^7;//mouse_byte第7位

ucharbdatamouse_fuction;//功能信息字节

ucharmouse_buffer[11];//接收位数据缓冲区

ucharmouse_buffer_bit=0;//mouse_buffer[mouse_buffer_bit]

ucharmouse_data[3];//接收鼠标数据缓冲区,分别存放:

功能信息字节,x位移量,y位移量

ucharmouse_data_bit=0;//mouse_data[mouse_data_bit]

uintmove_x=10000;//存放横坐标

uintmove_y=10000;//存放纵坐标

voidInit_mouse(void)

{

TCON=0x00;////中断触发方式0

EA=1;//开放中断

EX1=1;//允许外部中断1

ET0=0x01;//允许全局中断,允许设定时器/计数器0溢出中断开定时器中断0

PX1=1;//设置中断优先级设外部中断1为最高优先级别

}

 

/***********************************************************************

发送数据

************************************************************************/

voidmouse_send_data(uchardat)

{

uchari;

EX1=0;/*关闭外部中断1*/

ACC=dat;/*将要发送的数据放入A寄存器*/

mouse_CLK=0;/*拉低时钟线*/

delay10us(200);/*延时100us以上*/

mouse_SDA=0;/*拉低数据线*/

delay10us(40);

mouse_CLK=1;/*释放时钟线*/

for(i=0;i<=7;i++)/*低位在前,一次发送8个数据位*/

{

while(mouse_CLK==1);/*等待设备拉低时钟线*/

mouse_SDA=(dat>>i)&0x01;/*发送数据位*/

while(mouse_CLK==0);/*等待设备释放时钟线*/

}

while(mouse_CLK==1);

mouse_SDA=~P;/*发送校验位,奇校验*/

while(mouse_CLK==0);

while(mouse_CLK==1);

mouse_SDA=1;/*发送停止位*/

while(mouse_CLK==0);

while(mouse_CLK==1);/*应答位*/

while(mouse_CLK==0);

EX1=1;/*打开外部中断1*/

}

/*********************************************

奇校检

**********************************************/

ucharCheckout(void)

{

ACC=mouse_byte;

if(~P==mouse_buffer[9])

return1;

else

return0;

}

/*********************************************************

数据分析及处理

**********************************************************/

voiddata_analyse(void)

{

//将收到的11位信号中截取8位数据放进mouse_byte

mouse_byte_bit0=mouse_buffer[1];

mouse_byte_bit1=mouse_buffer[2];

mouse_byte_bit2=mouse_buffer[3];

mouse_byte_bit3=mouse_buffer[4];

mouse_byte_bit4=mouse_buffer[5];

mouse_byte_bit5=mouse_buffer[6];

mouse_byte_bit6=mouse_buffer[7];

mouse_byte_bit7=mouse_buffer[8];

if(Checkout())//如果校验正确

{

if(mouse_data_bit<3)

mouse_data[mouse_data_bit++]=mouse_byte;

if(mouse_data_bit==3)

{

mouse_data_bit=0;

if(mouse_data[0]&0x10)//如果"Xsignbit"为1,表示鼠标向左移

{

move_x-=(256-mouse_data[1]);//x坐标减

}

else

{

move_x+=mouse_data[1];//x坐标加

}

if(mouse_data[0]&0x20)

{

move_y-=(256-mouse_data[2]);//y坐标减

}

else

{

move_y+=mouse_data[2];//y坐标加

}

}

}

}

/**************************************************

外部中断1

***************************************************/

voidReceiveData(void)interrupt2

{led=0;

if(mouse_buffer_bit<=10)

{

while(mouse_CLK==0);//等待设备拉高时钟线

mouse_buffer[mouse_buffer_bit++]=mouse_SDA;//接收数据

}

if(mouse_buffer_bit==10)

{

data_analyse();//数据分析及处理

mouse_buffer_bit=0;

}

}

#endif

 

/********************XXX.H文件部分**********************/

#ifndefLCD1602_4_H

#defineLCD1602_4_H

#include

#include"DELAY52.h"

#defineLCD1602_DATAP0

#defineCLEARSCREENLCD1602_write_cmd(0x01)

sbitLCD1602_RS=P2^5;

sbitLCD1602_RW=P2^6;

sbitLCD1602_EN=P2^7;

//**********************************************************************

voidLCD1602_Init(void);//液晶初始化

voidLCD1602_write_cmd(ucharcommand);//写命令

voidLCD1602_write_data(uchartemp);//写数据

voidLCD1602_set_xy(ucharx,uchary);//设置坐标

voidLCD1602_write_char(ucharx,uchary,uchardat);//写一个字符到第x行y列

voidLCD1602_write_string(ucharx,uchary,uchar*s);//写字符串到第x行y列

voidLCD1602_Read_BF(void);//读忙信号

voidnum(ucharx,uchary,uintn);//在第x行,第y列显示整型数字n

//**********************************************************************

voidLCD1602_Init(void)

{

LCD1602_write_cmd(0x28);

LCD1602_write_cmd(0x28);

LCD1602_write_cmd(0x28);//设置4位数据传输模式

LCD1602_write_cmd(0x0C);

LCD1602_write_cmd(0x80);

CLEARSCREEN;

}

 

voidLCD1602_Read_BF(void)

{

LCD1602_RW=1;//RW1

LCD1602_RS=0;//RS0

LCD1602_EN=1;//EN1ReadBF

LCD1602_DATA=LCD1602_DATA&0x0F|0xf0;

while(LCD1602_DATA&0x80);

LCD1602_EN=0;

}

voidLCD_en_write(void)//EN端产生一个高电平脉冲,写LCD

{

LCD1602_EN=1;

_nop_();

LCD1602_EN=0;

}

//*************************************

voidLCD1602_write_cmd(ucharcommand)

{

LCD1602_Read_BF();

LCD1602_RS=0;//RS0

LCD1602_RW=0;//RW0

LCD1602_DATA&=0x0F;

LCD1602_DATA=command&0xf0|LCD1602_DATA&0x0f;

LCD_en_write();

command=command<<4;

LCD1602_DATA&=0x0F;

LCD1602_DATA=command&0xf0|LCD1602_DATA&0x0f;

LCD_en_write();

}

//*********************************

voidLCD1602_write_data(uchardat)

{

LCD1602_Read_BF();

LCD1602_RS=1;//RS1

LCD1602_RW=0;//RW0

LCD1602_DATA&=0x0F;

LCD1602_DATA=dat&0xf0|LCD1602_DATA&0x0f;

LCD_en_write();

dat=dat<<4;

LCD1602_DATA&=0x0F;

LCD1602_DATA=dat&0xf0|LCD1602_DATA&0x0f;

LCD_en_write();

}

//**********************************************

voidLCD1602_set_xy(ucharx,uchary)

{

ucharaddress;

y&=0x0f;

if(!

x)

address=0x80+y;

else

address=0xc0+y;

LCD1602_write_cmd(address);

}

//****************************************************************

voidLCD1602_write_char(ucharx,uchary,uchardat)

{

LCD1602_set_xy(x,y);

LCD1602_write_data(dat);

}

//*********************************************************************

voidLCD1602_write_string(ucharx,uchary,uchar*s)

{

LCD1602_set_xy(x,y);

while(*s)

{

LCD1602_write_data(*s);

s++;

}

}

voidnum(ucharx,uchary,uintn)

{

uchari,length,a[6]={0,0,0,0,0,0};

uintnx=n;

if(n==0){LCD1602_write_char(x,y,'0');return;}

for(i=0;i<6;i++)

{

if(nx>=1)length++;

nx/=10;

}

nx=n;

for(;length>0;length--)

{

a[length-1]=nx%10+48;

nx/=10;

}

LCD1602_write_string(x,y,a);

}

#endif

 

/********************XXX.H文件部分**********************/

#ifndefDELAY52_H

#defineDELAY52_H

#defineucharunsignedchar

#defineuintunsignedint

#include

//起始值delayus

(1)=27us,间隔9.9us

voiddelay10us(uintt)

{

while(t--);

}

voiddelayms(uintt)

{

uinti;

ucharj;

for(i=0;i

for(j=0;j<125;j++);

}

#endif

 

先按上面接口连接,然后即可编译下载,测试~~~~然后在修改成自己的代码。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1