 list      p=16f877            ; list directive to define processor
   #include <p16f877.inc>        ; processor specific variable definitions
   
   __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF
      ; NOTA:
      ; Oscilador utilizado 4MHz.--------> servira para setear la usart a 31,250 Kbd??
      ; ************************************************
velocity equ 0x20 ; Registro para guardar Byte alto de la conversion.-
velocity1 equ 0x21 
velocity2 equ 0x22 
velocity3 equ 0x23
velocity_0 equ 0x24 ; velocity seteada a 0000 0000, equivale a un note off
contador equ 0x25 ; Registro utilizado en demora.-
canal_on equ 0x26 ; mensaje midi de sistema, note on, toma los los valores de canal 2,3,4,5 (ej: note on canal 5 = 1001 0100)
canal_off equ 0x27
nota equ 0x28
vel_max equ 0x29
 
      ; ************************************************
     ORG     0x00             
     goto    Inicio              

     
     


;----------------------------------------------------------------------
      
       ;***Envio_usart***
Envio_usart
      movwf TXREG 
      nop
      btfss PIR1,TXIF ;test if TXREG empty
      goto $-1 ;wait until TXREG empty
      ;bsf STATUS,5
      ;btfss TXSTA,1
      ;goto $-1
      ;bcf STATUS,5  
      return

Inicio 

     ;seteo usart

 bsf     STATUS,RP0      ; goto page 1
           movlw   0x80     ; RC7 is RX input
           movwf   TRISC	   ; RC3 is Prog. Sw.
           movlw   01h             ; 31250 baud for MIDI
           movwf   SPBRG
           movlw   b'00100000'     ; async tx 8 bit
           movwf   TXSTA
           bcf     STATUS,RP0              ; return to page 0
           movlw   0x80     ; async rx 8 bit
           movwf   RCSTA
      
Main
ADC 
      bsf STATUS,5
      movlw 0XFF
      movwf TRISA
      bsf STATUS,5
      movlw 0x02 ; Justificado a la izquierda, Fosc/8, RA0_RA1_RA2_RA3_RA5 analógico, VDD, VSS.
      movwf ADCON1
      bcf STATUS,5 ; Banco 0
      movlw 0x41 ; Fosc/8, Canal 0, módulo habilitado.-
      movwf ADCON0
     
      call Demora_20us
      movwf velocity
      movlw 0x49 ; Fosc/8, Canal 1, módulo habilitado.-
      movwf ADCON0
      
      call Demora_20us
      movwf velocity1
      movlw 0x51 ; Fosc/8, Canal 2, módulo habilitado.-           
      movwf ADCON0
      
      call Demora_20us
      movwf velocity2
      movlw 0x59 ; Fosc/8, Canal 3, módulo habilitado.-
      movwf ADCON0
      
      call Demora_20us
      movwf velocity3
      goto Comparar
 

    


Demora_20us
      movlw 0x05  ; CONTADORES
      movwf contador ; Iniciamos contador.-
Repeticion 
      decfsz contador ; Decrementa Contador1.-
      goto Repeticion ; Si no es cero repetimos ciclo.-
      bsf ADCON0,2
Espera
      btfsc ADCON0,GO ; Espera a que termine conversion.-
      goto Espera
      movf ADRESH,W ;----------------> REVISAR ASUNTO MSB LSB
      return 

Comparar 
      bcf STATUS,5 
      movlw 0x91 ; canal 2 ; inicio canal (note on)
      movwf canal_on 
      movlw 0x81 ; canal 2 ; inicio canal (note off)
      movwf canal_off      
      movlw 0x01 ; nota 1, inicio nota
      movwf nota
      movlw 0x00
      movwf velocity_0 ; inicio velocity = 0, equivalente a note off
      movlw 0x7F
      movwf vel_max
                      
       
       clrw
ch2    
       bcf STATUS,5  
       iorwf velocity,F
       btfsc STATUS,Z ; si velocity diferente de 0, salta y envia el dato
       goto ch3
       movf canal_on,w
      
       call Envio_usart 
       movf nota,w
       
       call Envio_usart
       bcf velocity,7         	   
       movf velocity,w
       
       call Envio_usart 
       movf canal_off,w
       
       call Envio_usart 
       movf nota,w
       
       call Envio_usart 
       movf velocity_0,w
      
       call Envio_usart 
       clrw ; deja en 0 a W
ch3
 incf canal_on ; canal 3,4,5
 incf canal_off ; canal 3,4,5
       iorwf velocity1,F ; testea siguiente registro de velocity, linea xxx
       btfsc STATUS,Z
       goto ch4 
       movf canal_on,w
      
       call Envio_usart 
       movf nota,w
        
       call Envio_usart 
      bcf velocity1,7
       movf velocity1,w
       
       call Envio_usart 
        movf canal_off,w
      
      call Envio_usart 
      movf nota,w
      
      call Envio_usart 
      movf velocity_0,w
      
      call Envio_usart 
      clrw ; deja en 0 a W
ch4
 incf canal_on ; canal 3,4,5
 incf canal_off ; canal 3,4,5
       iorwf velocity2,F
       btfsc STATUS,Z 
        goto ch5
       movf canal_on,w
      
      call Envio_usart 
      movf nota,w
     
      call Envio_usart 
	   bcf velocity2,7
      movf velocity2,w
      
      call Envio_usart 
       movf canal_off,w
      
      call Envio_usart 
      movf nota,w
       
      call Envio_usart 
      movf velocity_0,w
      
      call Envio_usart 
      clrw ; deja en 0 a W
ch5
 incf canal_on ; canal 3,4,5
 incf canal_off ; canal 3,4,5
       iorwf velocity3,F
       btfsc STATUS,Z
      goto Main
     movf canal_on,w
   
      call Envio_usart 
      movf nota,w
    
      call Envio_usart 
      incf canal_on ; canal 3,4,5
      bcf velocity3,7
      movf velocity3,w
      
      call Envio_usart 
     movf canal_off,w
    
      call Envio_usart 
      movf nota,w
    
      call Envio_usart 
      movf velocity_0,w
   
      call Envio_usart
       goto Main 

      end 


