;Sound Beep Demo
;Generate Sound on pin P3.7

;Define Speaker Port & Speaker Pin
SPKER	EQU	P3
SPKBIT	EQU	10000000b

SNDCON	EQU	50		;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
	MOV	R2,#35		;Load Frequency to R2
	MOV	R3,#10		;Load Length  to R3
	LCALL	SOUND
	
LOOP:	LJMP	LOOP		;Loop here

;*** 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
END

