welcome: please sign in
location: Diff for "MicrodecMidi"
Differences between revisions 6 and 7
Revision 6 as of 2010-08-14 02:57:47
Size: 2742
Editor: guest
Comment:
Revision 7 as of 2010-08-14 03:19:11
Size: 3739
Editor: guest
Comment:
Deletions are marked like this. Additions are marked like this.
Line 16: Line 16:
sts ucsr0a,r17 ; set USART to single speed
sts ubrr0h,r17 ; set USART to 31.25kbps baud rate for midi
sts UCSR0A,r17 ; set USART to single speed
sts UBRR0H,r17 ; set USART to 31.25kbps baud rate for midi
Line 19: Line 19:
sts ubrr0l,r17 sts UBRR0L,r17
Line 21: Line 21:
sts ucsr0c,r17 ; set USART to 8 bit mode sts UCSR0C,r17 ; set USART to 8 bit mode
Line 23: Line 23:
sts ucsr0b,r17 ; turn on USART reciever sts UCSR0B,r17 ; turn on USART reciever
Line 26: Line 26:
===== Receive Data from the USART =====
Line 27: Line 28:
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 data byte. After it receives the data, it updates a register that the Function program accesses to modify a parameter. It then resets itself to wait for another MIDI command byte. This code is run each sample period.
Line 29: Line 31:
; get delay settings
lds r17,ucsr0a ; get USART control register
sbrs r17,rxc0 ; check if byte has been recieved
rjmp shift_000023 ; skip buffer read
lds r17,udr0 ; get data byte
brts update_000023 ; skip if command byte already recieved
andi r17,$f0 ; mask off channel number - recieve all channels for now
cpi r17,$b0 ; check if controller
brne shift_000023 ; do nothing if not correct command byte
set ; set t register to indicate correct command byte recieved
rjmp shift_000023 ; finish off
; 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
Line 41: Line 47:
update_000023: ; update delay time with pitch bend value update_UID: ; update data register with MIDI data
Line 43: Line 49:
sbrc r17,$07 ; check if msb is set - command byte
rjmp resetusart_000023 ; dont process data if command byte
tst r14 ; check if first byte
brne secondbyte_000023 ; skip to second byte if not
cpi r17,$47 ; check if controller number 71
brne resetusart_000023
inc r14 ; set byte counter to first byte recieved
rjmp shift_000023 ; finish off
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)
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
Line 52: Line 61:
secondbyte_000023: ; get second byte secondbyte_UID: ; get second byte
Line 54: Line 63:
lsl r17 ; shift 14b number to 16b number
;lsl r0
;rol r17
mov r27,r17 ; move data to desired delay register
;mov r26,r0
cpi r27,$03 ; check if desired delay is less than 9ms
brsh resetusart_000023 ; finish off if delay time is greater than 9ms
ldi r27,$03 ; set desired delay to 9ms
clr r26
mov r27,r17 ; move MIDI data to register that the Function program
            ; references
Line 64: Line 66:
resetusart_000023: ; reset usart resetusart_UID: ; reset usart
Line 68: Line 70:

done_UID: ; place for rest of program

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.

   1 ; initialize USART for midi transfer and new jump address
   2 ldi r30,$0f ; set new jump address
   3             ; this value will need to be changed if there is more
   4             ; initialization code that follows the USART
   5             ; initialization.     
   6 ldi r17,$00
   7 sts UCSR0A,r17 ; set USART to single speed
   8 sts UBRR0H,r17 ; set USART to 31.25kbps baud rate for midi
   9 ldi r17,$27
  10 sts UBRR0L,r17
  11 ldi r17,$06
  12 sts UCSR0C,r17 ; set USART to 8 bit mode
  13 ldi r17,$10
  14 sts UCSR0B,r17 ; turn on USART reciever
  15 

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 data byte. After it receives the data, it updates a register that the Function program accesses to modify a parameter. It then resets itself to wait for another MIDI command byte. This code is run each sample period.

   1 ; receive MIDI data
   2 lds r17,UCSR0A ; get USART control register
   3 sbrs r17,RXC0 ; check if byte has been received
   4 rjmp done_UID ; finish off if no byte has been received
   5 lds r17,UDR0 ; get data byte if one arrived
   6 brts update_UID ; skip if command byte already received
   7                 ; the t-bit is used to indicate a command byte recieved
   8                 ; although another bit in a register could also be used
   9 andi r17,$f0 ; mask off channel number - receive all channels for now
  10              ; this can be set to look for a particular MIDI channel
  11 cpi r17,$b0 ; check if appropriate MIDI command number
  12             ; can be set to any MIDI command
  13 brne done_UID ; do nothing if not correct command byte
  14 set ; set t register to indicate correct command byte received
  15 rjmp done_UID ; finish off
  16 
  17 update_UID: ; update data register with MIDI data
  18 
  19 sbrc r17,$07 ; check if msb is set - indicates command byte
  20              ; if command byte is received at this point, it is out
  21              ; of sequence, and the program should reset itself
  22 rjmp resetusart_UID ; dont process data if command byte
  23 tst r14 ; check if first byte - r14 holds the byte counter (0 = first byte)
  24 brne secondbyte_UID ; skip to second byte if not
  25 cpi r17,$47 ; if first byte, check if controller number 71
  26             ; can be set to any controller number
  27 brne resetusart_UID ; do not process data if not the correct controller
  28 inc r14 ; set byte counter to first byte received
  29 rjmp done_UID ; finish off
  30 
  31 secondbyte_UID: ; get second byte
  32 
  33 mov r27,r17 ; move MIDI data to register that the Function program 
  34             ; references
  35 
  36 resetusart_UID: ; reset usart
  37 
  38 clt ; reset command byte indicator
  39 clr r14 ; reset byte counter
  40 
  41 done_UID: ; place for rest of program
  42 

MicrodecMidi (last edited 2010-08-21 01:55:47 by guest)