Generating Tones Example
Last Modified: 2006-11-07
find:

basket

Acroname Robotics  
 

Contents

Introduction

In this example, a BrainStem GP 1.0 outputs a square wave on a digital IO pin.  The square wave is at an audible frequency and can be used to drive an audio transducer. 

A reflex loop can turn a digital IO pin on and off quickly enough to create audible tones.  Timing parameters for reflexes may be stored in the Scratchpad.  By altering the timing parameters, different tones can be generated. 

Source Code - Reflex for Creating Square Wave

The reflex routine uses two timers, a reflex enabler, and a digital output.  One timer determines how long the output pin is high.  The other determines how long the output pin is low.  Each timer gets its value from the same location in the Scratchpad so the high and low periods are the same.  A reflex enabler makes it easy to stop the tone reflex. 

The reflex source code consists of two files.  The first file has macro defined values that determine what IO the reflex will use.  This file may be included in a TEA program that implements the reflex. 

/* filename: tonedef.tea */ #define thisMODULE 2 #define doutTONE 0 #define tonePAD 0 #define toneENA 0 #define msgON 0 #define msgOFF 1 #define msgTON 2 #define msgTOFF 3 #define msgCHK 4 #define vecCHK 100

The second file is the leaf file that defines the reflex.  The reflex can be compiled by entering leaf "tone" from the Console.  It can be loaded by entering batch "tone.bag" from the Console. 

/* filename: tone.leaf */ #include <aCmd.tea> #include <aGarciaDefs.tea> #include <aGPReflexes.tea> #include <aMotoReflexes.tea> #include "tonedef.tea" module[thisMODULE] { message[msgON] { thisMODULE, cmdDIG_IO, doutTONE, 1 } message[msgOFF] { thisMODULE, cmdDIG_IO, doutTONE, 0 } message[msgTON] { thisMODULE, cmdPTMR_SET, 21, tonePAD } message[msgTOFF] { thisMODULE, cmdPTMR_SET, 22, tonePAD } message[msgCHK] { thisMODULE, cmdRFLXE_CHK, toneENA, 0 } vector[aGP_RFX_TIMER_21] { msgCHK } vector[vecCHK] { msgTOFF, msgON, } vector[aGP_RFX_TIMER_22] { msgTON, msgOFF, } }

Source Code - TEA Main Calling Routine

The following TEA program uses the reflex to generate some tones.  First, the digital IO pin for the tone is set to an output.  The subroutine tone_out plays a tone.  It writes to a reflex enabler port and sets it as enabled.  This will let the reflex run continuously.  Then it writes the tone input parameter to the scratch pad.  That parameter specifies half the period of the square wave in 0.1ms units.  The frequency in Hertz will be 1 / (2 * 0.0001 * tone).  The routine launches the reflex by writing to a raw input port.  A sleep statement using the t input parameter lets the tone reflex run for a while.  Finally, the routine sets the reflex enabler to be disabled.  This stops the tone.  There is some latency between disabling the reflex and when it actually stops since the reflex must cycle to the point where it checks the reflex enabler.  Because of this latency a pause is necessary before playing another tone.  This pause should be greater than twice the value of the last tone parameter.  Attempting to launch the reflex while it is already running may cause a timer conflict error.  Entering a tone parameter of 0 will also cause an error.  The final sleep delay in the tone_out routine ensures that tone parameters between 1-49 will work. 

/* filename: tone.tea */ #include <aCore.tea> #include <aDig.tea> #include "tonedef.tea" void tone_out(int tone, int t) { /* re-enable the reflex, set tone period, start reflex */ aCore_Outportc(aPortRflxEna + toneENA, (char)(0x80 | vecCHK)); aCore_Outporti(aPortScratch + tonePAD, tone); aCore_Outportc(aPortRawInput + 100, 0); /* emit tone for a while */ aCore_Sleep(t); /* disable the reflex */ aCore_Outportc(aPortRflxEna + toneENA, 0); aCore_Sleep(100); } void main() { int i; /* configure the output pin */ aDig_Config(doutTONE, ADIG_OUTPUT); /* emit several 0.25sec tone bursts */ for (i = 1; i < 30; i++) { tone_out(i, 250); } }

Comments

The techniques in this example may be used to generate sound effects, create buzzes or clicks, play tunes, or provide audible sensor feedback.  A piezo audio transducer may be placed between the digital output and ground.  A magnetic audio speaker will require a driver or amplifier.  In addition to creating sounds this reflex may also be used to flash lights. 

Revision History:

  • 2003-11-17: Example Created.
 

Related Examples:

Using Reflexes to configure IR transmit, receive and triggering a TEA program.

voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved.