User`s manual

RabbitFLEX User’s Manual www.rabbit.com 73
6.4 Software Walk-Through
In this section, we will begin with the speaker_tone.c sample program and modify it to demonstrate
more RabbitFLEX functionality. Note that to make the modifications and run the sample program, you
will need the speaker option, appropriate digital inputs and outputs, and the demo board that is included
with the RabbitFLEX tool kit. However, even if you do not have these items, you should still be able to
read along to gain more understanding of how to use the RabbitFLEX software.
6.4.1 Studying speaker_tone.c
The speaker_tone.c sample program demonstrates how to use the tone driver. For the purposes of
RabbitFLEX, it is not necessary to understand all of the details of this sample program, but essentially it
builds waveforms at different frequencies to be used with the tone driver. The refwave_organ() and
refwave_saw() functions build the waveforms; the buildwave() function adapts the organ and
saw waveforms to different frequencies.
In the following walk-through, we will only be making changes to the main() function, so it is probably
worth studying main() to understand it.
void main(void)
{
char ch; // Holds the user command
char repeat; // Indicates whether or not the tone should repeat
int request_exit; // Indicates if user has requested to exit the program
The above section of code simply declares some variables for later use in main().
// This function must be called to initialize the RabbitFLEX board
brdInit();
The brdInit() function must be called at the beginning of every RabbitFLEX BL300F program. It sets
up internal data structures, initializes analog capabilities, sets up CPU registers appropriately, etc. If this
function is not called, then much of the RabbitFLEX I/O capability will be unavailable.
// This function sets the correct PWM channel to use for the speaker.
// Simply pass a pointer to the speaker structure.
flexSpeakerPWM(&MY_SPEAKER);
The above function is used to set the PWM (Pulse Width Modulation) channel that the speaker uses. This
introduces the idea of using data structures to pass information into the RabbitFLEX functions.
MY_SPEAKER is a macro that has been defined at the top of the program to flex_speaker. So, with-
out the macro definition, this line would be:
flexSpeakerPWM(&flex_speaker);
flex_speaker is a data structure of type Flex_IOPin. These data structures contain all of the infor-
mation that the RabbitFLEX functions need to know about the given pin. In this case, the structure con-
tains the correct PWM channel. Note that instead of passing the data structure itself, we pass a pointer to
the data structure. We will see this again and again in other RabbitFLEX functions.