How to output analog quantities through the serial port of a microcontroller?
I. Introduction
If you encounter a situation in electronic circuit design, that is, the microcontroller has no DAC resources and no extra PWM ports to use. Only one serial port is still available. So how to output analog voltage through this port?
Use the port of the microcontroller to output a square wave signal with an adjustable duty cycle. After passing the RC low-pass filter to remove the AC component, the DC signal in the square wave signal can be output. Then there is only one question left, how to use UART to output a square wave signal with a high-level duty cycle that can be changed?
The serial port of the microcontroller sends a signal waveform of one byte, including a start bit and a stop bit. They are 0 and 1 respectively, which cannot be changed. The data bits in the middle eight bits can be changed. For example, sending 0x44 will contain two high levels. In this way, there are 8 high and low level data bits that can be controlled. Then this can achieve 3-bit DAC output. If you want to achieve 8bit DAC output. This requires 256 data bits that can be controlled. In this way, as long as 32 consecutive sent bytes are combined together, a 256-bit signal waveform that can control high and low levels can be formed. This achieves 8-bit DAC output. Let's test the performance of this serial port analog output through a microcontroller.
2. Test circuit
Design a test circuit board based on STM32F103. I had a lot of this chip on hand, so I chose it for the experiment. Use its serial port 2 for testing. Serial port 1 is used for program download. Filter the TXD2 signal using an RC low-pass filter. In fact RXD2 is not used. In addition, there will be two ADC boot ports. In this way, you can use the ADC to collect the signal sent from the outside, and then send it out from TXD2 to verify the function of collecting and synthesizing the audio signal. Lay out the single-sided test circuit board. A flying lead is included, jumpered using 0 ohm resistors.
▲ 图1.2.1 测试电路原理图
▲ 图1.2.2 PCB版图
After a minute, two test PCBs were obtained, one of which was slightly corroded. Use another piece that is more normal for testing.
Solder the circuit. Place on breadboard to test. The breadboard directly provides 3.3V working power to the test circuit. The program is downloaded via the probe clip.
3. Software testing
Set the basic parameters of UART2 of the microcontroller. The output baud rate is 1MHz. This increases the update rate of the output signal. The time for each bit of the output signal can be measured by an oscilloscope to be 1 microsecond, which corresponds to a baud rate of 1MHz. Enable DMA output mode, UART2 can continuously send 32 bytes of memory content through DMA.
D:\zhuoqing\window\ARM\IAR\STM32\Test\2024\Test1\Core\Src\main.c
Set 32 bytes to 0, the output voltage waveform, and the DC voltage measured after filtering is 0.326V. The pulses seen are the high pulses corresponding to the stop bits of each byte. Set all 32 bytes to 0xff , and the low pulse you see is the starting bit of each byte. The filtered DC voltage read is 2.9V. Set 32 bytes to 0x55 . At this time, a positive and negative alternating pulse waveform appears, and the DC voltage after filtering is 1.61V.
The following sets the 32 bytes, the number of high-level bits changes from 0 to 255, and measures the output DC voltage after filtering. It can be seen that as the number of bits increases, the output DC voltage increases linearly. Since the serial port sends a byte with a start bit and a stop bit, the corresponding DC signal does not start from zero, and the highest level is not the highest value of the output signal.
▲ 图1.3.1 设置32字节中高电平位数与滤波之后的直流电压
Set the number of bits of 1 in the 32 bytes in the output DMA to change according to a sine wave. After the serial port signal is filtered, a perfect sine waveform is output. Set the output data to increase, and the filtered waveform will show a sawtooth waveform. A slow change occurs on the falling edge, which is caused by the output RC low-pass filtering. This is the case of outputting high and low levels, and RC low-pass filtering causes slow changes on both rising and falling edges.
※
Summarize
※
This article tested the use of a microcontroller serial port to output a PWM waveform, and then used low-pass filtering to form a DC voltage. Using this method, it is indeed possible to generate a DAC port for the microcontroller under special circumstances.
· END ·