Most of my projects are actually games (if you didn't notice)
Up until tonight, I have been using some mini push buttons, pull up resistors and some wire to construct a make-shift control pad / joystick type setup.
I thought of a much easier way of doing things and in no time at all, came up with just a few lines of code that will grab data from a nes control pad for use in all sorts of applications (like games!) or just to control whatever you want really.
In a nutshell, the NES control pad has five connections to make it all work.
In order to 'read' the condition of all eight buttons of the control pad, we send a logic 1, followed by a logic 0 on the Latch wire. This will store the condition of all eight buttons of the control pad, into 1 byte of memory (8 d-type flip flops) which is on a chip inside the control pad. At this time, the condition for the a-button will be present on the Data wire.
If this is a logic 1 then we store a logic 1 in the first bit of our nes_flags variable. We then send out a clock pulse to the Clock wire of the nes control pad which will shift all bits within the control pad. Now the next bit that will be on the Data wire will be the condition of the b-button. We then save this condition (1 or 0) to the nes_flags variable, bit 1.
We then do this until the condition of all eight buttons has been determined.
Here is the NES control pad timing diagram (thanks to http://seb.riot.org/nescontr/)
Here is the code to make it all happen:
Code: Select all
Sub check_control_pad() nes_latch = 1 // grab the state of the buttons nes_latch = 0 // by giving a positive edge to the latch enable For x = 0 To 7 nes_flags.bits(x) = nes_data // the first bit (button a) will be shown on the nes_data pin, we read it, then clock through nes_clock = 1 // the remaining 7 bits to check the state of all buttons. nes_clock = 0 Next End Sub
Hope this is handy for someone!