⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouse.h

📁 对PS2鼠标的操作通过1602来进行显示!
💻 H
字号:
#include"delay.h"
#ifndef MOUSE_H
#define MOUSE_H

#define uchar unsigned char
#define uint unsigned int

sbit mouse_dat=P3^5;
sbit mouse_clk=P3^3;

sbit led1=P1^3;

uchar bdata mouse_byte;
sbit mouse_byte_bit0=mouse_byte^0;
sbit mouse_byte_bit1=mouse_byte^1;
sbit mouse_byte_bit2=mouse_byte^2;
sbit mouse_byte_bit3=mouse_byte^3;
sbit mouse_byte_bit4=mouse_byte^4;
sbit mouse_byte_bit5=mouse_byte^5;
sbit mouse_byte_bit6=mouse_byte^6;
sbit mouse_byte_bit7=mouse_byte^7;

uchar bdata mouse_function;

uchar mouse_buffer[11];
uchar mouse_buffer_bit=0;

uchar mouse_data[3];
uchar mouse_data_bit=0;

uint move_x=5000;
uint move_y=5000;

void init_mouse()
{
	TCON=0x00;
	EA=1;
	EX1=1;
	ET0=0x01;
	PX1=1;
}

void mouse_send_command(uchar dat)
{
	unsigned char i;
	EX1=0;                              /*关闭外部中断1*/
	ACC=dat;                            /*将要发送的数据放入A寄存器*/
	mouse_clk=0;                       /*拉低时钟线*/
	delay10us(200);                      /*延时100us以上*/
	mouse_dat=0;                        /*拉低数据线*/
	delay10us(40);
	mouse_clk=1;                       /*释放时钟线*/		   //??????????是否要释放数据线,而不是时钟线
	for(i=0;i<=7;i++)             /*低位在前,一次发送8个数据位*/
	{
		while(mouse_clk==1);           /*等待设备拉低时钟线*/
		mouse_dat=(dat>>i)&0x01;      /*发送数据位*/		//从低位到高位一位一位地把数据发送到数据线	  
		while(mouse_clk==0);           /*等待设备释放时钟线*/
	}
	while(mouse_clk==1);
	mouse_dat=~P;                       /*发送校验位,奇校验*/
	while(mouse_clk==0);
	while(mouse_clk==1);
	mouse_dat=1;                        /*发送停止位*/
	while(mouse_clk==0);							  
	while(mouse_clk==1);				/*应答位*/	 //????此处是否该将时钟线拉低
	while(mouse_clk==0);							 //????加上此句:mouse_SDA=0;最后还得释放数据线
	EX1=1;
}

uchar checkout()
{
	ACC=mouse_byte;
	if(~P==mouse_buffer[9])
		return 1;
	else 
		return 0;
}

void data_analyse()
{
	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)//如果"X sign bit"为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坐标加
			}	
		}
	}	
}

void receive_data(void) interrupt 2
{
	if(mouse_buffer_bit<=10)
	{
		while(mouse_clk==0);
		mouse_buffer[mouse_buffer_bit++]=mouse_dat;
	}
	if(mouse_buffer_bit==10)
	{
		data_analyse();
		mouse_buffer_bit=0;	
	}
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -