View Full Version : Re: quick fix for sound command?

- 9th July 2004, 01:44
I am writing a tune using the sound commands. Sound 114 is too low, Sound 115 is too high. Sound 114.5 would be perfect but of course not allowed. Do you know a simple way to "tweak" this 1 note on this 1 line and return it to normal on the next line? Thank you.

You won't be able to tweak the Sound Command, but you can easily create a routine to output any note you want of any duration...

SpeakerPin var PortB.0 ' Assign the Port of your choice here

CounterA var Word
PulseTime var Word
PulseRepeat var Word

.. ..

' Example 1kHz for 1 Second
' The Period of 1kHz is 1mS (total)...
' That's 500uS on, 500uS off
' and there's 1000 of them in one second so...

PulseTime=500:PulseRepeat=1000:Gosub MakeSound

' Example 2200Hz for 500mS
' Period of 2200Hz is 454uS
' That's 227uS on and 227uS off
' and there's 1100 of them in 500mS

PulseTime=227:PulseRepeat=1100:Gosub MakeSound

.. ..

For CounterA=1 to PulseRepeat
High SpeakerPin
PauseUS PulseTime
Low SpeakerPin
PauseUS PulseTime
Next CounterA

There are many other ways of approaching this (eg the PulseOut Command)... and if you have a fast PIC, you can create a Pause routine of your own with a resolution better than 1uS, which will reduce the errors (as my 2200Hz example actually produces 2202.6Hz - in theory, in practice it'll be somewhat lower).

See also my example September 2003 'Sound Command with 12-Bit MCU's.


PS. Folks may not have noticed but I originally had PAUSE instead of PAUSEUS in the subroutine... obviously Pause has a lower limit of 1mS and we are talking about uS delay times, so of course it has to be PauseUS.