Buzzer interfacing with atmega32

ASM Code for buzzer testing
; Created: 9/7/2018 8:24:31 AM
; Author : abhay
;
.INCLUDE "M32ADEF.INC"

.ORG 0
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
BACK:
LDI R16,0X01
OUT PORTD,R16
CALL DELAY
LDI R16,0X00
;COM R16
NOP
OUT PORTD,R16
CALL DELAY
call dfor
RJMP BACK
.ORG 0x300
DELAY:
;timing delay
LDI R29,1
L4: LDI R20,10
L1: LDI R21,100
L2: LDI R22,32
L3:
NOP
NOP
DEC R22
BRNE L3
DEC R21
BRNE L2
DEC R20
BRNE L1
DEC R29
BRNE L4
RET
dfor:
;for loop
ldi r30,10
loop: ldi r31,0
call delay
dec r30
brne loop
ret

Calculation for loop timings
crystal frequency = 16Mhz = 16,000,000 Hz
calculate time period of one clock cycle
1/16MHz = 62.5 nano second

clock cycle
NOP – no operation = 1 cycle
DEC – decrement = 1 cycle
BRNE – branch if not equal (basically a for a loop with down counting) = 1/2 cycle

this will be around 5 cycles

for example a delay of 1s
1second = 10^9 nano seconds = 1000,000,000
1000,000,000/(62.5 x 5)=3200,000
3200,00/32=100,000
100,000/100=1000
1000/200=5
5/5=1

so load the register with value
a=32
b=100
c=200
d=5

Leave a Reply

Your email address will not be published. Required fields are marked *