Thanks to Jinx
;emouse.pic - electronic mouse
;stand-alone 16F84 program to generate mouse data
;joecolquitt@clear.net.nz

;	org	211298
;        rev  31.12.98
;        rev  09.01.99

;Microsoft Mouse, 3 byte data stream
;1200 bps, 7 data bits, 1 stop bit, no parity
;1200 bps = 833.3us / bit = 8333 instruction cycles

;bit     7  6  5  4  3  2  1  0
;
;Byte 1  0  0  L  R  Y7 Y6 X7 X6
;Byte 2  0  0  Y5 Y4 Y3 Y2 Y1 Y0
;Byte 3  0  0  X5 X4 X3 X2 X1 X0
;
;X7 = 1 = left   X7 = 0 = right
;Y7 = 1 = up     Y7 = 0 = down
; L = 1 = left button
; R = 1 = right
;
;Other bits are speed
;
;First 20 movement instructions are for 1 pixel, after that
;4 pixels. Counter is reset when pushbutton is released

status	equ	03h
carry	equ	status0
zero	equ	status2
porta	equ	05h
portb	equ	06h
	mov	!OPTION, W
trisa	equ	85h
trisb	equ	86h
ram	equ	0ch

lin	equ	portb0	;pushbutton for left
rin	equ	portb1	;               right
uin	equ	portb2	;               up
din	equ	portb3	;               down
bin	equ	portb4	;               left button
comm	equ	portb5	;data to PC

start	equ	00h
w	equ	0	;instruction suffix = w = accumulator
f	equ	1	;                   = f = register

 orgdata ram

data    rb                ;byte to send
bit	equ	data7	;bit to send
bcnt    rb                ;bit counter
tdc1    rb                ;time delay counter
move    rb                ;pixel movement counter
pixel   rb                ;movement rate counter
pflags  rb                ;change movement rate flags
lfl	equ	pflags0
rfl	equ	pflags1
ufl	equ	pflags2
dfl	equ	pflags3

        config 0f2h       ;10MHz crystal                xxxx xx10
                          ;watchdog timer disabled      xxxx x0xx
                          ;power-up timer enabled       xxxx 0xxx
                          ;code not protected           xxx1 xxxx

	org	start
	jmp	entry

entry	mov	W, #00h	;Porta unused o/p
;*** WARNING: TRIS expanded in two instructions. Check if previous instruction is a skip instruction.
;        tris   porta

	mov	W, #1fh	;Portb = oooi iiii
;*** WARNING: TRIS expanded in two instructions. Check if previous instruction is a skip instruction.
;        tris   portb

	clr	Ra	;clear data latches
	clr	Rb

	mov	W, #80h	;Portb pull-ups off
	mov	!OPTION, W

	setb	comm	;idle

;---- main loop

main	sb	lin	;look for direction pushbuttons
	jmp	left

	sb	rin
	jmp	right

	sb	uin
	jmp	up

	sb	din
	jmp	down

	mov	W, #14h	;reset movement counter
	mov	pixel, W
	clr	pflags	;reset rate change flags

	sb	bin	;mouse button
	jmp	button
	jmp	main

;---- data output setups

left	snb	lfl	;if high-rate is set then 4pixel move
	jmp	left4
	decsz	pixel	;else decrement counter until 0
	jmp	left1
	setb	lfl	;then set flag
	jmp	left4

left1	mov	W, #9eh	;1001 1110
	call	send
	mov	W, #20h	;0010 0000  (24)
	call	send
	mov	W, #4fh	;0100 1111
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

right	snb	rfl
	jmp	right4
	decsz	pixel
	jmp	right1
	setb	rfl
	jmp	right4

right1	mov	W, #0feh	;1111 1110
	call	send
	mov	W, #2fh	;0010 1111  (2b)
	call	send
	mov	W, #0cfh	;1100 1111
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

up	snb	ufl
	jmp	up4
	decsz	pixel
	jmp	up1
	setb	ufl
	jmp	up4

up1	mov	W, #0e6h	;1110 0110
	call	send
	mov	W, #3fh	;0011 1111
	call	send
	mov	W, #0c8h	;1100 1000  (c9)
	call	send
	mov	W, #10h	;0001 0000
	call	send
	jmp	main

down	snb	dfl
	jmp	down4
	decsz	pixel
	jmp	down1
	setb	dfl
	jmp	down4

down1	mov	W, #0feh	;1111 1110
	call	send
	mov	W, #3fh	;0011 1111
	call	send
	mov	W, #0cbh	;1100 1011  (ca)
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

left4	mov	W, #9eh	;1001 1110
	call	send
	mov	W, #24h	;0010 0100  (20)
	call	send
	mov	W, #4fh	;0100 1111
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

right4	mov	W, #0feh	;1111 1110
	call	send
	mov	W, #2bh	;0010 1011  (2f)
	call	send
	mov	W, #0cfh	;1100 1111
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

up4	mov	W, #0e6h	;1110 0110
	call	send
	mov	W, #3fh	;0011 1111
	call	send
	mov	W, #0c9h	;1100 1001  (c8)
	call	send
	mov	W, #10h	;0001 0000
	call	send
	jmp	main

down4	mov	W, #0feh	;1111 1110
	call	send
	mov	W, #3fh	;0011 1111
	call	send
	mov	W, #0cah	;1100 1010  (cb)
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

button	mov	W, #0fch	;1111 1100
	call	send
	mov	W, #3fh	;0011 1111
	call	send
	mov	W, #0cfh	;1100 1111
	call	send
	mov	W, #0f0h	;1111 0000
	call	send

bhold	sb	bin	;wait for button release
	jmp	bhold

out	mov	W, #0feh	;1111 1110
	call	send
	mov	W, #3fh	;0011 1111
	call	send
	mov	W, #0cfh	;1100 1111
	call	send
	mov	W, #0f0h	;1111 0000
	call	send
	jmp	main

;---- ouput data bits

send	mov	data, W
	mov	W, #08h
	mov	bcnt, W
sbit	sb	bit	;test
	jmp	b0
b1	clrb	comm	;'1'
	call	bitdel
	jmp	newbit
b0	setb	comm	;'0'
	call	bitdel

newbit	rl	data
	decsz	bcnt
	jmp	sbit
	ret

;---- comms timing delay

bitdel	mov	W, #00h	;bit-length delay
	mov	tdc1, W
lp1	decsz	tdc1
	jmp	lp1
	mov	W, #00h
	mov	tdc1, W
lp2	decsz	tdc1
	jmp	lp2
	mov	W, #0ach
	mov	tdc1, W
lp3	decsz	tdc1
	jmp	lp3
	ret

Interested:

Code:

Comments: