Buzzer Question

Status
Not open for further replies.

daredavel

New Member
can can someone teach me how to generate buzzer alarm ni pic16f877a? using peizo buzzer 5v.. just a simple alarm will do.. Can you share a sample assembly codes guys? that would be great! thank u!
 
Here's one way you might do it.

Regards, Mike

Code:
        radix   dec

spkr    equ     1               ; spkr on RB0

;--< variables >---------------------------------------------------

delayhi equ     0x20            ; DelayCy() subsystem
beepctr equ     0x21            ; beep subroutine

;--< macros >------------------------------------------------------

clock   equ     4               ; 4, 8, 12, 16, 20 (MHz), etc.
usecs   equ     clock/4         ; cycles/microsecond multiplier
msecs   equ     clock/4*1000    ; cycles/millisecond multiplier

DelayCy macro   delay           ; 11..327690 cycle range
        movlw   high((delay-11)/5)+1
        movwf   delayhi
        movlw   low ((delay-11)/5)
        call    uDelay-((delay-11)%5)
        endm

;******************************************************************
;   Main program                                                  *
;******************************************************************
        org     0x000
v_reset
        call    beep            ; beep                            |B0
        DelayCy(100*msecs)      ; delay 1/10th second             |B0
        goto    v_reset         ; loop forever                    |B0

;******************************************************************
;  beep (32-msec 500-Hz)                                          *
;******************************************************************
beep
        movlw   32              ;                                 |B0
        movwf   beepctr         ; beepctr = 32                    |B0
loop
        movf    PORTB,W         ;                                 |B0
        xorlw   1<<spkr         ;                                 |B0
        movwf   PORTB           ; toggle speaker pin
        DelayCy(1*msecs-6)      ; delay 1-msec minus 6 cycles     |B0
        decfsz  beepctr,F       ; done? yes, skip, else           |B0
        goto    loop            ;                                 |B0
        return                  ;                                 |B0

;******************************************************************
;  K8LH DelayCy() subsystem 16-bit uDelay(11..327690 cycle) sub'  *
;******************************************************************
        nop                     ; entry for (delay-11)%5 == 4     |B0
        nop                     ; entry for (delay-11)%5 == 3     |B0
        nop                     ; entry for (delay-11)%5 == 2     |B0
        nop                     ; entry for (delay-11)%5 == 1     |B0
uDelay  addlw   -1              ; subtract 5 cycle loop time      |B0
        skpc                    ; borrow? no, skip, else          |B0
        decfsz  delayhi,F       ; done?  yes, skip, else          |B0
        goto    uDelay          ; do another loop                 |B0
        return                  ;                                 |B0

;******************************************************************
        end
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…