3.1 Sine Wave Generation Using NCO Module

As discussed in Case Study: Intruder Detection System Using Discrete Fourier Transform (DFT) by Correlation, a sine wave of desired frequency can be generated using the NCO module. Code Snippet for Sine Wave Generation Using NCO Module shows a code snippet for generating a square wave at 1 kHz. The square wave generated by this method is passed through a fourth order Sallen Key filter.

Code Snippet for Sine Wave Generation Using NCO Module


     // NCO output should be routed to a GPIO pin using PPS module and the I/O
    // registers and clock configuration bits must be correctly configured
    // prior to this segment of code.
    
    
    // Configure NCO1 to produce 1kHz square signal using 16 MHz clock source
    
    #define F_OSC         16000000UL    // External oscillator frequency (Hz))
    #define F_NCO   	1000UL        // Output frequency of NCO (Hz)
    #define NCO_ACCU_MAX  1048576UL     // 2^20
    
    NCO1CLK     = 0x06;     // PWS = 0 (ignored for Fixed duty cycle operation)
                            // NCO clock source is EXT OSC
    
    NCO1INC = (2*F_NCO*NCO_ACCU_MAX)/F_OSC;
    NCO1CON = 0x80;         // Fixed duty cycle (50%), Polarity = non-inverted
                            // NCO module enabled

Figure   1 shows a block diagram of the system.

Figure 3-3. Block Diagram of Sine Wave Generation Using NCO

The simulation of the Sallen Key filter and the resulting waveforms are illustrated in Figure   2.

Figure 3-4. Simulation of a Sallen Key Filter

The output sine wave along with its resulting frequency spectrum is captured using an oscilloscope, see Figure   3. The fundamental frequency at 1 kHz is the most dominant while the other harmonic frequencies at 2 kHz, 3 kHz, 4 kHz, and so on are negligible or are very small.

Figure 3-5. Frequency Spectrum of a Sine Wave Generated Using NCO Module