Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PS/2 mouse communication issue

Status
Not open for further replies.

akvii

Member level 5
Joined
Nov 4, 2006
Messages
92
Helped
13
Reputation
26
Reaction score
10
Trophy points
1,288
Activity points
1,849
mouse communication

Dear All

I am interfacing ps/2 mouse with pic 18F452....i have sent all the initial commands to mouse ie reset (FF from pic and i get FA,AA and 0 from mouse),then i send remote setting command and I get FA from mouse,then i send enable command and get FA from the device, after that I dont receive any thing from the mouse

I even tried remote mode but when I send read command,I get FA(which I should be) and instead of 3 data packets i get AA

I will be very gratefulu if any one kindly help me out
 

akvii

Member level 5
Joined
Nov 4, 2006
Messages
92
Helped
13
Reputation
26
Reaction score
10
Trophy points
1,288
Activity points
1,849
ps2 mouse communication

here is the code...written in PICBASIC


Device 18F452

XTAL = 20 ' Declaring 20 MHz crystal

' LCD DECLARATION'''''
Declare LCD_TYPE 0
Declare LCD_INTERFACE 4
Declare LCD_LINES 2
Declare LCD_DTPIN PORTB.4
Declare LCD_ENPIN PORTB.3
Declare LCD_RSPIN PORTB.2

ALL_DIGITAL TRUE

Symbol DATA_PIN = PORTB.1 'CONNECT DATA PIN AT PORTB.1
Symbol CLK_PIN = PORTB.0 'CONNECT DATA PIN AT PORTB.0

Dim CNT As DWord

Dim TMP As Word
Dim DATA_SEND As Word
Dim TEMP As Word'Byte
Dim VAR1 As Word
Dim PARITY As Word
Dim I As Word
Dim RECV_BYTE As Word
Dim RECV_BYTE_1 As Word
Dim RECV_BYTE_2 As Word
Dim RECV_BYTE_3 As Word
Dim RECV_BYTE_4 As Word
Dim RECV_BYTE_5 As WordDim j As Byte

Clear ' CLEAR ALL VARIABLES

DelayMS 250
Cls
Print At 1,1,"PS/2 MOUSE "
DelayMS 2000 ' WAIT FOR 2 SECONDS TO STABILZE MOUSE
Cls


AA: DelayMS 10
DATA_SEND=$ff
Print At 1,1,"RESET"
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2 ' GRAB FIRST BYTE FROM MOUSE
If RECV_BYTE <> $FA Then GoTo AA ' IF FIRST BYTE IS NOT FA, THEN RESET IT AGAIN
GoSub RECV_BYTE_FROM_PS2 ' GRAB SECOND BYTE FROM MOUSE
If RECV_BYTE <> $AA Then GoTo AA ' IF SECOND BYTE IS NOT AA, THEN RESET IT AGAIN
GoSub RECV_BYTE_FROM_PS2' GRAB THIRD BYTE FROM MOUSE
If RECV_BYTE <> $00 Then GoTo AA ' IF THIRD BYTE IS NOT 00, THEN RESET IT AGAIN
Call PRINT_DATA ' PRINT WHAT IS RECEIVED FROM MOUSE
DelayMS 100


' DelayMS 5000
'Cls
' SEND ENABLE COMMAND
cc: DelayMS 10
Print At 1,1,"ENABLE"
DATA_SEND=$F4 ' ENABLE MOUSE
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2
Print At 2,1,HEX2 RECV_BYTE 'Print WHAT IS RECEIVED FROM MOUSE
If RECV_BYTE <> $FA Then GoTo cc ' IF ITS NOT FA,SEND ENABLE COMMAND AGAIN
DelayMS 1000
Cls





While 1=1
Inc CNT
GG: DelayMS 10
DATA_SEND=$EB ' READ DATA
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2 ' GRAB FIRST BYTE FROM MOUSE

GoSub RECV_BYTE_FROM_PS2 ' GRAB SECOND BYTE FROM MOUSE

GoSub RECV_BYTE_FROM_PS2' GRAB THIRD BYTE FROM MOUSE

Call PRINT_DATA ' PRINT

'''''HERE I GET FA AA AND 00''''''
DELAYMS 50 ' GET DATA ATFER EVERY 50 MILLI SECONDS
Wend

RECV_BYTE_FROM_PS2:
Input CLK_PIN
Input DATA_PIN
DelayUS 35
While CLK_PIN = 1 And DATA_PIN = 1 : Wend
While DATA_PIN = 1 : Wend
While CLK_PIN = 0 :Wend

For I = 0 To 10
While CLK_PIN =1: Wend
TMP.10 = DATA_PIN
'var1 = tmp.10
TMP=TMP>>1
While CLK_PIN=0 :Wend
Next I
RECV_BYTE=TMP.BYTE0
Return


SEND_BYTE:

Call FIND_PARITY
DATA_SEND.8 = PARITY.0
Output DATA_PIN
Output CLK_PIN
Set DATA_PIN
Set CLK_PIN
DelayUS 40
Clear CLK_PIN
DelayUS 110
Clear DATA_PIN
DelayUS 5
Set CLK_PIN
Input CLK_PIN
TEMP = DATA_SEND
'SENDING DATA AND PARITY BIT
For I = 0 To 8
While CLK_PIN = 1 : Wend ' WAIT TILL CLOCK IS HI
DATA_PIN = TEMP.0 ' TRANSFER DATA TO PORT
TEMP = DATA_SEND >> 1 ' SHIFT RIGHT
While CLK_PIN = 0 : Wend ' WAIT TILL CLOCK IS LOW
Next
'--------------------------------------
DATA_PIN = 1
Input DATA_PIN
While CLK_PIN = 1 : Wend ' WAIT TILL CLOCK IS HI
While DATA_PIN = 1 : Wend ' WAIT TILL DATA IS HI
Return



PRINT_DATA:
Print At 1,1,HEX2 RECV_BYTE_1," ",HEX2 RECV_BYTE_2," ",HEX2 RECV_BYTE_3," ",HEX2 RECV_BYTE_4,HEX2 RECV_BYTE_5
Print At 2,1,HEX2 RECV_BYTE," ",HEX3 CNT
Return
 

Nahian

Member level 3
Joined
Aug 1, 2007
Messages
64
Helped
6
Reputation
12
Reaction score
0
Trophy points
1,286
Activity points
1,604
Re: ps2 mouse communication

I have the same problem . mouse dont give data after setting stream or remote mode. only returns 0xFA. I am using AVR Atmega32:cry:
 

ankitnayan

Newbie level 4
Joined
Oct 18, 2010
Messages
5
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,313
can someone pls post the code for interfacing optical mouse with pic18f4550???
 

akvii

Member level 5
Joined
Nov 4, 2006
Messages
92
Helped
13
Reputation
26
Reaction score
10
Trophy points
1,288
Activity points
1,849
here is the working code

Device 18F452

Xtal = 20' =FOSC/1000000
Declare LCD_Type 0
Declare LCD_Interface 4
Declare LCD_Lines 2
Declare LCD_DTPin PORTB.4
Declare LCD_ENPin PORTB.3
Declare LCD_RSPin PORTB.2

Symbol DATA_PIN = PORTB.1
Symbol CLK_PIN = PORTB.0

Dim MILLIMETER As Float
Dim MILLIMETER_1 As Float
Dim INCHES As Float
Dim TOTAL_COUNT As Dword
Dim CNT_2 As Dword
Dim TMP As Word
Dim TMP_1 As Word
Dim DATA_SEND As Byte
Dim TEMP As Word
Dim PARITY As Byte
Dim I As Byte
Dim RECV_BYTE As Byte
Dim RECV_BYTE_1 As Byte
Dim RECV_BYTE_2 As Byte
Dim RECV_BYTE_3 As Byte
Dim ACK_BIT As Byte
Clear


Symbol RBIF= INTCON.0 ' RB4:7 IF
Symbol INT0IF= INTCON.1 '' INT IF
Symbol TMR0IF= INTCON.2 ' TMR0 Overflow Interrupt Flag
Symbol RBIE= INTCON.3 ' B4:B7 IE
Symbol INT0IE= INTCON.4 '' INT IE
Symbol TMR0IE = INTCON.5 ' TMR0 Overflow Interrupt Enable
Symbol PEIE= INTCON.6 ''
Symbol GIE = INTCON.7 '' Global Interrupt Enable
Symbol GIEL= INTCON.6 ' LOW PRIORITY ENABLE
Symbol GIEH = INTCON.7 ' HIGH PRIORITY ENABLE
Symbol CODO =%00000000
Symbol CIDO =%00000010
Symbol CIDI =%00000011
' MICE COMMANDS
Symbol REST =$FF
Symbol Resend =$FE
Symbol DEFAULT =$F6
Symbol Disabl =$F5
Symbol Enbl =$F4
Symbol SetSR =$F3
Symbol ReadTyp =$F2
Symbol SetRem =$F0
Symbol SetWrap =$EE
Symbol ResWrap =$EC
Symbol ReadDat =$EB
Symbol SetStrM =$EA
Symbol StatReq =$E9
Symbol SetRes =$E8
Symbol SetSca21=$E7
Symbol SetSca11=$E6
' MICE BUTTONS
Symbol LButt =1
Symbol MButt =4
Symbol RButt =2
Symbol SFast =25 ' HOW FAST
' Symbol RBPU = INTCON2.7 '0 = PORTB pull-ups are enabled by individual port latch values
' Symbol INTEDG0= INTCON2.6 '1 = Interrupt0 on rising edge
' Symbol INTEDG1= INTCON2.5 '1 = Interrupt1 on rising edge
' Symbol INTEDG2= INTCON2.4 '1 = Interrupt2 on rising edge
' Symbol TMR0IP = INTCON2.2 'TMR0 Overflow Interrupt Priority bit 1=High
' Symbol RBIP = INTCON2.0 'RB Port Change Interrupt Priority bit 1=High



'GIE = 0

MAIN:
DelayMS 250
Cls
Print At 1,1,"Distance Meter"
DelayMS 2000
'Cls

AA: DelayMS 10
DATA_SEND=REST
Print At 2,15,Hex2 DATA_SEND
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2
RECV_BYTE_1 = RECV_BYTE
If RECV_BYTE <> $FA Then GoTo AA

GoSub RECV_BYTE_FROM_PS2
RECV_BYTE_2 = RECV_BYTE
If RECV_BYTE <> $AA Then GoTo AA

GoSub RECV_BYTE_FROM_PS2
RECV_BYTE_3 = RECV_BYTE
If RECV_BYTE <> 0 Then GoTo AA

MM: DelayMS 10
DATA_SEND= $F0 ' SET REMOTE MODE
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2
'Print At 1,1,HEX2 RECV_BYTE

'If RECV_BYTE <> $FA Then GoTo MM

'''''''''''
NN: DelayMS 10
DATA_SEND=$E8
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2
Print At 1,1,Hex2 RECV_BYTE
If RECV_BYTE <> $FA Then GoTo NN

NNN: DelayMS 10
DATA_SEND=$03
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2
Print At 2,1,Hex2 RECV_BYTE
If RECV_BYTE <> $FA Then GoTo NNN


Cls

While 1=1
QWERTY:

DATA_SEND=$EB
GoSub SEND_BYTE
GoSub RECV_BYTE_FROM_PS2
GoSub RECV_BYTE_FROM_PS2
RECV_BYTE_1 = RECV_BYTE
GoSub RECV_BYTE_FROM_PS2
RECV_BYTE_2 = RECV_BYTE

If RECV_BYTE_1.0 = 1 Or RECV_BYTE_1.1 = 1 Or RECV_BYTE_1.2 = 1 Then
TOTAL_COUNT = 0
EndIf

If RECV_BYTE_1.4 = 0 Then
TOTAL_COUNT = TOTAL_COUNT + RECV_BYTE_2
EndIf

If RECV_BYTE_1.4 = 1 Then
TOTAL_COUNT = TOTAL_COUNT - ($ff - RECV_BYTE_2)
If TOTAL_COUNT < 1 Then TOTAL_COUNT = 0
EndIf

INCHES = TOTAL_COUNT/800
MILLIMETER_1 = INCHES * 25.4
MILLIMETER = TOTAL_COUNT * 0.03175
Print At 1,1,Dec4 TOTAL_COUNT," ",Dec3 INCHES
Print At 2,1,Dec3 MILLIMETER'," "',DEC2 MILLIMETER_1
DelayMS 10
GoTo QWERTY


Wend




RECV_BYTE_FROM_PS2:
Input CLK_PIN
Input DATA_PIN

While CLK_PIN = 1 And DATA_PIN = 1 : Wend

While CLK_PIN = 0 :Wend
For I = 0 To 10
While CLK_PIN =1: Wend
TMP_1.10 = DATA_PIN
TMP_1 = TMP_1 >> 1
While CLK_PIN=0 :Wend
Next I
RECV_BYTE=TMP_1.Byte0
TMP_1 = 0

Return

SEND_BYTE:
Call FIND_PARITY
TMP = 0
TEMP = 0
TEMP.Byte0 = DATA_SEND
TEMP.8 = PARITY.0
Output DATA_PIN
Output CLK_PIN
DATA_PIN = 1
CLK_PIN = 1
DelayUS 40
CLK_PIN = 0
DelayUS 110
DATA_PIN = 0
DelayUS 25
CLK_PIN = 1
Input CLK_PIN

'SENDING DATA AND PARITY BIT
For I = 0 To 8
While CLK_PIN = 1 : Wend
DATA_PIN = TEMP.0
TEMP = TEMP >> 1
While CLK_PIN = 0 : Wend
Next
'--------------------------------------
DATA_PIN = 1
Input DATA_PIN
While DATA_PIN = 1 : Wend
While CLK_PIN = 1 : Wend

Return


FIND_PARITY:
PARITY=0
TMP=DATA_SEND.Byte0
For I=0 To 7
PARITY=PARITY+TMP.0
TMP = TMP >> 1
Next I
PARITY=PARITY // 2
If PARITY =0 Then
PARITY =1
Else
PARITY=0
EndIf
Return
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top