C1224 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

The DAC

This board's DAC is 12 bits over 0 to 3.3 V:

V=code4095×3.3

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 1/fs, repeats at

f=fsN

A sine table centered on mid-scale holds round(2047.5+Asin⁡(2πk/N)) for an amplitude of A codes. By sampling theory (C9), the output contains the wanted frequency plus images around multiples of fs; 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 fs/N 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
f=fsstep232,step=ffs232

This is direct digital synthesis (DDS), the heart of most modern signal generators; its frequency resolution is fs/232.

Worked example

What DAC code gives 1.0 V?

Show the solution
  1. Scale the voltage to a code. 1.03.3×4095=1240.9 so 1241.
  2. Check what it gives. 1241×3.34095=1.0001V

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
  1. Step. Rounded to a whole number: 44010000×232=188978561
  2. Resolution. 10000232=2.3μHz 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
  1. One step of the phase increment. Δf=fs232=1044.29×109Hz=2.33μHz
  2. 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?

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.