Square Wave Generator
- Updated2025-01-28
- 7 minute(s) read
Generates a point-by-point square wave using direct digital synthesis (DDS). The synthesis runs continuously using the block diagram clock rate to produce a real-time frequency. The block diagram clock rate is the top-level clock rate, unless the Square Wave Generator Express VI is inside a single-cycle Timed Loop configured with a different clock rate.
During run time, you can use the Normalize Signal Generation Parameters VI to convert frequency, phase, and duty cycle parameters to fixed-point units normalized to the clock rate.

Dialog Box Options
| Option | Description |
|---|---|
| Square Wave Parameters | Contains the following options:
|
| Output | Contains the following options:
|
| Execution Mode | Contains the following options:
|
| Signal Output Preview | Displays a preview of the configured square wave. |
| Clock | Contains the following options:
|
Inputs/Outputs
reset
—
Sets the VI to the initial state determined by phase offset (periods). The VI resets automatically when it first runs.
duty cycle (periods)
—
Specifies the scaled duty cycle, in number of periods, according to the following formula. duty cycle (periods) = duty cycle (%) / 100 To save FPGA resources, leave the duty cycle (periods) input unwired and use the configuration dialog box to specify the duty cycle. If you change the value of the duty cycle (periods) input at run time, the square wave updates on the next period edge. The value you wire to this terminal must be an unsigned fixed-point data type with a 32-bit word length and a 0-bit integer word length. If you wire a data type with a different configuration to this terminal, LabVIEW coerces the configuration to be <+,32,0>.
amplitude
—
Specifies the amplitude of the square wave. The default is 32767, so square wave out is either –32767 or 32767.
offset
—
Specifies the DC offset of the square wave. The default is 0.
frequency (periods/tick)
—
Specifies the scaled output frequency, in number of periods per tick of the FPGA clock, according to the following formula. frequency (periods/tick) = frequency (Hz) / FPGA clock rate To save FPGA resources, leave the frequency (periods/tick) input unwired and use the configuration dialog box to specify the frequency. If you change the value of the frequency (periods/tick) input at run time, the square wave updates on the next call. If you change the frequency, the DDS preserves the position in the period so only the rate changes on the next call. The value you wire to this terminal must be an unsigned fixed-point data type with a 32-bit word length and a 0-bit integer word length. If you wire a data type with a different configuration to this terminal, LabVIEW coerces the configuration to be <+,32,0>.
phase offset (periods)
—
Specifies the scaled phase offset, in number of periods, according to the following formula. phase offset (periods) = phase offset (deg) / 360 To save FPGA resources, leave the phase offset (periods) input unwired and use the configuration dialog box to specify the phase offset. If you change the value of the phase offset (periods) input at run time, the square wave updates on the next call. To avoid glitch conditions, do not change the value of the phase offset (periods) input by more than the value of the frequency (periods/tick) input for each call to the VI. The value you wire to this terminal must be an unsigned fixed-point data type with a 32-bit word length and a 0-bit integer word length. If you wire a data type with a different configuration to this terminal, LabVIEW coerces the configuration to be <+,32,0>.
square wave out
—
Returns a Boolean or an 8-, 16-, or 32-bit integer containing the square wave output. If the Square Wave Generator Express VI is inside a single-cycle Timed Loop, square wave out is a single sample point. If the Square Wave Generator Express VI is outside a single-cycle Timed Loop, square wave out updates after each edge of the square wave. |
Amplitude is the distance between the offset and peak. In the following example, the offset is 5 and the amplitude is 10.
To pass the square wave to an output line, you can wire square wave out to an I/O resource using the FPGA I/O Node.
Over time, the Express VI returns the expected period length and frequency. However, some jitter might occur. For example, suppose you want to create a 6.25 MHz square wave using a 40 MHz top-level clock. In this case, a clock cycle is 25 ns and a square wave period is 160 ns, so the average square wave period should be 6.4 clock cycles long. Each square wave period is an integer number of cycles, but the Express VI returns period lengths of 6, 6, 6, 7, and 7. As a result, the average period length is 6.4 clock cycles, and the average frequency is 6.25 MHz.
If you place the Express VI outside a single-cycle Timed Loop and run the FPGA VI on a development computer, the timing is not precise. So, the Express VI returns a square wave that alternates between the high and low value.
Examples
Refer to the following example files included with LabVIEW FPGA Module.
- labview\examples\CompactRIO\FPGA Fundamentals\FPGA Math and Analysis\Generation\Square Wave\Square Wave.lvproj
- labview\examples\R Series\FPGA Fundamentals\FPGA Math and Analysis\Generation\Square Wave\Square Wave.lvproj
reset
—
duty cycle (periods)
—
amplitude
—
square wave out
—