PIC 16F84A to interface with NES controller

Discussion in 'Embedded Systems and Microcontrollers' started by clabland, Feb 4, 2009.

  1. clabland

    Thread Starter New Member

    Feb 3, 2009
    Hey all, this is my first post here. I'm working on a project and was wondering if I could get any feed back, so here it goes:

    The idea:
    I'm trying to use a PIC 16F84A to convert the serial output of an NES controller to parallel outputs on 8 pins of the PIC (I will then use these and an old USB keyboard controller board to send the inputs to my PC).

    NES controller background:
    The nintendo deck sends a 12us pulse down a wire called the "Latch" to the controller. This tells the 4021 shift register in the controller to store all the button states. After this 12us pulse the controller pulses the "Data" wire of the controller either high or low. If low, the A button is pressed. The nintendo deck then pulses high 6us down a "Clock" wire, then low 6us. This process is repeated for every button. The controller pulses low on Data for a button press, high for the button not being pressed. The pulses are always in this order: A B Select Start Up Down Left Right.
    (More info can be found here if im not clear enough: http://web.mit.edu/tarvizo/web/nes-controller.html)

    My approach:
    Here is a diagram of what my circuit will look like. I will be using 5 volts for VDD.

    I have attached a text file of the code I have written. To account for the 12us and 6us delays the controller is expecting, I will need some sort of delay loop. I haven't written these in yet, but comments are present where the delays should be. Basically my code begins by pulsing the Latch, then reading the first Data pulse. Then the 7 Clock pulse and Data reads take place. With every read, the value of data is stored in a defined STORAGE register, which is then read after the clocking and the proper pins are given high pulses on the PIC to send out to the USB keyboard.

    So I guess my first question is will this work/is this plausible? If so, any recommendations on what kind of crystal I should use to clock the PIC? I really appreciate any feedback and please let me know if I can clarify anything that isn't clear. Thanks!

    NOTE: The nintendo deck does this whole algorithm every 60hz.

    Thanks for reading!
  2. brunoip

    New Member

    Oct 23, 2009
    Have you got the source code in Basic?
  3. Markd77

    Senior Member

    Sep 7, 2009
    Excellent little project!
    I think the readdata doesn't quite work - it only does the rlf if PORTA,0 is high, compare to the similar code below which works fine. The file in mine needs clearing before use.
    The 6us might be a bit tricky running at 4MHz, it is only 6 instructions and calling readdata has already used 2 of them. Did you mean ms?
    __CONFIG needs changing to wdt off and HS osc.
    There is probably a much easier way of setting PORTB.
    I'd make these changes and see what happens in MPLab Sim - you can use the stimulus to change your data pin.
    I'd go for a 4MHz or 20MHz parallel cut crystal because good timing might be important and they are cheap enough anyway.

    compare with:

    rlf lapsetimeL,F
    bsf lapsetimeL,0 ;assume binary 1, ie >0.5sec button press
    movfw timesincebuttonchanged
    sublw 0x19 ;0.5 seconds
    btfsc STATUS,C ;carry bit 0 if borrow ie more than 0.5 seconds
    bcf lapsetimeL,0 ;binary 0, <0.5 sec button press
  4. bluebrakes

    Senior Member

    Oct 17, 2009
    Interesting project. I shall follow this one, as I have a similar problem.