Introduction
This
page contain information and examples to receive MIDI messages
with a PIC and an USART.... Most of the recent Microchip PICs
are fitted with a configurable hardware USART ( universal serial
asynchronous Receiver transmitter) perfect for MIDI communications.
Older PICs, like the famous 16F84 do not have this feature inside.
To receive MIDI with them, a software emulation is required.
There are many valid algorithms to do this, but they are less
efficient than harware USARTs anyway. Those methods are well
described in many DIYers sites and i will not write anymore
about this. Also there is no reason today to purchase a PIC
without an USART if you want to do MIDI with! The 16F628-20
is costless ,twice memory, 5 times the speed than a PIC16F84-04.
I
cover only the base and simplest code for MIDI reception with
recent PICs. I will keep under silence all other methods like
the use of interrupts ( very efficient but more complex ), transmitting
.... This short page is dedicaced to DIYers with a basic knowledge
of PIC and assembler. All codes examples are in MASM. It is
the best langage for all embedded MIDI PIC based applications.
Speed is alway required for MIDI sotfwares. Comments are in
green color.
Generic
schematic and pcb
First
a little generic schematic and pcb to download ... It is perfect
for first MIDI trials with a PIC16F628 ...It is the pcb of my
MIDI clock converter MC628
also all firmware provided in this page will work ... Valid
your circuit with those software before starting your code ...
This will ensure you have a working hardware to play with...
  
PIC's
USART configuration for MIDI
The first thing to
do to implement MIDI I/O, is to initialise the PIC USART . This
is achieved by following lines. Place this code at the begining
of the program. MIDI rate is 31250 bauds 8bits serial interface.
The USART clock generator must be set at this rate first. Then
config bytes are set to start the USART. Notice that the RX
pin of port B must be input and the TX pin an output before
the USART init...At the end of this code the PIC is ready to
receive or transmit any MIDI messages, theoricaly ;)
;-----------------------------port inits -------
clrf PORTA ; zero ports A et B
clrf PORTB
movlw 0x07
movwf CMCON ; set port A mode I/O
digital
clrf TRISB ; port B is OUT
bsf TRISB,1 ; RX is IN
clrf TRISA ; port A is OUT
;-----------------------------usart config-------
bsf STATUS,RP0 ; zap to bank1
movlw 0x09 ; value for the BRG
movwf SPBRG ; init BRG 31250 bauds
bcf TXSTA,SYNC
bcf STATUS,RP0 ; come back to bank0
bsf RCSTA,SPEN; start the USART rx/tx
bsf RCSTA,CREN; both line can be resume
in one here
Receive
a MIDI byte
Example of code to
use for MIDI reception. Many time more simple than MIDI 32 usec
emulation loop :) This code wait for a MIDI byte. The received
value is then placed into the W register.
|
;------------------------read a MIDI byte-------
btfss PIR1,RCIF ;Wait for RX flag
goto $-1; Loop until a byte is received
movf RCREG,W ; read the USART register
; the MIDI byte is now avalable in W register for future
use...
|
Send
a MIDI byte
Now an example of
code to use for MIDI send. This code send a MIDI byte contained
into the W register.
|
;------------------------Write a MIDI byte-------
movwf RCREG ; Write W to the USART
register
; the MIDI byte is automaticaly send ....
|
That's all for now
... Check Microchip datasheet , USART chapter - to learn more
...
|