📄 mouse.bak
字号:
/****************************************************************
****************************************************************/
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "com.h"
#define NOERROR 0 //错误标志
unsigned char DIREC = 0 ; //方向
int SError = NOERROR;
int portbase = 0x3f8; //串口地址
void interrupt(*oldvects[2])(); //定义了一个中断数组,用来存放中断返回值。
static unsigned char temp; //串口接收
unsigned int count = 0;
unsigned char LastStatus = 0;
unsigned char OldStatus = 0;
unsigned char RecStatus = 0;
unsigned int x,y;
static unsigned int s1[4],s2[4];
static unsigned int Newx1,Newy1,Newx2,Newy2;//1为按下,2为弹起
unsigned char up_flag,down_flag; //按下弹起标志
unsigned char touchup,touchdown; //按下或弹起
/***************************************************************************
函 数: void up()
函数说明:
入口参数:
****************************************************************************/
void up(void)
{
switch(RecStatus)
{
case 0x01:
s2[0]= (int)temp & 0x0f;
RecStatus=0x02;
break;
case 0x02:
s2[1]= (int)temp & 0x7f ;
RecStatus=0x03;
break;
case 0x03:
s2[2]= (int)temp & 0x0f;
RecStatus=0x04;
break;
case 0x04:
s2[3]= (int)temp & 0x7f;
RecStatus=0x05;
break;
case 0x05:
RecStatus=0x00;
touchup=0;
/*********************************
240--256
320--192
480--128
640--96
800--76.8
**********************************/
if(DIREC)
{
s2[2]<<=7;Newy2=(s2[2]|s2[3]);
s2[0]<<=7;Newx2=(s2[0]|s2[1]);
}
else
{
s2[0]<<=7;Newy2=(s2[0]|s2[1]);
s2[2]<<=7;Newx2=(s2[2]|s2[3]);
}
up_flag=1;
break;
default:
break;
}
}
/***************************************************************************
函 数: void down()
函数说明:
入口参数:
****************************************************************************/
void down(void)
{
switch(RecStatus)
{
case 0x01:
s1[0]= (int)temp & 0x000f;
RecStatus=0x02;
break;
case 0x02:
s1[1]= (int)temp & 0x007f ;
RecStatus=0x03;
break;
case 0x03:
s1[2]= (int)temp & 0x000f;
RecStatus=0x04;
break;
case 0x04:
s1[3]= (int)temp & 0x007f;
RecStatus=0x05;
break;
case 0x05:
RecStatus=0x00;
touchdown=0;
if(DIREC)
{
s1[2]<<=7;Newy1=(s1[2]|s1[3]);
s1[0]<<=7;Newx1=(s1[0]|s1[1]);
}
else
{
s1[0]<<=7;Newy1=(s1[0]|s1[1]);
s1[2]<<=7;Newx1=(s1[2]|s1[3]);
}
down_flag=1;
break;
default:
break;
}
}
/*****************************************************************************************
串口中断服务程序
******************************************************************************************/
void far interrupt com_int(void)
{
unsigned char Status = 0;
char Loop = 0;
while(Loop == 0)
{
Status = inportb(portbase + IIR) & RX_MASK;
switch(Status)
{
case NO_INT:
LastStatus = 1;
break;
case URS_ID:
inportb(portbase + LSR); //撤销中断
LastStatus = 2;
break;
case RX_ID:
temp = inportb(portbase + RXR);
LastStatus = 3;
break;
case FDT_ID:
inportb(portbase + RXR); //撤销中断
LastStatus = 4;
break;
case TBRE_ID:
LastStatus = 5;
break;
case HSR_ID:
inportb(portbase + MSR); //撤销中断
LastStatus = 6;
break;
default:
LastStatus = 7;
break;
}
if((LastStatus == 1) || (LastStatus == 7)) break;
}
outportb(ICR, EOI); /*中断应答*/
if((temp == 0xb1)||(temp == 0xb0))
{
if(temp == 0xb1)
RecStatus = 0x01,touchdown = 1;
else if(temp == 0xb0)
RecStatus = 0x01,touchup = 1;
}
else
{
if (touchdown == 1)
down();
else if(touchup == 1)
up();
}
}
/********************************************************************************
函数功能: 设置中断向量函数,com1用中断4向量号为0xc。
com2用中断3向量号为0xb,该中断号是固定的用户不可以修改。
函数入口:
*********************************************************************************/
void setvects(void)
{
if(portbase == 0x3f8)
{
oldvects[0] = getvect(0xc); /*保存中断4的向量,以便在退出时恢复*/
setvect(0xc, com_int); /*把中断4向量改为com_int*/
}
if(portbase == 0x2f8)
{
oldvects[0] = getvect(0xb);
setvect(0xb, com_int);
}
}
/**************************************************************************************
函数功能: 恢复原来的中断向量
函数入口:
***************************************************************************************/
void resvects(void)
{
if(portbase == 0x3f8)
setvect(0xc, oldvects[0]);
if(portbase == 0x2f8)
setvect(0xb, oldvects[0]);
}
/*******************************************************************
函数功能: 打开串口中断和系统中断。
函数入口:
********************************************************************/
void i_enable(void)
{
int c;
disable();
c = inportb(portbase + MCR) | MC_INT;
outportb(portbase + MCR, c);
outportb(portbase + IER, RX_INT);
if(portbase == 0x3f8)
c = inportb(IMR) & IRQ4;
if(portbase == 0x2f8)
c = inportb(IMR) & IRQ3;
outportb(IMR, c);
enable();
}
/*******************************************************************
函数功能: 关闭串口中断和系统中断。
函数入口:
********************************************************************/
void i_disable(void)
{
int c;
disable();
if(portbase == 0x3f8)
c = inportb(IMR)| ~IRQ4;
if(portbase == 0x2f8)
c = inportb(IMR)|~IRQ3;
outportb(IMR, c);
outportb(portbase + IER, 0);
c = inportb(portbase + MCR) & ~MC_INT;
outportb(portbase + MCR, c);
enable();
}
/*******************************************************************
函数功能: 打开串口(打开RTS,与DTS)。
函数入口:
********************************************************************/
void comm_on(void)
{
int c;
i_enable();
c = inportb(portbase + MCR) | DTR | RTS;
outportb(portbase + MCR, c);
}
/*******************************************************************
函数功能: 关闭串口。
函数入口:
********************************************************************/
void comm_off(void)
{
i_disable();
outportb(portbase + MCR, 0);
resvects();
}
void OpenSer(void)
{
setvects();
comm_on();
}
/***************************************************************************
函数说明: 串口初始化函数SerInit
入口参数: Port 串口地址如0x3f8;Speed 速率 如115200
Parity 奇偶效验NO_PARITY无,EVEN_PARITY偶效验,ODD_PARITY奇效验
Bits 数据位数如8,StopBit 停止位如1。
****************************************************************************/
SerInit(int Port, unsigned long int Speed, int Parity, int Bits, int StopBit)
{
int flag = 0;
int Setting = 0;
char c;
int divisor;
//波特率计算公式:BRD=1843200/16*波特率=115200/波特率
// 表 波特率除数
// 位速率 BRDH BRDL
// 50 09h 00h
// 110 04h 17h
// 300 01h 80h
// 1200 00h 60h
// 2400 00h 30h
// 4800 00h 18h
// 9600 00h 0Ch
// 19200 00h 06h
// 115200 00h 01h
if (Speed == 0) //避免除数为零
flag = -1;
else
divisor = (int) (115200L/Speed);
portbase = Port;
if (portbase == 0) //避免端口地址为零
flag = -1;
if (Bits < 5 || Bits > 8) //避免数据位错
flag = -1;
if (StopBit != 1 && StopBit != 2) //避免停止位错
flag = -1;
if (Parity != NO_PARITY && Parity != ODD_PARITY && Parity != EVEN_PARITY)
flag = -1;
disable(); //屏蔽中断
c = inportb(portbase + LCR); //读出控制寄存器
outportb(portbase + LCR, (c | 0x80)); //打开除数寄存器
outportb(portbase + DLL, (divisor & 0x00FF)); //初始化波特率
outportb(portbase + DLH, ((divisor >> 8) & 0x00FF)); //初始化波特率
outportb(portbase + LCR, c); //关闭除数寄存器
enable(); //开放硬件中断
Setting = Bits-5; //得到位数
if(StopBit == 1) //得到停止位
Setting |= 0x00;
else
Setting |= 0x04;
Setting |= Parity; //得到校验位
disable();
outportb(portbase + LCR, Setting);
enable();
if (!flag)
OpenSer();
return flag;
}
/***************************************************************************
函 数:
函数说明:
入口参数:
****************************************************************************/
void interrupt new_int33()
{
unsigned int r_ax;
r_ax=_AX;
switch(r_ax)
{
/***********/
case 0x03:
if(up_flag==1)
{
up_flag=0; //清标志位
_CX=Newx1; //横向坐标
_DX=Newy1; //纵向坐标
_BX=0x01;
asm
{
mov ss:[bp+12],CX;
MOV ss:[bp+10],DX;
MOV ss:[bp+14],BX;
}
}
break;
/*****获取鼠标按下信息******/
case 0x05:
{
_CX=Newx1; //横向坐标
_DX=Newy1; //纵向坐标
asm
{
mov ss:[bp+12],CX;
MOV ss:[bp+10],DX;
}
}
break;
/*****获取鼠标释放信息******/
case 0x06:
{
_CX=Newx2; //横向坐标
_DX=Newy2; //纵向坐标
asm
{
mov ss:[bp+12],CX;
MOV ss:[bp+10],DX;
}
}
break;
}
}
void main(int argc,char *argv[])
{
unsigned long speed = 9600; /*波特率参数*/
int parity = NO_PARITY; /*奇偶效验*/
int bits = 8; /*数据位数*/
int stopbits = 1; /*停止位数*/
if(argc != 2)
{
printf("example:mouse.exe 2");
/*命令行参数*/
}
switch(atoi(argv[1]))
{
case 1:
portbase = 0x3f8;
break;
case 2:
portbase = 0x2f8;
break;
default:
break;
}
/*********************************
240--256
320--192
480--128
640--96
600-- 102.4
800--76.8
**********************************/
//串口初始化
SerInit(portbase, speed, parity, bits, stopbits);
setvect(0x33,new_int33);
keep(0,_SS+_SP/16-_psp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -