; WRITTEN BY: TOM SCARFF ; DATE: 3/11/2002 ; ITERATION: 1.1 ; FILE SAVED AS: panic.ASM ; MODIFICATION: ; ; FOR: PIC16F877 ; CLOCK: 4.00 MHz CRYSTAL ; INSTRUCTION CLOCK: 1.00 MHz T= luS ; PROGRAMME FUNCTION: To read MIDI in, detect panic switch pressed ; and to transmit ' all notes off' it out. list p=16F877 ; tells the assembler which PIC #include "p16F877.inc" ; general register file __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _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. ;******************************** ; Variable Assignment Addresses ; ( from 20h) for PIC16F877 ;******************************** rcvreg equ 20h midich equ 21h count equ 22h count_chan equ 23h count_note equ 24h note equ 25h ;************************************ ; PROGRAMME Reset Point ;************************************ org 00 ; reset vector goto init ;****************************************** ; Receive Data subroutine ;****************************************** rxdata btfss PIR1,RCIF ; test for incoming data goto $-1 movf RCREG,W movwf rcvreg return ;****************************************** ; transmission complete subroutine ;***************+************************** txchar bsf STATUS,RP0 btfss TXSTA,1 ; test for end of transmission goto $-1 bcf STATUS,RP0 return ;****************************************** ; initalise software ;****************************************** init bcf STATUS,RP0 ;page 0 clrf PORTA clrf PORTB clrf PORTC clrf PORTD clrf PORTE bsf STATUS,RP0 ; enable weak pull-up resistors movlw 07h ; rtcc/256 movwf OPTION_REG bcf STATUS,RP0 bsf STATUS,RP0 MOVLW 6 MOVWF ADCON1 ;set port A as digital CLRF TRISA ;all pins outputs movlw 0FFh movwf TRISB ;all pins inputs CLRF TRISD ;all pins outputs movlw 04h ; 0000 0100 movwf TRISE ; bit 2 i/p bcf STATUS,RP0 ; return to page 0 ; Set up USART bsf STATUS,RP0 ; goto page 1 movlw b'10001111' ; RC7 is RX input movwf TRISC ; RC0-3 i/p MIDI Ch. 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 clrw movwf midich clrf PORTA ; set all outputs to '0000 0000' clrf PORTB clrf PORTD movlw 04h ; 0000 0100 movwf PORTE goto start ;************************************************ ; Main Programme ;*********************************************** start btfsc PIR1,RCIF ; test for incoming data goto test_switch movf RCREG,W ; available data movwf TXREG ; so transmit call txchar goto start test_switch btfss PORTB,0 ; switch pressed? goto panic0 btfss PORTB,1; switch pressed? goto panic1 goto start ;---------------------------------------------- panic0 movlw 10h ; 16 channels movwf count movlw 00h movwf midich next_chan movlw 0B0h ; all notes off addwf midich,W movwf TXREG call txchar movlw 07Bh movwf TXREG call txchar movlw 00h movwf TXREG call txchar incf midich,F decfsz count,F goto next_chan goto start ;------------------------------------------------------------ panic1 movlw 10h movwf count_chan movlw 00h movwf midich chan_loop movlw 80h movwf count_note movlw 00h movwf note note_loop movlw 80h addwf midich,W movwf TXREG call txchar movf note,W movwf TXREG call txchar movlw 00h movwf TXREG call txchar incf note,F decfsz count_note,F goto note_loop incf midich,F decfsz count_chan,F goto chan_loop goto start ;============================================================= end