STC8H Development (VIII): NRF24L01 wireless audio transmission (walkie-talkie prototype)

Publisher:PositiveVibesLatest update time:2022-06-08 Source: eefocusKeywords:NRF24L01 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:NRF24L01 Reference address:STC8H Development (VIII): NRF24L01 wireless audio transmission (walkie-talkie prototype)

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

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号