App Note 315 - PIC32 Embedded Audio Sinewave Generator
App Note 315 - PIC32 Embedded Audio Sinewave Generator
1. Purpose
This application note illustrates how to create an embedded audio sinewave generator using an Anadigm PIC32 development board. Two potentiometers control the amplitude and frequency, while an LCD displays both values.
The amplitude can be controlled between ±25 mV and ±3 V, and the frequency between 20 Hz and 20 kHz on a logarithmic scale. The application uses the Okika DualApex PIC32 development board and can also be implemented on the QuadApex board.
Source revision: 1.0.0. Software versions and programming steps below describe the original application.
2. The Hardware
2.1 AnadigmDesigner2 Circuit
The audio sinewave generator circuit in AnadigmDesigner2 (AD2) consists of an OscillatorSine CAM and a GainInv CAM.
The OscillatorSine CAM generates a sinewave with a fixed amplitude of 2.5 V. The GainInv CAM controls the final amplitude. Although the OscillatorSine CAM provides some amplitude control, its range is limited, so a separate GainInv CAM is used.
Note: Figure 1 shows a second, empty FPAA because the DualApex board has two FPAAs. The board can also be configured to use just one FPAA.
Figure 2 shows the clock settings for the primary circuit.
Both the OscillatorSine and GainInv CAMs use a 100 kHz clock in the primary circuit. The application varies the clock frequency as the sinewave frequency changes to create the smoothest output waveform.
Figure 3 shows the initial CAM parameter settings. During operation, the application varies the frequency of OscillatorSine and the gain of GainInv.
2.2 The Board Set-up
Add two 10 kΩ potentiometers to the DualApex board. Connect each potentiometer between +3.3 V and ground, with the wipers connected to ADC channels 0 and 1 as shown in Figure 4.
This application uses an output buffer provided on the DualApex board. The buffer is populated with surface-mount components on the underside of the board and implements a unity-gain low-pass filter with a corner frequency of approximately 22 kHz.
The buffer converts the FPAA’s differential output, referenced to +1.5 V, into a single-ended, ground-referenced buffered output.
- Push the DIP switches labelled S15 to the right to connect output #3 of FPAA #1 to the output buffer.
- Position jumper J6 to the left to enable the −3.3 V supply for the output buffer.
The hardware is now ready for the PIC32 microcontroller to be programmed.
3. The Software
3.1 IDE and Compiler
The original project uses the Microchip XC32 compiler, version 1.34, with MPLAB IDE version 8.92. The note also identifies MPLAB X IDE as an alternative. Refer to Microchip’s installation instructions for the chosen IDE and compiler.
3.2 Creating the Project
First, generate the code for the CAM algorithms and API in AnadigmDesigner2:
- Open the AD2 circuit supplied with the original application package and save it in the project directory.
- Open Dynamic Config > Algorithmic Method, then click Generation Options.
- Select the Clocks tab and check FPAA1. This enables code generation for CAM clock management. Click OK.
- Click CAM Functions and expand the tree on the left.
- Select GainInv1 and check
double setGainInv(double G). The other function is not required. - Select OscillatorSine1 and check
void setOscillatorSine(double Fo, double A). The other functions are not required. Click OK. - Set the destination to the directory containing the AD2 file and project code. Click Generate, then Close when generation is complete.
AD2 generates these four files:
CAMCode.cCAMCode.hApiCode.cApiCode.h
Place SigGen32.c and SigGen32.h from the original application package in the same directory.
Required project files: The PDF describes these accompanying files but does not include the AD2 design or the SigGen32 source files. Obtain them before attempting to build the application.
In the original MPLAB IDE, create and build the project as follows:
- Open Project > Project Wizard and click Next.
- Select PIC32MX440F512H from the Device list, then click Next.
- Select the Microchip XC32 C-Compiler Toolsuite as the Active Toolsuite and click Next.
- Browse to the project directory, enter a project name, and click Next.
- Select the four AD2-generated files and the two SigGen32 project files. Click Add >>, then Next and Finish.
- Select Project > Build All or click the Build All icon.
- If warnings mention Debug or Debugger, select Project > Build Configuration > Release and build again.
3.3 Programming the Board
After compilation, program the generated .hex file into the board’s PIC32 microcontroller. The five-pin programming header, P7, is at the top right of the board and has the following labelled pins:
- PGC
- PGD
- GND
- VDD
- VPP
The original note specifies a Microchip PICkit2 as an example programmer. Connect the programmer to the corresponding labelled pins and program the .hex file into the on-board PIC32.
3.4 Running the Application
- Power up the board. A green LED indicates that power is present.
- Check the second green LED next to the microcontroller, which indicates that the FPAAs have been configured correctly.
- The LCD displays Frequency and Amplitude.
- Monitor the output at the bottom right of the board and turn the potentiometers to vary amplitude and frequency.
The amplitude range is ±25 mV to ±3 V. The frequency range is 20 Hz to 20 kHz, with logarithmic frequency control implemented in software.
Note: The output amplitude may decrease as the frequency approaches 20 kHz because the board’s output buffer is configured as a low-pass filter with a corner frequency of approximately 22 kHz. Refer to the board manual for instructions on changing this corner frequency.
3.5 Changing the Application
Modify the AnadigmDesigner2 circuit to add functionality and adapt the application code accordingly. For assistance, contact Okika Devices Support.
