==== MIDI ==== Here is an example of code that will allow the MICrODEC to receive MIDI data over the serial port. Because of the way interrupts are handled, it is difficult to have a generic MIDI interface in the Main loop which communicates with the Function programs. It is possible to do this, but it would cut down on the processing time within the Function programs. Therefore, to accommodate MIDI, each Function must have its own implementation. This has the net effect of maximizing processing time, program by program. ===== Initialize the USART ===== This first section of code is an initialization routine for talking MIDI, and must be placed at the beginning of the Function program. This sets up the USART for the 31.25kbps data transfer rate, and sets the start bits and stop bits. {{{#!highlight nasm ; initialize USART for midi transfer and set new jump address ldi r30,$0f ; set new jump address ; this value will need to be changed if there is more ; initialization code that follows the USART ; initialization. ldi r17,$00 sts UCSR0A,r17 ; set USART to single speed sts UBRR0H,r17 ; set USART to 31.25kbps baud rate for midi ldi r17,$27 sts UBRR0L,r17 ldi r17,$06 sts UCSR0C,r17 ; set USART to 8 bit mode ldi r17,$10 sts UCSR0B,r17 ; turn on USART reciever }}} ===== Receive Data from the USART ===== This code only shows how to receive data from the USART, although transmitting data would be much simpler. The first thing that happens is that the code checks to see if any USART data has arrived, and if it has, it checks for a MIDI command byte. If a MIDI command byte is received, it then checks for a MIDI controller byte. After it receives the appropriate controller data, it receives a MIDI data byte and updates a register that the Function program can accesses. It then resets itself to wait for another MIDI command byte. If bytes are received out of order, or the wrong command or controller bytes are received, it also resets itself. A better implementation would include a counter that resets if a certain amount of time has elapsed between bytes. {{{#!highlight nasm ; receive MIDI data lds r17,UCSR0A ; get USART control register sbrs r17,RXC0 ; check if byte has been received rjmp done_UID ; finish off if no byte has been received lds r17,UDR0 ; get data byte if one arrived brts update_UID ; skip if command byte already received ; the t-bit is used to indicate a command byte recieved ; although another bit in a register could also be used andi r17,$f0 ; mask off channel number - receive all channels for now ; this can be set to look for a particular MIDI channel cpi r17,$b0 ; check if appropriate MIDI command number ; can be set to any MIDI command brne done_UID ; do nothing if not correct command byte set ; set t register to indicate correct command byte received rjmp done_UID ; finish off update_UID: ; update data register with MIDI data sbrc r17,$07 ; check if msb is set - indicates command byte ; if command byte is received at this point, it is out ; of sequence, and the program should reset itself rjmp resetusart_UID ; dont process data if command byte tst r14 ; check if first byte - r14 holds the byte counter ; (0 = first byte, 1 = second byte) brne secondbyte_UID ; skip to second byte if not cpi r17,$47 ; if first byte, check if controller number 71 ; can be set to any controller number brne resetusart_UID ; do not process data if not the correct controller inc r14 ; set byte counter to first byte received rjmp done_UID ; finish off secondbyte_UID: ; get second byte mov r27,r17 ; move MIDI data to register that the Function program ; references resetusart_UID: ; reset usart clt ; reset command byte indicator clr r14 ; reset byte counter done_UID: ; place for rest of program }}}