Digital Music Programming II: Basic Stamp MIDI output




This lab demonstrates how to get MIDI output using a Basic Stamp IIsx microcontroller.

The following program is written for the Basic Stamp IIsx microcontroller. The program emits a MIDI note once every second which has a duration of 1/2 of a second.

cout.bsx
1
2
3
4
5
6
7
8
9
10
outpin     con     8    
baudmode   con    60    
outpause   con     0    

top:
   serout outpin, baudmode, outpause, [$90, 60, 127]
   pause 493            
   serout outpin, baudmode, outpause, [$90, 60, 0]
   pause 492
   goto top: 

The program consists of two sections: (1) a header which defines some constants so that the rest of the program is easier to read, and (2) an infinite loop which plays the same note over and over.

The header (lines 1-3) contains three constant variable declarations. This variables are replaced with their numeric values in the body of the program when the program is compiled, so no memory storage space is used to hold these variables when the program is running (except as constant numbers). The three constants are:

  1. outpin -- output pin from the microcontroller which is used to send MIDI data. This value can range from 0 to 15, since there are 16 programmable pins on the Basic Stamp IIsx. In the example program, and pictures below, pin 8 is used to send the MIDI data out of the microcontroller, but any other pin would work just as well.
  2. baudmode -- speed of serial data transmission. In this case, the value is 60 which means a baud rate of 31250 bits per second (standard MIDI data rate) translated according to this equation: (2500000/rate)-20 for the Basic Stamp IIsx microcontroller.
  3. outpause -- how many units of 0.4 milliseconds (0-65535) to wait before sending serial data.

The body of the program (lines 5-10) does all of the work. There are three functions used in the body of the program: serout, pause, and goto. Here is an explanation of each line:

  1. top:
    This is a label which is used to mark a location in the program. Line 10 will send the program control back to this point.
  2. serout outpin, baudmode, outpause, [$90, 60, 127]
    This line sends a note-on message on MIDI channel 1 with note 60 (middle-c) at the loudest possible attack velocity (127). Note that the MIDI command is in hexadecimal form: $90 this is equivalent to the decimal number 144, but is in an easier form to read the MIDI command and MIDI channel off of.
  3. pause 493
    Pause for 493 milliseconds. Sending the MIDI data takes time, and interpreting each command in the Basic Stamp program takes time as well. This value was measured to keep the tempo at exactly 60 beats per minute. Theoretically the value should be 500 for a pause of 500 milliseconds.
  4. serout outpin, baudmode, outpause, [$90, 60, 0]
    Send a note-off command.
  5. pause 492
    Pause 492 milliseconds.
  6. goto top:
    Go to the top: label, which in this case means to start the program over from the beginning.

The circuit connected to the Basic Stamp for sending MIDI data is shown below. The program uses I/O pin #8 (DIP pin #13) for the MIDI output, but you can change the program to use any output pin for MIDI out. The +5v power source for the circuit below can come from the DIP pin #21 from the microcontroller.



Exercises

  1. Set the pause between note-on and note-off messages to 1 or 2 milliseconds. Then examine the signal going through the MIDI cable on an oscilloscope. Here is what you should see:

    Notice that start bits and stop bits which are used to mark the data being transmitted on the serial connection. The data is sent upside down, since 1's are sent as 0 volts and 0's are sent as +5 volts. Whenever there is no data being sent, the MIDI cable is set to 0 volts. When a new data value is sent on the cable, the first bit sent is always a 0 (+5 volts), then the 8-bit data byte is sent one bit at a time from highest bit to lowest bit. The a stop bit of 0 is sent.

  2. Output some other MIDI note or MIDI Data from the program.

  3. Output a glissando rather than repeating one note.

  4. generate a random walk melody (or rhythm) using the random Basic Stamp function.