Author Topic: SNES Controller  (Read 9298 times)

Punnie

  • Newbie
  • *
  • Posts: 2
SNES Controller
« on: June 21, 2013, 09:45:12 am »

Hey folks,
Iam new to Microcontroller Programming, as a first Project i decide to develope a simple SNES-to-USB Gamepad device.
It's working like intended, Iam able to play my favourite old school SNES games on an Emulator.
Feel free to comment my Code


Greets Punnie

Code: [Select]
//Developed by Punnie, feel free to edit and copy.


//Good Documentation of the SNES Controller (German) http://www.rn-wissen.de/index.php/SNES_Controller
#include "DigiJoystick.h"




boolean pinState = false;
int clockPin = 2; //yellow on a European Controller
int latchPin = 5; //orange
int dataPin = 0;  // red
//White is the +5v and brown ground
uint16_t controllerRegister = 0x0000;


void setup(){
  pinMode(clockPin,OUTPUT);
  pinMode(latchPin,OUTPUT);
  pinMode(dataPin,INPUT);
  digitalWrite(clockPin,HIGH);
  digitalWrite(latchPin,LOW);
}


void loop(){
//Initalize the Gamepad have to be done once before reading the Gamepad's Register
digitalWrite(latchPin, HIGH);
delayMicroseconds(12);
digitalWrite(latchPin, LOW);


//Read the Gamepad's Register 16 bit long
for(int i=0; i<16; i++)
{
delayMicroseconds(6);
digitalWrite(clockPin, LOW);
pinState=!digitalRead(dataPin);
   if(pinState){
     controllerRegister |= (0x01<<i);
   }else{
     controllerRegister &= ~(0x01<<i);
   }


delayMicroseconds(6);
digitalWrite(clockPin, HIGH);
}


//Set the Buttons on the USB-HID-Controler
DigiJoystick.setButtons((char) controllerRegister , (char) (controllerRegister >> 8));
DigiJoystick.delay(16);
}

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: SNES Controller
« Reply #1 on: June 21, 2013, 10:13:55 am »
Thanks for sharing! That's a great first project!

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: SNES Controller
« Reply #2 on: June 21, 2013, 06:33:07 pm »
Have you tried using the built in shiftIn() arduino function? It is designed for doing these sorts of simple SPI things. The difference is that shiftIn() only does one byte at a time (but that's okay - you can combine them) and that it reads the data pin after setting the clock high, where yours reads it when the clock is low. This probably would still work, but maybe the bits would be shifted over by one depending on how the gamepad's shift register works.

Punnie

  • Newbie
  • *
  • Posts: 2
Re: SNES Controller
« Reply #3 on: June 22, 2013, 12:02:47 pm »
Thanks you for the advice, i will have a look at the function. Don't know most of the built in functions :(

mybook4

  • Newbie
  • *
  • Posts: 12
Re: SNES Controller
« Reply #4 on: March 05, 2014, 08:36:08 pm »
Punnie, thanks for the snes post.  I want to implement this on my Digispark and just want to make sure I have things hooked up correctly.

Here's a a pic of the Digispark https://s3.amazonaws.com/digispark/images/m/digispark2.jpg)
and here's the schematic https://s3.amazonaws.com/digistump-resources/files/97a1bb28_DigisparkSchematic.pdf

Is the following correct?


SNES clock pin is connected to P2 on the Digispark (JP3-3 on the Digispark schematic)
SNES latch pin is connected to P5 on the Digispark (JP3-6 on the Digispark schematic)
SNES data pin is connected to P0 on the Digispark (JP3-1 on the Digispark schematic)

mybook4

  • Newbie
  • *
  • Posts: 12
Re: SNES Controller
« Reply #5 on: March 11, 2014, 05:11:29 am »
To follow up, I utilized Punnie's code (with some slight modification).  The SNES controller works great in Windows and Linux (should work in every OS that supports USB HID Joysticks).

The following pinouts were correct:
SNES clock pin is connected to P2 on the Digispark (JP3-3 on the Digispark schematic)
SNES latch pin is connected to P5 on the Digispark (JP3-6 on the Digispark schematic)
SNES data pin is connected to P0 on the Digispark (JP3-1 on the Digispark schematic)

I added the following code to the setup() function in order to set all axis (which aren't used an SNES controller) to neutral positions:

Code: [Select]
  DigiJoystick.setX((byte) 0x80);
  DigiJoystick.setY((byte) 0x80);
  DigiJoystick.setXROT((byte) 0x80);
  DigiJoystick.setYROT((byte) 0x80);
  DigiJoystick.setZROT((byte) 0x80);
  DigiJoystick.setSLIDER((byte) 0x80);

P.S. You can get small ABS plastic project boxes from RadioShack that are very cheap (< $4).  With the project box, a drill, a hot glue gun, and a USB female-male extension cable, I enclosed the Digispark and secured the USB and SNES controller wires.

mybook4

  • Newbie
  • *
  • Posts: 12
Re: SNES Controller
« Reply #6 on: November 15, 2014, 03:48:53 pm »
I have a rev 2 digispark (pin 1 is connected to the onboard LED).  I'd like to use the digispark's pin 1 as an input in order to turn this project into a dual SNES controller adapter.  Does anyone know whether or not I can safely use pin 1 as a digital input?

From the digispark schematic (https://s3.amazonaws.com/digistump-resources/files/97a1bb28_DigisparkSchematic.pdf), it looks like pin 1 (PB1) is connected to the LED, a resistor, and then to GND.

Do I need to cut out the LED to use pin 1?