Generating sound
In microcontroller systems, beeper is used for indicating certain occurrences, such as push of a button or an error. To have the beeper started, it needs to be delivered a string in binary code - in this way, you can create sounds according to your needs. Connecting the beeper is fairly simple: one pin is connected to the mass, and the other to the microcontroller pin through a capacitor, as shown on the following image.
As with a button, you can employ a macro that will deliver a BEEP ROUTINE into a program when needed.
Macro BEEP has two arguments:
BEEP macro freq , duration:
freq: frequency of the sound. The higher number produces
higher frequency
duration: sound duration. Higher the number, longer
the sound.
Example 1: BEEP 0xFF, 0x02
The output has the highest frequency and duration
at 2 cycles per 65.3mS which gives 130.6 mS
Example2: BEEP 0x90, 0x05
The output has a frequency of 0x90 and duration of
5 cycles per 65.3mS. It is best to determine these macro parameters
through experimentation and select the sound that best suits the
application.
The following is the BEEP Macro listing:
BEEP MACRO FREQ, DURATION
MOVLW FREQ
MOVWF BEEP_TEMP1
MOVLW DURATION
CALL BEEPSUB
ENDM
BEEPINIT MACRO
BCF BEEPPORT
BSF STATUS, RPO
BCF BEEPTRIS
BCF STATUS,RPO
ENDM
BEEPSUB MOVWF BEEP_TEMP2;SET THE SOUND DURATION
CLRF TMRO ;INITIALIZE THE COUNTER
BCF BEEPPORT
BSF STATUS,RPO
BCF BEEPPORT
MOVLW PRESCBEEP ;SET PRESCALER FOR THRO
MOVWF OPTION_REG ; OPTION <- U
BCF STATUS,RPO
BEEPA
BCF INTC0N,T0IF;CLEAR THRO OVERFLORA FLAG
BEEPB
BSF BEEPPORT
CALL B_WAIT ;DURATION OF LOGICAL "1"
BCF BEEPPORT
CALL B_WAIT ;DURATION OF LOGICAL "0"
BTFSS INTCON,TOIF;CHECK THRO OVERFLORA FLAG,
GOTO BEEPB ;SKIP IF SET
DECFSZ BEEP_TEMP2,1 ;IS BEEP_TEHP2 = 0 ?
GOTO BEEPA ;IF NOT, JUMP BACK TO BEEP
RETURN
B_WAIT
MOVFW BEEP_TEMP1
MOVWF BEEP_TEMP3
B_WAITA
DECFSZ BEEP_TEMP3,1
GOTO B_WAITA
RETURN
The following example shows the use of a macro in a program. The program produces two melodies which are obtained by pressing T1 or T2. Some of the previously discussed macros are included in the program.
PROCESSOR 16f84
#include "P16F84.inc"
__CONFIG _CP_0FF & _WDT_0FF & _PWRTE_0N & _XT_0SC
Cblock OxOC ;RAM strating address
PRESCraait
Beep_TEHPl ;Belongs to macro "BEEP"
Beep_TEHP2
Beep_TEHP3
HIcnt ;Auxiliary variable for macro pausems
LOcnt
LOOPcnt
endc
#define BEEPport PORTA,3 ;Port and pin beeper is located at
if define BEEPtris THIS A, 3
ORG 0x00 ;Reset vector
goto Main
ORG 0x04 ; Interrupt vector
goto Main ;no intertupt routine
include "Romux_lib.inc"
include "button. inc "
include "beep. i nc "
Main ;Beginning of the program
banksel TRISA
movlw b'00010111 initializing port A
ncvwf TRISA " ;TRISA <- 0x17
banksel POSTB
Loop
BEEPinit ;Initialising Beeper
button PORTA, 0,0, Playl ;Button 1
button PORTA, 1,0, Play2 ;Button 2
goto Loop Playl
BEEP OxFF, 0x02
BEEP 0x90, 0x05
BEEP OxCO, 0x03
BEEP OxFF, 0x03 ;First tune
goto Loop PI ay 2
BEEP Oxbb, 0x02
BEEP 0x87, 0x05
BEEP 0xa2, 0x03
BEEP 0x98, 0x03 ;Second tune
goto Loop
End ;End of program
User Comments
No Posts found !Login to Post a Comment.