반도체 AI 보안 인더스트리 4.0 SDV 스마트 IoT 컴퓨터 통신 특수 가스 소재 및 장비 유통 e4ds plus

[Serial] ST Yuji Kawano Engineer (21) - Can MCU generate sine and cosine waves?

기사입력2022.11.14 14:00

Generate triangle wave without using MCU or RAM

Generate y=ax+b using software, increasing x in a constant cycle
After preparing the arbitrary waveform and RAM data table, the DAC outputs this value.

[Editor's Note] Generally, when we think of semiconductors, we tend to think of semiconductors that are familiar to the general public, such as the CPU and memory of computers. On the other hand, MCUs (Micro Controller Units), which are used as core semiconductors for operating electronic products, are semiconductors that are still unfamiliar to the general public, even though they are commonly used in all electronic products that we easily come across. These MCUs have recently been in the media due to the semiconductor shortage, and have begun to attract the attention of the general public. Accordingly, our magazine has prepared a place to learn about MCUs through a series of articles by Yuji Kawano, Manager of ST Microelectronics, a company specializing in MCU semiconductors.

■ Question


Can the MCU generate sine and cosine waves? Can the frequency also be changed?

■ Answer

If your MCU has a built-in digital-to-analog converter (DAC), you can use the MCU to generate sine or cosine waves. The DAC can output any analog voltage within a specified range. For example, if the DAC supports 10 bits of resolution, it can express voltages in the range from 2 to the power of 2, that is, in steps of (1,024) 1 = 1,023, and the output voltage will correspond to the value assigned to the register by the user (less than 1,023). The frequency can be changed by changing the output cycle.

To generate a waveform, you must first store a table of voltages to be output in RAM. After that, you must make the DAC output the values sequentially and periodically. You may also be interested in whether it can output arbitrary waveforms of other shapes, in addition to sine and cosine waves.

Figure 1 shows an example of a sine wave generated with an 18-step output cycle. A DAC supporting 10-bit resolution can change the output value up to 1,023 steps. By filtering the output signal using a capacitor at the output terminal, a sine wave with almost ideal smoothness can be obtained.

The CPU or DMA transfers data from RAM to the DAC in a fixed cycle determined by the timer. The maximum voltage is 1.00 V. To change the waveform of the frequency, simply change the timer cycle to adjust the switching interval of the output data.

▲Figure 1: Method of generating sine waves using DAC

■ Description

○ DAC operating principle

A DAC supporting n-bit resolution can set the output voltage in steps obtained by dividing the reference voltage (Vref) by 2²-1. For example, if the resolution is 10 bits and Vref is 1.0 V, the step for the output voltage can be obtained by the following formula: 1.0 V ÷ 1023 (= 2¹⁰-1) = 0.0009775 V. For this, the output voltage register must provide 10 bits. Now, if the output value register has a value of m, the DAC will output a voltage of m × 0.0009775 V. As a result, if the output value register has a binary value of [0000000000], it outputs a voltage of 0×0.0009775V=0V, if the binary value is [1000000000], it outputs a voltage of 512×0.0009775V=0.5005V, and if the binary value is [111111111], it outputs a voltage of 1023×0.0009775V=1.0V(max).

○ DAC method

There are a variety of methods that can be used to divide the voltage, but DACs in MCUs tend to use a ladder resistance method where Vref is divided by resistors connected in series. Figure 2 shows the simplest method that can be used, which simply divides the voltage by connecting resistors in series. This is called the series resistance method, and is also known as a voltage-selective method.

A 10-bit DAC requires 1,023 registers, a 12-bit DAC requires 4,095 registers, and a 16-bit DAC requires 65,535 registers (Figure 2 shows a DAC supporting 4-bit resolution). However, it is nearly impossible and quite impractical to manufacture this many registers on a silicon wafer without unwanted variation. In fact, the series resistor method is used for DACs supporting 10-bit or more resolution. For higher resolution, the R-2R method or binary resistor method is generally used.

▲Figure 2: DAC method (serial resistance method)

Since the focus of this article is on the functionality of the MCU, let's only look at the series resistor method, which has a simple structure that is easy to understand. For other methods, please refer to the articles on DAC architecture at the following URL (http://ednjapan.com/edn/articles/0704/01/news003.html) (Japanese only).

Now, each bit of the output value register corresponds to a switching group. The SW1 group corresponds to the bit at point 0 (LSB), the SW2 group corresponds to the first bit, the SW3 group corresponds to the second bit, and the SW4 group corresponds to the third bit (MSB). When each bit is set to 1, all switches in that group are connected to the Vref side, and when each bit is set to 0, they are connected to the GND side.

Figure 2 shows the DAC scheme when the output value register is set to binary 1100. The SW3 and SW4 switches are connected to the Vref side, and the SW1 and SW2 switches are connected to the GND side. In this configuration, the output terminal Vout has a voltage of 12/15 of Vref. Since the register value is equal to the numerator, the output voltage can be generated through this very simple circuit.

○ Method for improving DAC driving performance and waveform smoothness

For some DACs, the outputs from the registers and switches are connected directly to the MCU terminals to form the DAC output terminals. In such cases, different voltages match different resistor combinations, resulting in different output impedance values, and the drive performance is reduced by the output resistance. For this reason, the DAC output terminal generally provides an amplifier to enhance the drive performance.

Figure 2 shows an example of using an OP amp voltage follower. However, if an analog circuit such as an OP amp is used at the output terminal, an analog gain (full-scale) error or offset error may occur, so the DAC cannot accurately output the values of 0 V and Vref.

Typically, users can choose whether to output the signal directly through the MCU or through an amplifier.

Even at the highest resolution, the DAC outputs a step-like waveform. To obtain an ideally smooth analog waveform, a smoothing circuit is installed at the output terminal. A band-pass filter is the ideal solution, but a capacitor across the output and GND can be effective.

○ Cycle (frequency) control

The frequency can be changed by changing the period for rewriting the DAC output value register. Figure 1 shows the process of generating a sine wave with a frequency of 1 Hz by rewriting the output value register with a period of 0.0556 s (1 s/18).

To rewrite the DAC output value register at regular intervals, a time-measuring function such as a timer must be used. The actual rewriting of the output value register is done by the CPU, but if the MCU provides direct memory access (DMA) capability, the register can be rewritten in DMA mode. This allows the CPU to perform other tasks with increased efficiency.

By using a timer to trigger the CPU or DMA (including interrupts) at regular intervals, the DAC output value register data can be replaced with RAM table data. By setting the RAM table data to a value for a sine wave, the DAC can generate a sine wave.

In order to repeatedly output the same waveform, data for one cycle must be set in the RAM table so that the MCU can repeatedly read the data. This method of repeatedly using the same data in the RAM table is called circular mode.

○ Method of generating waveforms other than sine waves

The RAM table data used to generate sine waves can also be used to generate cosine waves. The only difference between sine and cosine waves is the phase difference of 90°. To generate a cosine wave, simply have the MCU read the table starting from the value corresponding to 90° (see Figure 3(a)).

To generate a triangle wave, the RAM table data should be set to increase linearly with a constant gradient. Let the MCU read the data sequentially until it reaches the maximum, and then read the data in the reverse direction. In this way, a triangle wave with equal increasing and decreasing absolute slope can be generated (see Figure 3(b)). In general, instead of creating a table in RAM, it is best to follow this procedure: Use software to generate the formula y = ax + b, increment x at a constant cycle to calculate y, and then write the result of the calculation to the DAC output value register. This allows us to generate triangle waves without using any RAM.

To create a noise wave, you need to generate random numbers and then have the DAC output those values. Some modern MCUs support random number generation, so creating a noise wave is relatively simple.

To create an arbitrary waveform, you simply prepare a data table in RAM that matches the waveform you want to create, and then have the DAC output this value.

▲Figure 3: Cosine wave/triangle wave generation method using DAC