博客首页 | 排行榜 |

设计我最赞的博客

个人档案
博文分类
控制电路(Control Circuitry)  2008-10-13 22:18
Advances in video game technologies and controllers are changing the ways we interact with gaming consoles. If you don‘t want to buy expensive power pads and controllers, you can follow Jeff's lead and build your own controllers with much of the same circuitry.

Dance dance revolution! And what a revolution it was!


Konami‘s "Dance Dance Revolution"(DDR) was one of the first "video games" to involve its participants in a level of physical activity beyond typical exploration and defensive thumb twiddling. Although there is a significant amount of memorization involved with many games (e.g., when to use a particular weapon, where to find a hidden prize, or how to obtain a goal), the sedentary posture associated with playing them isn‘t conducive to combating obesity. DDR requires players to memorize and perform foot movements in time with on-screen cues and "hip" soundtracks. Instead of using a typical hand-held joystick, the player, or "dancer," steps on a colorful floor mat divided into pressure-sensing areas. Body weight triggers the input. Screen cues designate which areas to step on. Successful triggering,stepping on only the appropriate area at the appropriate time, scores high points. This is similar to learning to dance by following painted footprints on the floor.

It's no wonder music provides a connection to others. Our lives are full of naturally occurring rhythmic sounds and many individuals even carry a music player around with them. It‘s not too hard to understand why the DDR craze has caught on. Arcades offer a string of DDR machines. They usually have railing that not only keeps the dancer from losing their balance,but it also provides added leverage to accomplish tough moves. It‘s crazy to see the footwork necessary to compete at the upper levels of these contests.

The newest variation to the dancing theme is to perform in a new way. Activision's "Guitar Hero" (GH) lets you perform like a rock star. Don‘t want to take years of music lessons?No problem. With GH, you can perform like a rock star right away. Out with the floor mat and in with the guitar controller. The controller doesn‘t have strings; it has just five buttons for one hand and a strummer(button) and a whammy bar (one-half of an analog joystick) for the other hand. The on-screen cuing is similar to DDR. Pick your song and start performing. While you aren't actually playing the real notes (after all, you have only five buttons), it‘s your synchronization that produces the highest scores.

This month, I'll show you how to investigate the Sony PlayStation 2‘s(PS2) controller interface and replace a controller all with the same circuitry. First, you'll use the circuitry in Figure 1 to simulate the PS2 system and make inquiries about which peripheral is plugged into it. Then, the same circuitry will be configured to look like a PS2 peripheral controller. To begin, I will examine the communications medium of the PS2.

SEARCH
With your favorite search engine,you can look around the ether for information on the PS2 (or any other system you are interested in)。 Of course, everyone is trying to get you to buy something, so you might have to work a bit to get through the ads. In this case, although I could have hooked up a scope to the PS2 and figured out the interface, I knew I could save some valuable time by seeing what information was already out there. A number of sites offer the same pinout and command reference, but I like Hardware Book (www.hardwarebook. info) because it has a lot of information on one site.

The next roadblock was (and often is) a special connector that might be necessary to make a connection to proprietary equipment. The least expensive approach is to get a controller expansion cable. The advantage of the cable is that you get both male and female connectors for a single price, sometimes for as low as $3. With a connection pinout and an idea about what the communication protocol is, drawing up a schematic and putting together a prototype is straightforward.The PS2 interface requires five signal lines (besides power and ground)。 The controller operations require 16 bits plus four analog inputs. That‘s a total of 25 control lines. Some 28-pin, flash-memorybased microcontrollers have just enough I/Os. I could have chosen to go to a 40-pin device, but I wanted to keep it as inexpensive as possible.

The design in Figure 1 can be used to perform two different tasks. It'll begin its life acting like a PS2 gaming system and send out command bytes looking for a controller. When a controller is plugged into the circuit, the controller will answer back with data bytes indicating what it is and the status of its inputs. Because the reply can be several bytes in length, I wanted to collect this information and display it in an intelligent format other than on an LED. This requires being verbose. I chose adding an LCD to the circuit because all of the inputs would not be necessary when acting as a system.
点击查看Figure 1

If I was doing this project without knowing the protocol,I would first program it to look like a controller to the PS2 and display the commands it sent. I would then ensure the system sends out two bytes, 0x01 and 0x42. Without a real controller hooked up to answer these commands,communication would cease. However, I would now have the format necessary to interrogate a controller,the same information an Internet search provided, so I could continue.

To look like a PS2 system, the microcontroller needs to communicate with a SPI format as a master and provide an *SS (ATT) select line to the controller (see Figure 2)。 The controller acknowledges each byte received with an ACK. This enables the controller to signal when it is finished processing the byte received from the system. This includes writing a data byte (reply) to the SPI buffer. Remember that the SPI mode uses separate data in and out lines,enabling data to pass in both directions at the same time. The only catch is that the data pertinent to the last command isn‘t passed until the next command is clocked.



If a controller is connected, it will respond with its own identification, as well as a number of data bytes defined by its identification. As long as the controller continues to acknowledge each byte it receives, the master will continue to send out additional idle commands.(While the documentation I found states 0xFF, I see 0x00 being sent for idle.) When the system retrieves the expected number of data bytes, it will cease communications by raising ATT. The controller need not ACK once the system brings ATT high.

The SPI port on the Microchip Technology PIC16F737 is set up with a 250-kHz clock, the maximum rate of a PlayStation 1 (PS1)。 (The PS2 will run at 500 kHz, but running slower is acceptable.) The clock idles high and data changes on the falling edge and is read on the rising edge. In Table 1, you can see that if I send out an 0x01, a controller will respond with meaningless data (because it doesn't know what I want yet)。 If the second byte I send is a 0x42, a controller should respond with a byte identifying itself.


At this point, I will need to adjust the number of additional requests depending on the type of controller that is responding. Once I have gathered all of the data, I will use the LCD to identify the controller.

I have a standard 4 × 20 LCD. Although the display has an 8-bit data bus, I am using the alternate 4-bit mode to converse with it. The LCD requires three control lines: register select (RS) low for conversing with the control register and high when talking to the data register; read/write (RW) high to read and low to write data;and data strobe (E) to latch the data. In 4-bit mode, you must perform two actions for each byte (MSN then LSN)。Because the LCD powers up in 8-bit mode, the first transfer is an 8-bit transfer. Although the data bus has eight I/O lines, only the top four are connected to the LCD. So, when you write a byte to those eight lines, only the top four have useful data (logic levels)。 Because the lower four are not connected, the data on them goes nowhere. This sequence places the LCD into 4-bit mode and it will remain in 4-bit mode until it is powered down. This is the only time that your transfer will be a single-action transfer. All communications must now consist of two transfers (MSN then LSN)。 This is important to remember for two reasons. To stay in sync with the LCD, all remaining transfers must be in pairs. Second, if you are using a debugger that keeps the power on and you halt and restart the program again,the single transfer that initializes the LCD to 4-bit mode will put the LCD out of sync the second time around because its already in 4-bit mode!

Figure 3 shows how the circuit is used to identify controllers that are plugged into the connector J1. (Note that the SDI and SDO need to be remapped on the connector because the SPI I/Os can not be remapped by the application. This can be done by moving the jumpers on JPx.) When I powered up the circuit, I thought I had something wired incorrectly because there was no response from any controller plugged in. Then it hit me. I wasn‘t providing the correct data. The correct data should have been 0x01, but I was sending 0x01. Is that a typo? No it isn't. Let me explain. Had I paid sufficient attention to the specifications, I wouldn‘t have assumed the data was most significant bit (MSB) first (as in standard SPI hardware)。 In fact, it is least significant bit (LSB) first. Because I couldn't define this using the hardware SPI, I had a choice. I had to either bit bang an SPI port or transform all of the data going to or coming from the hardware SPI. I chose the latter. I simply remapped all of the data and stored it in reverse order. I originally thought I could use a routine to take the data and reverse it before writing it into the SPI buffer (or after reading a byte from the SPI buffer),but I found that this took too much time (more on this later)。


SETTING THE STAGE
Now that we‘ve identified various controllers using the circuit to emulate a PS2, it's time to make the circuit act as a controller to the PS2. This application has less to do with emulating the PS2, however, because we are not "in charge" of the timings. Everything is a lot more critical. We need to keep track of the present button (digital)

and joystick (analog) states and apply the current status to a table. The table is, in fact, a list of the responses that we will use when the system sends a command to query the status of the controller. The buttons are all tested and the data table is updated accordingly in the main loop of the application. That‘s all that happens in the main loop.

Converting the four joystick positions is a bit different because the conversions take time. Although the ADC produces an interrupt on "endof- conversion," if an interrupt occurs during a transmission from the PS2,the transmission will be lost. The PS2 will give up if it gets no response(ACK) in approximately 60 μs. (That's 60 instructions running with a 1-μs instruction time.) Instead of using the interrupt, I poll the interrupt flag in the main loop. The main loop can then branch to a read and store the conversion whenever the conversion is finished, yet still be interrupted by the SPI interrupt whenever communication is requested. The A/D routine also increments the analog channel so the next joystick is converted, all four in round-robin fashion.

SPI communication from the PS2 controller has top priority and is the only interrupt routine. Once a SPI interrupt has occurred (the last of 8 bits are clocked into the receiver), there is no time for lollygagging around. I treat all communications (while ATT is low) in this interrupt without leaving after each character is processed. I look for the 0x01 (0x80 in your reverse case) and then look for a query command(0x42)。 The data I need to reply with is already formatted in a table and the bytes can be transmitted to the PS2 as it continues requesting data. If at any point the ATT line goes high, the system has discontinued communication, so the interrupt routine is exited and the main loop continues updating the status of the buttons and joysticks.
The documentation I found so far has not mentioned any other commands other than 0x42. This seems rather limited. I know from experimenting that the analog joystick has a button on it that can change its format from "standard digital pad" to "analog controller." When initially powered, it defaults to "standard digital pad." This might be so it can be backward-compatible to a PS1. On the PS2, when a game like "Guitar Hero" is used, the controller is automatically redefined as an"analog controller." This means that there must be other commands issued by the PS2 that the controller can respond to. A little more fishing around on the ‘Net revealed a text file on Lynxmotion (www.lynxmotion. com/images/files/ps2cmd01.txt) that has a bit more sketchy information. I'll leave this for you to investigate if you are interested. As you can see in Figure 4, I left room in the code for"other additional commands" to be recognized and responded to. But for my purposes, this is all that‘s necessary to achieve my goal.



SPILLING THE BEANS
I thought that I might be able to display PS2 communications using the LCD. The PS2 sent out communications about every 18 ms. At that rate, the data filled up the screen and rewrote all 80 characters over and over, which was faster than I could view anything. It turned out to be pretty much useless. By monitoring the ATT input and an additional input from a push button on the microcontroller, I paused the display on one full command of data until I pushed the button. In reality, because there are two data paths, one from the PS2 and one from the controller, I want to collect both at once. This isn't going to happen with a single SPI.

The circuit has a jumper (JPx) that routes which line (CMD or DATA) gets fed to the SDI input. This way, you can choose to look at either the commands or the responses in the monitor mode. Because the real-time data overruns the LCD, an alternative approach would be to ship the data out using the microcontroller‘s UART and observe the CMD or DATA communications using HyperTerminal. If we receive 9 bytes at a potential 60 times per second, and if we need to send a format of at least 3 bytes(two hexadecimal ASCII characters and a space) for each byte received, that is 1,620 bps (i.e., 9 × 60 × 3), or a data rate of 16,200 bps (i.e., 1,620 × 10 bits)。The UART‘s next largest standard value would be 19.2 kbps. Of course, you would need to add a level shifter on the TX output if you wanted to implement this function.
 
AXE MEN
Once the controller is recognized by the PlayStation, the hard part is done. Constructing the controller of your fantasies takes a bit of dreaming. Commercial video games use buttons and joysticks that can take some pretty heavy abuse. You might imagine some special implementation for a physically challenged friend. For this project, I wanted to convert an old guitar into a controller. To prototype the inputs, I cut out a guitar shape from a piece of thin masonite. Photo 1 shows a picture of my youngest son Kris and me battling it out"Guitar Hero" style to one of his favorite tunes. I can‘t come close to his mastery, even when I play in the lowest levels! I can only hope that college life contains time for studies amongst the heavy concert schedule. Incidentally, the DDR dance pad input device looks like a"standard digital pad." You can emulate that just as easily with this circuit.

This is the first time I've jumped into the gaming venue. If you‘d like to see more on this subject, or if you have any other areas of interest, drop me an e-note. I'm always looking for ways to broaden my horizons.


Jeff Bachiochi (pronounced BAH-key-AHkey)has been writing for Circuit Cellar since 1988. His background includes product design and manufacturing. He may be reached through the magazine(jeff.bachiochi@circuitcellar.com) or his web site (www.imaginethatnow.com)。

PROJECT FILES
To download code, go to ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2008/214.

RESOURCES
Hardware Book, www.hardwarebook.info.

Lynxmotion, Inc., www.lynxmotion.com/images/files/ps2cmd01.txt.

SOURCE
PIC16F737 Microcontroller
Microchip Technology, Inc.
www.microchip.com

类别:测量与传感器 |
上一篇:可控硅(Triac Behavior) | 下一篇:指纹数据采集(Fingerprint Data Acquisition)
以下网友评论只代表其个人观点,不代表本网站的观点或立场