001-40881 AN2025 Analog Sine Wave Generation With PSoC R 1 Demonstration With CTCSS
001-40881 AN2025 Analog Sine Wave Generation With PSoC R 1 Demonstration With CTCSS
001-40881 AN2025 Analog Sine Wave Generation With PSoC R 1 Demonstration With CTCSS
AN2025 demonstrates how to generate a sine wave using two methods, lookup table and filtering, with PSoC® 1. The
document also shows how to implement a Continuous Tone Coded Squelch System (CTCSS) carrier generator in
PSoC 1. There are three projects associated with this document. The first two show how to generate a sine wave
using the lookup table and filtering methods, and the third project demonstrates CTCSS implementation.
Then Vref = 2.5 V and the output will swing from 0 V (2.5 V Figure 3. Output Waveform
to 2.5 V) to 5 V (2.5 V to +2.5 V). The input data format is
set to offset binary (0 to 254). 6 300
Output Voltage(Volts)
Table 1. DAC Input/Output Table 5 250
DAC Input
4 200
SNo DAC Input Output Voltage(volts) 3 150
2 100
1 0x00 (0) 0V
1 50
2 0x7F (127) 2.5V 0 0
3 0xFE (254) 5V 1 6 11 16 21 26 31 36 41 46 51 56 61
For a sampled system, the sample rate required to Sample No.
produce the desired output frequency is given by the
following formula:
Calculate the maximum output frequency achievable using
the 8-bit DAC as follows:
Let us consider an example to generate a 60-Hz sine
wave using 64 samples. For an output frequency of 60 Hz
and 64 samples, the desired sample rate is 3.84 ksps.
Maximum Sample rate for an 8-bit DAC = 125 Kbps
Using the formula, the desired output frequency may be
generated by varying the sample rate or the number of Maximum No. of samples = 256
samples. Note that reducing the number of samples Maximum Output Frequency achievable = 488 Hz
increases the error in the output sine wave.
A 64-point LUT is created, which resembles a sine wave. Firmware
The formula to create the samples of the LUT is:
After reset, all hardware settings from the device
configuration are loaded into the device, and main.c is
executed. The following operations are performed in
Where, main.c:
= Sample number (0 to 63) Counter16 is started.
= Full scale count for the DAC wrt
Vref(2.5V), which is 127 for DAC8 Counter16 interrupt is enabled.
= Value at which DAC produces a zero cross DAC8 is started at high power.
voltage of sine wave(2.5 V), which is 127 for DAC8
Using the formula given earlier, the following LUT is
Global interrupts are enabled. Any interrupt in the
code will be serviced only if the global interrupt is
created and stored as a ROM array in main.c.
enabled.
const char SineTable64[] = {
127, 139, 152, 164, 176, 187, 198, 208, The pointer to the LUT is initialized to 0.
217, 225, 233, 239, 244, 249, 252, 253,
254, 253, 252, 249, 244, 239, 233, 225, An infinite loop is entered. After that, all the
217, 208, 198, 187, 176, 164, 152, 139, operations take place inside the Counter’s ISR.
127, 115, 102, 90, 78, 67, 56, 46,
37, 29, 21, 15, 10, 5, 2, 1, Reading from the LUT and writing to the DAC8 take place
0, 1, 2, 5, 10, 15, 21, 29, inside the Counter’s ISR. The function Counter_ISR in
37, 46, 56, 67, 78, 90, 102, 115 main.c performs the operations. This function is declared
}; /* 64 samples store in ROM */ as an interrupt handler by using the following code.
#pragma interrupt_handler Counter_ISR;
void Counter_ISR(void)
void Counter_ISR(void)
The value from the LUT that corresponds to the
pointer is read
{
/* Update the DAC with the value in
The DAC is updated with this value
* lookup table pointed the variable
* Pointer */ The Pointer is incremented
115
ROM
TABLE
/* Reset Pointer if greater than or (LUT)
equal to 64 */
if (Pointer >= 64) Pointer = 0; Figure 5. Scope Capture of Output Observed on P0[3]
}
To execute the function on interrupt, a jump to this
function must be done inside the ISR. This is done by
placing the following code inside the _Counter16_1_ISR
inside the Counter16_1INT.asm file.
_Counter16_1_ISR:
Implementation
Let us see how this is implemented in PSoC 1. The project
consists of the following UMs:
BPF
Center_Frequency: A 16-bit PWM UM used to
generate the required square wave. In this case,
it is set to generate a 5-kHz sine wave.
Configuring the BPF UM
BPF4_1 : A four-pole BFP (BPF4 UM) is used to
The BPF4 User Module implements a four-pole BPF. The
achieve the necessary filtering.
center frequency and Q (ratio of center frequency to
bandwidth) are functions of the clock frequency and the
PGA_1: A PGA UM is used for amplifying or ratios of the capacitor values chosen. You can set or
attenuating the input signal to BPF4_1. adjust the center frequency by controlling the sample rate
clock. Any of the classical all-pole filter configurations
Column_Clock: A 16-bit PWM UM is used to (Butterworth, Gaussian, Bessel, and Chebyshev) can be
generate the column clock required for the implemented. The filter output can drive the analog output
BPF4_1 UM (per BPF4_1 UM wizard). bus.
Figure 8. BPF4 Configuration Wizard Figure 9. 5-kHz Square Wave and the Filtered Sine Wave
Filter
Parameters
Advantages
Clock divider Frequency
settings response
Figure 8 shows the BPF4 configuration wizard. Based on 1) Implementation is purely in hardware. The CPU
the design requirements, you can set filter parameters can be used to implement any other functionality.
such as centre frequency, bandwidth, sampling frequency, 2) Obtain higher-frequency sine waves that cannot
gain of the filter, and filter type. be obtained by the DAC method because of the
limitation of the maximum DAC update rate and
CPU overhead.
The clock divider setting shows the clock parameters that
must be set for the analog column to obtain the expected
frequency response. The frequency response plots show Disadvantages
the nominal frequency response and the expected
frequency response.
1) Because of the discrete values of the
capacitance, you may have to use a trial-and-
Because of the discrete nature of the capacitance, there is error method to achieve the right expected filter
a difference between the nominal frequency response and response.
the expected frequency response. The filter parameter
setting should be set such that the expected frequency
response matches the design requirements. In the current Continuous Tone Coded Squelch
example project, which is designed to obtain a 5-kHz sine System (CTCSS)
wave, the filter parameter settings satisfy this condition.
The slight gain provided by the filter is compensated by Continuous Tone Coded Squelch System (CTCSS) or
using same amount of attenuation in the PGA. similar mechanisms are used in most handheld radios to
allow multiple users to share one carrier frequency. The
After all of the UMs are configured, the firmware simply goal of CTCSS is to allow the receiving radio to suppress
consists of starting all the UMs. Connect P0[1] to P0[7] (or squelch) signals that are not intended for its user. If a
externally, and a 5-kHz sine wave is obtained on P0[5], as pair of radios is set to the same CTCSS tone, then the
shown in Figure 9. Change the output frequency by audio transmitted by one radio is received on the other
changing frequency of center frequency PWM. Also, radio’s speaker.
change the center frequency in the BPF UM wizard. CTCSS works by mixing a single tone with the transmitted
voice audio at all times. A radio receiving a signal checks
if the CTCSS tone selected for that radio is present. If the
tone is present, the receiving radio outputs the voice
audio. If the tone is not present, any signal received is not
sent to the speaker. This allows multiple radios to coexist
on the same carrier frequency in the same area without
users having to listen to everyone.
CTCSS Carrier Frequencies In the example project, the Timer8 is set to approximately
10 kHz. The frequency of the output waveform is
CTCSS uses 38 frequencies between 67.0 Hz and 250.3 determined by the step size of the index through the LUT.
Hz as the selection tones. Table 2 lists the frequency For example, for a 10-kHz update rate and a step size of
associated with each of the tones. Some radios use ‘1’, the resulting sine wave has a frequency of about 39 Hz
additional “nonstandard” frequencies for CTCSS (10,000/256). If a step size of ‘2’ is used, the resulting sine
functionality. This application uses only the standard wave has a frequency of twice that, at about 78 Hz. The
CTCSS frequencies. Excel file used to generate the LUT is provided along with
Table 2. Standard CTCSS Frequencies this application note. It contains the options for the clock
frequency and the number of steps in LUT, and generates
Tone Frequency Tone Frequency the LUT based on them.
1 67.0 Hz 20 131.8 Hz The 256-byte LUT is stored in ROM and is accessed using
the MCU’s INDEX instruction. The INDEX instruction
2 71.9 Hz 21 136.5 Hz uses a base address, which is hardcoded as the operand
3 74.4 Hz 22 141.3 Hz in the instruction and an index, which is the value in the
Accumulator at the start of the command. When the
4 77.0 Hz 23 146.2 Hz INDEX instruction is executed, the table entry pointed to
5 79.7 Hz 24 151.4 Hz by the sum of the base address and the index is loaded
into the Accumulator.
6 82.5 Hz 25 156.7 Hz
The 8-bit index for the LUT comes from the upper byte of
7 85.4 Hz 26 162.2 Hz a 16-bit index (iCTCSSFreqIndex in the project). The
8 88.5 Hz 27 167.9 Hz upper byte can be considered as the integer portion of the
index and the lower byte as the fractional portion of the
9 91.5 Hz 28 173.8 Hz index. To step through the LUT, a 16-bit index increment
10 94.8 Hz 29 179.9 Hz (iCTCSSFreqInc in the project), also having an integer
portion and a fractional portion, is added to the 16-bit
11 97.4 Hz 30 186.2 Hz accumulated index. This allows the use of fractional
12 100.0 Hz 31 192.8 Hz increments, which creates a more accurate frequency than
is possible with an integer increment.
13 103.5 Hz 32 203.5 Hz
In the example described earlier, where a step size of ‘2’
14 107.2 Hz 33 210.7 Hz resulted in an output frequency of 78 Hz, the index
15 110.9 Hz 34 218.1 Hz increment is 0x0200. To get an output frequency of 67 Hz
(CTCSS tone 1), a step size of 1.72 is needed. The index
16 114.8 Hz 35 225.7 Hz increment in this case is 0x01b3.
17 118.8 Hz 36 233.6 Hz
Frequency Selection
18 123.0 Hz 37 241.8 Hz
In this project, a function is provided that sets the index
19 127.3 Hz 38 250.3 Hz increment value for the desired frequency. This function,
SetCTCSSFreq(), is passed a 1-byte argument that is
Frequency Generation – Implementation the CTCSS tone number. The tone number is manipulated
(subtract 1 and multiply by 2) to convert it into an index for
In the project accompanying this application note, the
a 38-entry, 16-bit LUT, which contains index increment
CTCSS frequencies are generated by transferring data
values.
from a 256-entry, 8-bit LUT into a DAC6 UM at a fixed
rate. The DAC is updated at a constant rate in an ISR that
is controlled by an 8-bit timer (Timer8 UM). The frequency Phase Coherence
of the CTCSS tone is adjusted by varying the step size A side effect of the way in which the output frequency is
through the LUT. changed (the accumulated index variable is not cleared) is
that the sine wave exhibits no discontinuity when the
The 256-byte LUT contains data that represents one cycle frequency changes. Although this is not critical for CTCSS
of a sine wave. This data is stored in sign and magnitude generation, it may be required in other waveform
format. Sign and magnitude is the native format of the applications.
DAC data register. Writing to the DAC is more efficient in
this format.
ISR Overhead
If the PSoC microprocessor also has other tasks, the
amount of CPU overhead used by the ISR while
generating the CTCSS output is important. Three factors
influence the ISR overhead: the DAC6 analog column
clock speed, CPU clock speed, and Sine_Timer clock rate.
The Sine_Timer ISR involves a write stall when updating
the CTCSS_DAC. A write stall pauses the CPU until the
start of the rising edge of the Phi1 clock in the analog
column. This is done to prevent glitches on the DAC
output. (See the Analog Synchronization Interface section
in the Analog Interface chapter of the Technical Reference
Manual (TRM) for more details on write stall.) The worst-
case stall period for the DAC_Timer period used in this
project is 5.1 sec. Apart from the write stall, the ISR takes
an additional 93 CPU clock cycles to execute.
With a DAC update rate of 10.088 kHz, the worst-case
ISR overhead is 15%. Table 3 shows the ISR overhead for
different CPU clock rates, assuming an analog column
clock of 197 kHz and a DAC update rate of 10.088 kHz.
If different timing is used for this application, the resulting harmonics of the output frequency is the major contributor
ISR overhead differs from those listed in Table 3. to distortion. In this case something more than a simple R-
C low pass filter is needed to clean up the waveform. A
Low Pass Filter User Module can be used.
Frequency Limitation
In some designs, due to limited resources, the source for
Multiple clocks are required to implement this technique. the DAC’s analog column clock must be shared with
The maximum frequency obtainable from this another function. For this, a slower clock must be used.
implementation thus depends on the clocks and on the This affects the ISR overhead; it increases the maximum
user module interdependencies. Consider this block stall time, which in turn increases the waveform distortion
diagram. by moving the DAC clock frequency closer to the output
frequency. Ideally, a frequency close to the DAC6
Figure 11. Block Diagram to Highlight the Clocks Required maximum update rate is used.
ISR Timer
ISR instructions
DAC
Output In this project, a 6-bit DAC is selected over an 8-bit DAC.
(including LUT) Sample
The 8-bit DAC results in more precise steps, but it uses
more analog SoC blocks. Experiments show that the
The ISR timer triggers the ISR, in which the value from the number of steps per waveform has a much greater impact
LUT is loaded into the DAC. The slowest of these three on the accuracy of the output waveform than the DAC
blocks will determine the maximum frequency of the resolution does.
output. The ISR timer cannot be faster than the maximum
update rate of the DAC. The maximum update rate of the
DAC is 750 kHz. Performance
Thus, the output of the timer is limited to the same value After configuring the timers as described in the previous
as the DAC update rate, The ISR overhead for the highest sections and using an external crystal, the data was
frequency of the DAC is the sum of CPU cycles for DAC collected to determine the frequency accuracy and the
stall and 93 CPU clock cycles for the rest of the ISR, as waveform distortion of the output.
explained in the section earlier. The CPU cycles for the
DAC write stall, for an update rate of 750 kHz and CPU The output frequencies were measured for all 48 CTCSS
clock speed of 24 MHz is 32 cycles. tones using a Fluke 87 Digital Multimeter. The results of
these measurements are shown in Table 4. The range of
Thus, the total delay due is 24 MHz/(93+32) = 192 kHz. the frequency error is +0.10% to 0.13%.
The ISR instruction implementation is the slowest of the
three blocks. Therefore, the maximum frequency possible The waveform distortions for selected CTCSS tones (1, 7,
for one output sample is 192 kHz. At least four samples 13, 19, 26, 32, and 38) are calculated based on
are required to create a sine wave. measurements taken with a Hewlett-Packard 3585A
spectrum analyzer. The measurements are taken directly
Therefore, the absolute maximum frequency possible for a from the output of the PSoC microcontroller with no
sine wave output with this implementation is 48 kHz. Also reconstruction filter. The results are graphed in the
note that the accuracy is higher at lower output Waveform Distortion table below. The maximum distortion,
frequencies with higher steps into the LUT, as explained in 3.8%, occurred at the highest output frequency, as
the Trade-offs and Performance sections. expected.
Additional testing was run using a DAC update rate
Trade-offs (Sine_Timer) of 6.5 kHz. The maximum distortion
increased to 6.3%.
Trade-offs that must be made when designing a waveform
generator.
ISR overhead versus DAC update rate is one trade-off that
must be considered. More steps in a wavelength results in
a lower harmonic distortion of the waveform. Fewer steps
in a wavelength reduce the ISR overhead, leaving more of
the CPU available for other tasks.
Waveform distortion versus output-filter complexity is
another part of the same trade-off. If the DAC update rate
is much higher than the frequency of the output waveform,
the major contributor to distortion is at the DAC update
frequency. In this case a simple R-C low pass filter on the
output (often called a reconstruction filter) can acceptably
reduce the waveform distortion. If the DAC update rate is
too close to the frequency of the output waveform, the
4%
3%
2%
1%
0%
50 100 150 200 250 300
Figure 13 and Figure 14 show the waveforms for Tone 1 and Tone 38, respectively. They are captured with a Tektronix TDS
3034 Digital Storage Oscilloscope. Notice that the individual DAC increments are more apparent at the higher frequency. This
is because there are fewer steps per cycle at higher frequencies. The result is greater waveform distortion.
Figure 13. Tone 1 (67.0-Hz) Waveform Figure 14. Tone 38 (250.3-Hz) Waveform
Document History
Document Title: AN2025 – Analog – Sine Wave Generation with PSoC® 1 (Demonstration with CTCSS)
Document Number: 001-40881
PSoC cypress.com/psoc
Power Management ICs cypress.com/pmic
Touch Sensing cypress.com/touch
USB Controllers cypress.com/usb
Wireless Connectivity cypress.com/wireless
All other trademarks or registered trademarks referenced herein are the property of their respective owners.
Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
© Cypress Semiconductor Corporation, 2007-2017. This document is the property of Cypress Semiconductor Corporation and its subsidiaries, including
Spansion LLC (“Cypress”). This document, including any software or firmware included or referenced in this document (“Software”), is owned by
Cypress under the intellectual property laws and treaties of the United States and other countries worldwide. Cypress reserves all rights under such
laws and treaties and does not, except as specifically stated in this paragraph, grant any license under its patents, copyrights, trademarks, or other
intellectual property rights. If the Software is not accompanied by a license agreement and you do not otherwise have a written agreement with
Cypress governing the use of the Software, then Cypress hereby grants you a personal, non-exclusive, nontransferable license (without the right to
sublicense) (1) under its copyright rights in the Software (a) for Software provided in source code form, to modify and reproduce the Software solely for
use with Cypress hardware products, only internally within your organization, and (b) to distribute the Software in binary code form externally to end
users (either directly or indirectly through resellers and distributors), solely for use on Cypress hardware product units, and (2) under those claims of
Cypress’s patents that are infringed by the Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely for
use with Cypress hardware products. Any other use, reproduction, modification, translation, or compilation of the Software is prohibited.
TO THE EXTENT PERMITTED BY APPLICABLE LAW, CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD
TO THIS DOCUMENT OR ANY SOFTWARE OR ACCOMPANYING HARDWARE, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. To the extent permitted by applicable law, Cypress reserves the right to
make changes to this document without further notice. Cypress does not assume any liability arising out of the application or use of any product or
circuit described in this document. Any information provided in this document, including any sample design information or programming code, is
provided only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test the functionality and
safety of any application made of this information and any resulting product. Cypress products are not designed, intended, or authorized for use as
critical components in systems designed or intended for the operation of weapons, weapons systems, nuclear installations, life-support devices or
systems, other medical devices or systems (including resuscitation equipment and surgical implants), pollution control or hazardous substances
management, or other uses where the failure of the device or system could cause personal injury, death, or property damage (“Unintended Uses”). A
critical component is any component of a device or system whose failure to perform can be reasonably expected to cause the failure of the device or
system, or to affect its safety or effectiveness. Cypress is not liable, in whole or in part, and you shall and hereby do release Cypress from any claim,
damage, or other liability arising from or related to all Unintended Uses of Cypress products. You shall indemnify and hold Cypress harmless from and
against all claims, costs, damages, and other liabilities, including claims for personal injury or death, arising from or related to any Unintended Uses of
Cypress products.
Cypress, the Cypress logo, Spansion, the Spansion logo, and combinations thereof, WICED, PSoC, CapSense, EZ-USB, F-RAM, and Traveo are
trademarks or registered trademarks of Cypress in the United States and other countries. For a more complete list of Cypress trademarks, visit
cypress.com. Other names and brands may be claimed as property of their respective owners.