;Sound All Beep Demo
;Generate Sound on pin P3.7

;Define Speaker Port & Speaker Pin
SPKER	EQU	P3
SPKBIT	EQU	10000000b

SNDCON	EQU	3		;SOUND CONSTANT

	ORG	0000H


INIT:	CLR	EA		;DISABLE ALL INTTERRUPT
	ANL	P3,#11000011B	;CLEAR BIT 2 TO 5

	MOV	A,#0FFH
	MOV	P1,A

;DEMO SOUND
	LCALL	SBEEP		;RESET BEEP
	MOV	R1,#1
	LCALL	DSEC

	LCALL	FBEEP		;DIRECT	FUNCTION KEY BEEP
	MOV	R1,#1
	LCALL	DSEC

	LCALL	XBEEP		;HEX KEY BEEP
	MOV	R1,#1
	LCALL	DSEC

	LCALL	OBEEP		;OPERATE KEY BEEP
	MOV	R1,#1
	LCALL	DSEC


	LCALL	LBEEP		;WARNING BEEP
	MOV	R1,#1
	LCALL	DSEC

	LCALL	RBEEP		;READY BEEP
	MOV	R1,#1
	LCALL	DSEC

	LCALL	EBEEP		;ERROR BEEP
	MOV	R1,#1
	LCALL	DSEC


	LCALL	HBEEP		;SUCCESS BEEP
	MOV	R1,#1
	LCALL	DSEC

	LCALL	UBEEP
	MOV	R1,#1
	LCALL	DSEC

LOOP:	LJMP	LOOP


;*** SOUND ***
;function Generate Sound
;input freq in R2 & Length in R3
;use R2,R4,R3,R5
;R2=freq
;R3=Length
;R4=buffer reload freq form R2
;R5=Length correction
;no return
SOUND:	MOV	A,#SNDCON	;load Length correction
	MOV	R5,A

SNDNX:	MOV	A,R2		;Load freq
	MOV	R4,A		;and save to R4

;Toggle Speaker bit
	MOV	A,SPKER		;Read Port value
	XRL	A,#SPKBIT	;Modify Speaker bit
	MOV	SPKER,A		;and Write back to Speaker Port

;Frequency time delay
SNDLP:	NOP
	NOP
	NOP
	NOP
	NOP
	DJNZ	R4,SNDLP	;test frequency time is done

	DJNZ	R5,SNDNX	;
	DJNZ	R3,SOUND	;test Length time is done

;Set Speaker bit High for protect Transistor PNP
	ORL	SPKER,#SPKBIT	;Set Speaker bit =1
	RET


; ********** nBEEP SUB **********
; ANY BEEP SUBROUTINE
; REG = A,R2,R3,R4,R5,DPTR

FBEEP:	MOV	R2,#50H		;DIRECT FUNCTION KEY BEEP
	MOV	R3,#15H
	LCALL	SOUND
	RET

XBEEP:	MOV	R2,#40H		;HEX KEY BEEP
	MOV	R3,#15H
	LCALL	SOUND
	RET

OBEEP:	MOV	R2,#30H		;OPERATE KEY BEEP
	MOV	R3,#15H
	LCALL	SOUND
	RET

HBEEP:	MOV	R2,#18H		;SUCCESS BEEP
	MOV	R3,#50H
	LCALL	SOUND
	MOV	R2,#12H
	MOV	R3,#60H
	LCALL	SOUND
	RET

LBEEP:	MOV	R2,#90H		;WARNING BEEP
	MOV	R3,#90H
	LCALL	SOUND
	RET
RBEEP:	MOV	R2,#4		;READY BEEP
RBEEP1:	MOV	A,R2
	PUSH	ACC		;-[
	MOV	R2,#70H
	MOV	R3,#15H
	LCALL	SOUND
	MOV	R2,#14H
	LCALL	DMSEC
	POP	ACC		;-]
	MOV	R2,A
	DJNZ	R2,RBEEP1
	RET

EBEEP:	MOV	R2,#90H		;ERROR BEEP
	MOV	R3,#00H
	LCALL	SOUND
	MOV	R2,#0C0H
	MOV	R3,#00H
	LCALL	SOUND
	RET
SBEEP:	MOV	R2,#5FH		;RESET BEEP
	MOV	R3,#20H
	LCALL	SOUND
	MOV	R2,#3FH
	MOV	R3,#20H
	LCALL	SOUND
	MOV	R2,#2FH
	MOV	R3,#40H
	LCALL	SOUND
	RET

UBEEP:	MOV	R2,#0DH		;USER BEEP (37)
	MOV	R3,#60H
	LCALL	SOUND
	RET

; ********** DMSEC SUB ********** 
; DELAY	1/1000 SECOND
; IN  =	R2
; REG =	R2,R3

DMSEC:	MOV	R3,#230			;1 MSEC	LOOP
DMSEC1:	NOP
	NOP
	DJNZ	R3,DMSEC1
	DJNZ	R2,DMSEC
	RET
; ********** DSEC SUB ********** 
; DELAY	SECOND
; IN  =	R1
; REG =	R1,R2,R3,R4

DSEC:	MOV	R2,#10
	LCALL	DTSEC
	DJNZ	R1,DSEC
	RET
; ********** DTSEC SUB ********** 
; DELAY	1/10 SECOND
; IN  =	R2
; REG =	R2,R3,R4

DTSEC:	MOV	R3,#179
DTSEC1:	MOV	R4,#0
DTSEC2:	DJNZ	R4,DTSEC2
	NOP
	NOP
	DJNZ	R3,DTSEC1
	DJNZ	R2,DTSEC
	RET


END

