Pulse Width Modulation
A pulse-width modulator (PWM) circuit is often used as a simple digital to analog converter to produce analog waveforms that require only relatively low frequencies, typically less than 100KHz. Many digital devices like microprocessors and FPGAs use pulse-width modulation to create low-bandwidth analog signals because they require few resources – just a single output pin and a simple passive RC filter. The digital part of a PWM circuit functions by generating a chain of pulses at some fixed frequency, with each pulse potentially having a different width. This digital signal is passed through a simple low-pass filter that integrates the digital waveform to produce an analog voltage proportional to the average pulse width over some interval (the interval is determined by the RC time constant and the pulse frequency). For example, if the pulses are high for an average of 10% of the available pulse period, then an integrator will produce an analog value that is 10% of the Vdd voltage.
The PWM signal must be integrated to define an analog voltage. The integrator 3dB frequency should be an order of magnitude lower than the PWM frequency, so that signal energy at the PWM frequency is filtered from the signal. For example, if an audio signal must contain up to 5KHz of frequency information, then the PWM frequency should be at least 50KHz (and preferably even higher). In general, in terms of analog signal fidelity, the higher the PWM frequency, the better.
The figure below shows a representation of a PWM integrator producing an output voltage by integrating the pulse train. Note the steady-state filter output signal amplitude ratio to Vdd is the same as the pulse width duty cycle (duty cycle is defined as pulse-high time divided by pulse-window time).
The PWM Ã¢â‚¬Å“windowÃ¢â‚¬Â frequency defines the regular, recurring starting point of a pulse. For anything other than a 0V output, there will be a pulse rising edge at the start of every new pulse window. Then the length of the ensuing pulse will determine the resulting analog voltage. Before you design a PWM circuit, you must know (at least) the maximum frequency content and dynamic range of the analog signal you intend to produce, as well as minimum clocking frequency you can use to produce the PWM waveform.
As an example, letÃ¢â‚¬â„¢s look at a PWM signal used to produce a single-channel audio output to drive a speaker at a fast food drive-through window. The design specifications will require that the audio signal have something like up to 10KHz of signal bandwidth, and perhaps a 48dB dynamic range (dynamic range, or DNR, is the ratio between the smallest and largest reproducible output signal). A 48dB DNR requires an 8-bit digital value (an 8 bit number can have 256 values, and 20 x log10(256) = 48dB). So, each pulse window must support a pulse that can have up to 256 different widths.
If we want to preserve 10KHz of bandwidth in the resulting analog signal, we must use a window frequency of at least 10x the analog bandwidth, or 100KHz in this example. Each window must support 256 pulse width end points, so we need to have time slices that are (1/(256 x 100KHz)), or 40ns long. That is certainly possible with an FPGA-based custom circuit based on counters, but that is too short a time slice to be manageable through software running on a processor.
As a second example, letÃ¢â‚¬â„¢s say we want to control the brightness of an LED. In this case, we need to define two things: the maximum frequency at which we want to vary the LED between maximum brightness (totally on) and minimum brightness (totally off); and the number of brightness levels. If we want to ramp the LED between Ã¢â‚¬Å“all offÃ¢â‚¬Â and Ã¢â‚¬Å“all onÃ¢â‚¬Â in one second (seems reasonable!), and if we want 16 different brightness levels (also seems reasonable), then we need a period of 1/(16 x .5Hz), or 125ms. That period is easily manageable by software running on a processor.
Pulse Density Modulation
A Pulse Density Modulator (PDM) circuit is a variation on the PWM that works in a similar fashion. Like a PWM circuit, a PDM circuit drives a digital pulse train on a single digital pin, and a low-pass filter integrates the digital signal to produce an analog signal. But in a PDM circuit, the pulse widths are constant, but the period between the pulses change.
A PDM has the advantage of switching more frequently than a PWM circuit, and so the resulting waveform can have higher bandwidth or better characteristics. Consider a PWM with an 8-bit sample (and so 256 different pulse widths). To achieve an analog voltage that is 50% of Vdd, the PWM would have one pulse that terminates at 128 clock cycles, but the PDM would alternate between high and low on every clock. In trade-off, the PDM signal requires more overhead to produce.
Both PWM and PDM circuits are frequently used in many different digital systems. We will use both in upcoming projects.
The Blackboard PWM IP Block
BlackboardÃ¢â‚¬â„¢s PWM block is programmable over a wide range of frequencies. The main PWM clock is produced by a clock divider that is driven by a 100MHz clock; the divider is enabled by LED_CNTL register bit 4, and the ratio is set by the LED_CNT_FRQ register. The divider sets the PWM_CLK frequency to (100MHz / 2*(PWM_CNT_FRQ)), where PWM_CNT_FRQ can be any number between 0 and 50 million. If PWM_CNT_FRQ is not set, it defaults to 12, and that creates a 4.167MHz PWM_CLK.
The PWM_PR value sets the PWM period (or Ã¢â‚¬Å“window sizeÃ¢â‚¬Â) as a number of PWM_CLKs, and the PWM_DC value sets the duty cycle (or PWM pulse high time) as a number of PWM_CLKs.