Skip to content

Commit 2321b24

Browse files
committed
Update example for PWM/PDM
1 parent c7ce869 commit 2321b24

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
#include <AutoAnalogAudio.h>
22
AutoAnalog aaAudio;
33

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

69
void setup() {
7-
8-
//Startup the PDM Microphone and PWM(pseudo DAC) on pin 5
9-
aaAudio.begin(1, 1);
10-
aaAudio.autoAdjust = 0;
11-
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress)
10+
Serial.begin(115200);
11+
aaAudio.begin(1, 1); //Startup the PDM Microphone and PWM(pseudo DAC) on pin 5
12+
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress)
1213
aaAudio.dacBitsPerSample = 16;
1314
aaAudio.setSampleRate(16000);
14-
1515
}
1616

1717
void loop() {
1818

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

0 commit comments

Comments
 (0)