Created
September 24, 2011 21:56
-
-
Save zer0her0/1239918 to your computer and use it in GitHub Desktop.
NES to Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Reading a NES Controller | |
| By Sebastian Tomczak | |
| 21 July 2007 | |
| Adelaide, Australia | |
| bit 7 = A | |
| bit 6 = B | |
| bit 5 = select | |
| bit 4 = start | |
| bit 3 = up | |
| bit 2 = down | |
| bit 1 = left | |
| bit 0 = right | |
| */ | |
| /* INITIALISATION */ | |
| int latch = 2; // set the latch pin | |
| int clock = 3; // set the clock pin | |
| int datin = 4;// set the data in pin | |
| byte controller_data = 0; | |
| /* SETUP */ | |
| void setup() { | |
| Serial.begin(57600); | |
| pinMode(latch,OUTPUT); | |
| pinMode(clock,OUTPUT); | |
| pinMode(datin,INPUT); | |
| digitalWrite(latch,HIGH); | |
| digitalWrite(clock,HIGH); | |
| } | |
| /* CONTROLLER READ */ | |
| void controllerRead() { | |
| controller_data = 0; | |
| digitalWrite(latch,LOW); | |
| digitalWrite(clock,LOW); | |
| digitalWrite(latch,HIGH); | |
| delayMicroseconds(2); | |
| digitalWrite(latch,LOW); | |
| controller_data = digitalRead(datin); | |
| for (int i = 1; i <= 7; i ++) { | |
| digitalWrite(clock,HIGH); | |
| delayMicroseconds(2); | |
| controller_data = controller_data << 1; | |
| controller_data = controller_data + digitalRead(datin) ; | |
| delayMicroseconds(4); | |
| digitalWrite(clock,LOW); | |
| } | |
| } | |
| /* PROGRAM */ | |
| void loop() { | |
| controllerRead(); | |
| Serial.println(controller_data, BIN); | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment