; File header.asm derived from: 

; File MIDISEND.ASM
; Assembly code for a 10MHz PIC16F84 microcontroller
;
; Description: Send a midi message once every second
; Author: Ross Bencina
; Last Modified: 25 / 8 / 99

;

; CPU configuration
	processor 16f84
	include	  <p16f84.inc>
	__config  _HS_OSC & _WDT_OFF & _PWRTE_ON

; ports (on PORTA)
MIDIOUT	equ 0
WSIN	equ 4

; variables

	
temp	equ	H'1F'
xmit	equ	H'1D'

; Program

	org	0	  ; start at address 0

	
start:	
	bsf	status, rp0
	movlw	B'00010000'
	movwf	TRISA
	bcf	status, rp0	; init port A as output except for ra4
	bsf	PORTA, MIDIOUT	; init midi out pin state 

mainloop:
subloop:
	btfsc	PORTA, WSIN
	goto	subloop
	call	xmitmidi
	goto	mainloop
  
xmitmidi:
	clrf	xmit
; wait 389
	movlw	D'129'
	movwf	temp
loop0:	decfsz	temp,f
	goto	loop0
	nop
; CYCLE 390 INPUT bit 0
	movlw	D'1'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 258
	movlw	D'85'
	movwf	temp
loop1:	decfsz	temp,f
	goto	loop1
; CYCLE 651 INPUT bit 1
	movlw	D'2'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 257
	movlw	D'85'
	movwf	temp
loop2:	decfsz	temp,f
	goto	loop2
	nop
; CYCLE 911 INPUT bit 2
	movlw	D'4'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 257
	movlw	D'85'
	movwf	temp
loop3:	decfsz	temp,f
	goto	loop3
	nop
; CYCLE 1171 INPUT bit 3
	movlw	D'8'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 258
	movlw	D'85'
	movwf	temp
loop4:	decfsz	temp,f
	goto	loop4
; CYCLE 1432 INPUT bit 4
	movlw	D'16'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 257
	movlw	D'85'
	movwf	temp
loop5:	decfsz	temp,f
	goto	loop5
	nop
; CYCLE 1692 INPUT bit 5
	movlw	D'32'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 58
	movlw	D'19'
	movwf	temp
loop6:	decfsz	temp,f
	goto	loop6
; CYCLE 1753 START bit
	bcf	PORTA,MIDIOUT
; wait 79
	movlw	D'26'
	movwf	temp
loop7:	decfsz	temp,f
	goto	loop7
; CYCLE 1833 OUTPUT bit 0
	btfsc	xmit,0
	bsf	PORTA,MIDIOUT
	btfss	xmit,0
	bcf	PORTA,MIDIOUT
; wait 76
	movlw	D'25'
	movwf	temp
loop8:	decfsz	temp,f
	goto	loop8
; CYCLE 1913 OUTPUT bit 1
	btfsc	xmit,1
	bsf	PORTA,MIDIOUT
	btfss	xmit,1
	bcf	PORTA,MIDIOUT
; wait 36
	movlw	D'11'
	movwf	temp
loop9:	decfsz	temp,f
	goto	loop9
; CYCLE 1953 INPUT bit 6
	movlw	D'64'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 37
	movlw	D'12'
	movwf	temp
loop10:	decfsz	temp,f
	goto	loop10
; CYCLE 1993 OUTPUT bit 2
	btfsc	xmit,2
	bsf	PORTA,MIDIOUT
	btfss	xmit,2
	bcf	PORTA,MIDIOUT
; wait 76
	movlw	D'25'
	movwf	temp
loop11:	decfsz	temp,f
	goto	loop11
; CYCLE 2073 OUTPUT bit 3
	btfsc	xmit,3
	bsf	PORTA,MIDIOUT
	btfss	xmit,3
	bcf	PORTA,MIDIOUT
; wait 76
	movlw	D'25'
	movwf	temp
loop12:	decfsz	temp,f
	goto	loop12
; CYCLE 2153 OUTPUT bit 4
	btfsc	xmit,4
	bsf	PORTA,MIDIOUT
	btfss	xmit,4
	bcf	PORTA,MIDIOUT
; wait 56
	movlw	D'18'
	movwf	temp
loop13:	decfsz	temp,f
	goto	loop13
	nop
; CYCLE 2213 INPUT bit 7
	movlw	D'128'
	btfsc	PORTA,WSIN
	addwf	xmit,f
; wait 17
	movlw	D'5'
	movwf	temp
loop14:	decfsz	temp,f
	goto	loop14
	nop
; CYCLE 2233 OUTPUT bit 5
	btfsc	xmit,5
	bsf	PORTA,MIDIOUT
	btfss	xmit,5
	bcf	PORTA,MIDIOUT
; wait 76
	movlw	D'25'
	movwf	temp
loop15:	decfsz	temp,f
	goto	loop15
; CYCLE 2313 OUTPUT bit 6
	btfsc	xmit,6
	bsf	PORTA,MIDIOUT
	btfss	xmit,6
	bcf	PORTA,MIDIOUT
; wait 76
	movlw	D'25'
	movwf	temp
loop16:	decfsz	temp,f
	goto	loop16
; CYCLE 2393 OUTPUT bit 7
	btfsc	xmit,7
	bsf	PORTA,MIDIOUT
	btfss	xmit,7
	bcf	PORTA,MIDIOUT
; wait 76
	movlw	D'25'
	movwf	temp
loop17:	decfsz	temp,f
	goto	loop17
; CYCLE 2473 STOP bit
	bsf	PORTA,MIDIOUT
	return
