Friday, December 7, 2012

1-Wire Joystick (PIC12F1840)

UPDATE 12.1209:
Success! I have three thumbsticks (each with their own PIC12F1840) communicating over the same One-Wire interface to another PIC12F1840, which is displaying the thumbstick information on an I2C LCD.
Bus-backoff is working well, speed was dropped a bit (now only 50 reports per second per stick), and collisions are unusual. The important thing is making sure no incorrect or corrupted data is reported.

I only have three thumbsticks connected, because I didn't want to waste my precious perf board on a fourth.

I love it when things work.

UPDATE 12.1208:
Well, it's working. I need to clean up the code a bit. But - it's working! BTW: The red dongle to the top is the PICKIT3 programmer for the PIC12F1840. The umbilical going to the lower-right is the connection for my analyzer (USBee ZX). And the LCD is a 2x16 character with an I2C interface. Yes, I'm bit-banging two pins on the PIC to emulate an I2C interface. Right now, the code consumes 717 words of PIC memory, out of 4096 words. I'm going to shrink that a bit. There's an I2C interface module, a LCD interface module, the One-Wire interface module, and the small bit of main code.

 Idle - thumbstick (button) unpressed and the thumbstick centered.

 The thumbstick (button) pressed.

 Y-Axis (Up-Down) fully DOWN.

 Y-Axis (Up-Down) fully UP.

 X-Axis (Left-Right) fully LEFT.

  X-Axis (Left-Right) fully RIGHT.

Back to the idle state.

 UPDATE 12.1207:
I've been working on the code and I found a minor hardware issue - the X-Axis was reversed. So I fixed that and updated the schematic (below). Added a few notes, too.

The code is pretty much done on the transmitter side - I need to work on the receiver now. Maybe just a PIC with an attached I2C LCD to display the info. That should be entertaining.

Each report from the device contains 3 bytes - the first byte is the identifier, plus the button information. High nibble is always 'D' (B'1101'), next nibble is the device address in bits 3-2, and finally the button state information in bit 0. The next two bytes are the X-Axis and Y-Axis information. Reports are only made if a state change to the button, X-Axis or Y-Axis is detected. Reports consume 400us on the bus, and there is always 5ms between reports - to allow for other devices (up to 4 total) to get on the bus and report. That means that there will be a maximum of 200 reports per second - is that a good enough response time? I've also implemented a crude bus-backoff algorithm just in case some other device is already using the bus to report.

That's it for now - more as progress is made.


Here's a little module I whipped up. It's a PIC12F1840 interfaced to a PS1 thumbstick. It has a 1-wire interface to whatever wants to use the joystick information. There are two axis - x and y - and a pushbutton (press the thumbstick). Each module can have a unique address (2 bits), so you can have up to 4 modules on a system. The device information is encoded in the packet sent to the host.

I'm still working on the code. I am using a variant of the 40khz IR transmitter - I am going to bump up the speed - and the same work will need to be done on the 40khz IR receiver code on the host.

More as I make progress.

No comments:

Post a Comment