C12 · Lesson 24 · The DAC and waveform generation
Making any voltage, and any waveform, from numbers
PWM fakes an analog level by switching fast; a digital-to-analog converter produces the level itself. Write a 12-bit number to the DAC and its output pin moves to that fraction of 3.3 V. Write a new number at regular intervals from a table, and the output traces a waveform: a sine for a test signal, a ramp for a sweep, a recorded sound. This is how function generators, audio players and signal synthesizers work inside, and the arithmetic of sample rates and table lengths is C9's sampling theory put to work.
- A timer interrupts 8000 times a second; each time, the handler writes the next entry of a 32-entry sine table to the DAC. The output is a 250 Hz sine, drawn in steps on the timeline's dac lane.
- Zoom in to see the steps: each lasts one sample period, 125 µs. A low-pass filter after the DAC would smooth them into a clean sine.
- Change TOP from 1249 to 624: the sample rate doubles, and so does the frequency.
Predict
A 64-entry table is played at 16 kHz. What frequency comes out?
250 Hz: one full table is one period, and 64 samples at 16,000 per second take 4 ms. Doubling the table's length and the sample rate together keeps the frequency and makes each step smaller.
Name it
The DAC
This board's DAC is 12 bits over 0 to 3.3 V:
so a step is 0.81 mV. In C, DAC_DATA = code;; in MicroPython, DAC(0).write(code).
Table-driven waveforms
A table of N samples, written one every , repeats at
A sine table centered on mid-scale holds for an amplitude of A codes. By sampling theory (C9), the output contains the wanted frequency plus images around multiples of ; a low-pass reconstruction filter after the DAC removes them, and the steps with them.
Any frequency: the phase accumulator
To play frequencies that aren't exactly, keep a 32-bit phase that grows by a step each sample and wraps around:
phase += step; // wraps at 2^32: one full period
DAC_DATA = sine[phase >> 27]; // the top 5 bits pick one of 32 entries
This is direct digital synthesis (DDS), the heart of most modern signal generators; its frequency resolution is .
Worked example
What DAC code gives 1.0 V?
Show the solution
- Scale the voltage to a code. so 1241.
- Check what it gives.
Answer code 1241, which gives 1.0001 V.
Worked example
A phase accumulator runs at 10 kHz. What step plays 440 Hz?
Show the solution
- Step. Rounded to a whole number:
- Resolution. so any audible frequency can be set essentially exactly.
Answer a step of 188,978,561.
Worked example
A 32-bit phase accumulator updates at 10 kHz. What is the smallest frequency step it can make?
Show the solution
- One step of the phase increment.
- Where the fine tuning comes from. Direct digital synthesis gets its extraordinarily fine tuning from the accumulator's width: at the same sample rate, each extra bit halves the step.
Answer 2.33 µHz.
Check
What DAC code gives 1.25 V?
Check
The DAC holds 1000. What's its output?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
A 16-sample table is played at 16000 samples per second. What frequency comes out?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
What sample rate plays a 32-sample table at 440 Hz?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
Check
A phase accumulator updates 8000 times a second. What 32-bit step makes 440 Hz?
Check
A full-scale 32-entry sine table centered at mid-scale: what's entry 5?
Check
A DAC plays a sine table at 8 kHz. Which component smooths the steps out of the output?
Check
Make the DAC output MV millivolts (1250 mV), as close as its 12 bits allow, using integer arithmetic.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
In MicroPython, set the DAC to 1.65 V.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
The 12-bit DAC spans 0 to 3.3 V.
(a)
What code gives 1.2 V?
(b)
What is one step?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
Check
A 32-bit phase accumulator updates at 10 kHz.
(a)
What step plays 100 Hz?
(b)
What is its frequency resolution?
Write units the way you normally would: 1.5 mHz, 1500 µHz and 0.0015 Hz are the same answer.
Explain why
Why does a phase accumulator give finer frequency control than stepping through a table one entry per sample?
Stepping one entry per sample can only make frequencies , and other table lengths or sample rates, so most frequencies are out of reach. A phase accumulator advances a 32-bit number by any amount each sample and uses its top bits to choose the table entry, so the table is effectively read at a fractional rate: sometimes the same entry twice, sometimes skipping one. The frequency is times the step over , and since the step can be any integer, frequencies come in steps of , microhertz at audio sample rates. The waveform's shape still comes from the table, so a small table serves every frequency.
Practice
Keep practicing
13 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.