	; WRITTEN BY:           TOM SCARFF
       	; DATE:                 20/08/2002
      	; ITERATION:            1.0
      	; FILE SAVED AS:        foot_ped.ASM
      	; FOR:                  PIC16F877
      	; CLOCK:                4.00 MHz CRYSTAL                                        
      	; INSTRUCTION CLOCK:    1.00 MHz  T= luS
      	; PROGRAMME FUNCTION:   Foot pedal 
				; 8 controller and switch i/ps to MIDI out


	list p=16F877           ; list directive to define processor
	#include "p16f877.inc"        ; processor specific variable definitions


	__CONFIG _CP_ALL & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_OFF & _LVP_OFF & _DEBUG_OFF & _CPD_OFF 



; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.





#DEFINE   PAGE0 BCF 3,5       
#DEFINE   PAGE1 BSF 3,5  




;*************************************************************
;	      Constant Assignments
;*************************************************************


CARRY   	EQU    	0       ; CARRY BIT

;**************************************************************
;	  Variable Assignment Addresses
;**************************************************************
;

;
;
temp1           equ     0x20
temp            equ     0x21

port_time	equ	28h
midich		equ	29h
ped_val		equ	2Ah
sw_flags	equ	2Bh
sw_flags_2	equ	2Ch
pedal_val	equ	2Dh

temp_lo		equ	2Eh
temp_hi		equ	2Fh
control_lo	equ	30h
control_hi	equ	31h


base_addr	equ	32h
temp_data	equ	33h
AD_DATA		equ	34h
dlyreg2		equ	35h
dlyreg3		equ	36h

tab0	equ	3Ah
tab1	equ	3Bh
tab2	equ	3Ch
tab3	equ	3Dh
tab4	equ	3Eh
tab5	equ	3Fh
tab6	equ	40h
tab7	equ	41h
ctrller	equ	42h
s


;

  ;************************************
  ;       Initialize EEPROM
  ;************************************

		org 2100h

	dw 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
	




;************************************************************
;       PROGRAMME Reset Point
;************************************************************
              org     00               ; reset vector
              goto    init

	org 04			; interrupt vector
	goto init

;
;--------------------------------------------------------------------------

init       
	 PAGE1 
	movlw 0FFh
	movwf TRISB		;all pins inputs	
        movwf TRISA             ;all inputs
	movwf TRISD
	clrf ADCON1	; Set porta as analogue I/P's
       
          PAGE0    

	

	

	; Set up USART

   	 PAGE1 
           bsf     STATUS,RP0      ; goto page 1
           movlw   b'10001111'     ; RC7 is RX input
           movwf   TRISC	   
           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   b'10010000'     ; async rx 8 bit
           movwf   RCSTA
   
            bsf STATUS,RP0 ; enable weak pull-up resistors	
            movlw 07h        ; rtcc/256
            movwf OPTION_REG
            bcf STATUS,RP0

	  movf PORTC,W ; read midich. switches
          andlw 0Fh
          movwf midich



	goto main
;-------------------------------------------------------------------------

;************************************
;	Switch Debounce Delay (40mS)
;************************************

sw_dbnc	movlw 40h	; Delay routine
	movwf dlyreg2
debnce	call delay3
	decfsz dlyreg2
	goto debnce
	return

;*************************************
;	 Delay Subroutine (770uS)
;*************************************
delay3    movlw .255
          movwf dlyreg3
dly3      decfsz dlyreg3
          goto dly3
          nop
          return





;******************************************
;       transmission complete subroutine
;***************+**************************

txchar  	 bsf       STATUS,RP0
                   btfss TXSTA,1   ; test for end of transmission
                   goto  $-1
                   bcf   STATUS,RP0
                   return

;*********************************************
;       Start A/D conversion Subroutine
;*********************************************
   
convert bsf ADCON0,2     ;set GO/DONE Bit
        btfsc ADCON0,2
        goto $-1

	PAGE1
	rrf ADRESL,F
	PAGE0
	btfss ADRESH,0
	goto shift_r
	PAGE1
	bsf ADRESL,7
	rrf ADRESL,W
	;;;;;;;;;;;;;;;;;;andlw 07Fh
	PAGE0
	movwf temp_lo

shift_r
    	PAGE1
	rrf     ADRESL,W
        andlw   07Fh
	PAGE0
        movwf   temp_lo

	rrf ADRESH,W
	;;;;;;;;;;;;;;;;;;;;;;andlw 07Fh
	movwf temp_hi

        return	

;************************************************************************
;       Output MIDI Pitch Bend data
;***********************************************************************
  
pitchbend 	

	movlw   0E0h	; Pitch-bend + ch.?
	addwf   midich,W  
        movwf   TXREG   ; send chazacter from W
        call    txchar
  

	movf    temp_lo,W
        movwf   TXREG   ; send character from W
        call    txchar
  
        movf	temp_hi,W       
        movwf   TXREG   ; send character from W
        call    txchar
        return

;************************************************************************
;       Output MIDI Control data
;***********************************************************************
  
control	

	movlw   0B0h	; Control change + ch.?
	addwf   midich,W  
        movwf   TXREG   ; send chazacter from W
        call    txchar
  

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;movlw   01h
	movf  	control_hi,W
        movwf   TXREG   ; send character from W
        call    txchar
  
        movf	temp_hi,W       
        movwf   TXREG   ; send character from W
        call    txchar

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	movlw   .33
	movf	control_lo,W
        movwf   TXREG   ; send character from W
        call    txchar
  
        movf	temp_lo,W       
        movwf   TXREG   ; send character from W
        call    txchar

        return
  
;+++++++++++++++++++++++++++++++++++++++++++++++++

 

;*****************************************************
;		Control-on subroutine
;*****************************************************

ctrlon    movf midich,W  
	  addlw 0B0h ; Control Change 
	movwf   TXREG   ; send character from W
        call    txchar
         

          movf  ped_val,W ; controller value
	movwf   TXREG   ; send character from W
        call    txchar
          

          movlw 07fh   ; control on 
	movwf   TXREG   ; send character from W
        call    txchar
          
	  return

;*****************************************************
;		Control-off subroutine
;*****************************************************

ctrloff   movf midich,W  
	  addlw 0B0h ; Control Change 
	movwf   TXREG   ; send character from W
        call    txchar
          

          movf ped_val,W
	movwf   TXREG   ; send character from W
        call    txchar
          

          movlw 00h	; control off 
	movwf   TXREG   ; send character from W
        call    txchar
         
	  return


;*****************************************************
;		Start-on subroutine
;*****************************************************

starton   movlw 0FAh ;Start
	movwf   TXREG   ; send character from W
        call    txchar
          
	  return

;*****************************************************
;		Continue subroutine
;*****************************************************

continue  movlw 0FBh ; Continue
	movwf   TXREG   ; send character from W
        call    txchar
          
	  return


;*****************************************************
;		Stop subroutine
;*****************************************************

stop	  movlw 0FCh ;Stop
	movwf   TXREG   ; send character from W
        call    txchar
            
	  return

;************************************
;	Portamento +time on
;************************************

portmon	  movf midich,W  
	  addlw 0B0h ; Control Change 
	movwf   TXREG   ; send character from W
        call    txchar
          
	movlw .05   ;  Portamento Time control 
	movwf   TXREG   ; send character from W
        call    txchar
      

	movf port_time,W
	movwf   TXREG   ; send character from W
        call    txchar
        

        movf  pedal_val,W ; controller value
	movwf   TXREG   ; send character from W
        call    txchar
        

        movlw 07fh   ; control on 
	movwf   TXREG   ; send character from W
        call    txchar
       
	return

;************************************
;	Portamento off
;************************************

portmoff  movf midich,W  
	  addlw 0B0h ; Control Change 
	movwf   TXREG   ; send character from W
        call    txchar
          

        movf pedal_val,W ; controller value
	movwf   TXREG   ; send character from W
        call    txchar
        

        movlw 00h   ; control off
	movwf   TXREG   ; send character from W
        call    txchar
        
	return
         


;****************************************************************
;		Main Programme
;****************************************************************

   
main	clrf sw_flags
	clrf sw_flags_2

;+++++++++++++++++++++++++++++++++++++++++++++++++++++
;	Read pots on analogue inputs
;+++++++++++++++++++++++++++++++++++++++++++++++++++++

read_pots
	clrf tab0
	clrf tab1
	clrf tab2
	clrf tab3
	clrf tab4
	clrf tab5
	clrf tab6
	clrf tab7

ch0	   movlw B'01000001' ; channel 0
           movwf ADCON0
	   call convert
           call convert
	   movf tab0,W
	   
           subwf temp_lo,W ; (f) - (W)--> (dest)
           btfsc STATUS,Z ; Skip if result -Ve. ie f<W
	   goto ch1 ; new data? no!
	   movf temp_lo,W
	   movwf tab0
	   
	   
	   movlw 07Fh
	   andwf temp_lo
	   call pitchbend
	
           call sw_dbnc

	
   
ch1        movlw B'01001001' ; channel 1
           movwf ADCON0
	   call convert
           call convert
	   movf tab1,W
           
           subwf temp_lo,W ; (f) - (W)--> (dest)
           btfsc STATUS,Z ; Skip if result not same
	   goto ch2 ; no change, so do not o/p data
	   movf temp_lo,W
	   movwf tab1
	   
	   movlw .1	;modulation
	   movwf control_hi   
	   movlw .33  
	   movwf control_lo

	   movlw 07Fh
	   andwf temp_lo
	   call control
	
           call sw_dbnc

          
    
ch2         movlw B'01010001' ; channel 2
            movwf ADCON0
	    call convert
            call convert
	    movf tab2,W
            
            subwf temp_lo,W ; (f) - (W)--> (dest)
            btfsc STATUS,Z ; Skip if result -Ve. ie f<W
	    goto ch3 ; no
  	    movf temp_lo,W
	    movwf tab2
	    
           movlw .7	; volume
	   movwf control_hi   
	   movlw .39
	   movwf control_lo

	    
	    movlw 07Fh
	    andwf temp_lo
	    call control
	
            call sw_dbnc


ch3         movlw B'01011001' ; channel 3
            movwf ADCON0
	    call convert
            call convert
            movf tab3,W
            subwf temp_lo,W ; (f) - (W)--) (dest)
            btfsc STATUS,Z ; Skip if result -Ve. ie f<W            
            goto ch4 ; no!
	    movf temp_lo,W
	    movwf tab3
	    

	   movlw .10	; Pan
	   movwf control_hi   
	   movlw .42  
	   movwf control_lo

	   
	    movlw 07Fh
	    andwf temp_lo
	    call control
	
            call sw_dbnc




ch4         movlw B'01100001' ; channel 4
            movwf ADCON0
	    call convert
            call convert
            movf tab4,W
            subwf temp_lo,W ; (f) - (W)--) (dest)
            btfsc STATUS,Z ; Skip if result -Ve. ie f<W            
            goto ch5 ; no
	    movf temp_lo,W
	    movwf tab4


	   movlw .2	;breath
	   movwf control_hi   
	   movlw .34 
	   movwf control_lo

	  
	    
	    movlw 07Fh
	    andwf temp_lo
	    call control
	
            call sw_dbnc

   
ch5         movlw B'01101001' ; channel 5
            movwf ADCON0
	    call convert
            call convert
            movf tab5,W
            subwf temp_lo,W ; (f) - (W)--) (dest)
            btfsc STATUS,Z ; Skip if result -Ve. ie f<W            
            goto ch6 ; no
	    movf temp_lo,W
	    movwf tab5


	   movlw .8	;balance
	   movwf control_hi   
	   movlw .40  
	   movwf control_lo

	   
	   
	    movlw 07Fh
	    andwf temp_lo
	    call control
	
            call sw_dbnc

	
ch6         movlw B'01110001' ; channel 6
            movwf ADCON0
	    call convert
            call convert
            movf tab6,W
            subwf temp_lo,W ; (f) - (W)--) (dest)
            btfsc STATUS,Z ; Skip if result -Ve. ie f<W            
            goto ch7 ; no
	    movf temp_lo,W
	    movwf tab6


	   movlw .11	;expression
	   movwf control_hi   
	   movlw .43 
	   movwf control_lo

	  
	  
	    movlw 07Fh
	    andwf temp_lo
	    call control
	
            call sw_dbnc

        
ch7         movlw B'01111001' ; channel 7
            movwf ADCON0
	    call convert
            call convert
            movf tab7,W
            subwf temp_lo,W ; (f) - (W)--) (dest)
            btfsc STATUS,Z ; Skip if result -Ve. ie f<W            
            goto sw0 ; no		;;;;;;;;;;;;;;;;;;;;;;;;;;;;;111111111111111
	    movf temp_lo,W
	    movwf tab7


 	   movlw .5	;portamento time
	   movwf control_hi   
	   movlw .37 
	   movwf control_lo

	  
	    	
	    movlw 07Fh
	    andwf temp_lo
	    call control
	
            call sw_dbnc



;-------------------------------------------


sw0	btfss PORTB,7
	goto sw0_on
	goto sw1

sw0_on	movlw .64 ; Sustain
	movwf ped_val
	call ctrlon
	call sw_dbnc

	btfss PORTB,7 ; is sw. released?
	goto $-1
	call sw_dbnc

	movlw .64 ; Sustain
	movwf ped_val
	call ctrloff
	call sw_dbnc

;-------------------------------------------


sw1	btfss PORTB,6
	goto sw1_on
	goto sw2

sw1_on	movlw .66 ; Sostenuto
	movwf ped_val
	call ctrlon
	call sw_dbnc

	btfss PORTB,6 ; is sw. released?
	goto $-1
	call sw_dbnc

	movlw .66 ; Sostenuto
	movwf ped_val
	call ctrloff
	call sw_dbnc

;-------------------------------------------


sw2	btfss PORTB,5
	goto sw2_on
	goto sw3

sw2_on	movlw .67 ; Soft Pedal
	movwf ped_val
	call ctrlon
	call sw_dbnc

	btfss PORTB,5 ; is sw. released?
	goto $-1
	call sw_dbnc

	movlw .67 ; Soft Pedal
	movwf ped_val
	call ctrloff
	call sw_dbnc


;-------------------------------------------


sw3	btfss PORTB,4
	goto sw3_on
	goto sw4

sw3_on	movlw .68 ; Legato Pedal
	movwf ped_val
	call ctrlon
	call sw_dbnc

	btfss PORTB,4 ; is sw. released?
	goto $-1
	call sw_dbnc

	movlw .68 ; Legato Pedal
	movwf ped_val
	call ctrloff
	call sw_dbnc

;-------------------------------------------


sw4	btfss PORTB,3
	goto sw4_on
	goto sw5

sw4_on	movlw .69 ; Hold 2 Pedal
	movwf ped_val
	call ctrlon
	call sw_dbnc

	btfss PORTB,3 ; is sw. released?
	goto $-1
	call sw_dbnc

	movlw .69 ; Hold 2 Pedal
	movwf ped_val
	call ctrloff
	call sw_dbnc

;-------------------------------------------


sw5	btfss PORTB,2
	goto sw5_on
	goto sw6

sw5_on	movlw .65 ;  portamento value
	movwf ped_val
	call ctrlon
	call sw_dbnc

	btfss PORTB,2 ; is sw. released?
	goto $-1
	call sw_dbnc

	movlw .65 ;  portamento value
	movwf ped_val
	call ctrloff
	call sw_dbnc

;-------------------------------------------


sw6	btfss PORTB,1
	goto sw6_on
	goto sw7

sw6_on	
	call starton
	call sw_dbnc

	btfss PORTB,1 ; is sw. released?
	goto $-1
	call sw_dbnc

;-------------------------------------------


sw7	btfss PORTB,0
	goto sw7_on
	goto sw8

sw7_on	
	call stop
	call sw_dbnc

	btfss PORTB,0 ; is sw. released?
	goto $-1
	call sw_dbnc

	
;-------------------------------------------


sw8	btfss PORTD,7
	goto sw8_on
	goto sw9

sw8_on	
	call continue
	call sw_dbnc

	btfss PORTD,7 ; is sw. released?
	goto $-1
	call sw_dbnc

	

;-------------------------------------------

sw9	

	goto ch0         






                

                end

