using a PIC 16F84 You are the
visitor n.
Best view with 1024x768 | ||
Back to Home Page | Similar topics on this site: PIC 16F84 F.F. Evaluation Board PIC - PS/2 mouse interfacing and communication Connecting a microcontroller to RS-232 ports and PS/2 devices |
Introduction As explained in the article Host - PS/2 mouse hardware communication on this site, to test the protocol needed to communicate with a PS/2 mouse I have interfaced it with a properly programmed PIC 16F84. If you are interested in a more complete program, written for a PIC 16F877 but suitable for many other PIC microcontrollers, you can have a look at the article PIC - PS/2 mouse interfacing and communication on this site. Pay attention: PS/2 devices are different from serial mice, concerning both communication protocol and electrical levels; in PS/2 mice levels used for supply and data exchange are 0 and +5V, in serial mice we instead find the RS-232 levels, +12V and -12V. This article is concerning a PS/2 mouse. The PIC too must receive a +5V supply. In particular, common (or ground) pins of PIC and PS/2 mouse must be connected together; the same for +5V pins. To connect the mouse and the microcontroller together I suggest a flying PS/2 plug, to which you can connect the four needed wires. Such wires can then be carried to a prototype board and connected on it to the wires coming from the proper PIC's pins. To operate the PIC I have used the version previous to the one described in the article PIC 16F84 F.F. Evaluation Board on this site. For connections refer to the article about Host - PS/2 mouse hardware communication, to the PS/2 connector pin-out and to the first lines of the assembler list below. If in this way the mouse shouldn't show any sign of life, connect two pull-up resistors between data and clock and +5V supply (that, I repeat, must be the same for the microcontroller and the mouse). Proper values are around 3-10 KOhm. Take care to connections, to avoid unpleasant damages to devices! Instead of damaging a PIC or a mouse, if you are not really sure of acting correctly you can write me. Acknowledgement and disclaimers The material contained in these pages is the result of preliminary tests, project developement and verify carried on by the undersigned in first person. I guarantee the absolute originality of the material, though obviously not excluding that on the net it could be possible to find something analogous. Being this material at free disposal of whoever desires it, it is absolutely forbidden any form of use for commercial purposes. I don't assume, of course, any responsibilty for possible direct or indirect damages to things or persons deriving by the use of the informations contained in this article. PIC and Microchip are registered trademarks. The assembler program. Below you can find the list of the assembler program. As you can see watching it, it is substantially a set of routines I have written to send command to the mouse and to receive its answers. As it is a program I have used to carry on some tests, it is not optimized, neither concerning memory usage, nor concerning other aspects. It also contains some sections I used only to check the correspondence between the real protocol and the one described in some other papers on the net (I recognized an important difference about parity; you can find the correct description of the communication protocol in the article Host - PS/2 mouse hardware communication on this site). To show which section of the program is currently executed and eventual error conditions, I used different kinds of blinking of a LED connected to the PIC (for whom who doesn't know it, a series resistor between the PIC's pin and the LED is needed; proper values are from 330 Ohm to 1 KOhm). Values used for waiting cycles refers to a clock frequency of 4 MHz, that is 1 million instructions per second. Data sent from the mouse about motion and switches status are not used. If you wish, to you now the task to use my routines making them suitable for your applications. Good luck! |
;page
1 ; filename:
mouseps2.asm ;
As these lines should be open collector, ; RA0 (Clock) and RA1 (Data) are used ; RB0 is used (as output) for giving the user PROCESSOR 16F84 RADIX DEC __CONFIG 3FF1H ; XT ; /PWRO enable ; WDT disable INCLUDE "P16F84.INC" TOCS EQU 05H CIN EQU 0 RA0 EQU 0 ORG 0CH count RES 1 | ; page 2 cbit
RES 1 hexd0
RES 1 number RES 1 ; WARNING: always keep at 0 the
RP0 bit ORG 00H goto start ORG 50H start bsf STATUS,RP0 ; RB1-7: INPUT movlw
11111111B ; RA4 :INPUT (unused) movlw
11111111B |
; page 3 ; disables Clock and Data movlw 11111111B ; assigns fosc/4 to TMR0 bcf OPTION_REG,TOCS ; assigns the prescaler to TMR0
with value 256 ; bsf OPTION_REG,PSA ;
movlw 00H movlw 00H ; waits 1 s call del1s ; signals three times "sent command" call cmdsnt ; sends RESET command (FFH) movlw 0FFH | ; page 4 ; waits 1s call del1s ; sends ENABLE command (F4H) movlw 0F4H ; waits 1 s call del1s ;
sends DEFAULT SETTINGS command movlw 0F6H call del1s ; requests data transmission (EBH) movlw 0EBH call del1s ; requests data transmission (EBH) movlw 0EBH call del1s ; sets stream mode (EAH) movlw 0EAH |
;
page 5 call cmdts ; requests data transmission (EBH)
after movlw 0EBH ; continuous data reception (every 100 ms) datain ; requests data transmission (EBH) movlw 0EBH | ; page 6 xorlw
0FFH recby ; data reception - returns the
received byte in movlw 00H waitst ; waits for the clock to go to 0 and inserts movf PORTA,W ;
sets high RB0 pin when bsf STATUS,RP0 |
; pag.7 ; if start bit is different from STBIT signals btfsc
STATUS,Z stbok movlw 08H ; riceve gli 8 bit waitb movf PORTA,W carry1 bsf STATUS,C carry0 bcf STATUS,C storeb rrf byteio,F ; receives parity bit waitpy movf PORTA,W ; calculates the parity of the
byte in byteio call pargen | ;
page 8 pbok nop ; waits for the stop bit waitsb movf PORTA,W ;
if the stop bit is different from ENDBITsignals btfsc STATUS,Z ebok bsf STATUS,RP0 ; if byteio = FFH it means that an
error movlw
00H endrec return ; not necessary goto fine ; calculates the parity of the byte in byteio pargen movlw 08H pcycle xorwf byteio,W |
; page 9 andlw 00000001B ;
sends on PORTA,DOUT the L.S.bit outbit andlw 1H outz bcf STATUS,RP0 outuno bsf STATUS,RP0 endss return ; waits for the clock (PORTA,CIN) to change from wcup btfss PORTA,CIN ;
waits for the clock (PORTA,CIN) to change from wcdown btfsc PORTA,CIN ;
waits for data (PORTA,DIN) to change wdup btfss PORTA,DIN ; waits for data (PORTA,DIN) to
change wddown btfsc PORTA,DIN | ; pag.10 ; error in start bit reception (difference from estb bsf STATUS,RP0 cycle2 movlw 00H ozero bcf PORTB,RB0 ouno bsf PORTB,RB0 cycle3 decfsz delay1 ; error in start bit reception
(difference from eparb bsf STATUS,RP0 cycle4 movlw 00H |
; page 11 ozero1 bcf PORTB,RB0 ouno1 bsf PORTB,RB0 cycle5 decfsz delay1 ; error in stop bit reception
(difference from eendb bsf STATUS,RP0 cycle6 movlw 00H ozero2 bcf PORTB,RB0 ouno2 bsf PORTB,RB0 cycle7 decfsz delay1 | ; page 12 ;
command sent - sets alternately 1 and 0 on RB0 2 times, cmdsnt bsf STATUS,RP0 cycle8 movlw 00H ozero3 bcf PORTB,RB0 ouno3 bsf PORTB,RB0 cycle9 decfsz delay1 ; receives in byteo and byteio the byte to send sendby bcf STATUS,RP0 ; forces clock to 0 and waits
about 200 us bcf STATUS,RP0 |
; page 13 bsf STATUS,RP0 ; puts 42H in delay1 (about 200 us) movlw 42H cycle1 decfsz delay1 ; releases clock bsf STATUS,RP0 detbit bcf STATUS,C bitz movlw 00H bituno movlw 01H ; TESTS PARITY INVERSION xorlw 00000001B | ; page 14 movlw
ENDBIT ; delay routine of about 1 ms del1ms movlw 02H cyc21 decfsz delay1 ; delay routine of about 10 ms del10m movlw 0DH cyc51 decfsz delay1 ; delay routine of about 100 ms del.1s movlw 0A0H cyc41 decfsz delay1 ; delay routine of about 1 s del1s |
; page 15 movlw
05H cyc11 decfsz delay1 ; indicates that a command is about to be sent -
sets RB0 cmdts bsf STATUS,RP0 | ; page 16 bcf
TRISB,RB0 cyc31 decfsz delay1 fine END |
Have a nice work! |