【Follow me第二季第4期】-任务3:学习PDM麦克风技术知识,调试PDM麦克风,通过串...
[复制链接]
本次任务为学习PDM麦克风,并打印麦克风采集到的数据。
1、麦克风不需要安装单独的库,在板载资源里有“PDM.h”;
2、初始化串口,启动PDM,选择通道及设置采样频率;如果启动失败,则打印启动PDM失败;
static const char channels = 1;
static const int frequncy = 24000;
short sampleBuffer[512];
volatile int samplesRead;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
PDM.onReceive(onPDMdata);
if(!PDM.begin(channels,frequncy))
{
Serial.println("Failed to start PDM!!!");
while(1);
}
}
3、循环执行声音采样,并打印采样值;
void loop() {
// put your main code here, to run repeatedly:
if(samplesRead)
{
for(int i = 0; i < samplesRead; i++)
{
Serial.println(sampleBuffer[i]);
}
samplesRead=0;
}
delay(1);
}
4、用串口查看数据;可选择数据才看,也可选择波形查看;波形查看更直观。
总体代码如下:
#include "PDM.h"
static const char channels = 1;
static const int frequncy = 24000;
short sampleBuffer[512];
volatile int samplesRead;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
PDM.onReceive(onPDMdata);
if(!PDM.begin(channels,frequncy))
{
Serial.println("Failed to start PDM!!!");
while(1);
}
}
void loop() {
// put your main code here, to run repeatedly:
if(samplesRead)
{
for(int i = 0; i < samplesRead; i++)
{
Serial.println(sampleBuffer[i]);
}
samplesRead=0;
}
delay(1);
}
void onPDMdata(){
int bytesAvailable = PDM.available();
PDM.read(sampleBuffer,bytesAvailable);
samplesRead = bytesAvailable / 2;
}
|