Skip to main content
Engineering LibreTexts

9.3: Waving Quickly

  • Page ID
    35844
  • So what’s all this business about needing to do this sort of thing very fast? One application might be the direct digital synthesis of arbitrary waveforms. The idea is to create a waveform of an arbitrary shape; not just the usual sines, squares and triangles (although that's possible, too). Arbitrary waveforms can be realized using analog oscillator techniques coupled with wave shaping circuits but it can be a challenge to do well. Instead, consider creating a large table of integer values. Typically, the table size would be a nice power of two, like 256. Each entry in the table would be the digitized value of the desired waveform. A simple ramp might look like this:

    unsigned short int ramp_table[] = { 0, 1, 2, 3, /* and so on */};
    

    A more complicated wave might look like this:

    unsigned short int squiggly_table[] = { 0, 21, 15, 33, /* etc */};
    

    These values could then be sent sequentially to a digital-to-analog converter (DAC) to create the desired waveform. Once we get to the end of the table, we simply loop back to the start to make the next cycle. With a 256 entry table, we can use an unsigned char as the table index and once it reaches 255, incrementing it will cause it to roll over back to 0, automatically. The post increment operator is ideal for this. For the code below, assume PORT is the memory location of the DAC we are writing to.

    unsigned char i = 0;
    
    // loop forever
    while ( 1 )
    {
        PORT = ramp_table[i++];
    
        // now wait between each value, dependent on sample rate
        delay();
    }
    
    • Was this article helpful?