About PWM, DAC and Audio
PWM is the abbreviation of pulse width modulation. Since there are many articles introducing it, you can do your homework.
Wikipedia Pulse-width_modulation
Baidu Encyclopedia PWM Technology
Most low-end MCUs do not have DAC conversion, but can use PWM simulation for audio transmission
The frequency of sound waves in normal conversations is between 500-2000Hz, and the frequency range of sound waves that can be heard by the human ear is between 20Hz and 20kHz.
For calls, 8kHz bandwidth can achieve better voice transmission effect
Simulate DAC through PWM. Because PWM is a square wave, its frequency will introduce background noise. The frequency of the background noise is a multiple of the PWM frequency.
When the PWM frequency is 8KHz, the background noise is very loud, as obvious as the transmitted audio. The background noise can be effectively suppressed by adjusting the PWM frequency to above 16kHz.
Implementation of wireless audio transmission
Sending part
Receiving part
The following implements the transmission of mono 8kHz 8bit sampled audio signals.
Sending part
The sending part needs to implement 8kHz sampling and send 8000 bytes of data per second through NRF24L01.
Voice input
Voice input can use electret microphone plus S9013 amplification input or directly use MAX9814. It is recommended to use the latter in the testing stage to ensure that the sampling input is not distorted, and then replace it with the electret microphone circuit after adjustment.
ADC Audio Sampling
Because ADC sampling needs to achieve an accurate 8000 samples per second, DMA cannot be used. Under STC8H (including other MCUs such as STM32), it is impossible to accurately adjust the number of samples per second under DMA. Because the ADC sampling frequency, sampling period and conversion period are fixed in different MCUs, it is difficult to achieve 8kHz sampling. There are two specific implementation methods:
1. Timer driven acquisition
It is relatively easy to initiate ADC conversion in an interrupt by setting the timer to 8kHz. At this time, ADC also needs to be implemented as an interrupt mode, because the conversion time of ADC is relatively long. If a synchronous ADC conversion is performed in the timer interrupt, it is easy to affect the main process. There needs to be interrupt processing for the timer and interrupt processing for the ADC. The interrupt processing of the timer is simply used to initiate the conversion, and the interrupt of the ADC is used to read the result.
2. Continuous acquisition and timing reading
By setting the timer to 8kHz, set the ADC acquisition to a cyclic mode (interrupt acquisition, but initiate again when interrupted), and only read the acquisition results in the timer interrupt. This method can also achieve 8kHz sampling. Because this method actually consumes more power, the former method is still used in actual use.
NRF24L01 Transmit
When the bandwidth of NRF24L01 is set to 1Mbps, the actual transmission speed can reach 23k bytes per second, so there is no problem with the transmission of 8bit 8kHz sampling. Because of the response and retransmission mechanism of NRF24L01 during transmission, it is easy to cause transmission interruption when the signal is not good. In order to avoid the influence of fluctuations in transmission time, a double array is used as a buffer in the implementation. The logic between sampling and sending is as follows:
Two 256-byte arrays are used as global variables, and the variables are defined to point to the array number and write position currently being written.
When the ADC interrupt reads the result, write to the array and position of the current number and move the position. When an array is full, mark this array as sendable and switch to the next array to continue writing.
In the main process, determine whether there is an array that can be sent. If it can be sent, send all the data in groups of 32 bytes in a loop.
Because under normal signal strength, the sending speed of NRF24L01 is faster than the sampling speed, so basically the sending of NRF24L01 is sending->waiting->sending.
Receiving part
The receiving part is to store the data received by NRF24L01 and set each value as the duty cycle of PWM output according to the frequency of 8kHz to realize DAC simulation.
RNF24L01 Receiver
Because NRF24L01 sends in a centralized manner, and PWM restores at a uniform speed, buffering is also required when receiving. The mechanism of receiving is similar to that of sending.
Two 256-byte arrays are used as global variables, and the variables are defined to point to the array number and write position currently being written.
NRF24L01 receives data through interrupts. When receiving, it writes and moves the position to the currently numbered array and position. When an array is full, it marks the array as available and switches to the next array to continue writing.
PWM analog DAC restoration
Initialize a PWM output. The PWM period is 256, which corresponds to an 8-bit duty cycle adjustment range. Ensure that the PWM frequency is not less than 16kHz. In the interrupt of the 8kHz timer, determine the array and position currently read, read a value each time, and set it as the PWM duty cycle. If the array is not available, do nothing. If the duty cycle is set to 0 at this time, noise will be generated.
Audio output
During the test phase, you can connect a 200R resistor in series with the PWM output and then connect it to the speaker to hear the output audio. This resistor cannot be too small. If the resistance is less than 100R during the test, the MCU will be repeatedly restarted due to insufficient power supply. After confirming that there is no problem with the audio output, you can replace it with a PAM8403 audio amplifier module.
When using the PAM8403 module
The module needs to be powered independently. If the module and the MCU are both powered by USB2TTL during testing, the MCU will be underpowered and the sound output will be abnormal.
The output of the module and the MCU do not need to share the same ground, that is, the PWM output and ground of the module MCU can be directly connected to the audio input of PAM8403
Because it is a mono signal, only one channel of PAM8403 can be used, either L or R
Demo Code
GitHub FwLib_STC8/tree/master/demo/spi/nrf24l01_audio
Gitee FwLib_STC8/tree/master/demo/spi/nrf24l01_audio
Wiring Instructions
In the test, the sending part uses STC8H3K32S2, and the receiving part uses STC8H1K08. You can use any model of the STC8H series.
Common connections (NRF24L01)
8H3K32S2/8H1K08 NRF24L01
P35(SS, Ignored) => CSN 16
P34(MOSI) => MOSI 15
P33(MISO) => MISO 14
P32(SPCLK) => CLK 13
P36(INT2) => IRQ 17
P37(IO) => CE 18
Sending part
STC8H3K32S2 MAX9814
P11(ADC1) => MIC
3.3V => VDD
3.3V => GAIN
GND => A/R
GND => GND
ADC, if it is STC8H3K32S2, use ADC sampling to connect AVcc, AGnd and ADC_Vref+ correctly
AVcc => 3.3V
AGnd => GND
ADC_Vref+ => 3.3V
P11 => Output(MAX9814) or MIC
Receiving part
STC8H1K08 PAM8403
P10(PWM1P) => 200R => L or R Input
GND => _|_ Input
Ext 3.3V/5V => VCC
Ext GND => GND
Notice:
The pin layout of the MCU is not necessarily the same. Both STC8H3K32S2 and STC8H1K08 are 20-pin packages, but the pin layout is different.
When burning the sending part and the receiving part, pay attention to swapping RX_ADDRESS and TX_ADDRESS in nrf24l01.c
Effect Demonstration
B station video https://www.bilibili.com/video/BV1kZ4y1Z78v
Debugging instructions
Because this demonstration actually includes the timer, ADC sampling, NRF24L01 sending, receiving, and PWM modulation, any problem in any link will cause the demonstration to fail. During debugging, it is necessary to follow the principle of breaking the whole into parts and confirming one by one to confirm whether each node is working properly.
Timer debugging
Because the 8kHz output is difficult to observe, you can use a uint16_t global variable to increment to 8000 and then output it to the serial port to observe whether the time interval is correct.
ADC Debug
First, use the synchronization mode to check whether the ADC acquisition is correct. The ADC wiring of STC8H1K and STC8H3K is different. If the wiring is incorrect, the output is noise.
After the synchronous sampling is correct, collect and check whether it is correct through interruption mode
After the interrupt is OK, you can combine it with the timer and initiate sampling through the timer
NRF24L01 Debug
You can refer to the previous example to see if the SPI driver nRF24L01 wireless module can function normally when running NRF24L01 alone for transmission and reception.
PWM Debug
If conditions permit, you can use a logic analyzer. After the output is normal, use audio to test it. You can refer to the example of PWM audio output. Play an 8-bit audio in a loop to check whether the PWM output is correct. Because the audio is large, you need to use a chip with a Flash capacity of at least 32K bytes, such as STC8H3K32S2.
Minimum system debugging
The minimum system does not use ADC at the transmitter end, but uses a fixed 8-bit audio as input for transmission. The receiver does not connect an external audio amplifier, but directly uses a 200-ohm series speaker for inspection. If it works normally, the audio playback effect should be very good.
After the minimum system is debugged, you can start debugging the ADC. Finally, add the audio amplifier module.
Previous article:USB functional test of STC8H8K64U (unsuccessful)
Next article:STC8H Development (VII): I2C drive MPU6050 three-axis acceleration + three-axis angular velocity detection module
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Xilinx XPE Power Estimation Software Setup Advisory
- Anlu SparkRoad FPGA development board interface definition
- Analysis and Design of TL431 Feedback Loop
- Understanding the past, present and future of the Matter Protocol in one article
- Schematic diagram of various application circuits of LM324
- [Voice and vision module based on ESP32S3]-The materials have not arrived yet, so use ESPcam to test QR code recognition first
- Circuit Schematic Analysis Method
- Hongmeng Development Board Neptune (Part 3) - Problems encountered in the development environment
- Live broadcast at 10:00 am today [Unlocking new possibilities of TI Sitara AM2x MCU in motor drive]
- Capacitors and inductors