295浏览
查看: 295|回复: 0

MAX98357 I2S功放模块测试

[复制链接]
MAX98357 I2S功放模块【参考1】是一款 I2S 接口的数字式D类功放。这个模块使用3.3V~5V宽电压供电,可以切换左右声道。

这次编写简单的代码进行发声测试。

硬件连接如下:






  
MAX98357
  
ESP32S3
用途
SPK+/- 连接喇叭
连接喇叭正负极,喇叭输出
DIN   
48
从  ESP32S3 发送的 I2S数据
BCLK
45
从  ESP32S3 发送的 I2S  Clock
LRC  
35
从  ESP32S3 发送的 I2S 左右声道选择信号
GND
GND
VCC
5V
供电


按照上述方案连接好后,烧录如下代码:

  1. #include <I2S.h>
  2. const int frequency = 440; // frequency of square wave in Hz
  3. const int amplitude = 32000; // amplitude of square wave
  4. const int sampleRate = 8000; // sample rate in Hz
  5. const int bps = 16;
  6. const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
  7. short sample = amplitude; // current sample value
  8. int count = 0;
  9. i2s_mode_t mode = I2S_PHILIPS_MODE; // I2S decoder is needed
  10. // i2s_mode_t mode = ADC_DAC_MODE; // Audio amplifier is needed
  11. // Mono channel input
  12. // This is ESP specific implementation -
  13. //   samples will be automatically copied to both channels inside I2S driver
  14. //   If you want to have true mono output use I2S_PHILIPS_MODE and interlay
  15. //   second channel with 0-value samples.
  16. //   The order of channels is RIGH followed by LEFT
  17. //i2s_mode_t mode = I2S_RIGHT_JUSTIFIED_MODE; // I2S decoder is needed
  18. void setup() {
  19.   Serial.begin(115200);
  20.   Serial.println("I2S simple tone");
  21.   delay(5000);
  22.     //setAllPins(int sckPin, int fsPin, int sdPin, int outSdPin, int inSdPin);
  23.   I2S.setAllPins(45        , 35       , 48       , 48          , -1);
  24.   // start I2S at the sample rate with 16-bits per sample
  25.   if (!I2S.begin(mode, sampleRate, bps)) {
  26.    
  27.     Serial.println("Failed to initialize I2S!");
  28.     while (1); // do nothing
  29.   }
  30. }
  31. void loop() {
  32.    while (Serial.available()) {
  33.     char c = Serial.read();
  34.     if (c == '3') {
  35.       ESP.restart();
  36.     }
  37.     // 主机端发送 l, 回复 z 用于识别串口
  38.     if (c == '1') {
  39.       Serial.print('z');
  40.     }
  41.     // 主机端发送 l, 回复 z 用于识别串口
  42.     if (c == '2') {
  43.       Serial.printf("getSckPin:%d getFsPin:%d getDataPin:%d",
  44.                       I2S.getSckPin(),
  45.                       I2S.getFsPin(),
  46.                       I2S.getDataPin());
  47.     }   
  48.   }
  49.   
  50.     if (count % halfWavelength == 0 ) {
  51.       // invert the sample every half wavelength count multiple to generate square wave
  52.       sample = -1 * sample;
  53.     }
  54.     if(mode == I2S_PHILIPS_MODE || mode == ADC_DAC_MODE){ // write the same sample twice, once for Right and once for Left channel
  55.       I2S.write(sample); // Right channel
  56.       I2S.write(sample); // Left channel
  57.     }else if(mode == I2S_RIGHT_JUSTIFIED_MODE || mode == I2S_LEFT_JUSTIFIED_MODE){
  58.       // write the same only once - it will be automatically copied to the other channel
  59.       I2S.write(sample);
  60.     }
  61.     // increment the counter for the next sample
  62.     count++;
  63. }
复制代码

代码实现的是生成一个 440Hz的音频,然后通过喇叭播放出来。

[media=x,500,375]【DFRobot的 max98357 iis 测试】 https://www.bilibili.com/video/B ... 819bfc37d4672cb6d54[/media]


参考:
1.https://www.dfrobot.com.cn/goods-3573.html


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail