home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 8 / AU_CD8.iso / CoverDiscs / November_1999 / rtr / software / Drum next >
Text File  |  1999-08-30  |  9KB  |  240 lines

  1.  ;MIDI Drum
  2.  ;For an 10 MHz clock
  3.  ;A0 - MIDI Serial out
  4.  ; 
  5.  ; 0838 - A/D converter
  6.  ;B0 - ~CS not Chip Select
  7.  ;B1 - Clock
  8.  ;B2 - Data output (multiplex select)
  9.  ;B3 - Data input (converted value)
  10.  
  11.  ;Note for 16F84 invert the state of the PWRTE
  12.       LIST P=16C84
  13.        
  14.  
  15.  C       EQU     0
  16.  PCL     EQU     2
  17.  Z       EQU     2
  18.  PORTB   EQU     6
  19.  PORTA   EQU     5
  20.  TRISB   EQU     86H
  21.  TRISA   EQU     85H
  22.  EECON1  EQU     88H
  23.  INTCON  EQU     0BH
  24.  EEDATA  EQU     8
  25.  EEADR   EQU     9
  26.  STATUS  EQU     3
  27.  RP0     EQU     5
  28.  INC     EQU     1
  29.  cts     EQU     2
  30.  rts     EQU     3
  31.  sBuf    EQU    0ch     ;Serial send buffer
  32.  Scount  EQU    0dh     ;Tempory Counter
  33.  Cdown   EQU    0eh     ;Time delay countdown
  34.  rBuf    EQU    0fh     ;Recieve buffer
  35.  val     EQU    10h     ;Value from A/D
  36.  chan    EQU    11h     ;Channel to digitise
  37.  Tcount  EQU    12h     ;Tempory loop counter
  38.  Ccount  EQU    13h     ;Channel counter
  39.  
  40. ;
  41.    ORG 0
  42.         GOTO    Start
  43.         
  44.  
  45. ;Send a serial byte
  46. Send    movlw   9               ;Number of data bits to send
  47.         movwf   Scount
  48.         bcf     PORTA,0         ;Start bit
  49.         call    cDelay          ;Extra delay compensation
  50. Sloop   call    Delay
  51.         btfsc   sBuf,0          ;Set the next bit
  52.         bsf     PORTA,0
  53.         btfss   sBuf,0
  54.         bcf     PORTA,0
  55.         rrf     sBuf,f          ;Shift for next time
  56.         decfsz  Scount,f
  57.         goto    Sloop           ;Round until finished
  58.         bsf     PORTA,0         ;Stop bit
  59.         call    cDelay          ;Extra delay compensation
  60.         call    Delay
  61.         call    Delay
  62.         call    cDelay          ;For good measure
  63.         return
  64.         
  65. cDelay  movlw   2               ;Small delay at end of send
  66.         movwf   Cdown           
  67.         goto    Dloop
  68.         
  69. hDelay  movlw   10              ;half delay for serial recieve 10MHz clock
  70.         movwf   Cdown           ; 31,250 Baud rate
  71.         goto    Dloop
  72.         
  73. rDelay  movlw   21              ;Delay for serial recieve 10MHz clock
  74.         movwf   Cdown           ; 31,250 Baud rate
  75.         goto    Dloop
  76.         
  77. Delay   movlw   22              ;Delay for serial send 10MHz clock
  78.         movwf   Cdown           ; 31,250 Baud rate
  79. Dloop   decfsz  Cdown,f
  80.         goto    Dloop
  81.         return
  82.         
  83.  
  84. Start
  85.         BSF     STATUS,RP0      ;SELECT REGISTER BANK 1
  86.         MOVLW   0Ah             ;Bits 3 & 2 inputs
  87.         MOVWF   TRISA^80H
  88.         MOVLW   0F8h            ;port B I/O
  89.         MOVWF   TRISB^80H
  90.         MOVLW   00H             ;Enable Pull ups on inputs
  91.         MOVWF   1               ;Option register
  92.       
  93.         BCF     STATUS,RP0      ;SELECT REGISTER BANK 0
  94.         BSF     PORTA,cts       ;CTS disabled
  95.         MOVLW   5               ;Inital state of outputs
  96.         MOVWF   PORTB
  97.  
  98.         movlw   20h             ;Fill buffer with FFs
  99.         movwf   4
  100.         movlw   8
  101.         movwf   Ccount
  102. pml     clrf    0
  103.         comf    0,f
  104.         incf    4,f
  105.         decfsz  Ccount,f
  106.         goto    pml       
  107.  
  108. Main
  109.       movlw     20h             ;Start of buffer
  110.       movwf     4               ;Indirect register
  111.       movlw     8               ;Number of channels to read
  112.       movwf     Ccount          ;Into counter
  113. Ml    call      conv            ;do conversion
  114.       call      hyst            ;Add in hystrisis
  115.       subwf     0,w             ;test to see if more than last time
  116.       btfss     STATUS,C
  117.       call      play            ;Call play if bigger than last time
  118.       movf      val,w           ;Get current reading
  119.       movwf     0               ;save in buffer
  120.       incf      4,f             ;Move to next buffer position
  121.       decfsz    Ccount,f
  122.       goto      Ml              ;Read all 8 channels
  123.       goto      Main            ;Loop
  124.  
  125. conv  movlw     5               ;Bits to send
  126.       movwf     Tcount   
  127.       movf      Ccount,w        ;Get channel           
  128.       iorlw     18h             ;Add start bit and single conversion bit
  129.       movwf     chan            ;Channel to convert
  130.       bcf       PORTB,0         ;Lower ~CS
  131. clop  btfss     chan,4          ;Put data on output
  132.       bcf       PORTB,2         ;Put a zero
  133.       btfsc     chan,4
  134.       bsf       PORTB,2         ;Or a one
  135.       bsf       PORTB,1         ;Raise clock
  136.       call      ckd             ;Clock delay
  137.       bcf       PORTB,1         ;Lower clock
  138.       rlf       chan,f          ;Move sending bit pattern
  139.       decfsz    Tcount,f        ;loop round five times
  140.       goto      clop
  141.       call      ckd
  142.       call      ckd             ;Multiplexer settiling time
  143.       movlw     9               ;bits to recieve + 1
  144.       movwf     Tcount
  145.       clrf      val
  146. ilop  rlf       val,f           ;Shift up pattern to recieve the next bit
  147.       bcf       val,0           ;make it a zero
  148.       btfsc     PORTB,3
  149.       bsf       val,0           ;make it a one if need be
  150.       bsf       PORTB,1         ;Clock up
  151.       call      ckd             ;Clock delay
  152.       bcf       PORTB,1         ;Clock down
  153.       decfsz    Tcount,f
  154.       goto      ilop            ;Round again
  155.       bsf       PORTB,2         ;Data to the converter set to a one
  156.       bsf       PORTB,0         ;Raise ~CS as we are finished
  157.       movf      val,w           ;Get value in w
  158.       return
  159.       
  160. ckd   return                    ;1.25 uS delay
  161.  
  162. play  btfsc     STATUS,Z        ;Return if they are the same
  163.       return
  164. sag   movf      val,w           ;get the value
  165.       movwf     0               ;put it in the buffer
  166.       call      conv            ;Get same channel again
  167.       subwf     0,w             ;Test to see if more than last time
  168.       btfsc     STATUS,Z        ;It is the same so don't play it
  169.       goto      sag
  170.       btfss     STATUS,C        ;Is it greater (still rising)
  171.       goto      sag             ;Go round if it is bigger
  172.       movlw     99h             ;Note On Channel 10 (drums)
  173.       movwf     sBuf
  174.       call      Send
  175.       rrf       PORTB,w         ;Get mapping shift switches
  176.       andlw     18h             ;Look at only 2 bits (4 mappings)
  177.       addwf     Ccount,w        ;add in the channel
  178.       call      map             ;Map channel number to note (instrument)
  179.       movwf     sBuf            ;Send it
  180.       call      Send
  181.       clrf      STATUS,C        ;Adjust velocity to 7 bits
  182.       rrf       0,w
  183.       movwf     sBuf            ;Send velocity
  184.       call      Send
  185.       clrf      val             ;Set val to FF to inhibit firing next time round
  186.       comf      val,f
  187.       return                    ;back with it
  188.       
  189. hyst  incf      0,f             ;Add hystresis to stored value
  190.       btfsc     STATUS,Z        ;Make sure it does not wrap round to 0
  191.       comf      0,f
  192.       incf      0,f             ;Add hystresis to stored value
  193.       btfsc     STATUS,Z        ;Make sure it does not wrap round to 0
  194.       comf      0,f
  195.       incf      0,f             ;Add hystresis to stored value
  196.       btfsc     STATUS,Z        ;Make sure it does not wrap round to 0
  197.       comf      0,f
  198.       return
  199.       
  200. map                             ;Map a MIDI drum sound to a channel number
  201.                                 ;Change this to suite yourself
  202.         addwf   PCL,f
  203. ;Mapping 0
  204.         retlw   59      ;Ch0 Ride cymbal 2
  205.         retlw   57      ;Ch1 Crash cymbal 2
  206.         retlw   58      ;Ch2 Vibraslap
  207.         retlw   56      ;Ch3 Cow bell
  208.         retlw   54      ;Ch4 Tambourine
  209.         retlw   50      ;Ch5 Hi tom
  210.         retlw   48      ;Ch6 Hi hat open
  211.         retlw   39      ;Ch7 Hand clap
  212. ;Mapping 1
  213.         retlw   32      ;Ch0 Sticks
  214.         retlw   22      ;Ch1 Metronome bell
  215.         retlw   29      ;Ch2 Snare roll
  216.         retlw   37      ;Ch3 Side stick
  217.         retlw   38      ;Ch4 Snare M
  218.         retlw   42      ;Ch5 Hi hat closed
  219.         retlw   30      ;Ch6 Castanet
  220.         retlw   18      ;Ch7 Scratch pull
  221. ;Mapping 2        
  222.         retlw   20      ;Ch0 Click noise
  223.         retlw   19      ;Ch1 Finger snap
  224.         retlw   16      ;Ch2 Whip slap
  225.         retlw   25      ;Ch3 Brush tap
  226.         retlw   26      ;Ch4 Brush swirl
  227.         retlw   34      ;Ch5 Open rim shot
  228.         retlw   35      ;Ch6 Bass drum M
  229.         retlw   36      ;Ch7 Bass drum H
  230. ;Mapping 3
  231.         retlw   35      ;Ch0 Bass drum
  232.         retlw   38      ;Ch1 Snare
  233.         retlw   46      ;Ch2 Hi hat open
  234.         retlw   49      ;Ch3 Crash cymbal
  235.         retlw   51      ;Ch4 Ride cymbal
  236.         retlw   55      ;Ch5 Splash Cymbal
  237.         retlw   41      ;Ch6 Floor Tom L
  238.         retlw   43      ;Ch7 Floor Tom H
  239.  
  240.   end