Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial mouse data manipulation

Status
Not open for further replies.

kevin5059

Technical User
Oct 24, 2005
6
GB
Hi, I'm doing a project with using a serial mouse to control the frequency and volume of the sound that emit from the speaker. However, I faced with a hard problem with my C programming sicne I'm new in this field. I got some data format about the serial mouse from this website:


For your information, I'm using the serial mouse that work at 1200 baud with 1 stop bit and no parity. At the end, I try to developed my own C codes to read the data from the mouse and display it to the screen. However, the data are not as what I expected.

Here are my C code:

Code:
[green]
#include <dos.h>
#include <stdio.h>
#include <string.h>
#include <beck.h>[/green]
[blue]/*beck.h is self-created header file*/[/blue]
[green]
#define  Byte1_Mask     0x40
#define  Left_Mask      0x20
#define  Right_Mask     0x10

#define  Waiting_For_Byte1     0     /*0100 0000*/
#define  Waiting_For_Byte2     1     /*0010 0000*/
#define  Waiting_For_Byte3     2     /*0001 0000*/

#define  Mou_Write_Latch        0x202
#define  Mou_Read_Latch         0x201
#define  Mou_Control_Latch      0x200
#define  Speaker                0x308

#define   Status_Register       1
#define   Data_Register         2
#define   Mouse_IRQ             3
#define   Delay                 50      	
[/green]
int x=0;
int y=0;
char Byte1,Byte2,Byte3;
signed char dx,dy;
int Mouse_State=Waiting_For_Byte1;
int Ready=0;
int s=0;
int Left_Button=0;
int Right_Button=0;

[blue]/*interrupt service routine- get the 3 bytes from the mouse and manipulate into Byte1, Byte2 and Byte3*/[/blue]
void interrupt Mouse_Interrupt(void)
{
      char ch;
      ch=Mouse_Read(Data_Register);
      [blue]/*Mouse_Read is a service funtion in beck.h*/
      /*It is used to read the 8 bits data from the serial mouse*/[/blue]

   switch(Mouse_State)
   {
   	case Waiting_For_Byte1:
      	if(ch & Byte1_Mask)
         [blue]/*check for the first byte*/[/blue]
         {
         	Byte1=ch;
            Left_Button = ((ch & Left_Mask)>>5);
            Right_Button= ((ch & Right_Mask)>>4);
            Mouse_State=Waiting_For_Byte2;
         }
         break;
      case Waiting_For_Byte2:
      	Byte2=ch;
         Mouse_State= Waiting_For_Byte3;
         break;
      case Waiting_For_Byte3:
      	Byte3=ch;
         dx = (signed char)(((Byte1 & 0x03)<<6) |(Byte2 & 0x3F));
         dy = (signed char)(((Byte1 & 0x0C)<<4) |(Byte3 & 0x3F));
         Mouse_State=Waiting_For_Byte1;
         Ready=1;
         break;
    }
}

[blue]/*setup the hardware interrupt*/[/blue]
void Mouse_On(void)
{
	Enable_int(Mouse_IRQ);
   [blue]/*Enable the interrupt at Line 3 or Mouse_IRQ*/[/blue]
   Install_ISR(Mouse_IRQ,1,Mouse_Interrupt);
   [blue]/*assign the Mouse_Interrupt function as the interrupt service routine*/[/blue]
   Mouse_Write(Status_Register, 0x30);
   [blue]/*set bit 4 and bit 5 of status register*/[\blue]
}

[blue]/*remove the hardware interrupt*/[/blue]
void Mouse_Off(void)
{
	Remove_ISR(Mouse_IRQ);
   [blue]/*disable the interrupt at line 3*/[/blue]
}

int main(void)
{
   unsigned int Frequency =1000;
   unsigned int Volume=1000;

   Mouse_On();
   [blue]/*setup hardware interrupt*/[/blue]

   do
   {
   	if(Ready==1)
      {
      	Ready=0;
         Frequency=Frequency+dx;
         Volume= Volume+dy;

         printf("dx=%d\n",dx);
         printf("dy=%d\n",dy);
         printf("Left Button=%d\n",Left_Button);
         printf("Right Button=%d\n",Right_Button);

         printf("Frequency=%d\n",Frequency);
         printf("Volume=%d\n",Volume);
         printf("\n");
       }
   }while(!Key_Pressed());

   Mouse_Off();
   [blue]/*remove hardware interrupt*/[/blue]
   Set_Stdio_Focus(SHELL_FOCUS);
   return 0;
}
[blue]/*End of main program*/[/blue]

The values of dx and dy should become positive when the mouse is moving rightward and upward, but this program keep on sending me a negative value and cause my Frequency and Volume down to negative value. I'm very new in C language, someone who know it please give me your hand. Thank you!!!
 
A mouse range of +/- 128 seems rather small. Are you sure about the dx/dy computation? Try changing them to short or int in the declaration.

 
Yes xwb, the mouse range is from +127 to -128, that why I declared my dx/dy as char(8 bits). For the dx/dy computation, I think that should be no problem since I got this manipulation code from the web and just made some modification. A question is, why do I need to change my declaration to short or int?? short (16 bits) and int ( 32 bits ) which is not relevant to my purpose rite??? Anyway, i'm not very sure about this. Please give me some guide.Thank.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top