	;Expt6.asm
        ;Project: Creating a tune
	;List P = 16F84
	;#include <p16F84.inc>
	;__CONFIG 1Bh    ;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC


	ORG 0		;This is the start of memory for the program.
SetUp	BSF 03,5	;Go to Bank 1
	CLRF 06		;Make all port B output		
	BCF 03,5	;Go to Bank 0 - the program memory area.	
	CLRF 06		;Clear outputs of junk 	
SetUp1	MOVLW 01	;
	MOVWF 0Ch
	GOTO Main

Table	ADDWF 02h,1	
	RETLW 0A8h	;duration  -  168 loops
	RETLW 5Bh	;G  -  392Hz  1.27mS HIGH,LOW - 91 loops	
	RETLW 0FAh	;duration  -  250 loops
	RETLW 6Bh	;E  -  330Hz  1.51mS HIGH,LOW - 107 loops	
	RETLW 46h	;duration  -  70 loops
	RETLW 6Bh	;E  -  330Hz
	RETLW 54h	;duration  -  84 loops
	RETLW 5Bh	;G  -  392Hz
	RETLW 5Eh	;duration  -  94 loops
	RETLW 51h	;A  -  440Hz  -  1.13mS HIGH,LOW - 81 loops
	RETLW 0FCh	;duration  -  252 loops
	RETLW 7Ah	;D  -  292Hz  -  1.71mS HIGH,LOW - 122 loops
	RETLW 0FFh	;End of table
	RETLW 0FFh	;End of table

Delay	NOP		;Create 10uS delay
	NOP		
	NOP
	NOP
	NOP
	NOP
	RETURN

Delay2 	CALL Delay	;Create 3mS delay		
	DECFSZ 1A,1
	GOTO Delay2
	RETURN
	
Delay3	NOP		;250mS delay
	DECFSZ 1A,1
	GOTO Delay3
	DECFSZ 1B,1
	GOTO Delay3
	RETURN

Main	DECF 0C,1	;Dec jump value to re-look at values		
	MOVF 0Ch,0	;Copy jump-value into W
	CALL Table	;Return with table-value in W
	MOVWF 0F	;Length of note into file 0F
	INCF 0Ch,1	;Increment the table-value
	MOVF 0Ch,0	;Copy jump-value into W
	CALL Table	;Return with table-value in W
Main1	MOVWF 0D	;Frequency of note into file 0D
	MOVWF 0E	;Frequency of note into file 0E
Main2	BSF 06,7
	CALL Delay	;Create HIGH time
	DECFSZ 0D,1	;Each loop = 14uS
	GOTO Main2	
Main3	BCF 06,7	
	CALL Delay	;Create HIGH time
	DECFSZ 0E,1
	GOTO Main3
	DECFSZ 0F,1	;Length of note
	GOTO Main1
	BCF 06,7
	CALL Delay2	;3mS between notes
	CALL Delay2	;3mS between notes
	CALL Delay2	;3mS between notes	
	INCF 0Ch,1	;Increment pointer to next value in table
	MOVF 0Ch,0	;Copy jump-value into W
	CALL Table	;Return with table-value in W
	MOVWF 10h	;Put "end of table" into file 10h
	MOVLW 0FFh	;Check for 'end of table'
	XORWF 10h,0	;Compare file 10h with FF (result in W)
	BTFSC 03,2	;Look at Zero flag in status file
	GOTO Main4	;Start again
	INCF 0Ch,1	;Increment the table-value	
	GOTO Main	;Go to next note	
Main4	CALL Delay3
	CALL Delay3
	CALL Delay3	
	GOTO SetUp1

	END		;Tells assembler end of program


