Bascom and AVR, Sound


The simplest way of getting sound from an AT90S2313 is to attach a small loudspeaker using a series resistor (experiment!) to an output pin. connect the other side of the speaker to Ground.
Use the Sound statement:
Sound Portpin, Soundtime, High/Low-time
where Soundtime is the number of pulses sent to the output pin and High/Low-time the time the output pin is pulled High, then Low. Bascom's help is not clear on how this is done exactly.
Example:
Sound, Portb.0, 100, 500
will produce a tone of app. 600 Hz during app. 120 milliseconds:


You will have to experiment with Soundtime and High/Lowtime.
(If you want exact frequencies, use a Timer function)

An obvious application is a morse code beacon program:


with this program:
sendmorse.bas
(program source is not shown as it is too long)

Sending speed, tone and code timing is determined from the constant speed. I did not do my utmost to get the timing accurate. It seems to 'sound' ok.
The morse code character set is in the Data block 'Morse_alfabet'. Basically, the program does a Lookupstr to get the morse code string for the character to send from the Data block. It then scans all elements in the morse code character and sounds 'dot' or 'dash' tones. A special case is the '<' character that signifies that no character space is to be sent. Special morse code characters are for instance 'KA', meaning 'Start of text' and 'SK', meaning 'End of text', where the two characters are send without inter-character spacing.

DTMF
Dual Tone Multi-Frequency (DTMF) is used in radio-amateur applications to access repeaters. It is a system where the algebraic summation of two sine-waves with different frequency is sent as a tone to transmit a code. DTMF can be made with:
Dtmfout, Dtmfcode, Dtmftime
where Dtmfcode is a decimal code number from 0 to 15, and Dtmftime is the time the DTMF tone is transmitted in milliseconds.
A variant is:
Dtmfout, String, Dtmftime
where all digits to send are in a string.
An example, sending all 16 DTMF tones:
dtmf.bas
$regfile = "2313def.dat"
$crystal = 4000000

Dim Counter As Byte

Enable Interrupts

Do
  For Counter = 0 To 15
    Dtmfout Counter , 500
    Waitms 250
  Next Counter
  Wait 1
Loop

End
Dtmfout uses Timer1 on interrupt basis, you cannot use Timer1 in another interrupt!
The DTMF output is on OC1, which is PortB.3 (pin 15) on the AT90S2313.

TOC