PDA

View Full Version : Turn OFF Sound command



Gator_sound
- 18th November 2006, 11:45
Hi all,

I am wondering how its possible to turn off the sound command using 18LF1320. I am creating an alarm clock and I am using the sound command to generate the alarm. I've tried to make the pin connected to the piezo low, i.e. PortB.0 = 0 but no luck. Any ideas? Thanks for the helps in advance. I tried looking through the forums for this topic but no luck.

sayzer
- 18th November 2006, 11:51
Are you saying that, you use SOUND command to drive your piezzo and it works.
Then it continues sounding but you can not stop it?

If this is the case, post your sound command here.


-------------------------------

Gator_sound
- 19th November 2006, 00:56
Sayzer,

I have the code where it goes on for a certain amount and off for half a second but I would like to press a button and the entire sound command turns off, like a regular alarm clock. Relating to the alarm clock the buzzer would go on until you press a button, once you press the buzzer will stop sounding. the following is my code:

INCLUDE "MODEDEFS.BAS"
DEFINE OSC 4

OSCCON = %01100110
ADCON1 = %11111111 'PINS TO DIGITAL OUTPUTS

INPUT PORTB.2 'START BUTTON
INPUT PORTB.4 'STOP BUTTON
OUTPUT PORTB.0 'LED
OUTPUT PORTB.1 'PIEZO

PORTB.1 = 0
PORTB.0 = 0

CONT_SOUND:
IF PORTB.2 == 0 THEN 'turns the alarm on
CONTINUESOUND:
Sound PORTB.1, [1,255] 'Sound command
GoSub CHECKING_STOP
ENDIF

GoTo CONTINUESOUND

CHECKING_STOP:
IF PORTA.4 == 0 Then
Toggle PORTB.0 'LED indication that it has made it to the loop
PORTB.1 = 0
EndIF

Return

STOP_BUZZER:
PORTB.1 = 0
TOGGLE PORTB.0
GoTo CONT_SOUND
End

Gator_sound
- 19th November 2006, 02:24
Sayzer,

I got it to stop going off. I just put the frequency to 0.

For example, SOUND PORTB.1, [0,5]

Than go where ever I need to go. Thanks Sayzer for inquiring about my problem. Feel bad I didn't figure it out in the first place.

sayzer
- 19th November 2006, 08:42
I am not sure how your code is designed to work; may be you just added an example to show the idea here.

I modified your code a little bit as below. May be it is similar to what you need.



INCLUDE "MODEDEFS.BAS"

OSCCON = %01100110 'Set for 4Mhz, but need to check the rest!
TRISA = %00000000 'All outputs.
TRISB = %00010100
'PORTB.2 'START BUTTON
'PORTB.4 'STOP BUTTON
'PORTB.0 'LED
'PORTB.1 'PIEZZO

ADCON1 = %11111111 'All digital


'===========Variables & Hardware assignment=============
ALM_Flag var bit 'Alarm status
LED VAR PORTB.0 'Pin for LED
PIEZZO var PORTB.1 'Pin for Piezzo
Start_BTN VAR PORTB.2 'Pin for Start Button
Stop_BTN VAR PORTB.4 'Pin for Stop Button


'==========Initial Settings=============================
low LED
LOW PIEZZO
ALM_Flag = 0


Start:

IF Start_BTN = 0 THEN
WHILE Start_BTN = 0 : WEND 'Wait for finger release!
ALM_Flag = 1 'Turns the alarm flag ON once.
ENDIF


IF Stop_BTN = 0 Then
WHILE Stop_BTN = 0 : WEND 'Wait for finger release!
ALM_Flag = 0 'Turns the alarm flag OFF once.
ENDIF

IF ALM_Flag = 1 then
Sound PIEZZO, [1,255] 'Run Sound Command
'Depending on the piezzo type you have, you can just use HIGH and LOW commands.
'Example: HIGH PIEZZO 'This will run the piezzo until you LOW the Piezzo pin.
HIGH LED 'LED indicates that Alarm is ON. Or do you want this to Flash?
else
LOW PIEZZO 'Stop sounding.
LOW LED 'Alarm is OFF.
ENDIF

GoTo Start


End




There are couple of things to pay attention.

1. Exactly when do you need to TOGGLE the LED?

2. Are buttons pressed by a user? If yes, "WHILE" will help you to wait for finger release. Check the example.

3. Check your piezzo type. You may just use HIGH and LOW commands to drive it. Thus, couple of lines will be good enough for your code.

-----------------------

Gator_sound
- 22nd November 2006, 08:19
sayzer,

The LEDs are there to indicate that the code went into the sub routine or through the loop. I tried driving the piezo though with the high and low command but it did nothing. Its why I resorted to asking the forum. I think I'll use the while statement though. I was just testing out the different components of my project with respect to their code. I'm doing an alarm clock with some RF technology integrated. I used LEDs to check if they my code was working properly as a way of debugging. I understand its not the most efficient way of doing things but it worked well with small parts of my code.

Thanks for taking the time to revise and update my code. I'll look more into it some more in a bit. I appreciate it.