Circuit design and electronics

I completed a 2-part assignment for this week. The first part was to build a sorta-random sound generator based on a schematic that Joe Paradiso made available. The second part was to build a circuit that could transmit MIDI messages - this will go towards my final project.

Sound generator

The description for this assignment is here.
Electronics knowledge base from 2001 can be found here.

PDF of the circuit diagram
Video with sound of my board in action! (use quicktime to view)

Midi Output

Midi output (and probably input) is going to be important to my final project. For this reason I've decided that one important step between now and completing my final project is to learn how to emit (and receive) MIDI messages from a PIC. I've found several resources online that give directions on how this can be done. Here are several of the more useful ones:

MIDI is basically serial data transmitted at 31,250 baud (bits-per-second) through a circuit with a resistance of about 660 ohms. Parity must be set to NONE, the number of stop bits must be set to 1 and finally the number of data-bits must be set to 8. The baud rate MIDI uses means that during transmission each bit should last for 32 micro-seconds. MIDI is transmitted in chunks of 3 bytes (each byte is 8 bits) and each transmitted byte is padded with a start bit (0) before it and a stop bit (1) after it. The three bytes of the typical MIDI message consist of one status byte and 2 data bytes (in that order). There is a lot of information online about what makes up valid status/data bytes. The bits in each byte are transmitted Least-Significant-Byte (LSB) first.

So if I wanted to transmit the number 144 (this is the code for a NOTE-ON in MIDI), the bit-pattern would be:

what I want to tranmit: 10010000
what bits get put on the line (read left-to-right): 0 00001001 1
(note: the spaces are just to show that there is a start and stop bit)

I was doing some testing while I developed my circuit, and I was sending the following bytes down the MIDI line: 144 144 144

This sequence caused me a lot of headache, because I didn't realize for the longest time that IT'S NOT VALID MIDI. It's three control bytes in a row and no data bytes. So even when I had the hardward constructed correctly, the computer was still not registering any MIDI messages coming through. Be careful of that.

Read my notes from this testing session here (note that in Hexidecimal format, 144 is 0x90)

Since MIDI is really just serial data, it's pretty easy (in theory) to transmit it, since many microcontroller development languages have routines for transmitting and receiving serial built-in. The signaling in MIDI is based on current rather than voltage though, which means that in order to transmit a message, you have to have a certain amount of current flowing through the circuit. For longer MIDI cables, you'll need more current pushing through the circuit.

Resting state on the line is when no current is flowing. This is actually a logical 1. So I think about it like this:

  • Want to transmit a 1 -> no current flows
  • Want to transmit a 0 -> flow that current!
Here's the circuit that I have working:

Note: my connection of +5v to pin 5 and the pin on my microcontroller to pin 4 is backwards from just about every diagram I've seen online. I can only imagine that they are thinking about the connector reversed from the way I'm thinking about it. If you want to use my diagram, think about my connector numbering as if you're looking at a frontal view of the female connector.

So, I noted the following things about my setup (and using the rs232-sending routines in the CSC C compiler environment for the PIC):

  • Resting state for the output pin was +5v, a logical 1
  • The fact that a transmitted byte starts with a start bit of 0 is important, because a 0 is different than the resting state of the pin, so the other end will notice that something is being transmitted.
  • You don't really need a more complicated circuit than this, for short-distance MIDI cables. (I've heard antecdotal evidence that this kind of circuit will work through 25 foot cables just fine..)
Note: RS232 is a communications specification for data transmission which specifies signal voltages, signal timing, signal function, a protocol for information exchange, and mechanical connectors. Actual RS232 signaling specifies that voltages of -3v to -25v with respect to signal ground (pin 7) are considered logic '1' (the marking condition), whereas voltages of +3v to +25v are considered logic '0' (the spacing condition). The range of voltages between -3v and +3v is considered a transition region for which a signal state is not assigned. In order to generate the appropriate voltages for RS232 from a PIC microcontroller, a MAXIM MAX233 chip is typically used. For MIDI this is not necessary, because MIDI operates from 0-5v
- see http://www.camiresearch.com/Data_Com_Basics/RS232_standard.html

The MAXIM MAX233 chip converts between "TTL levels" (0 to +5 volts) of the PIC and "RS232 levels" (+9 to -9 volts) of the serial line. The magic of the MAXIM chip is that it does this conversion with a single +5 V supply; oscillators and charge pumps internal to the chip generate the required + and - 9 volts.
- from http://web.media.mit.edu/~r/projects/picsem/irx2/

Here's the diagram for another circuit that can probably push more current through the wires, for longer distances:

- from http://fargo.itp.tsoa.nyu.edu/~tigoe/pcomp/pic/pic-midi-pbpro.shtml

You can download a sample .c file (for the CCS PIC C compiler) here which shows how to transmit MIDI from pin B0 of a PIC microcontroller (used in conjunction with my handdrawn wiring diagram above, implemented on an IRX board)

Craig Sapp also has some good diagrams about this:

  • midi-in circuit (note: this is the view of the female connector, solder-side)
  • midi-out circuit (note: this is the view of the female connector, solder-side)

Midi Input

I'm still working on getting MIDI input working. One word of wisdom is that it's nice to have a chip that has an onboard UART (Universal Asynchronous Receiver Tranismitter). Sometimes they are called USART's too (add "Synchronous" to the acronym). Having a UART onboard allows the microcontroller to hand off the task of transmitting and receiving serial data to some dedicated hardware, rather than having to do all of the signalling and receiving in software (which ties up the chip's resources that could be in use for other things). The Asynchronous part is key - it means that in receiving the chip will take in several messages and buffer them before needing the attention of the rest of the chip. (For instance, in the PIC16F876 the USART can have received 2 bytes and be in the process of receiving a third all at the same time). However, that third byte will be dropped if neither of the buffered bytes have been read by the time it's fully in.

Back to main page


This site was whacked using the TRIAL version of WebWhacker. This message does not appear on a licensed copy of WebWhacker.