Entering the world of bitbanging...

Jamster

Senior Member
Hi everyone,

The plan is to try to upgrade my non HID-compliant mouse so that it uses the PS/2 interface. I've spent a few hours looking at the protocol and have mocked up a first attempt at bitbanging the protocol. The code below should send a single byte to the computer and will hopefully develop into sending more useful information like keyboard AT commands (keyboard emulation seems a lot more simple so I think I'll start there).

Currently I would like someone wise in the ways of bitbanging to have a look at the code and see if it is feasable to work, although I'm still open to general help with the task. I will be able to test by the weekend as I need to grab my old mouse from my other house and steal the PS/2 cable from it.

Code:
SYMBOL CLKpin = pin2
SYMBOL CLK = 2
SYMBOL DataPin = 0
SYMBOL varout = b0
sendPS2:
 if CLKpin = 1 then  'CLK must be high to send data.
  low DataPin
  pulsout CLK,2  'Tell PC we're sending data
  for b1=0 to 7  'send data:
   b2 = varout & 1  'mask bit (%?xxxxxxx)
   
   if b2 = 1 then  'change DataPin accordingly
    high DataPin
   else
    low DataPin
   endif
   
   pulsout CLK,2  'Tell pc to read bit
   varout = varout / 2 'shift variable right
  
  next b1 
  
  b2 = varout % 2  'Decide parity
  if b2 = 0 then
   high DataPin
  else
   low DataPin
  endif
  pulsout CLK,2  'Tell PC to read parity
  
  high DataPin
  pulsout CLK,2  'Stop bit
   
 else
  goto sendPS2  'If CLK wasn't high keep waiting.
 endif
I would rather not hack into an old PS/2 mouse as I would much prefer a PICAXE solution, even though it would be easier. :)

Many thanks,
Jamster :)
 

Jamster

Senior Member
Ok so I think I have a routine to recieve data from the computer, again can someone have a look at this and feedback please. :)

Code:
SYMBOL CLK = 2
SYMBOL CLKbin = %00000100
SYMBOL DataPin = 0
SYMBOL DataPinin = pin0
SYMBOL varin = b3

setint CLKbin,%00000000

interrupt:
receivePS2:
 do : loop until DataPinin = 0 and CLKpin = 1 'Wait until the host has released the DataPin
 varin = 0   'Reset variable
 high CLKpin   'preset CLK pin high
 for b1=0 to 7
  pulsout CLK,2   'Pulse CLK 
  if DataPinin = 1 then
   varin = varin + 1 'Set rightmost bit with 1...
  endif    '...or ignore
  varin = varin * 2  'Shift the bit left.
 next b1
 pulseout CLK,2   'ask for parity
 b1 = varin % 2
 if DataPin = b1 then  'parity incorrect
  'ERROR!!!!!!
 endif
 
 low DataPin   'ACK
 pulsout CLK,2   'Tell computer
 high DataPin
I dont have the Simulator on this PC so there may very well be syntax errors I havn't spotted. :)

Thanks again,
Jamster
 
Last edited:

westaust55

Moderator
I suspect that while it is not difficult to develop the PS/2 clock and data wave forms at slow speed, the problem may be the ability to generate the waveforms for the PS/2 protocol with the correct timing.
A native PIC chip can do this (and I have seen examples on the internet). Rev Ed do this in the PICAXE firmware for the KBIN and KBLED commands.
However PICAXE BASIC is considerably slower and the timing may become an issue – at least it was prior to the newer M2 and X2 parts with higher clock speeds.
See this web page for some timing details: http://www.networktechinc.com/ps2-prots.html
 

westaust55

Moderator
EPE Magazine in August 2004 did a project for interfacing pS/2 mouse to a PIC.
PIC program is likely in C or assembler but there should be some useful information if you obtain the magazine for that month or the article.

PIC to PS/2 Mouse and Keyboard Interfacing - all you need to know to make a standard issue PS/2 keyboard and mouse work with your PIC designs.
http://www.epemag.wimborne.co.uk/0804.htm
http://www.epemag3.com/index.php?option=com_content&task=view&id=228&Itemid=53



EDIT:
keep in mind that the PS/2 bus is similar to the i2c bus in so far as the two pins should be defined as inputs when not controlling and as open collector outputs when controlling the bus.
To properly establish this with a PICAXE you would need some small signal NPN transistors driven by the PICAXE chip when operating as outputs with resistors from +5V to the transistor collector so the transistor is in effect an open collector output and the resistor is the bus signal line pull-up.
 
Last edited:

Jamster

Senior Member
Thanks for all these links, they'll provide some light bedtime reading. :p

Speed is going to be a major problem with this circuit, the document in the first post seems to make it clear that you need to get the timings perfect fot it to work. It might be a good time to splash out on a Logic analyzer so I can see the timings :)

I'll edit the code to set the inputs when I'm at the PE; can you expand on how to wire it as open collector though - surely that would stop it working as an input?

Thanks,
Jamster :)
 

Jamster

Senior Member
In the document in the original post is says:
Note: When looking through examples on this website, you'll notice I use a few tricks when implementing an open-collector interface with PIC microcontrollers. I use the same pin for both input and output, and I enable the PIC's internal pullup resistors rather than using external resistors. A line is pulled to ground by setting the corresponding pin to output, and writing a "zero" to that port. The line is set to the "high impedance" state by setting the pin to input. Taking into account the PIC's built-in protection diodes and sufficient current sinking, I think this is a valid configuration. Let me know if your experiences have proved otherwise.
I assume that will be the same with a PICAXE, I'm sure I've seen it used in other circuits so I shall assume so until told otherwise but I will retain the external pullup resistors for the sake of being able to change the value.

...Although it would be helpfull if the links to the code worked....

I'll keep adding to this post as I go on so it doesn't keep 'bumping' to the top of the forum. :)

Jamster

----------------------------
Well it so far isn't working, th PICAXE can detect that the computer is holding the Clock line low (telling it not to send) but once the PICAXE sends the data there is no response from the computer, not even a "resend command". Thus if there is any nice people out there with a Logic analyser that could test the attached code (View attachment PS2 possibly.....bas) for me to see whether what is being sent is correct I would be very thankfull. :) I will order one today/tommorrow for myself but it wont be delivered till late next week.
 
Last edited:
Top