I'm planning to make a small MIDI transport box ( an encoder plus 6 buttons) but i don't have a lot of experience in serial communications( I have used shift registers but that's all) I would like to implement it in software because the device i'm using have no serial peripheral. I tried to look at software examples on the net , but i didn't find anything that would make everything clear. I'm planning to use a TI MSP430G2201.

Could someone help me understand how to transmit midi data? with something like pseudo code.

I know what i want to transmit, that part is clear, i understand how MIDI messages work, but i don't understand how to set up protocol for midi.

  • 1
    \$\begingroup\$ Are you asking how to code a software UART or how to transmit MIDI specifically? MIDI data is no different to any other data - as long as you set your communications parameters to 31250,N,8,1. I'm not familiar with MSP430 but I see that the MSP430G2153 does have a hardware UART. \$\endgroup\$
    – MikeJ-UK
    Aug 13, 2012 at 13:20
  • \$\begingroup\$ Sorry, I'm a total noob in this topic. But this model don't have hardware UART, I have a guide to software uart i read but i don't know how to apply it to MIDI \$\endgroup\$ Aug 13, 2012 at 15:40
  • \$\begingroup\$ @MattMagyar I think mikej is suggesting picking a version with the HW uart. It's a good suggestion unless you have a lot of experience with microcontrollers and interrupts or a lot of time on your hands. \$\endgroup\$
    – kenny
    Aug 13, 2012 at 16:48

2 Answers 2


You say that you know how to implement a software UART and that you understand the format of MIDI messages. Your question suggests that you just want to transmit MIDI data using six buttons - perhaps to transmit control-change or program-change messages. There is no protocol as such to do this, you just need to transmit the data bytes in the correct order.

The simplest way would be to implement a bit banging UART transmitter. This would need to toggle one of the processor's IO port pins. First, the pin is driven low for 32us to represent the start bit. Next the pin is driven high or low to represent each of the 8 data bits in the byte you wish to transmit with the least-significant-bit sent first. The pin is driven to the appropriate level for 32us for each bit. Finally, the pin is driven high for at 32us to represent the stop bit. You might like to view wikipedia's article on asynchronous serial communications.

For instance, to transmit a control change message to set "Effect Control 1" (Controller number 0C) to the value 7 on channel 3, you would need to transmit 3 data bytes - 0xB3, 0x0C, 0x07 in sequence.

(Be aware that this "bit banging" technique may not be suitable if your processor is going to be doing a lot of other work simultaneously).

Of course you will also need the standard opto-isolated interface as recommended by the Midi Manufacturers' Association which also has several useful free pdfs containing message formats etc.

  • \$\begingroup\$ sorry, i know my question was not very clear and really stupid. I read a guide about SW Uart but i have never used it and lacking any experience in asynchronous makes every description of MIDI really confusing, mysterious and I have no idea how to get going in it. I want to make a MIDI OUT, and only MIDI IN need an opto isolator \$\endgroup\$ Aug 13, 2012 at 18:42

Bot a hardware UART and software "bit banging" accomplish the same thing: put high and low signals on a digital pin emanating from the microcontroller.

That signal is not suitable for MIDI, nor for RS-232; it has to be conditioned to drive the line properly.

MIDI differs from RS-232. RS-232 is based on voltage levels, whereas MIDI is based on a current loop. MIDI is actually defined not in terms of a specification of currents or voltages, but by a reference circuit diagram.

To drive a MIDI line, you provide a current source which turns on and off a LED inside an opto-coupler on the other end. When no current flows, this is a mark, or 1. A zero is indicated by sourcing 5 mA of current.

You can source the current from the high side using a PNP transistor circuit. A low voltage from your controller to the base of the transistor will turn on the 5 mA; a high level will turn it off.

Many circuits you can find on the internet naively use an NPN transistor, preceded by a logic inverter, which is completely silly because if you don't already have inverters in your circuit, you need a whole new chip (which typically provides six of the inverters).

If you're driving the communication in software, the issue of inversion is moot, of course, since you can invert the logic yourself, but it makes more sense to design a circuit that can later be reused with a UART without needing an inverter.

The rate is 31.250 kbps. If you do it in software, you have to use timing loops or whatever technique match this rate as accurately as possible. This was chosen for MIDI because 1Mhz clocks are common, and 31.250 kHz is 1 MHz divided by 32. The serial baud rates like 38400 are not nice divisors of a round MHz frequency. A 1Mhz clock has to be scaled to 24Mhz before it is divisible by 38400.

Like in RS-232 communication, when nothing is being transmitted, the line is in a 1 state. So in MIDI this means no current is flowing. MIDI uses the 8N1 format for framing individual bytes: a byte is encoded as ten bits: a start bit whose value is 0, 8 bits of data, and stop bit whose value is 1. Thus, 3125 bytes per second can be transmitted (bit rate divided by ten).

In other words, the communication of each byte begins when current is driven to signal a zero. Then eight more bits follow which are just level changes clocked evenly from the start of the initial zero. Then the line returns to 1 for a clock period (the stop bit) before another before another byte can be transmitted. The other byte can be transmitted after an arbitrary additional pause, an that pause is not a multiple of any clock, which is why this is asynchronous serial communication.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.