; beep.asm
; load a counter, decrement it, toggle the line and start over
; This program simply drives a piezo element connected between PB0 and Ground.
; You may try connecting between PB0 and Vcc to get more drive.
; Try a Murata PKM13EPY-4000 piezo.  It makes a great annoyance device.
; Play around with the delay tone constant to see what works best.
; I believe that the bit toggle routine illustrates an efficient algorithm
; using the AVR instruction set.
; 
; Written by Brian Hammill 5/98
; This code is given as is and placed in the public domain.  No warranty of any kind
; is implied or given.  Questions or comments about the code may be directed
; to me via Email: hammill@email.exide.com.  I don't promise to support this
; code, but will offer suggestions as my schedule allows.  Good luck. 


.INCLUDE "1200def.INC"
RESET:
	SBI DDRB, 0
BEEP:
	
; set up port B, 0 as an output.  We have piezo buzzer connected to PB0.
	;SBI DDRB, 0	
	
	LDI R19, 247 ; Beep tone constant.
;This loop takes 3 cycles to execute so the beep tone freq is
; f = XTAL/(3*R19)
; f = 3686400/(3*64) = 19200 Hz
		
BEEP_LOOP1:
	dec R19
	brne BEEP_LOOP1
	
	; toggle the bit after count goes to 0
	SBIS PORTB, 0	; Skip if bit is set
	rjmp set_bit0
	CBI PORTB, 0	; Clear if set
	rjmp BEEP


set_bit0:
	SBI PORTB, 0
	rjmp BEEP

	
	; Check whether bit is set or clear and change to opposite.  Want to generate
	; approximately 4KHz signal on PB0.  Toggle line every 921 machine cycles.
	; 3686400/(230*4) = 4006
	
