![](/media/cache/profiles/951e5e38cb91253e90bcae5ccd7baf3c.jpg.50x50_q85.jpg)
This example demonstrates the reading of the PlayStation controller in the Nucleo.
Intro
This example demonstrates the reading of the PlayStation controller in the Nucleo.
Parts
STM32 Nucleo F446RE |
Connector for the PS controller |
LED 2pcs |
Register 100k ohm |
Register 470 ohm 2pcs |
Breadboard |
Wires |
Wiring diagram
This circuit diagram was created by fritzing.
PS con. | Nucleo |
---|---|
1 | MISO PC_11 PullUp |
2 | MOSI PC_12 |
3 | - |
4 | GND |
5 | 3.3v 3v3 |
6 | CS PD_2 |
7 | SCK PC_10 |
8 | - |
9 | - |
main.cpp@0:871415d77569, 2016-03-12 (annotated)
- Committer:
- beaglescout007
- Date:
- Sat Mar 12 10:09:49 2016 +0000
- Revision:
- 0:871415d77569
Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beaglescout007 | 0:871415d77569 | 1 | #include "mbed.h" |
beaglescout007 | 0:871415d77569 | 2 | |
beaglescout007 | 0:871415d77569 | 3 | // SPI interface |
beaglescout007 | 0:871415d77569 | 4 | SPI pad_spi(PC_12, PC_11, PC_10); // MOSI(should pullup), MISO, SCK |
beaglescout007 | 0:871415d77569 | 5 | DigitalOut pad_cs(PD_2); |
beaglescout007 | 0:871415d77569 | 6 | |
beaglescout007 | 0:871415d77569 | 7 | // LED |
beaglescout007 | 0:871415d77569 | 8 | DigitalOut red(PB_5); |
beaglescout007 | 0:871415d77569 | 9 | DigitalOut yellow(PA_10); |
beaglescout007 | 0:871415d77569 | 10 | |
beaglescout007 | 0:871415d77569 | 11 | // PS pad initialize |
beaglescout007 | 0:871415d77569 | 12 | void pspad_init() |
beaglescout007 | 0:871415d77569 | 13 | { |
beaglescout007 | 0:871415d77569 | 14 | pad_spi.format(8, 3); |
beaglescout007 | 0:871415d77569 | 15 | pad_spi.frequency(250000); |
beaglescout007 | 0:871415d77569 | 16 | } |
beaglescout007 | 0:871415d77569 | 17 | |
beaglescout007 | 0:871415d77569 | 18 | // Read PS Pad state |
beaglescout007 | 0:871415d77569 | 19 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
beaglescout007 | 0:871415d77569 | 20 | // SE -- -- ST U R D L L2 R2 L1 R1 TR O X SQ |
beaglescout007 | 0:871415d77569 | 21 | void pspad_read(unsigned short *pad) |
beaglescout007 | 0:871415d77569 | 22 | { |
beaglescout007 | 0:871415d77569 | 23 | pad_cs = 0; |
beaglescout007 | 0:871415d77569 | 24 | wait_us(500); |
beaglescout007 | 0:871415d77569 | 25 | |
beaglescout007 | 0:871415d77569 | 26 | pad_spi.write(0x80); |
beaglescout007 | 0:871415d77569 | 27 | pad_spi.write(0x42); |
beaglescout007 | 0:871415d77569 | 28 | pad_spi.write(0); |
beaglescout007 | 0:871415d77569 | 29 | pad_spi.write(0); |
beaglescout007 | 0:871415d77569 | 30 | int d1 = pad_spi.write(0); |
beaglescout007 | 0:871415d77569 | 31 | int d2 = pad_spi.write(0); |
beaglescout007 | 0:871415d77569 | 32 | |
beaglescout007 | 0:871415d77569 | 33 | pad_cs = 1; |
beaglescout007 | 0:871415d77569 | 34 | |
beaglescout007 | 0:871415d77569 | 35 | *pad = (char)~d1 << 8 | (char)~d2; |
beaglescout007 | 0:871415d77569 | 36 | } |
beaglescout007 | 0:871415d77569 | 37 | |
beaglescout007 | 0:871415d77569 | 38 | int main() |
beaglescout007 | 0:871415d77569 | 39 | { |
beaglescout007 | 0:871415d77569 | 40 | pspad_init(); |
beaglescout007 | 0:871415d77569 | 41 | |
beaglescout007 | 0:871415d77569 | 42 | unsigned short pad; |
beaglescout007 | 0:871415d77569 | 43 | |
beaglescout007 | 0:871415d77569 | 44 | while(1) |
beaglescout007 | 0:871415d77569 | 45 | { |
beaglescout007 | 0:871415d77569 | 46 | pspad_read(&pad); |
beaglescout007 | 0:871415d77569 | 47 | |
beaglescout007 | 0:871415d77569 | 48 | // Circle button |
beaglescout007 | 0:871415d77569 | 49 | yellow = pad & 4; |
beaglescout007 | 0:871415d77569 | 50 | |
beaglescout007 | 0:871415d77569 | 51 | // X button |
beaglescout007 | 0:871415d77569 | 52 | red = pad & 2; |
beaglescout007 | 0:871415d77569 | 53 | |
beaglescout007 | 0:871415d77569 | 54 | wait_ms(16); |
beaglescout007 | 0:871415d77569 | 55 | } |
beaglescout007 | 0:871415d77569 | 56 | } |