· 8 years ago · Mar 16, 2018, 04:04 AM
1/*
2================================================================================
3
4 File........... NES Controller Test Code
5 Purpose........ To demonstrate how to interface to an NES controller
6 Author......... Joseph Corleto
7 E-mail......... corleto.joseph @ gmail.com
8 Started........ 04/13/2016
9 Finished....... 04/14/2016
10 Updated........ --/--/----
11
12================================================================================
13 Notes
14================================================================================
15- The NES controller contains one 8-bit 4021 shift register inside.
16
17- This register takes parallel inputs and converts them into a serial output.
18
19- This code first latches the data and then shifts in the first bit on the data line.
20 Then it clocks and shifts in on the data line until all bits are received.
21
22- What is debugged are the button states of the NES controller.
23
24- A logical "1" means the button is not pressed. A logical "0" means the button is
25 pressed.
26
27- This code shifts the first bit of data into the LSB.
28
29- The order of shifting for the buttons is shown in the table below:
30
31 Bit# | Button
32 --------------
33 0 | A
34 --------------
35 1 | B
36 --------------
37 2 | Select
38 --------------
39 3 | Start
40 --------------
41 4 | Up
42 --------------
43 5 | Down
44 --------------
45 6 | Left
46 --------------
47 7 | Right
48 --------------
49
50- The NES controller pinout is shown below (looking into controllers
51 connector end):
52 __________
53 / |
54 / O 1 | 1 - Ground
55 | | 2 - Clock
56 | 7 O O 2 | 3 - Latch
57 | | 4 - Data Out
58 | 6 O O 3 | 5 - No Connection
59 | | 6 - No Connection
60 | 5 O O 4 | 7 - +5V
61 |___________|
62
63- Please visit http://www.allaboutcircuits.com to search for complete article!
64
65================================================================================
66 Updates
67================================================================================
68*/
69
70//===============================================================================
71// Header Files
72//===============================================================================
73
74//===============================================================================
75// Constants
76//===============================================================================
77// Here we have a bunch of constants that will become clearer when we look at the
78// readNesController() function. Basically, we will use these contents to clear
79// a bit. These are chosen according to the table above.
80const int A_BUTTON = 0;
81const int B_BUTTON = 1;
82const int SELECT_BUTTON = 2;
83const int START_BUTTON = 3;
84const int UP_BUTTON = 4;
85const int DOWN_BUTTON = 5;
86const int LEFT_BUTTON = 6;
87const int RIGHT_BUTTON = 7;
88
89//===============================================================================
90// Variables
91//===============================================================================
92byte nesRegister = 0; // We will use this to hold current button states
93byte currentByte = 0;
94
95void latchCode() {
96 nesRegister=255;
97 currentByte=0;
98}
99
100void clockCode() {
101 if(digitalRead(nesData)==LOW)
102 bitClear(nesRegister,currentByte);
103 currentByte++;
104 //after the 8th time, print out.
105 // we don't have to reset because latch will implicitly do it for us.
106 if (currentByte==8)
107 System.println(nesRegister,BIN);
108}
109
110//===============================================================================
111// Pin Declarations
112//===============================================================================
113//Inputs:
114int nesData = 4; // The data pin for the NES controller
115
116//Outputs:
117int nesClock = 2; // The clock pin for the NES controller
118int nesLatch = 3; // The latch pin for the NES controller
119
120//===============================================================================
121// Initialization
122//===============================================================================
123void setup()
124{
125 // Initialize serial port speed for the serial terminal
126 Serial.begin(9600);
127
128 // Set appropriate pins to inputs
129 pinMode(nesData, INPUT);
130
131 // Set appropriate pins to outputs
132 pinMode(nesClock, INPUT);
133 pinMode(nesLatch, INPUT);
134
135 attachInterrupt(digitalPinToInterrupt(nesClock),clockCode,FALLING_EDGE);
136 attachInterrupt(digitalPinToInterrupt(nesLatch),latchCode,FALLING_EDGE);
137}
138
139//===============================================================================
140// Main
141//===============================================================================
142void loop()
143{
144}