Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

UT.6.03x Embedded Systems - Shape the World - Lab 13: Digital-Analog Conversion (DAC)

OVERVIEW:

Our world is both digital and analog. We usually see analog things in real life, such as mechanical watches/clocks, light dimmers, and potentiometers. There are also digital devices, such as digital watches/clocks, switches, and buttons. Therefore, the ability of converting from analog to digital and from digital to analog is vital. In this chapter, we will learn about Digitization, Nyquist Theorem, and Digital-Analog Conversion (DAC).

DIGITIZATION:

Digitization means taking an analog signal and digitize it, using two concepts:

There are two methods of digitization:

digitization

In this chapter, we will cover the rate and analog standpoint. I think how the DAC works is related to how we use Calculus in calculating areas / volumes, because “how faithful is faithful enough?” is similar to “how small is small enough for a subinterval?”.

intuition

NYQUIST THEOREM:

If a signal is oscillating at a frequency f, in order to capture it faithfully, we must sample at a frequency that is strictly larger than 2f

For example, a runner can complete a circular lab within 60 seconds. I want to track that runner to see if he can run 10 laps without taking breaks. However, I’m a lazy person, so I only want to check him just a number of times, but I need to be sure that I miscount his laps. Based on Nyquist Theorem, in order to do that, I have to check him at most every 30 seconds.

DAC:

DAC stands for Digital-Analog Conversion. By default, machines work digitally, which means that they only understand TRUE and FALSE (That’s why the standard machine language only has 1 and 0). On the other hand, our world is represented by analog signals. Thus, in order to produce analog outputs, computers must have DAC. There are many methods for DAC, but in this chapter, we will only talk about Binary Weighted Circuit and R-2R Ladder.

  1. Binary Weighted Circuit:
    Has parallel resistors.
    Resistor values increase as the number of bits decrease. That means the largest resistor goes on the least significant bit.
    As the number of bits decreases, the resistor values get doubled.
    binary-weighted-circuit

  2. R-2R Ladder: Has a T-structure.
    Resistors which have value 2R are placed vertically, whereas resistors which have value R are placed horizontally.
    r-2r-ladder

The more bits of the DAC, the more accurate the analog signal will be. This is what makes me think DAC is similar to Calculus. The more segments we divide a curve into, the straighter each segment will become.

infinitesimal-subintervals

To solve the DAC circuit, we need to draw equivalent circuits in order to simplify the DAC. Then, we will use the voltage divider method to find Vout.

LAB 13:

There is a mistake I made in this lab. It was a deadly mistake, and it took me two days to figure it out. It wasn’t too difficult, but it was imperceptible. It was the integer division.

First, I declared an integer named frequency, which represents the frequency of the notes. frequency had big values, so when I took 1 and perform the / expression, it would return zero. Because / in integer perspective is “divide and take the integer part”, not “divide and take the whole real number”. That was why I couldn’t change the SysTick to vary the output frequency.

PARTS:

ek-tm4c123gxl

breadboard

parts

BREADBOARD LAYOUT:

There is a trick to wire up a DAC. Using to series and parallel laws, we can make a 3-kilohm resistor by wiring two 1.5-kilohm resistors in series. Also, we can create a 6-kilohm resistor by connecting two 12-kilohm resistors in parallel. 1.5 kilohms, 3 kilohms, 6 kilohms, and 12 kilohms altogether make a 4-bit DAC.

breadboard-layout

CODE:

Note that to be able to run and grade the labs, you have to follow the software requirements. Unfortunately, the software and the course don’t support Linux. However, if you follow the official instruction from Texas Instruments, you can upload and run your program on the LaunchPad.

See below for my dac.c, or view all lab files on my GitHub.

/**
 * UTAustinX: UT.6.03x Embedded Systems - Shape the World
 * Lab 13: Digital-Analog Conversion
 *
 * File Name: dac.c
 *
 * Description: Creates a sinusoid sound wave using 4-bit DAC
 * and simulate C, D, E, and G notes of the piano.
 *
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: April 22, 2016
 * Updated: June 23, 2017
 */

// 4-bit DAC connected to PB3-PB0

#include "dac.h"
#include "tm4c123gh6pm.h"

/**
 * Initializes 4-bit DAC.
 */
void InitDac(void) {
    volatile unsigned long delay;

    SYSCTL_RCGC2_R     |= 0x02;           // unlock port B clock
    delay               = SYSCTL_RCGC2_R; // allow time for clock to start
    GPIO_PORTB_LOCK_R  |= 0x4C4F434B;     // unlock GPIO Port B
    GPIO_PORTB_CR_R    |= 0x0F;           // allow change to PB3-PB0
    GPIO_PORTB_AMSEL_R &= ~0x0F;          // disable analog function on PB3-PB0
    GPIO_PORTB_PCTL_R   = 0x00;           // clear PCTL register on PB3-PB0
    GPIO_PORTB_DIR_R   |= 0x0F;           // PB3-PB0 outputs
    GPIO_PORTB_AFSEL_R &= ~0x0F;          // disable alternate function on PB3-PB0
    GPIO_PORTB_DEN_R   |= 0x0F;           // enable digital pins on PB3-PB0
    GPIO_PORTB_DR8R_R  |= 0x0F;           // enable 8-mA drive select on PB3-PB0
}

/**
 * Writes output to DAC.
 *
 * @param  data  4-bit data, 0 to 15
 */
void OutDac(unsigned long data) {
    // write every single value from the SineWave array
    GPIO_PORTB_DATA_R = data;
}

SIMULATOR TESTING:

REAL-BOARD TESTING: