Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MIDI Code Template...Experts Please Review

Status
Not open for further replies.

Jon Wilder

Active Member
16F628 MIDI Code Template...Experts Please Review

Hi there...as I'm learning more about the 16F628 I've decided to make a template for coding MIDI stuff that I know will work for MIDI. Was wanting to get a 2nd set of eyes to look it over and maybe offer advice regarding how I can improve on it or if it's fine the way it is. Any and all suggestions from the experts welcome.

This is a pre-canned template for the 16F628 that I wrote and have tested in a MIDI circuit. Just looking for pointers on how I can improve my coding technique in order to keep from becoming what the pros refer to as a "sloppy programmer".

For those of you who are just starting out in the world of MIDI with the 16F628 are more than welcome to use this template. Everything I do with the 16F628 is purely experimental and is considered open source. I plan to graduate to a higher level PIC uC here in the near future so please feel free to use this template for anything MIDI as it has been tested and it does work.

Code:
;***************************************************************************************************************
;***************************************************************************************************************
;**													      **
;**													      **
;**													      **
;**													      **
;**					 MIDI Software Template						      **
;**					 With 16F628 PIC Micro						      **
;**													      **
;**					         By							      **
;**					                  						      **
;**					     Jon Wilder							      **
;**													      **
;**													      **
;**													      **
;**													      **
;***************************************************************************************************************
;***************************************************************************************************************
;** 													      **
;**					   Processor Config						      **
;**													      **
;**													      **
;***************************************************************************************************************
;***************************************************************************************************************

		processor	16F628
		include		<P16F628.INC>
		
		__CONFIG 0x3D22	;disable cp, dcp, lvp, boden, wdt & enable mclre, pwrt, hs osc

;		external clock speed = 20MHz

;***************************************************************************************************************
;***************************************************************************************************************
;** 													      **
;**					   Port Initialization						      **
;**													      **
;**													      **
;***************************************************************************************************************
;***************************************************************************************************************  
		
		org	0x00		;start at address 0
		clrf	PORTA		;initialize Port A register
		clrf	PORTB		;initialize Port B register
		bsf	STATUS,RP0	;switch to Bank 1
		bcf	STATUS,RP1	;switch to Bank 1
		movlw	B'00000000'
		movwf	TRISA		;set Port A up as all outputs
		movlw	B'00000110'	;set Port B up as all outputs
		movwf	TRISB		;with the exception of RB1/RX and RB2/TX
		bcf	STATUS,RP0	;switch to Bank 0
		bcf	STATUS,RP1	;switch to Bank 0
		movlw	0x07		;disable on board
		movwf	CMCON		;comparator

;***************************************************************************************************************
;***************************************************************************************************************
;** 													      **
;**					   USART Initialization						      **
;**													      **
;**													      **
;***************************************************************************************************************
;***************************************************************************************************************                                       								
		bsf	STATUS,RP0	;switch to Bank 1
		bcf	STATUS,RP1	;switch to Bank 1
		movlw	0x09		;set Baud Rate Generator up for 
		movwf	SPBRG		;31,250 baud with 20MHz crystal
		bcf	TXSTA,SYNC	;enable asynchronous serial comm mode
		bcf	STATUS,RP0	;switch to Bank 0
		bcf	STATUS,RP1	;switch to Bank 0
		bsf	RCSTA,SPEN	;serial port enable
		bsf	RCSTA,CREN	;continuous receive enable

;***************************************************************************************************************
;***************************************************************************************************************
;** 													      **
;**					      Program Start						      **
;**													      **
;**													      **
;***************************************************************************************************************
;***************************************************************************************************************

Main
	
		Program Goes Here

;***************************************************************************************************************
;***************************************************************************************************************
;** 													      **
;**					      Subroutines						      **
;**													      **
;**													      **
;***************************************************************************************************************
;***************************************************************************************************************

Subroutines go here

		End
 

Attachments

  • MIDI Routine Template.asm
    4.6 KB · Views: 107
Last edited:
Hi,

Have you see the Template files provided by Microchip ..?

Found in ...Microchip/Mpasm/Template/Code


Code:
;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F628. This file contains the basic code               *
;   building blocks to build upon.                                    *  
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F628.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************


	list      p=16f628            ; list directive to define processor
	#include <p16f628.inc>        ; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _ER_OSC_CLKOUT & _MCLRE_ON & _LVP_ON

; '__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 DEFINITIONS
w_temp        EQU     0x70        ; variable used for context saving 
status_temp   EQU     0x71        ; variable used for context saving








;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main

; remaining code goes here










		END                       ; directive 'end of program'
 
Hi,

Have you see the Template files provided by Microchip ..?

Found in ...Microchip/Mpasm/Template/Code


Code:
;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F628. This file contains the basic code               *
;   building blocks to build upon.                                    *  
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F628.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************


	list      p=16f628            ; list directive to define processor
	#include <p16f628.inc>        ; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _ER_OSC_CLKOUT & _MCLRE_ON & _LVP_ON

; '__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 DEFINITIONS
w_temp        EQU     0x70        ; variable used for context saving 
status_temp   EQU     0x71        ; variable used for context saving








;**********************************************************************
		ORG     0x000             ; processor reset vector
		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main

; remaining code goes here









		END                       ; directive 'end of program'

Yes I have...but it doesn't include the USART setup routine for a MIDI configuration. I wrote my template specifically for a MIDI configuration with a 20MHz xtal.
 
Hi,

Was trying to show that you should include the ISR entry points, even if not used at this stage, and also use the config statement with individual parameters which are much easier to follow and change than just the config number.
 
Ah yes the configuration word. I mainly did it in hex just as an excersize to ensure that I understand exactly what the configuration word register does in regards to enabling/disabling the chip's special features. I wholeheartedly agree that using mnemonics for that makes it easier to see what's enabled/disabled.

Since I'm still a newbie at this, what exactly is the ISR entry and what is its purpose.
 
Hi,

Well the important thing for now is that the ISR entry point is at memory location 0x004 so your code should not occupy that space - hence the 'goto main' at location 0x000.

The ISR - Interrupt Service Routine is simple but equally compilcated and for now just know that its a routine that allows the Pic to break off from your main program flow and Service an Interrupt generated from one of many internal or external sources, then returning to your main program flow.

When you are ready for Interrupts look at your 628A data sheet and the more general Microchips documents like the 16F reference manual.
You will find masses of info on the Microchip site - the hard part is just finding it !
 
I get it...the ISR entries simply back up the current contents of the W and STATUS registers to allow you to return to the main program flow in a known state. Nice.

I'll be looking up the 16F reference manual. I have a copy of the F628A data sheet but I'm sure there's a bit more information in the 16F series reference manual.
 
Jon, if you are making a template for MIDI projects then it might be good to include the basic functions like send_MIDI_byte and receive_MIDI_byte, these would be used by all programs I think. Even a send_MIDI_command and receive_MIDI_command.

If you are interested I made a very simple MIDI note sender for use as a drum kit, to send MIDI note-on commands when buttons are pushed. The MIDI serial out code was done with bit-banging so it can use any cheap PIC, ie it does not need the PIC USART. You can see details here; MIDI-Bash2 MIDI drum controller source code is in C for PIC 16F876, but the MIDI serial out code is in assembler if I remember right.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top