DIY Serial MIDI Interface (80C51/80C31 Version)


Run out of ISA card slots in your PC? Want a real MIDI interface instead of the toy interface that is built-into your sound card? Got a COM port handy? Then build the Serial MIDI Interface.

This page describes how to build this full-bandwidth single-port (one input and one output) MIDI interface. The interface is buffered (that is, if the PC gets behind you won't lose data) and it works in Windows 3.1 and Windows 95 using a special (and very well-behaved) device driver. A simpler version, using an Atmel AT89C2051 microprocessor, is described here.

Some specifications:

A List of Parts

To build the Serial MIDI Interface, you'll need the following parts. Most of these parts are available from electronics distributors such as Digi-Key, Marshall Electronics, Jameco, B.G. Micro, and others.

Qty Part Number Description Reference Des.
1 Cap, 10uF, 16V, Elect. C1
2 Cap, 22pF, Cer. C2, C3
2 Cap, 33uF, Elect. C4, C5
1 Cap, 0.1uF, Cer. C6
6 1N5817 Diode, Schottky D1, D2, D3, D6 (optional), D7, D8
1 1N4148 Diode, Silicon D4
1 1N5232B Diode, 5.6V Zener, 500mW D5
2 Connector, DIN, 5 pin, 180deg, PCB mount J1, J2
1 Connector, Power J3 (optional)
1 Connector, DB-9 Female, PCB mount P1
1 2N2222A Transistor, NPN Q1
1 2N2907 Transistor, PNP Q2, Q3
1 Resistor, 6.8K, 1/8W R1
1 Resistor, 10K, 1/8W R2, R3, R4, R12, R14, R15
4 Resistor, 220, 1/8W R5, R7 (optional), R10, R11
1 Resistor, 2.2M, 1/8W R6
2 Resistor, 33K, 1/8W R8, R9
1 Resistor, 4.7K, 1/8W R13
1 80C51 or 87C51 Microprocessor, 128 byte RAM, 4K EPROM U1
1 PC-900 Optoisolator, Sharp U2
1 TL064CN Quad Opamp U3
1 Crystal, 12.000 MHz Y1

Microprocessor Care and Feeding

Depending on your available resources, you have several choices for the microprocessor, in decending order of preference:

Given a low-power single-chip processor, the interface should be able to steal power from the serial port without resorting to a wall adapter.

Construction

  1. Download the firmware, test program, and driver.
  2. Program the processor or EPROM with the SERMIDI.HEX file that you downloaded above.
  3. Wire the circuit shown in Figure 1 using a piece of perf board, preferably the kind with plated-thru holes. Don't forget to socket the ICs, all of 'em. If you blow something up in testing, it is much easier to plug in a new chip than to unsolder the poor lost soul.
  4. Make sure the crystal is close to the processor and keep the leads between C2, C3, ground, and the crystal short.
  5. Be sure to install Q1, Q2, and Q3 with the proper polarity. Notice that Q2 and Q3 are "upside down" from what you might expect.
  6. Double check the polarity of D4. As a tradition, I always install these diodes backwards at least once per project.
  7. Check your wiring when you're done, especially if you've built an EPROM version of the interface. The wiring between the microprocessor, EPROM, and address latch is a great source of potential error.
  8. If desired, build the interface to fit into a small plastic or metal project box. Here's an idea: Make the interface even smaller by connecting the serial cable directly to the board. Cut the end off of an otherwise perfectly good cable, strip and tin the 7 necessary wires, and connect them to a header strip. Wire the matching end of the header in place of P1. Be sure to provide good strain relief for the cable as it exits the project box.

[Serial MIDI Schematic]
Figure 1. Serial MIDI Interface Schematic
Click on this image to see a larger, printable version.

If you can't find the Sharp PC-900 opto-isolator, substitute the more common 6N138. It uses a different pinout, so you'll need to connect it like this:

[6N138]

I also have a quantity of PC-900s available. Order here.

Testing 123

Remove U1, U2, and U3 from the board. You did socket them, didn't you? Connect the board to a serial port. Run HyperTerminal or some other terminal program and select the COM port that is attached to the interface. Measure the voltage (from ground) at U1 pin 40, U2 pin 6, and U3 pin 4. In each case it should be between +5 and +6 volts. Likewise, measure the voltage at U3 pin 11. It should be between -5 volts and -15 volts. Exit the terminal program and disconnect the interface from the computer.

If all is well, install the ICs and reconnect the interface. Exit Windows into DOS. Run the supplied test program, SERTEST.EXE. The program accepts two optional parameters, the COM port (1, 2, 3, etc.) and the IRQ. If your interface is not connected to COM2, you will have to specify the COM port (and possibly IRQ, if the COM port is using a non-standard setting). Run the program from a DOS prompt (not in a Windows DOS box). The program should locate the interface and report the I/O port and IRQ. Then, connect a MIDI cable between the MIDI input and output connectors and type some characters on the PCs keyboard. The program will send each byte out the MIDI output and display whatever is received. If everything is working, you should see what you type on the keyboard. Congratulations!

If things are not working, try the following steps:

Installing the Driver

Once you have the interface working using SERTEST, you are ready to install the Windows device driver and start using your new interface.

To install the driver in Windows 95:

  1. Open the Add New Hardware applet in the Control Panel.
  2. Click Next. In the next dialog, select No in response to the Do you want Windows to search for your hardware? question. Click Next.
  3. Choose Sound, video and game controllers from the list. Click Next.
  4. Click the Have Disk button.
  5. Click the Browse button and locate the driver files. Click OK.
  6. Select Serial Port MIDI Interface and click OK to install the driver.
  7. Once the driver is installed a driver setup dialog box will appear. Select the COM port that the interface is (or will be) connected to and click on OK. Restart Windows in order to complete installation.

To install the driver in Windows 3.1:

  1. Open the control panel. Double click on the Drivers icon.
  2. Push the Add... button, select Unlisted or Updated Driver and click OK.
  3. Type the path to the driver files in the edit window in the dialog box. Click on OK.
  4. Once the driver is installed a driver setup dialog box will appear. Select the COM port that the interface is (or will be) connected to and click OK. Restart Windows in order to complete installation.

Once the driver is installed and Windows has restarted, run a Windows MIDI application (a good test program is MidiSpy--an example program from Maximum MIDI: Music Applications in C++. You can download the program as part of the book's demo here.) The program should list "SERMIDI In" as an input device and "SERMIDI Out" as an output device. Make sure the interface is connected to the proper COM port and select these two MIDI ports as active devices. Using the program, check to see that MIDI is properly received and sent using the interface.

That's all there is to it.

How It Works

The interface performs a simple function--simple to name, that is. Its sole function in the universe is to convert between two serial baud rates, 31,250 baud (MIDI) and 38,400 (PC). If the Intel 8051 microprocessor had two serial ports this would be a trivial task. By setting one serial port to 31,250 baud and the other one to 38,400 baud and sending bytes received by one using the other, the job would be done.

But this won't work using an 8051. Some of you in the audience might be thinking one or both of the following questions. If an 8051 can't do it, why not find some other chip that will? The PC has a serial port, MIDI is a serial protocol, can't you just set the PC to 31,250 baud and be done with it?

Two answers: cost, and no. While there are microprocessors out there that have two (or even more) serial ports, they are all much more expensive (and more rare) than an 8051. There are other single-serial-port microprocessors that could do the job as well as (that is, no worse than) the 8051, but the author finds that MIDI is very easy to implement using this device.

The PC's serial port cannot be programmed to work at the MIDI baud rate. Because it is implemented using integer divisors, the timing crystal that is used on PCs means that the serial port can go 19,200 baud or 38,400 baud, but never 31,250 baud.

Thus, a way had to be found to implement a second serial port using software in the 8051. This serial port runs at 38,400 baud and operates using a fancy 25-state state machine. (A state machine is a software scheme where the microprocessor jumps from state to state in response to external and internal events.) Each state in the code is carefully timed so that all of the processes can occur without skipping a beat.

Hardware Handshake

The interface uses a handshake algorithm to pace the transfer of data between the microprocessor and the PC. For example, if the PC has a byte for the interface, it asks the interface if it can accept a byte for MIDI output by pulsing the PCRTS line low (/RTS). The interface might not be able to accept another byte if it is in the middle of sending one already. When it can accept a byte, the interface responds by toggling the state of the IFCTS signal (CTS). In response, the PC sends the byte to the interface.

The reverse process is similar. If the interface has a byte to send to the PC (i.e., recieved MIDI data), it toggles the state of the IFRTS signal (DSR). If the PC is not busy (i.e, performing disk I/O or some other non-interruptable process), it responds by pulsing PCCTS low (/DTR). The interface can then send the serial data to the PC.

The PCRTS and PCCTS are inputs to the interface. They are both normally high and only pulse low for very short durations. These signals are designed to function this way because they also serve to provide power for the interface.

Some PC serial ports, especially those found on laptops, are heavily filtered to reduce EMI emissions. This filtering slows the rise and fall times of signals that the interface can process. To ensure that the Serial Port MIDI Interface will work with these slower rise-time COM ports, the driver measures the minimum pulse width that the port can handle and adjusts itself accordingly.

Odds and Ends

For most computers, that's all you need to know to use the Serial Port MIDI Interface. But there are a few computers (notably some Compaq laptops and Gateway 2000 systems) that don't behave as expected. These systems often have non-standard serial hardware and custom VxDs to handle this oddball hardware. As a result, you may experience some problems using the interface on these systems.

If you do, try adding the following line to your system.ini file. This applies to both Windows 3.1 and Windows 95. In the [SERMIDI] section, add the line:

[SERMIDI]
AlwaysUseIrq=1

This will change certain driver behaviors and allow the interface to work with these systems.