![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This program uses a NUCLEO-F401RE to control an SNES console by sending the button states. (1 = Unpressed, 0 = Pressed) Data, Clock and Latch from the SNES must be connected to the appropriate pins. The NUCLEO is powered by the SNES through E5V. The power jumper should be moved to E5V configuration when powered by SNES.
main.cpp@1:2a346daf070f, 2017-10-05 (annotated)
- Committer:
- kyancey
- Date:
- Thu Oct 05 15:21:03 2017 +0000
- Revision:
- 1:2a346daf070f
- Parent:
- 0:2af5c09cf1ee
Added some comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kyancey | 0:2af5c09cf1ee | 1 | #include "mbed.h" |
kyancey | 0:2af5c09cf1ee | 2 | |
kyancey | 0:2af5c09cf1ee | 3 | |
kyancey | 0:2af5c09cf1ee | 4 | DigitalOut Data(D2); |
kyancey | 0:2af5c09cf1ee | 5 | DigitalIn Latch(D3); |
kyancey | 0:2af5c09cf1ee | 6 | DigitalIn Clock(D4); |
kyancey | 0:2af5c09cf1ee | 7 | |
kyancey | 0:2af5c09cf1ee | 8 | void waitUntilHigh(DigitalIn x) |
kyancey | 0:2af5c09cf1ee | 9 | { |
kyancey | 0:2af5c09cf1ee | 10 | while(x != 1) {}; |
kyancey | 0:2af5c09cf1ee | 11 | } |
kyancey | 0:2af5c09cf1ee | 12 | |
kyancey | 0:2af5c09cf1ee | 13 | void waitUntilLow(DigitalIn x) |
kyancey | 0:2af5c09cf1ee | 14 | { |
kyancey | 0:2af5c09cf1ee | 15 | while(x == 1) {}; |
kyancey | 0:2af5c09cf1ee | 16 | } |
kyancey | 0:2af5c09cf1ee | 17 | |
kyancey | 0:2af5c09cf1ee | 18 | void sendButton(int button) |
kyancey | 0:2af5c09cf1ee | 19 | { |
kyancey | 0:2af5c09cf1ee | 20 | Data = button; |
kyancey | 0:2af5c09cf1ee | 21 | waitUntilLow(Clock); |
kyancey | 0:2af5c09cf1ee | 22 | waitUntilHigh(Clock); |
kyancey | 0:2af5c09cf1ee | 23 | } |
kyancey | 0:2af5c09cf1ee | 24 | |
kyancey | 1:2a346daf070f | 25 | // The SNES expects unpressed buttons to have a high state. |
kyancey | 1:2a346daf070f | 26 | // 1 = unpressed, 0 = pressed |
kyancey | 0:2af5c09cf1ee | 27 | void sendButtonsPressed(int b, int y, int select, int start, |
kyancey | 0:2af5c09cf1ee | 28 | int up, int down, int left, int right, |
kyancey | 0:2af5c09cf1ee | 29 | int a, int x, int l1, int r1) |
kyancey | 0:2af5c09cf1ee | 30 | { |
kyancey | 1:2a346daf070f | 31 | Data = 1; // Seems sensible to start with data being high |
kyancey | 0:2af5c09cf1ee | 32 | waitUntilHigh(Latch); |
kyancey | 0:2af5c09cf1ee | 33 | waitUntilLow(Latch); |
kyancey | 0:2af5c09cf1ee | 34 | |
kyancey | 0:2af5c09cf1ee | 35 | sendButton(b); |
kyancey | 0:2af5c09cf1ee | 36 | sendButton(y); |
kyancey | 0:2af5c09cf1ee | 37 | sendButton(select); |
kyancey | 0:2af5c09cf1ee | 38 | sendButton(start); |
kyancey | 0:2af5c09cf1ee | 39 | sendButton(up); |
kyancey | 0:2af5c09cf1ee | 40 | sendButton(down); |
kyancey | 0:2af5c09cf1ee | 41 | sendButton(left); |
kyancey | 0:2af5c09cf1ee | 42 | sendButton(right); |
kyancey | 0:2af5c09cf1ee | 43 | sendButton(a); |
kyancey | 0:2af5c09cf1ee | 44 | sendButton(x); |
kyancey | 0:2af5c09cf1ee | 45 | sendButton(l1); |
kyancey | 0:2af5c09cf1ee | 46 | sendButton(r1); |
kyancey | 0:2af5c09cf1ee | 47 | } |
kyancey | 0:2af5c09cf1ee | 48 | |
kyancey | 0:2af5c09cf1ee | 49 | int main() |
kyancey | 0:2af5c09cf1ee | 50 | { |
kyancey | 0:2af5c09cf1ee | 51 | while(1) { |
kyancey | 1:2a346daf070f | 52 | // Repeatedly press A |
kyancey | 0:2af5c09cf1ee | 53 | sendButtonsPressed(1, 1, 1, 1, //b,y,select,start |
kyancey | 0:2af5c09cf1ee | 54 | 1, 1, 1, 1, //up,down,left,right |
kyancey | 0:2af5c09cf1ee | 55 | 0, 1, 1, 1); //a,x,l1,r1 |
kyancey | 0:2af5c09cf1ee | 56 | |
kyancey | 0:2af5c09cf1ee | 57 | sendButtonsPressed(1, 1, 1, 1, //b,y,select,start |
kyancey | 0:2af5c09cf1ee | 58 | 1, 1, 1, 1, //up,down,left,right |
kyancey | 0:2af5c09cf1ee | 59 | 1, 1, 1, 1); //a,x,l1,r1 |
kyancey | 0:2af5c09cf1ee | 60 | } |
kyancey | 0:2af5c09cf1ee | 61 | } |