Skip to content

Commit

Permalink
Update example for PWM/PDM
Browse files Browse the repository at this point in the history
  • Loading branch information
TMRh20 committed Nov 8, 2024
1 parent c7ce869 commit 2321b24
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions examples/Platforms/NRF52/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#include <AutoAnalogAudio.h>
AutoAnalog aaAudio;

// REQUIRES MBed Enabled Core for NRF52 (XIAO 52840 Sense)
#if defined(USE_TINYUSB)
// Needed for Serial.print on non-MBED enabled or adafruit-based nRF52 cores
#include "Adafruit_TinyUSB.h"
#endif

void setup() {

//Startup the PDM Microphone and PWM(pseudo DAC) on pin 5
aaAudio.begin(1, 1);
aaAudio.autoAdjust = 0;
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress)
Serial.begin(115200);
aaAudio.begin(1, 1); //Startup the PDM Microphone and PWM(pseudo DAC) on pin 5
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress)
aaAudio.dacBitsPerSample = 16;
aaAudio.setSampleRate(16000);

}

void loop() {

aaAudio.getADC(320); // Get 320 Samples from the ADC
for (int i = 0; i < 320; i++) { // Copy them into the DAC Buffer
aaAudio.dacBuffer16[i] = (uint16_t)(aaAudio.adcBuffer16[i]);
aaAudio.getADC(320); // Get 320 16-bit signed samples from the ADC
for (int i = 0; i < 320; i++) { // Copy them into the DAC Buffer. The dac buffer is 16-bits unsigned
int16_t sample = aaAudio.adcBuffer16[i]; // Copy to signed 16-bit
sample += 0x8000; // Convert to unsigned 16-bit
aaAudio.dacBuffer16[i] = sample; // Copy back to unsigned 16-bit buffer
aaAudio.dacBuffer16[i] >>= 6; // Downsample to 10-bits for PWM output. With higher sample rates PWM can only handle 8-bits
}
aaAudio.feedDAC(0,320); // Feed the DAC with the ADC data
aaAudio.feedDAC(0, 320); // Feed the DAC with the ADC data
}

0 comments on commit 2321b24

Please sign in to comment.